dsh-context-compression-improved 0.8.0-beta.3 → 0.8.0-beta.5
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/CHANGELOG.md +20 -0
- package/eslint.config.js +30 -30
- package/package.json +163 -163
- package/packages/selector/dsh.plugin.json +16 -16
- package/packages/selector/lib/client.d.ts +28 -0
- package/packages/selector/lib/client.js +631 -7
- package/packages/selector/lib/{advisor-state.js → config.js} +170 -65
- package/packages/selector/lib/index.d.ts +13 -1
- package/packages/selector/lib/index.js +306 -50
- package/packages/selector/lib/profiles.d.ts +15 -0
- package/packages/selector/lib/pruner.d.ts +65 -0
- package/packages/selector/lib/pruner.js +408 -5
- package/packages/selector/src/client/CompressionProfileControls.tsx +67 -0
- package/packages/selector/src/client/CompressionProfileSelector.tsx +2 -0
- package/packages/selector/src/client/decode.ts +57 -49
- package/packages/selector/src/client/index.ts +41 -0
- package/packages/selector/src/client/locales/de.ts +26 -0
- package/packages/selector/src/client/locales/es.ts +26 -0
- package/packages/selector/src/client/locales/fr.ts +26 -0
- package/packages/selector/src/client/locales/it.ts +26 -0
- package/packages/selector/src/client/locales/ja.ts +26 -0
- package/packages/selector/src/client/locales/ko.ts +26 -0
- package/packages/selector/src/client/locales/ru.ts +26 -0
- package/packages/selector/src/client/locales.ts +52 -0
- package/packages/selector/src/client/monitor-fab.ts +307 -0
- package/packages/selector/src/client/settings-section.tsx +16 -2
- package/packages/selector/src/index.ts +133 -60
- package/packages/selector/src/profiles.ts +431 -394
- package/packages/selector/src/pruner/state.ts +57 -50
- package/packages/selector/src/pruner.ts +241 -1
- package/packages/selector/src/runtime/audit.ts +303 -286
- package/packages/selector/src/runtime/config.ts +756 -706
- package/packages/selector/src/runtime/monitor.ts +149 -0
- package/packages/selector/src/runtime/summary-command.ts +127 -0
- package/packages/selector/src/runtime/tokenpilot/advisor-prompt.ts +233 -188
- package/packages/selector/src/runtime/tokenpilot/advisor-state.ts +204 -149
- package/packages/selector/src/runtime/tokenpilot/intent-fold.ts +53 -0
- package/packages/selector/src/runtime/tokenpilot/intent-gate.ts +65 -0
- package/packages/selector/src/runtime/tokenpilot/intent-input-mask.ts +125 -0
- package/packages/selector/src/runtime/tokenpilot/intent-range.ts +37 -0
- package/packages/selector/src/runtime/types.ts +365 -346
- package/packages/selector/tests/auto-compact.client.spec.tsx +214 -213
- package/packages/selector/tests/code-skeleton.client.spec.ts +94 -93
- package/packages/selector/tests/custom-contract.client.spec.ts +223 -222
- package/packages/selector/tests/estimator-channel.client.spec.tsx +253 -252
- package/packages/selector/tests/estimator-route-registration.host.spec.ts +8 -6
- package/packages/selector/tests/monitor-fab.client.spec.ts +117 -0
- package/packages/selector/tests/preset-options-write.client.spec.ts +188 -187
- package/packages/selector/tests/profiles.client.spec.tsx +436 -434
- package/packages/selector/tests/runtime/audit.spec.ts +275 -274
- package/packages/selector/tests/runtime/intent-gate.spec.ts +62 -0
- package/packages/selector/tests/runtime/intent-summary.spec.ts +156 -0
- package/packages/selector/tests/runtime/monitor.spec.ts +236 -0
- package/packages/selector/tests/runtime/public/public-runtime.spec.ts +2734 -2731
- package/packages/selector/tests/runtime/summary-command.spec.ts +154 -0
- package/packages/selector/tests/settings-seat.client.spec.ts +1 -1
- package/scripts/packed-install-e2e.mjs +1 -1
- package/scripts/verify-release.mjs +2 -1
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 压缩监控浮动球(FAB)客户端单测——骨架来自 pm `d29bcb1^` 撤下的
|
|
3
|
+
* longtask-fab(计划 cci-monitor-fab task_3.1-3.4),数据层为注入依赖,
|
|
4
|
+
* 本文件自断言消费侧行为。真实时钟 + initialDelayMs=1,不用假时钟
|
|
5
|
+
* (jsdom × fake-timers 的 async advance 有已知坑)。
|
|
6
|
+
*/
|
|
7
|
+
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
8
|
+
import { initMonitorFab, type MonitorFabDeps, type MonitorSnapshotPayload } from '../src/client/monitor-fab.ts'
|
|
9
|
+
|
|
10
|
+
const FAB_ID = 'dsh-cci-monitor-fab'
|
|
11
|
+
|
|
12
|
+
function snapshotPatch(patch: Partial<MonitorSnapshotPayload> = {}): MonitorSnapshotPayload {
|
|
13
|
+
return {
|
|
14
|
+
ok: true,
|
|
15
|
+
startedAt: new Date().toISOString(),
|
|
16
|
+
gross: { exact: 10_000, estimated: 0 },
|
|
17
|
+
offsets: { exact: 1_200, estimated: 0 },
|
|
18
|
+
net: { exact: 8_800, estimated: 0 },
|
|
19
|
+
usage: { requests: 5, cacheHitRate: 0.812 },
|
|
20
|
+
pricing: { currency: 'USD', actualCost: '0.12', estimatedSavedCost: '1.30' },
|
|
21
|
+
intent: { override: undefined, observedEnabled: false, gate: { floorFraction: 0.45, growthTokens: 50_000 }, lastFold: undefined },
|
|
22
|
+
recentOffsets: [{ component: 'fresh', tokens: 1_200 }],
|
|
23
|
+
sessionScope: null,
|
|
24
|
+
...patch,
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function makeDeps(patch: Partial<MonitorFabDeps> & { snapshots?: MonitorSnapshotPayload[] } = {}): MonitorFabDeps & {
|
|
29
|
+
snapshots: MonitorSnapshotPayload[]
|
|
30
|
+
overrides: Array<{ sessionId: string | undefined, action: 'on' | 'off' | 'clear' }>
|
|
31
|
+
} {
|
|
32
|
+
const deps: MonitorFabDeps & {
|
|
33
|
+
snapshots: MonitorSnapshotPayload[]
|
|
34
|
+
overrides: Array<{ sessionId: string | undefined, action: 'on' | 'off' | 'clear' }>
|
|
35
|
+
} = {
|
|
36
|
+
t: (key: string) => key,
|
|
37
|
+
sessionIdOf: () => undefined,
|
|
38
|
+
enabledOf: () => true,
|
|
39
|
+
fetchSnapshot: vi.fn(async () => deps.snapshots[deps.snapshots.length - 1]!),
|
|
40
|
+
applyOverride: vi.fn(async (_sessionId: string | undefined, action: 'on' | 'off' | 'clear') => {
|
|
41
|
+
deps.overrides.push({ sessionId: undefined, action })
|
|
42
|
+
}),
|
|
43
|
+
snapshots: [snapshotPatch()],
|
|
44
|
+
overrides: [],
|
|
45
|
+
initialDelayMs: 1,
|
|
46
|
+
}
|
|
47
|
+
return Object.assign(deps, patch)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** 真实时钟下等条件成立(轮询 1ms 一轮,上限 500ms)。 */
|
|
51
|
+
async function waitFor(predicate: () => boolean): Promise<void> {
|
|
52
|
+
const deadline = Date.now() + 500
|
|
53
|
+
while (!predicate()) {
|
|
54
|
+
if (Date.now() > deadline) throw new Error('waitFor timeout')
|
|
55
|
+
await new Promise(resolve => setTimeout(resolve, 5))
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
afterEach(() => {
|
|
60
|
+
document.body.innerHTML = ''
|
|
61
|
+
localStorage.clear()
|
|
62
|
+
vi.restoreAllMocks()
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
describe('monitor fab (body-level overlay)', () => {
|
|
66
|
+
it('mounts idempotently — double init keeps a single FAB', () => {
|
|
67
|
+
const deps = makeDeps()
|
|
68
|
+
initMonitorFab(deps)
|
|
69
|
+
initMonitorFab(deps)
|
|
70
|
+
expect(document.querySelectorAll(`#${FAB_ID}`).length).toBe(1)
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
it('enabled gate hides the ball and suppresses polling', async () => {
|
|
74
|
+
const fetchSnapshot = vi.fn(async () => snapshotPatch())
|
|
75
|
+
initMonitorFab(makeDeps({ enabledOf: () => false, fetchSnapshot, initialDelayMs: 5 }))
|
|
76
|
+
const fab = document.getElementById(FAB_ID) as HTMLElement
|
|
77
|
+
await waitFor(() => fab.style.display === 'none')
|
|
78
|
+
expect(fetchSnapshot).not.toHaveBeenCalled()
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it('enabled: ball visible, first tick renders the snapshot', async () => {
|
|
82
|
+
initMonitorFab(makeDeps())
|
|
83
|
+
const fab = document.getElementById(FAB_ID) as HTMLElement
|
|
84
|
+
await waitFor(() => fab.style.display === 'block')
|
|
85
|
+
const net = document.querySelector('[data-net]') as HTMLElement
|
|
86
|
+
expect(net.textContent).toBe('8,800')
|
|
87
|
+
expect(fab.classList.contains('lit')).toBe(false)
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
it('negative net lights the ball (负节省警示)', async () => {
|
|
91
|
+
initMonitorFab(makeDeps({ snapshots: [snapshotPatch({ net: { exact: -800, estimated: 0 } })] }))
|
|
92
|
+
await waitFor(() => (document.getElementById(FAB_ID) as HTMLElement).classList.contains('lit'))
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
it('fetch failure flips the stale marker without throwing', async () => {
|
|
96
|
+
initMonitorFab(makeDeps({ fetchSnapshot: vi.fn(async () => { throw new Error('down') }) }))
|
|
97
|
+
await waitFor(() => document.querySelector('[data-stale]')?.classList.contains('show') === true)
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
it('override buttons drive applyOverride then refetch', async () => {
|
|
101
|
+
const deps = makeDeps()
|
|
102
|
+
initMonitorFab(deps)
|
|
103
|
+
await waitFor(() => (document.querySelector('[data-overoff]') as HTMLButtonElement)?.disabled === false)
|
|
104
|
+
;(document.querySelector('[data-overoff]') as HTMLButtonElement).click()
|
|
105
|
+
await waitFor(() => deps.overrides.length === 1)
|
|
106
|
+
expect(deps.overrides).toEqual([{ sessionId: undefined, action: 'off' }])
|
|
107
|
+
await waitFor(() => (deps.fetchSnapshot as ReturnType<typeof vi.fn>).mock.calls.length >= 2)
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
it('position persists across drag-free re-init via localStorage', () => {
|
|
111
|
+
localStorage.setItem('dsh.cci.monitor.pos', JSON.stringify({ x: 40, y: 60 }))
|
|
112
|
+
initMonitorFab(makeDeps())
|
|
113
|
+
const fab = document.getElementById(FAB_ID) as HTMLElement
|
|
114
|
+
expect(fab.style.left).toBe('40px')
|
|
115
|
+
expect(fab.style.top).toBe('60px')
|
|
116
|
+
})
|
|
117
|
+
})
|
|
@@ -1,187 +1,188 @@
|
|
|
1
|
-
// @vitest-environment jsdom
|
|
2
|
-
|
|
3
|
-
import { describe, expect, it } from 'vitest'
|
|
4
|
-
import { apply } from '../src/client/index.ts'
|
|
5
|
-
import type { CompressionSelectorInjected } from '../src/client/CompressionProfileSelector.tsx'
|
|
6
|
-
import type { ContextCompressionSettings } from '../src/profiles.ts'
|
|
7
|
-
|
|
8
|
-
/**
|
|
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.
|
|
15
|
-
*/
|
|
16
|
-
|
|
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>[]
|
|
23
|
-
readonly snapshot: () => Record<string, unknown>
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function createFormStub(initial: ContextCompressionSettings, commit = true): FormStub {
|
|
27
|
-
let revision = 1
|
|
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>[] = []
|
|
31
|
-
const listeners = new Set<() => void>()
|
|
32
|
-
return {
|
|
33
|
-
commits,
|
|
34
|
-
refused,
|
|
35
|
-
snapshot: () => structuredClone(value.settings as Record<string, unknown>),
|
|
36
|
-
form: {
|
|
37
|
-
getSnapshot: () => ({
|
|
38
|
-
status: 'ready' as const,
|
|
39
|
-
value: value as unknown as Record<string, unknown>,
|
|
40
|
-
revision,
|
|
41
|
-
writable: true,
|
|
42
|
-
base: undefined,
|
|
43
|
-
user: undefined,
|
|
44
|
-
mode: 'host' as const,
|
|
45
|
-
}),
|
|
46
|
-
subscribe: (listener: () => void) => {
|
|
47
|
-
listeners.add(listener)
|
|
48
|
-
return () => { listeners.delete(listener) }
|
|
49
|
-
},
|
|
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)
|
|
60
|
-
},
|
|
61
|
-
unset: () => Promise.resolve(false),
|
|
62
|
-
},
|
|
63
|
-
} as FormStub & { form: Record<string, unknown> }
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const DEFAULT_CUSTOM = {
|
|
67
|
-
version: 1,
|
|
68
|
-
unit: 'tokens',
|
|
69
|
-
fresh: { enabled: true, trigger: 4_096, target: 2_048 },
|
|
70
|
-
aggregate: { enabled: true, trigger: 16_384, target: 8_192 },
|
|
71
|
-
history: {
|
|
72
|
-
enabled: true,
|
|
73
|
-
trigger: 16_384,
|
|
74
|
-
keepRecentTurns: 2,
|
|
75
|
-
keepRecent: 16_384,
|
|
76
|
-
minReclaim: 8_192,
|
|
77
|
-
},
|
|
78
|
-
prefixPolicy: 'pressure-break',
|
|
79
|
-
} as const satisfies ContextCompressionSettings['custom']
|
|
80
|
-
|
|
81
|
-
function bindInjected(
|
|
82
|
-
presetOptions: ContextCompressionSettings['presetOptions'],
|
|
83
|
-
commit = true,
|
|
84
|
-
): {
|
|
85
|
-
injected: CompressionSelectorInjected
|
|
86
|
-
stub: FormStub
|
|
87
|
-
} {
|
|
88
|
-
const settings: ContextCompressionSettings = {
|
|
89
|
-
profile: 'tokenpilot-inspired',
|
|
90
|
-
custom: structuredClone(DEFAULT_CUSTOM),
|
|
91
|
-
autoCompact: { thresholdPercent: 80 },
|
|
92
|
-
codeSkeleton: { enabled: false },
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
//
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
//
|
|
112
|
-
locale
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
expect(stub.
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
//
|
|
133
|
-
// so
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
expect(stub.
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
await injected.savePresetOptions({
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
expect(stub.
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
})
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
|
|
3
|
+
import { describe, expect, it } from 'vitest'
|
|
4
|
+
import { apply } from '../src/client/index.ts'
|
|
5
|
+
import type { CompressionSelectorInjected } from '../src/client/CompressionProfileSelector.tsx'
|
|
6
|
+
import type { ContextCompressionSettings } from '../src/profiles.ts'
|
|
7
|
+
|
|
8
|
+
/**
|
|
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.
|
|
15
|
+
*/
|
|
16
|
+
|
|
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>[]
|
|
23
|
+
readonly snapshot: () => Record<string, unknown>
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function createFormStub(initial: ContextCompressionSettings, commit = true): FormStub {
|
|
27
|
+
let revision = 1
|
|
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>[] = []
|
|
31
|
+
const listeners = new Set<() => void>()
|
|
32
|
+
return {
|
|
33
|
+
commits,
|
|
34
|
+
refused,
|
|
35
|
+
snapshot: () => structuredClone(value.settings as Record<string, unknown>),
|
|
36
|
+
form: {
|
|
37
|
+
getSnapshot: () => ({
|
|
38
|
+
status: 'ready' as const,
|
|
39
|
+
value: value as unknown as Record<string, unknown>,
|
|
40
|
+
revision,
|
|
41
|
+
writable: true,
|
|
42
|
+
base: undefined,
|
|
43
|
+
user: undefined,
|
|
44
|
+
mode: 'host' as const,
|
|
45
|
+
}),
|
|
46
|
+
subscribe: (listener: () => void) => {
|
|
47
|
+
listeners.add(listener)
|
|
48
|
+
return () => { listeners.delete(listener) }
|
|
49
|
+
},
|
|
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)
|
|
60
|
+
},
|
|
61
|
+
unset: () => Promise.resolve(false),
|
|
62
|
+
},
|
|
63
|
+
} as FormStub & { form: Record<string, unknown> }
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const DEFAULT_CUSTOM = {
|
|
67
|
+
version: 1,
|
|
68
|
+
unit: 'tokens',
|
|
69
|
+
fresh: { enabled: true, trigger: 4_096, target: 2_048 },
|
|
70
|
+
aggregate: { enabled: true, trigger: 16_384, target: 8_192 },
|
|
71
|
+
history: {
|
|
72
|
+
enabled: true,
|
|
73
|
+
trigger: 16_384,
|
|
74
|
+
keepRecentTurns: 2,
|
|
75
|
+
keepRecent: 16_384,
|
|
76
|
+
minReclaim: 8_192,
|
|
77
|
+
},
|
|
78
|
+
prefixPolicy: 'pressure-break',
|
|
79
|
+
} as const satisfies ContextCompressionSettings['custom']
|
|
80
|
+
|
|
81
|
+
function bindInjected(
|
|
82
|
+
presetOptions: ContextCompressionSettings['presetOptions'],
|
|
83
|
+
commit = true,
|
|
84
|
+
): {
|
|
85
|
+
injected: CompressionSelectorInjected
|
|
86
|
+
stub: FormStub
|
|
87
|
+
} {
|
|
88
|
+
const settings: ContextCompressionSettings = {
|
|
89
|
+
profile: 'tokenpilot-inspired',
|
|
90
|
+
custom: structuredClone(DEFAULT_CUSTOM),
|
|
91
|
+
autoCompact: { thresholdPercent: 80 },
|
|
92
|
+
codeSkeleton: { enabled: false },
|
|
93
|
+
intentSummary: { enabled: false },
|
|
94
|
+
...(presetOptions === undefined ? {} : { presetOptions }),
|
|
95
|
+
}
|
|
96
|
+
const stub = createFormStub(settings, commit)
|
|
97
|
+
let options: Record<string, unknown> | undefined
|
|
98
|
+
const ctx = {
|
|
99
|
+
slots: {
|
|
100
|
+
inject: (_slot: string, factory: () => (() => void) | Generator<() => void>) => {
|
|
101
|
+
const result = factory()
|
|
102
|
+
return typeof result === 'function' ? result : () => {}
|
|
103
|
+
},
|
|
104
|
+
register: (registerOptions: Record<string, unknown>) => {
|
|
105
|
+
// The R4 overlay registration rides the same slots service; only the
|
|
106
|
+
// settings card carries the inject factory these tests drive.
|
|
107
|
+
if (registerOptions.name === 'settings.section') options = registerOptions
|
|
108
|
+
return () => {}
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
// apply consumes the services through their declarative-inject faces
|
|
112
|
+
// (ctx.locale / ctx.configForms), the same shape cordis binds on the host.
|
|
113
|
+
locale: { bind: () => (key: string) => key, register: () => {} },
|
|
114
|
+
configForms: { get: () => (stub.form as unknown as { getSnapshot(): unknown }) },
|
|
115
|
+
}
|
|
116
|
+
apply(ctx as never)
|
|
117
|
+
if (options === undefined) throw new Error('the settings card never registered')
|
|
118
|
+
const factory = options['inject'] as () => CompressionSelectorInjected
|
|
119
|
+
return { injected: factory(), stub }
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
describe('presetOptions writes commit the whole doc through configForms', () => {
|
|
123
|
+
it('keeps the estimator channel when a provider is chosen afterwards', async () => {
|
|
124
|
+
const { injected, stub } = bindInjected({ estimatorMode: 'host' })
|
|
125
|
+
await injected.savePresetOptions({ estimatorProvider: 'local-35b' })
|
|
126
|
+
expect(stub.snapshot()['presetOptions']).toEqual({ estimatorMode: 'host', estimatorProvider: 'local-35b' })
|
|
127
|
+
expect(stub.commits.length).toBe(1)
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
it('treats a retired review-gate patch as a no-op instead of a write', async () => {
|
|
131
|
+
const { injected, stub } = bindInjected({ estimatorMode: 'host' })
|
|
132
|
+
// The patch surface no longer carries these keys, and the decoders accept
|
|
133
|
+
// them only so a legacy document keeps loading. Cast past the removed type
|
|
134
|
+
// so the test pins the RUNTIME behaviour: nothing is written.
|
|
135
|
+
await injected.savePresetOptions({
|
|
136
|
+
reviewMode: true,
|
|
137
|
+
reviewTimeoutTurns: 8,
|
|
138
|
+
cacheHitDiscountAlpha: 0.2,
|
|
139
|
+
reviewHighImpactTokens: 6000,
|
|
140
|
+
} as unknown as Parameters<typeof injected.savePresetOptions>[0])
|
|
141
|
+
expect(stub.commits.length).toBe(0)
|
|
142
|
+
expect(stub.snapshot()['presetOptions']).toEqual({ estimatorMode: 'host' })
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
it('never loses sibling overrides across whole-doc commits', async () => {
|
|
146
|
+
const { injected, stub } = bindInjected({
|
|
147
|
+
estimatorMode: 'direct',
|
|
148
|
+
estimatorBaseUrl: 'http://192.168.100.242:8200',
|
|
149
|
+
dedupeToolResults: false,
|
|
150
|
+
readState: false,
|
|
151
|
+
})
|
|
152
|
+
await injected.savePresetOptions({ estimatorModel: 'Qwen3.6-35B-A3B' })
|
|
153
|
+
await injected.savePresetOptions({ estimatorProvider: 'local-35b' })
|
|
154
|
+
expect(stub.snapshot()['presetOptions']).toEqual({
|
|
155
|
+
estimatorMode: 'direct',
|
|
156
|
+
estimatorBaseUrl: 'http://192.168.100.242:8200',
|
|
157
|
+
dedupeToolResults: false,
|
|
158
|
+
readState: false,
|
|
159
|
+
estimatorModel: 'Qwen3.6-35B-A3B',
|
|
160
|
+
estimatorProvider: 'local-35b',
|
|
161
|
+
})
|
|
162
|
+
// Two user actions, two whole-doc commits.
|
|
163
|
+
expect(stub.commits.length).toBe(2)
|
|
164
|
+
for (const doc of stub.commits) {
|
|
165
|
+
// Each commit carries the complete document under the volatile field.
|
|
166
|
+
expect(Object.keys(doc)).toEqual(['settings'])
|
|
167
|
+
}
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
it('clears exactly the field a patch names with undefined', async () => {
|
|
171
|
+
const { injected, stub } = bindInjected({ estimatorMode: 'direct', estimatorApiKey: 'sk-stored' })
|
|
172
|
+
await injected.savePresetOptions({ estimatorApiKey: undefined })
|
|
173
|
+
expect(stub.snapshot()['presetOptions']).toEqual({ estimatorMode: 'direct' })
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
it('writes nothing when the patch already matches the stored section', async () => {
|
|
177
|
+
const { injected, stub } = bindInjected({ estimatorMode: 'host', estimatorProvider: 'local-35b' })
|
|
178
|
+
await injected.savePresetOptions({ estimatorMode: 'host' })
|
|
179
|
+
expect(stub.commits.length).toBe(0)
|
|
180
|
+
expect(stub.refused.length).toBe(0)
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
it('reports a save the Host did not commit', async () => {
|
|
184
|
+
const { injected } = bindInjected({ estimatorMode: 'host' }, false)
|
|
185
|
+
await expect(injected.savePresetOptions({ estimatorProvider: 'local-35b' }))
|
|
186
|
+
.rejects.toThrow(/were not saved/)
|
|
187
|
+
})
|
|
188
|
+
})
|