dsh-context-compression-improved 0.5.4 → 0.6.0

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 (28) hide show
  1. package/README.md +10 -1
  2. package/package.json +162 -140
  3. package/packages/selector/README.md +6 -1
  4. package/packages/selector/README.zh.md +6 -1
  5. package/packages/selector/dsh.plugin.json +1 -1
  6. package/packages/selector/lib/advisor-state.js +1 -1
  7. package/packages/selector/lib/client.d.ts +19 -104
  8. package/packages/selector/lib/client.js +42 -5
  9. package/packages/selector/lib/index.d.ts +9 -0
  10. package/packages/selector/lib/index.js +18 -50
  11. package/packages/selector/lib/profiles.d.ts +105 -0
  12. package/packages/selector/lib/pruner.js +3 -2
  13. package/packages/selector/package.json +114 -114
  14. package/packages/selector/src/client/index.ts +71 -8
  15. package/packages/selector/src/index.ts +35 -86
  16. package/packages/selector/src/pruner.ts +12 -3
  17. package/packages/selector/src/runtime/config.ts +1 -1
  18. package/packages/selector/tests/code-skeleton.client.spec.ts +93 -90
  19. package/packages/selector/tests/custom-contract.client.spec.ts +19 -1
  20. package/packages/selector/tests/helpers/legacy-namespace.ts +31 -0
  21. package/packages/selector/tests/host-preset-overlay.host.spec.ts +206 -206
  22. package/packages/selector/tests/preset-options-write.client.spec.ts +63 -82
  23. package/packages/selector/tests/preset-overlay-loader.e2e.host.spec.ts +206 -201
  24. package/packages/selector/tests/public/package-contract.client.spec.ts +2 -2
  25. package/packages/selector/tests/runtime/advice-never-withholds.host.spec.ts +3 -2
  26. package/packages/selector/tests/runtime/public/public-runtime.spec.ts +20 -17
  27. package/packages/selector/tests/settings-seat.client.spec.ts +2 -2
  28. package/packages/selector/tests/standing-generation.host.spec.ts +704 -697
@@ -6,82 +6,61 @@ import type { CompressionSelectorInjected } from '../src/client/CompressionProfi
6
6
  import type { ContextCompressionSettings } from '../src/profiles.ts'
7
7
 
8
8
  /**
9
- * The settings transport applies a `set` op AT its path, so the write path is
10
- * asserted through the ops it submits: a whole-section write is what silently
11
- * deleted `estimatorMode` (and every sibling override) whenever the user
12
- * touched a second field of the TokenPilot-inspired card.
9
+ * 0.1.7 contract: the compression document rides the entry config as ONE
10
+ * volatile whole-object field, so every write commits the full document
11
+ * through `configForms.set('settings', doc)`. The sibling-safety that the old
12
+ * path-addressed settings service provided is now enforced CLIENT-side:
13
+ * `planPresetOptionsOps` is still computed against the current doc and applied
14
+ * locally before the commit, so a second field write never erases the first.
13
15
  */
14
16
 
15
- type PathOp = { op: 'set', path: string[], value: unknown } | { op: 'unset', path: string[] }
16
-
17
- interface ScopeStub {
18
- readonly writes: PathOp[][]
17
+ interface FormStub {
18
+ form: Record<string, unknown>
19
+ /** Whole-doc commits the fake Host accepted. */
20
+ readonly commits: Record<string, unknown>[]
21
+ /** Whole-doc writes the fake Host refused. */
22
+ readonly refused: Record<string, unknown>[]
19
23
  readonly snapshot: () => Record<string, unknown>
20
24
  }
21
25
 
22
- /** Emulate the Host settings service: apply ops at their path, then bump the revision. */
23
- function createScopeStub(initial: ContextCompressionSettings, commit = true): ScopeStub {
26
+ function createFormStub(initial: ContextCompressionSettings, commit = true): FormStub {
24
27
  let revision = 1
25
- let value: Record<string, unknown> = structuredClone(initial) as unknown as Record<string, unknown>
26
- const writes: PathOp[][] = []
28
+ let value: Record<string, unknown> = { settings: structuredClone(initial) as unknown as Record<string, unknown> }
29
+ const commits: Record<string, unknown>[] = []
30
+ const refused: Record<string, unknown>[] = []
27
31
  const listeners = new Set<() => void>()
28
- const applyOps = (ops: readonly PathOp[]): void => {
29
- for (const op of ops) {
30
- const [head, ...rest] = op.path
31
- if (head === undefined) continue
32
- if (rest.length === 0) {
33
- if (op.op === 'set') value = { ...value, [head]: op.value }
34
- else delete value[head]
35
- continue
36
- }
37
- const child = { ...(value[head] as Record<string, unknown> | undefined) }
38
- const leaf = rest[rest.length - 1] as string
39
- if (op.op === 'set') child[leaf] = op.value
40
- else delete child[leaf]
41
- value = { ...value, [head]: child }
42
- }
43
- revision += 1
44
- for (const listener of listeners) listener()
45
- }
46
32
  return {
47
- writes,
48
- snapshot: () => value,
49
- // The scope face the injection factory binds.
50
- scope: {
33
+ commits,
34
+ refused,
35
+ snapshot: () => structuredClone(value.settings as Record<string, unknown>),
36
+ form: {
51
37
  getSnapshot: () => ({
52
- status: 'ready',
53
- value: value as unknown as ContextCompressionSettings,
54
- base: undefined,
55
- user: undefined,
38
+ status: 'ready' as const,
39
+ value: value as unknown as Record<string, unknown>,
56
40
  revision,
57
41
  writable: true,
58
- mode: 'host',
42
+ base: undefined,
43
+ user: undefined,
44
+ mode: 'host' as const,
59
45
  }),
60
46
  subscribe: (listener: () => void) => {
61
47
  listeners.add(listener)
62
48
  return () => { listeners.delete(listener) }
63
49
  },
64
- set: (field: string, fieldValue: unknown) => {
65
- const ops: PathOp[] = [{ op: 'set', path: [field], value: fieldValue }]
66
- writes.push(ops)
67
- applyOps(ops)
68
- return Promise.resolve()
69
- },
70
- unset: (field: string) => {
71
- const ops: PathOp[] = [{ op: 'unset', path: [field] }]
72
- writes.push(ops)
73
- applyOps(ops)
74
- return Promise.resolve()
75
- },
76
- mutate: (ops: readonly PathOp[]) => {
77
- writes.push([...ops])
78
- // A refused or lost write changes no document, so the revision the
79
- // confirmation read fences on never moves.
80
- if (commit) applyOps(ops)
81
- return Promise.resolve()
50
+ set: (_field: string, doc: unknown) => {
51
+ if (!commit) {
52
+ refused.push(doc as Record<string, unknown>)
53
+ return Promise.resolve(false)
54
+ }
55
+ value = { settings: structuredClone(doc) as unknown as Record<string, unknown> }
56
+ revision += 1
57
+ commits.push({ settings: structuredClone(doc) as unknown as Record<string, unknown> })
58
+ for (const listener of listeners) listener()
59
+ return Promise.resolve(true)
82
60
  },
61
+ unset: () => Promise.resolve(false),
83
62
  },
84
- } as ScopeStub & { scope: unknown }
63
+ } as FormStub & { form: Record<string, unknown> }
85
64
  }
86
65
 
87
66
  const DEFAULT_CUSTOM = {
@@ -91,7 +70,7 @@ const DEFAULT_CUSTOM = {
91
70
  aggregate: { enabled: true, trigger: 16_384, target: 8_192 },
92
71
  history: {
93
72
  enabled: true,
94
- trigger: 32_768,
73
+ trigger: 16_384,
95
74
  keepRecentTurns: 2,
96
75
  keepRecent: 16_384,
97
76
  minReclaim: 8_192,
@@ -104,8 +83,7 @@ function bindInjected(
104
83
  commit = true,
105
84
  ): {
106
85
  injected: CompressionSelectorInjected
107
- writes: PathOp[][]
108
- snapshot: () => Record<string, unknown>
86
+ stub: FormStub
109
87
  } {
110
88
  const settings: ContextCompressionSettings = {
111
89
  profile: 'tokenpilot-inspired',
@@ -114,7 +92,7 @@ function bindInjected(
114
92
  codeSkeleton: { enabled: false },
115
93
  ...(presetOptions === undefined ? {} : { presetOptions }),
116
94
  }
117
- const stub = createScopeStub(settings, commit)
95
+ const stub = createFormStub(settings, commit)
118
96
  let options: Record<string, unknown> | undefined
119
97
  const ctx = {
120
98
  slots: {
@@ -130,26 +108,26 @@ function bindInjected(
130
108
  },
131
109
  },
132
110
  // apply consumes the services through their declarative-inject faces
133
- // (ctx.locale / ctx.settingsScope), the same shape cordis binds on the host.
111
+ // (ctx.locale / ctx.configForms), the same shape cordis binds on the host.
134
112
  locale: { bind: () => (key: string) => key, register: () => {} },
135
- settingsScope: { bind: () => (stub as unknown as { scope: unknown }).scope },
113
+ configForms: { get: () => (stub.form as unknown as { getSnapshot(): unknown }) },
136
114
  }
137
115
  apply(ctx as never)
138
116
  if (options === undefined) throw new Error('the settings card never registered')
139
117
  const factory = options['inject'] as () => CompressionSelectorInjected
140
- return { injected: factory(), writes: stub.writes, snapshot: stub.snapshot }
118
+ return { injected: factory(), stub }
141
119
  }
142
120
 
143
- describe('presetOptions writes are path-addressed', () => {
121
+ describe('presetOptions writes commit the whole doc through configForms', () => {
144
122
  it('keeps the estimator channel when a provider is chosen afterwards', async () => {
145
- const { injected, writes, snapshot } = bindInjected({ estimatorMode: 'host' })
123
+ const { injected, stub } = bindInjected({ estimatorMode: 'host' })
146
124
  await injected.savePresetOptions({ estimatorProvider: 'local-35b' })
147
- expect(snapshot()['presetOptions']).toEqual({ estimatorMode: 'host', estimatorProvider: 'local-35b' })
148
- expect(writes).toEqual([[{ op: 'set', path: ['presetOptions', 'estimatorProvider'], value: 'local-35b' }]])
125
+ expect(stub.snapshot()['presetOptions']).toEqual({ estimatorMode: 'host', estimatorProvider: 'local-35b' })
126
+ expect(stub.commits.length).toBe(1)
149
127
  })
150
128
 
151
129
  it('treats a retired review-gate patch as a no-op instead of a write', async () => {
152
- const { injected, writes, snapshot } = bindInjected({ estimatorMode: 'host' })
130
+ const { injected, stub } = bindInjected({ estimatorMode: 'host' })
153
131
  // The patch surface no longer carries these keys, and the decoders accept
154
132
  // them only so a legacy document keeps loading. Cast past the removed type
155
133
  // so the test pins the RUNTIME behaviour: nothing is written.
@@ -159,12 +137,12 @@ describe('presetOptions writes are path-addressed', () => {
159
137
  cacheHitDiscountAlpha: 0.2,
160
138
  reviewHighImpactTokens: 6000,
161
139
  } as unknown as Parameters<typeof injected.savePresetOptions>[0])
162
- expect(writes).toEqual([])
163
- expect(snapshot()['presetOptions']).toEqual({ estimatorMode: 'host' })
140
+ expect(stub.commits.length).toBe(0)
141
+ expect(stub.snapshot()['presetOptions']).toEqual({ estimatorMode: 'host' })
164
142
  })
165
143
 
166
- it('never replaces the whole section, so sibling overrides survive', async () => {
167
- const { injected, writes, snapshot } = bindInjected({
144
+ it('never loses sibling overrides across whole-doc commits', async () => {
145
+ const { injected, stub } = bindInjected({
168
146
  estimatorMode: 'direct',
169
147
  estimatorBaseUrl: 'http://192.168.100.242:8200',
170
148
  dedupeToolResults: false,
@@ -172,7 +150,7 @@ describe('presetOptions writes are path-addressed', () => {
172
150
  })
173
151
  await injected.savePresetOptions({ estimatorModel: 'Qwen3.6-35B-A3B' })
174
152
  await injected.savePresetOptions({ estimatorProvider: 'local-35b' })
175
- expect(snapshot()['presetOptions']).toEqual({
153
+ expect(stub.snapshot()['presetOptions']).toEqual({
176
154
  estimatorMode: 'direct',
177
155
  estimatorBaseUrl: 'http://192.168.100.242:8200',
178
156
  dedupeToolResults: false,
@@ -180,22 +158,25 @@ describe('presetOptions writes are path-addressed', () => {
180
158
  estimatorModel: 'Qwen3.6-35B-A3B',
181
159
  estimatorProvider: 'local-35b',
182
160
  })
183
- for (const batch of writes) {
184
- for (const op of batch) expect(op.path.length).toBe(2)
161
+ // Two user actions, two whole-doc commits.
162
+ expect(stub.commits.length).toBe(2)
163
+ for (const doc of stub.commits) {
164
+ // Each commit carries the complete document under the volatile field.
165
+ expect(Object.keys(doc)).toEqual(['settings'])
185
166
  }
186
167
  })
187
168
 
188
169
  it('clears exactly the field a patch names with undefined', async () => {
189
- const { injected, writes, snapshot } = bindInjected({ estimatorMode: 'direct', estimatorApiKey: 'sk-stored' })
170
+ const { injected, stub } = bindInjected({ estimatorMode: 'direct', estimatorApiKey: 'sk-stored' })
190
171
  await injected.savePresetOptions({ estimatorApiKey: undefined })
191
- expect(snapshot()['presetOptions']).toEqual({ estimatorMode: 'direct' })
192
- expect(writes).toEqual([[{ op: 'unset', path: ['presetOptions', 'estimatorApiKey'] }]])
172
+ expect(stub.snapshot()['presetOptions']).toEqual({ estimatorMode: 'direct' })
193
173
  })
194
174
 
195
175
  it('writes nothing when the patch already matches the stored section', async () => {
196
- const { injected, writes } = bindInjected({ estimatorMode: 'host', estimatorProvider: 'local-35b' })
176
+ const { injected, stub } = bindInjected({ estimatorMode: 'host', estimatorProvider: 'local-35b' })
197
177
  await injected.savePresetOptions({ estimatorMode: 'host' })
198
- expect(writes).toEqual([])
178
+ expect(stub.commits.length).toBe(0)
179
+ expect(stub.refused.length).toBe(0)
199
180
  })
200
181
 
201
182
  it('reports a save the Host did not commit', async () => {