dsh-context-compression-improved 0.10.4 → 0.11.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.
- package/.agents/plans/improve/checklist.md +32 -0
- package/.agents/plans/improve/findings.md +102 -0
- package/.agents/plans/improve/spec.md +53 -0
- package/.agents/plans/improve/tasks.md +70 -0
- package/.gitattributes +2 -0
- package/.githooks/pre-push +37 -0
- package/.github/workflows/ci.yml +47 -0
- package/CHANGELOG.de.md +580 -0
- package/CHANGELOG.es.md +588 -0
- package/CHANGELOG.fr.md +576 -0
- package/CHANGELOG.it.md +577 -0
- package/CHANGELOG.ja.md +261 -0
- package/CHANGELOG.ko.md +254 -0
- package/CHANGELOG.md +557 -0
- package/CHANGELOG.ru.md +554 -0
- package/CHANGELOG.zh.md +247 -0
- package/CONTRIBUTING.md +22 -0
- package/README.de.md +96 -0
- package/README.es.md +96 -0
- package/README.fr.md +96 -0
- package/README.it.md +96 -0
- package/README.ja.md +102 -0
- package/README.ko.md +102 -0
- package/README.md +110 -15
- package/README.ru.md +96 -0
- package/README.zh.md +104 -14
- package/SECURITY.md +18 -0
- package/THIRD_PARTY_NOTICES.md +7 -25
- package/assets/board.png +0 -0
- package/assets/context.png +0 -0
- package/docs/assets/context-compression-selector-profiles.jpg +0 -0
- package/docs/assets/context-compression-selector-settings.png +0 -0
- package/docs/compat-0.1.5-report.md +77 -0
- package/docs/installation.de.md +104 -0
- package/docs/installation.es.md +104 -0
- package/docs/installation.fr.md +104 -0
- package/docs/installation.it.md +104 -0
- package/docs/installation.ja.md +78 -0
- package/docs/installation.ko.md +78 -0
- package/docs/installation.md +103 -0
- package/docs/installation.ru.md +104 -0
- package/docs/installation.zh.md +100 -0
- package/docs/repair-log.md +657 -0
- package/docs/shallow-teaching-preset-template.md +46 -0
- package/eslint.config.js +30 -0
- package/package.json +116 -129
- package/packages/selector/LICENSE +21 -0
- package/packages/selector/README.md +26 -0
- package/packages/selector/README.zh.md +24 -0
- package/packages/selector/THIRD_PARTY_NOTICES.md +32 -0
- package/packages/selector/docs/history-tool-call-working-set-spec.md +112 -0
- package/packages/selector/docs/native-tool-result-selector-spec.md +34 -0
- package/packages/selector/docs/subagent-cache-reuse-spec.md +46 -0
- package/{dsh.plugin.json → packages/selector/dsh.plugin.json} +1 -1
- package/{lib → packages/selector/lib}/client.d.ts +8 -0
- package/{lib → packages/selector/lib}/client.js +212 -3
- package/{lib → packages/selector/lib}/index.js +1 -1
- package/{lib → packages/selector/lib}/profiles.d.ts +34 -0
- package/{lib → packages/selector/lib}/pruner.d.ts +55 -0
- package/{lib → packages/selector/lib}/pruner.js +1925 -303
- package/{lib → packages/selector/lib}/savings-integral.js +126 -3
- package/packages/selector/lib/style.css +350 -0
- package/{lib → packages/selector/lib}/tool-result.js +9 -1
- package/packages/selector/package.json +178 -0
- package/packages/selector/screenshots.json +3 -0
- package/packages/selector/src/client/CompressionProfileControls.tsx +352 -0
- package/packages/selector/src/client/CompressionProfileSelector.module.css +199 -0
- package/packages/selector/src/client/CompressionProfileSelector.tsx +89 -0
- package/packages/selector/src/client/CustomPolicyEditor.tsx +216 -0
- package/packages/selector/src/client/EstimatorControls.tsx +277 -0
- package/packages/selector/src/client/bridge-scope.ts +125 -0
- package/packages/selector/src/client/decode.ts +68 -0
- package/packages/selector/src/client/fab-pos.ts +71 -0
- package/packages/selector/src/client/index.ts +438 -0
- package/packages/selector/src/client/locales/de.ts +167 -0
- package/packages/selector/src/client/locales/es.ts +167 -0
- package/packages/selector/src/client/locales/fr.ts +167 -0
- package/packages/selector/src/client/locales/it.ts +167 -0
- package/packages/selector/src/client/locales/ja.ts +167 -0
- package/packages/selector/src/client/locales/ko.ts +167 -0
- package/packages/selector/src/client/locales/ru.ts +167 -0
- package/packages/selector/src/client/locales.ts +334 -0
- package/packages/selector/src/client/monitor-fab.ts +445 -0
- package/packages/selector/src/client/preset-options.ts +88 -0
- package/packages/selector/src/client/savings-card.tsx +269 -0
- package/packages/selector/src/client/scope-face.ts +24 -0
- package/packages/selector/src/client/settings-section.tsx +156 -0
- package/packages/selector/src/compat/host-generation.ts +96 -0
- package/packages/selector/src/compat/llm-messages.ts +50 -0
- package/packages/selector/src/compat/settings-lease.ts +108 -0
- package/packages/selector/src/compat/tool-result.ts +91 -0
- package/packages/selector/src/css-modules.d.ts +6 -0
- package/packages/selector/src/deepseek-v4-tokenizer.ts +205 -0
- package/packages/selector/src/estimator-catalog.ts +104 -0
- package/packages/selector/src/index.ts +890 -0
- package/packages/selector/src/invariant.ts +113 -0
- package/packages/selector/src/preset-overlay.ts +749 -0
- package/packages/selector/src/profiles.ts +554 -0
- package/packages/selector/src/pruner/content.ts +199 -0
- package/packages/selector/src/pruner/session.ts +97 -0
- package/packages/selector/src/pruner/state.ts +61 -0
- package/packages/selector/src/pruner/tuning.ts +23 -0
- package/packages/selector/src/pruner/types.ts +78 -0
- package/packages/selector/src/pruner.ts +3222 -0
- package/packages/selector/src/runtime/adaptive-cost.ts +205 -0
- package/packages/selector/src/runtime/audit.ts +321 -0
- package/packages/selector/src/runtime/config.ts +887 -0
- package/packages/selector/src/runtime/custom-policy.ts +300 -0
- package/packages/selector/src/runtime/deepseek-official-pricing.ts +298 -0
- package/packages/selector/src/runtime/deepseek-v4-vision-tokens.ts +254 -0
- package/packages/selector/src/runtime/display-prefs.ts +98 -0
- package/packages/selector/src/runtime/fold-router.ts +143 -0
- package/packages/selector/src/runtime/measurement.ts +444 -0
- package/packages/selector/src/runtime/monitor.ts +341 -0
- package/packages/selector/src/runtime/panel-command.ts +213 -0
- package/packages/selector/src/runtime/reducers.ts +1755 -0
- package/packages/selector/src/runtime/retrieve.ts +505 -0
- package/packages/selector/src/runtime/savings-integral.ts +117 -0
- package/packages/selector/src/runtime/savings-projection.ts +229 -0
- package/packages/selector/src/runtime/savings-verify.ts +446 -0
- package/packages/selector/src/runtime/savings.ts +526 -0
- package/packages/selector/src/runtime/sediment-card.ts +126 -0
- package/packages/selector/src/runtime/sediment-clock.ts +87 -0
- package/packages/selector/src/runtime/sediment-guard.ts +136 -0
- package/packages/selector/src/runtime/sediment-publish.ts +144 -0
- package/packages/selector/src/runtime/sediment-span.ts +170 -0
- package/packages/selector/src/runtime/sediment.ts +209 -0
- package/packages/selector/src/runtime/session-events.ts +30 -0
- package/packages/selector/src/runtime/session-file-decode.ts +523 -0
- package/packages/selector/src/runtime/shallow-tools.ts +342 -0
- package/packages/selector/src/runtime/stats-backfill.ts +541 -0
- package/packages/selector/src/runtime/stats-db.ts +492 -0
- package/packages/selector/src/runtime/stats-schema.ts +123 -0
- package/packages/selector/src/runtime/stats-sync.ts +307 -0
- package/packages/selector/src/runtime/summary-command.ts +127 -0
- package/packages/selector/src/runtime/tail-trim.ts +177 -0
- package/packages/selector/src/runtime/token-count.ts +72 -0
- package/packages/selector/src/runtime/tokenpilot/advisor-prompt.ts +233 -0
- package/packages/selector/src/runtime/tokenpilot/advisor-state.ts +204 -0
- package/packages/selector/src/runtime/tokenpilot/advisor.ts +419 -0
- package/packages/selector/src/runtime/tokenpilot/benefit.ts +200 -0
- package/packages/selector/src/runtime/tokenpilot/dedup.ts +82 -0
- package/packages/selector/src/runtime/tokenpilot/estimator.ts +151 -0
- 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/tokenpilot/locator.ts +129 -0
- package/packages/selector/src/runtime/tokenpilot/read-state.ts +98 -0
- package/packages/selector/src/runtime/tokenpilot/sidechannel.ts +303 -0
- package/packages/selector/src/runtime/tool-names.ts +42 -0
- package/packages/selector/src/runtime/toolclass.ts +103 -0
- package/packages/selector/src/runtime/triage/masks.ts +128 -0
- package/packages/selector/src/runtime/triage/shallow.ts +132 -0
- package/packages/selector/src/runtime/triage/signals.ts +167 -0
- package/packages/selector/src/runtime/triage/types.ts +125 -0
- package/packages/selector/src/runtime/types.ts +396 -0
- package/packages/selector/src/runtime/value.ts +48 -0
- package/packages/selector/src/settings-bridge.ts +175 -0
- package/packages/selector/tests/advisor-report.host.spec.ts +223 -0
- package/packages/selector/tests/auto-compact.client.spec.tsx +214 -0
- package/packages/selector/tests/built/client-artifact.spec.ts +56 -0
- package/packages/selector/tests/cache-prefix-audit.spec.ts +123 -0
- package/packages/selector/tests/code-skeleton.client.spec.ts +98 -0
- package/packages/selector/tests/compat/compat-narrow-waist.spec.ts +150 -0
- package/packages/selector/tests/custom-contract.client.spec.ts +226 -0
- package/packages/selector/tests/estimator-catalog.spec.ts +70 -0
- package/packages/selector/tests/estimator-channel.client.spec.tsx +253 -0
- package/packages/selector/tests/estimator-route-registration.host.spec.ts +202 -0
- package/packages/selector/tests/fab-pos.client.spec.ts +47 -0
- package/packages/selector/tests/fixtures/sediment-pool/52adc205-cloud-5.orig.txt +231 -0
- package/packages/selector/tests/fixtures/sediment-pool/95fcba5d-cloud-25.orig.txt +236 -0
- package/packages/selector/tests/fixtures/sediment-pool/95fcba5d-cloud-38.orig.txt +220 -0
- package/packages/selector/tests/fixtures/sediment-pool/95fcba5d-cloud-42.orig.txt +140 -0
- package/packages/selector/tests/fixtures/sediment-pool/95fcba5d-cloud-53.orig.txt +117 -0
- package/packages/selector/tests/fixtures/sediment-pool/95fcba5d-local-10.orig.txt +67 -0
- package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-45.orig.txt +119 -0
- package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-49.orig.txt +119 -0
- package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-63.orig.txt +131 -0
- package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-75.orig.txt +196 -0
- package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-76.orig.txt +71 -0
- package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-85.orig.txt +126 -0
- package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-86.orig.txt +86 -0
- package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-87.orig.txt +50 -0
- package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-96.orig.txt +102 -0
- package/packages/selector/tests/helpers/legacy-namespace.ts +35 -0
- package/packages/selector/tests/helpers/legacy-settings/index.ts +15 -0
- package/packages/selector/tests/helpers/legacy-settings/legacy/provider.d.ts +41 -0
- package/packages/selector/tests/helpers/legacy-settings/legacy/provider.js +647 -0
- package/packages/selector/tests/helpers/legacy-settings/legacy/redact.js +78 -0
- package/packages/selector/tests/helpers/legacy-settings/legacy/util-values.js +234 -0
- package/packages/selector/tests/host-preset-overlay.host.spec.ts +204 -0
- package/packages/selector/tests/monitor-fab.client.spec.ts +240 -0
- package/packages/selector/tests/preset-options-write.client.spec.ts +192 -0
- package/packages/selector/tests/preset-overlay-loader.e2e.host.spec.ts +200 -0
- package/packages/selector/tests/preset-overlay.host.spec.ts +246 -0
- package/packages/selector/tests/profiles.client.spec.tsx +436 -0
- package/packages/selector/tests/ptc-custom-write.client.spec.ts +86 -0
- package/packages/selector/tests/public/package-contract.client.spec.ts +89 -0
- package/packages/selector/tests/runtime/adaptive-cost.spec.ts +167 -0
- package/packages/selector/tests/runtime/advice-never-withholds.host.spec.ts +230 -0
- package/packages/selector/tests/runtime/advisor-invariant.spec.ts +272 -0
- package/packages/selector/tests/runtime/advisor.spec.ts +226 -0
- package/packages/selector/tests/runtime/audit.spec.ts +275 -0
- package/packages/selector/tests/runtime/auto-compact-config.spec.ts +523 -0
- package/packages/selector/tests/runtime/char-basis.spec.ts +30 -0
- package/packages/selector/tests/runtime/code-skeleton.spec.ts +152 -0
- package/packages/selector/tests/runtime/deepseek-official-pricing.spec.ts +186 -0
- package/packages/selector/tests/runtime/deepseek-v4-tokenizer.spec.ts +122 -0
- package/packages/selector/tests/runtime/deepseek-v4-vision-tokens.spec.ts +122 -0
- package/packages/selector/tests/runtime/deprecated-preset-options.spec.ts +96 -0
- package/packages/selector/tests/runtime/fixtures/profile-baseline.json +273 -0
- package/packages/selector/tests/runtime/fixtures/session-v4-dump.jsonl +46 -0
- package/packages/selector/tests/runtime/fixtures/tokenizer-golden.json +106 -0
- package/packages/selector/tests/runtime/fixtures/vision-golden.json +459 -0
- package/packages/selector/tests/runtime/fold-router.spec.ts +140 -0
- package/packages/selector/tests/runtime/frequency-longstrings.spec.ts +74 -0
- package/packages/selector/tests/runtime/fresh-gate.spec.ts +43 -0
- package/packages/selector/tests/runtime/html-reducer.spec.ts +212 -0
- 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/jsonl-reducer.spec.ts +79 -0
- package/packages/selector/tests/runtime/line-mapping.spec.ts +153 -0
- package/packages/selector/tests/runtime/monitor.spec.ts +354 -0
- package/packages/selector/tests/runtime/panel-command.spec.ts +101 -0
- package/packages/selector/tests/runtime/prose-reducers.spec.ts +133 -0
- package/packages/selector/tests/runtime/ptc-profile.spec.ts +133 -0
- package/packages/selector/tests/runtime/public/public-runtime.spec.ts +2795 -0
- package/packages/selector/tests/runtime/read-input-cap.spec.ts +33 -0
- package/packages/selector/tests/runtime/savings-accounting.spec.ts +291 -0
- package/packages/selector/tests/runtime/savings-integral.spec.ts +169 -0
- package/packages/selector/tests/runtime/savings-projection.spec.ts +89 -0
- package/packages/selector/tests/runtime/savings-verify.spec.ts +226 -0
- package/packages/selector/tests/runtime/savings.spec.ts +139 -0
- package/packages/selector/tests/runtime/search-reducer.spec.ts +110 -0
- package/packages/selector/tests/runtime/sediment-card.spec.ts +111 -0
- package/packages/selector/tests/runtime/sediment-config.spec.ts +56 -0
- package/packages/selector/tests/runtime/sediment-guard.spec.ts +71 -0
- package/packages/selector/tests/runtime/sediment-masks.spec.ts +95 -0
- package/packages/selector/tests/runtime/sediment-orchestrator.spec.ts +166 -0
- package/packages/selector/tests/runtime/sediment-pool-share.spec.ts +65 -0
- package/packages/selector/tests/runtime/sediment-publish.spec.ts +142 -0
- package/packages/selector/tests/runtime/sediment-real-shapes.spec.ts +158 -0
- package/packages/selector/tests/runtime/sediment-settings.spec.ts +73 -0
- package/packages/selector/tests/runtime/sediment-signals.spec.ts +124 -0
- package/packages/selector/tests/runtime/sediment-span.spec.ts +92 -0
- package/packages/selector/tests/runtime/session-events.spec.ts +27 -0
- package/packages/selector/tests/runtime/session-file-decode.spec.ts +281 -0
- package/packages/selector/tests/runtime/settings-unwrap.spec.ts +58 -0
- package/packages/selector/tests/runtime/shallow-primitives.spec.ts +135 -0
- package/packages/selector/tests/runtime/sidechannel.spec.ts +241 -0
- package/packages/selector/tests/runtime/stats-backfill-scheduler.spec.ts +167 -0
- package/packages/selector/tests/runtime/stats-backfill.spec.ts +350 -0
- package/packages/selector/tests/runtime/stats-db.spec.ts +361 -0
- package/packages/selector/tests/runtime/stats-privacy.spec.ts +55 -0
- package/packages/selector/tests/runtime/stats-sync.spec.ts +278 -0
- package/packages/selector/tests/runtime/summary-command.spec.ts +154 -0
- package/packages/selector/tests/runtime/toc-and-bundled.spec.ts +159 -0
- package/packages/selector/tests/runtime/tokenizer-golden.spec.ts +53 -0
- package/packages/selector/tests/runtime/tokenpilot/benefit.spec.ts +217 -0
- package/packages/selector/tests/runtime/tokenpilot/dedup.spec.ts +52 -0
- package/packages/selector/tests/runtime/tokenpilot/estimator.spec.ts +79 -0
- package/packages/selector/tests/runtime/tokenpilot/locator.spec.ts +76 -0
- package/packages/selector/tests/runtime/tokenpilot/profile-baseline.spec.ts +121 -0
- package/packages/selector/tests/runtime/tokenpilot/read-state.spec.ts +82 -0
- package/packages/selector/tests/runtime/toolclass.spec.ts +156 -0
- package/packages/selector/tests/runtime/value.spec.ts +23 -0
- package/packages/selector/tests/savings-sessions.client.spec.tsx +140 -0
- package/packages/selector/tests/settings-seat.client.spec.ts +215 -0
- package/packages/selector/tests/standing-generation.host.spec.ts +724 -0
- package/packages/selector/tests/subagent-cache-reuse.host.spec.ts +269 -0
- package/packages/selector/tests/support/cache-prefix-audit.ts +105 -0
- package/packages/selector/tests/support/mock-adapter.ts +37 -0
- package/packages/selector/tests/support/ui-primitives.tsx +34 -0
- package/packages/selector/tsconfig.json +11 -0
- package/packages/selector/tsdown.client.config.ts +102 -0
- package/packages/selector/tsdown.config.ts +20 -0
- package/pnpm-workspace.yaml +76 -0
- package/screenshots.json +6 -3
- package/scripts/capture-profile-baseline.mjs +42 -0
- package/scripts/cell-browser-e2e.mjs +354 -0
- package/scripts/generate-tokenizer-fixtures.py +81 -0
- package/scripts/generate-vision-fixtures.py +208 -0
- package/scripts/hosts.d.mts +3 -0
- package/scripts/hosts.mjs +79 -0
- package/scripts/packed-components-smoke.mjs +763 -0
- package/scripts/packed-install-e2e.mjs +1346 -0
- package/scripts/sediment-latency-report.mjs +63 -0
- package/scripts/sync-hosts.mjs +174 -0
- package/scripts/toolclass-corpus-replay.mjs +281 -0
- package/scripts/verify-release.mjs +343 -0
- package/tests/TEST_INVENTORY.md +41 -0
- package/tsconfig.base.json +18 -0
- package/tsconfig.json +7 -0
- package/tsconfig.tests.json +15 -0
- package/vitest.built.config.ts +9 -0
- package/vitest.config.ts +54 -0
- /package/{assets → packages/selector/assets}/deepseek-v4/LICENSE.DeepSeek-V4-Pro.txt +0 -0
- /package/{assets → packages/selector/assets}/deepseek-v4/manifest.json +0 -0
- /package/{assets → packages/selector/assets}/deepseek-v4/tokenizer.json +0 -0
- /package/{assets → packages/selector/assets}/deepseek-v4/tokenizer_config.json +0 -0
- /package/{assets → packages/selector/assets}/deepseek-v4-vision-exp/LICENSE.DeepSeek-V4-Flash-Vision-Exp.txt +0 -0
- /package/{assets → packages/selector/assets}/deepseek-v4-vision-exp/manifest.json +0 -0
- /package/{assets → packages/selector/assets}/deepseek-v4-vision-exp/tokenizer.json +0 -0
- /package/{assets → packages/selector/assets}/deepseek-v4-vision-exp/tokenizer_config.json +0 -0
- /package/{cordis.patch.yml → packages/selector/cordis.patch.yml} +0 -0
- /package/{lib → packages/selector/lib}/index.d.ts +0 -0
- /package/{lib → packages/selector/lib}/invariant.d.ts +0 -0
- /package/{lib → packages/selector/lib}/invariant.js +0 -0
- /package/{lib → packages/selector/lib}/tail-trim.js +0 -0
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
|
|
3
|
+
import { describe, expect, it, vi } from 'vitest'
|
|
4
|
+
import {
|
|
5
|
+
DEFAULT_CUSTOM_COMPRESSION_POLICY,
|
|
6
|
+
DEFAULT_PTC_COMPRESSION_POLICY,
|
|
7
|
+
type ContextCompressionSettings,
|
|
8
|
+
} from '../src/profiles.ts'
|
|
9
|
+
import { apply } from '../src/client/index.ts'
|
|
10
|
+
import type { CompressionSelectorInjected } from '../src/client/CompressionProfileSelector.tsx'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* PTC writes only touch `ptcCustom`: the scope commits the whole settings
|
|
14
|
+
* document with the single field replaced, so `custom` / `tokenpilot-inspired`
|
|
15
|
+
* policies are never collateral damage.
|
|
16
|
+
*/
|
|
17
|
+
describe('ptcCustom write isolation', () => {
|
|
18
|
+
it('writes only ptcCustom and leaves custom untouched', async () => {
|
|
19
|
+
const originalCustom = structuredClone(DEFAULT_CUSTOM_COMPRESSION_POLICY)
|
|
20
|
+
const originalPtc = structuredClone(DEFAULT_PTC_COMPRESSION_POLICY)
|
|
21
|
+
let revision = 1
|
|
22
|
+
let value: ContextCompressionSettings = {
|
|
23
|
+
profile: 'ptc',
|
|
24
|
+
custom: originalCustom,
|
|
25
|
+
ptcCustom: originalPtc,
|
|
26
|
+
autoCompact: { thresholdPercent: 80 },
|
|
27
|
+
codeSkeleton: { enabled: false },
|
|
28
|
+
intentSummary: { enabled: false },
|
|
29
|
+
}
|
|
30
|
+
const form = {
|
|
31
|
+
getSnapshot: () => ({
|
|
32
|
+
status: 'ready' as const,
|
|
33
|
+
value: { settings: value },
|
|
34
|
+
base: undefined,
|
|
35
|
+
user: undefined,
|
|
36
|
+
revision,
|
|
37
|
+
writable: true,
|
|
38
|
+
mode: 'host' as const,
|
|
39
|
+
}),
|
|
40
|
+
subscribe: () => () => {},
|
|
41
|
+
set: vi.fn(async (_field: string, next: unknown) => {
|
|
42
|
+
revision += 1
|
|
43
|
+
value = structuredClone(next) as ContextCompressionSettings
|
|
44
|
+
return true
|
|
45
|
+
}),
|
|
46
|
+
unset: vi.fn(async () => false),
|
|
47
|
+
}
|
|
48
|
+
let injected: (() => CompressionSelectorInjected) | undefined
|
|
49
|
+
const ctx = {
|
|
50
|
+
effect: (install: () => unknown) => { install() },
|
|
51
|
+
locale: { register: vi.fn(() => () => {}) },
|
|
52
|
+
configForms: { get: vi.fn(() => form) },
|
|
53
|
+
inject: (services: string[], cb: (service: unknown) => void) => {
|
|
54
|
+
if (services.includes('configForms')) cb(ctx.configForms)
|
|
55
|
+
},
|
|
56
|
+
slots: {
|
|
57
|
+
inject: (_slot: string, install: () => unknown) => { install() },
|
|
58
|
+
register: (registration: { name?: string, inject?: () => CompressionSelectorInjected }) => {
|
|
59
|
+
if (registration.name === 'settings.section' && typeof registration.inject === 'function') injected = registration.inject
|
|
60
|
+
return () => {}
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
} as unknown as Parameters<typeof apply>[0]
|
|
64
|
+
|
|
65
|
+
apply(ctx)
|
|
66
|
+
const actions = injected?.()
|
|
67
|
+
expect(actions).toBeDefined()
|
|
68
|
+
if (actions === undefined) throw new Error('selector injection was not registered')
|
|
69
|
+
|
|
70
|
+
const editedPtc = {
|
|
71
|
+
...structuredClone(DEFAULT_PTC_COMPRESSION_POLICY),
|
|
72
|
+
history: { ...structuredClone(DEFAULT_PTC_COMPRESSION_POLICY).history, trigger: 18_000 },
|
|
73
|
+
}
|
|
74
|
+
await expect(actions.savePtcCustom(editedPtc)).resolves.toBeUndefined()
|
|
75
|
+
expect(form.set).toHaveBeenLastCalledWith('settings', expect.objectContaining({
|
|
76
|
+
ptcCustom: expect.objectContaining({ history: expect.objectContaining({ trigger: 18_000 }) }),
|
|
77
|
+
}))
|
|
78
|
+
expect(value.ptcCustom?.history.trigger).toBe(18_000)
|
|
79
|
+
// custom must be byte-identical to its pre-write value.
|
|
80
|
+
expect(value.custom).toEqual(originalCustom)
|
|
81
|
+
|
|
82
|
+
await expect(actions.resetPtcCustom()).resolves.toBeUndefined()
|
|
83
|
+
expect(value.ptcCustom).toEqual(DEFAULT_PTC_COMPRESSION_POLICY)
|
|
84
|
+
expect(value.custom).toEqual(originalCustom)
|
|
85
|
+
})
|
|
86
|
+
})
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs'
|
|
2
|
+
import { resolve } from 'node:path'
|
|
3
|
+
import { describe, expect, it } from 'vitest'
|
|
4
|
+
import { supportedHosts } from '../../../../scripts/hosts.mjs'
|
|
5
|
+
|
|
6
|
+
const root = resolve(import.meta.dirname, '../../..')
|
|
7
|
+
|
|
8
|
+
describe('standalone package contract', () => {
|
|
9
|
+
it('ships one install-facing package with no intra-repo dependency edge', () => {
|
|
10
|
+
const selector = JSON.parse(readFileSync(resolve(root, 'selector/package.json'), 'utf8')) as {
|
|
11
|
+
name: string
|
|
12
|
+
version: string
|
|
13
|
+
dependencies?: Record<string, string>
|
|
14
|
+
peerDependencies?: Record<string, string>
|
|
15
|
+
engines?: Record<string, string>
|
|
16
|
+
publishConfig?: { tag?: string }
|
|
17
|
+
}
|
|
18
|
+
expect(selector.name).toBe('dsh-context-compression-improved')
|
|
19
|
+
expect(selector.dependencies?.['@huggingface/tokenizers']).toBe('0.1.3')
|
|
20
|
+
expect(Object.keys(selector.dependencies ?? {})).not.toContain('dsh-context-compression-improved-runtime')
|
|
21
|
+
expect(Object.keys(selector.peerDependencies ?? {})).not.toContain('dsh-context-compression-improved-runtime')
|
|
22
|
+
expect(Object.keys(selector.peerDependencies ?? {})).not.toContain('@deepseek-ai/dsh-compaction-tool-result-pruner')
|
|
23
|
+
expect(selector.peerDependencies?.['@deepseek-ai/dsh-compaction-basic'])
|
|
24
|
+
.toBe(selector.engines?.dsh)
|
|
25
|
+
expect(selector.peerDependencies?.['@deepseek-ai/dsh-command-compact'])
|
|
26
|
+
.toBe(selector.engines?.dsh)
|
|
27
|
+
// compat-legacy 七线:selector=peer 一致性面,dsh-* peer 与 engines.dsh 同为
|
|
28
|
+
// hosts.mjs supportedHosts 枚举(长度取单源,补票新 rc 时零 spec 改动)。
|
|
29
|
+
expect(selector.engines?.dsh?.split(' || ')).toHaveLength(supportedHosts.length)
|
|
30
|
+
expect(selector.publishConfig?.tag).toBe('latest')
|
|
31
|
+
expect(existsSync(resolve(root, '../runtime/package.json'))).toBe(false)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('makes the repository root the one git install surface', () => {
|
|
35
|
+
const rootManifest = JSON.parse(readFileSync(resolve(root, '../package.json'), 'utf8')) as {
|
|
36
|
+
name: string
|
|
37
|
+
exports?: Record<string, unknown>
|
|
38
|
+
dependencies?: Record<string, string>
|
|
39
|
+
peerDependencies?: Record<string, string>
|
|
40
|
+
engines?: Record<string, string>
|
|
41
|
+
dsh?: { bundle?: { patch?: string }, client?: { inject?: string[] } }
|
|
42
|
+
}
|
|
43
|
+
expect(rootManifest.name).toBe('dsh-context-compression-improved')
|
|
44
|
+
for (const specifier of ['.', './invariant', './pruner', './client']) {
|
|
45
|
+
expect(rootManifest.exports?.[specifier]).toBeDefined()
|
|
46
|
+
}
|
|
47
|
+
expect(rootManifest.dependencies?.['@huggingface/tokenizers']).toBe('0.1.3')
|
|
48
|
+
expect(rootManifest.dependencies?.['js-yaml']).toBe('^4.3.2')
|
|
49
|
+
expect(rootManifest.dsh?.bundle?.patch).toBe('./packages/selector/cordis.patch.yml')
|
|
50
|
+
expect(rootManifest.dsh?.client?.inject).toEqual([
|
|
51
|
+
'@deepseek-ai/dsh-client-locale',
|
|
52
|
+
'@deepseek-ai/dsh-client-ui-primitives',
|
|
53
|
+
'@deepseek-ai/dsh-client-ui-slots',
|
|
54
|
+
'@deepseek-ai/dsh-client-ui-settings',
|
|
55
|
+
])
|
|
56
|
+
// v0.10.5 决策:root=安装面,engines.dsh 为 range(">=lower <upper"),
|
|
57
|
+
// 全部 dsh-* peer 与 engines 同串(下方 loop 钉住);selector 面保持枚举。
|
|
58
|
+
expect(rootManifest.engines?.dsh).toMatch(/^>=\S+ <\S+$/)
|
|
59
|
+
for (const range of Object.values(rootManifest.peerDependencies ?? {})) {
|
|
60
|
+
expect(range).toBe(rootManifest.engines?.dsh)
|
|
61
|
+
}
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it('uses the community package in the one Bundle patch', () => {
|
|
65
|
+
const patch = readFileSync(resolve(root, 'selector/cordis.patch.yml'), 'utf8')
|
|
66
|
+
expect(patch).toContain("name: 'dsh-context-compression-improved'")
|
|
67
|
+
expect(patch).not.toContain('@deepseek-ai/dsh-client-ui-context-compression-selector')
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
it('never ships a retired plugin config key in the Bundle patch', () => {
|
|
71
|
+
// Negative control: the patch is loaded by the host and validated against
|
|
72
|
+
// the plugin's own Config schema, but nothing tied the two together — so
|
|
73
|
+
// the retired review gate's `reviewQueueRoute` survived in this file after
|
|
74
|
+
// the key was removed from the schema, advertising a route that can never
|
|
75
|
+
// register. A retired key must fail here instead of shipping quietly.
|
|
76
|
+
//
|
|
77
|
+
// The pin is on KEY-SETTING lines, not on the whole document: the comments
|
|
78
|
+
// above the rows deliberately name the retired key while explaining why it
|
|
79
|
+
// is gone, and prose must not be mistaken for configuration.
|
|
80
|
+
const patch = readFileSync(resolve(root, 'selector/cordis.patch.yml'), 'utf8')
|
|
81
|
+
const lines = patch.split('\n')
|
|
82
|
+
for (const retired of ['reviewQueueRoute', 'reviewMode', 'reviewTimeoutTurns', 'reviewHighImpactTokens']) {
|
|
83
|
+
const setsRetired = lines.find(line => new RegExp(`^\\s*${retired}\\s*:`).test(line))
|
|
84
|
+
expect(setsRetired, `${retired} is still set in the Bundle patch`).toBeUndefined()
|
|
85
|
+
}
|
|
86
|
+
// And the live flag the routes row exists for is still wired.
|
|
87
|
+
expect(patch).toContain('estimatorCatalogRoute: true')
|
|
88
|
+
})
|
|
89
|
+
})
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import {
|
|
3
|
+
decideConservativeAdaptive,
|
|
4
|
+
deriveAdaptiveTokenBounds,
|
|
5
|
+
} from '../../src/runtime/adaptive-cost.ts'
|
|
6
|
+
import type {
|
|
7
|
+
ExactTokenizerTokenCount,
|
|
8
|
+
TokenizerEstimateTokenCount,
|
|
9
|
+
} from '../../src/runtime/token-count.ts'
|
|
10
|
+
|
|
11
|
+
const exact = (
|
|
12
|
+
tokens: number,
|
|
13
|
+
tokenizerRevision = 'DeepSeek-V4-Pro-0813',
|
|
14
|
+
): ExactTokenizerTokenCount => ({
|
|
15
|
+
kind: 'exact-tokenizer',
|
|
16
|
+
tokens,
|
|
17
|
+
tokenizerId: 'deepseek-v4-official',
|
|
18
|
+
tokenizerRevision,
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
const estimate = (
|
|
22
|
+
input: Omit<TokenizerEstimateTokenCount, 'kind'>,
|
|
23
|
+
): TokenizerEstimateTokenCount => ({ kind: 'tokenizer-estimate', ...input })
|
|
24
|
+
|
|
25
|
+
describe('Conservative Adaptive interval', () => {
|
|
26
|
+
it('subtracts removed tokens from the affected retained suffix instead of charging them twice', () => {
|
|
27
|
+
expect(deriveAdaptiveTokenBounds({
|
|
28
|
+
exactReclaimedTokens: 20_000,
|
|
29
|
+
earliestChangedSeq: 2,
|
|
30
|
+
previousPromptTokens: 30_500,
|
|
31
|
+
expectedTokenizerRevision: 'DeepSeek-V4-Pro-0813',
|
|
32
|
+
previousRequestMeasurement: exact(30_500),
|
|
33
|
+
measuredNodes: [
|
|
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
|
+
],
|
|
38
|
+
})).toEqual({
|
|
39
|
+
kind: 'available',
|
|
40
|
+
measurementKind: 'exact-tokenizer',
|
|
41
|
+
reclaimedLowerBoundTokens: 20_000,
|
|
42
|
+
affectedRetainedSuffixUpperBoundTokens: 500,
|
|
43
|
+
exactPrefixLowerBoundTokens: 10_000,
|
|
44
|
+
})
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it('subtracts the tokenizer-estimate conservative margin from reclaimed benefit', () => {
|
|
48
|
+
expect(deriveAdaptiveTokenBounds({
|
|
49
|
+
exactReclaimedTokens: 20_000,
|
|
50
|
+
earliestChangedSeq: 2,
|
|
51
|
+
previousPromptTokens: 30_500,
|
|
52
|
+
expectedTokenizerRevision: 'DeepSeek-V4-Pro-0813',
|
|
53
|
+
previousRequestMeasurement: estimate({
|
|
54
|
+
tokens: 30_300,
|
|
55
|
+
upperBoundTokens: 30_500,
|
|
56
|
+
estimatorId: 'deepseek-wire',
|
|
57
|
+
estimatorRevision: 'r1',
|
|
58
|
+
calibration: { sampleCount: 1, conservativeMarginTokens: 200 },
|
|
59
|
+
}),
|
|
60
|
+
measuredNodes: [{ seq: 0, count: exact(10_000), characterPressure: 40_000 }],
|
|
61
|
+
})).toMatchObject({
|
|
62
|
+
kind: 'available',
|
|
63
|
+
measurementKind: 'tokenizer-estimate',
|
|
64
|
+
reclaimedLowerBoundTokens: 19_800,
|
|
65
|
+
affectedRetainedSuffixUpperBoundTokens: 700,
|
|
66
|
+
})
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
it.each([
|
|
70
|
+
[{ kind: 'unavailable', reason: 'no request tokenizer' } as const, 'request-measurement-unavailable'],
|
|
71
|
+
[estimate({
|
|
72
|
+
tokens: 10, upperBoundTokens: 20,
|
|
73
|
+
estimatorId: 'e', estimatorRevision: 'r',
|
|
74
|
+
}), 'estimate-calibration-unavailable'],
|
|
75
|
+
] as const)('fails closed when D/S cannot be bounded: %s', (measurement, reason) => {
|
|
76
|
+
expect(deriveAdaptiveTokenBounds({
|
|
77
|
+
exactReclaimedTokens: 100,
|
|
78
|
+
earliestChangedSeq: 2,
|
|
79
|
+
previousPromptTokens: 200,
|
|
80
|
+
expectedTokenizerRevision: 'DeepSeek-V4-Pro-0813',
|
|
81
|
+
previousRequestMeasurement: measurement,
|
|
82
|
+
measuredNodes: [{ seq: 0, count: exact(50), characterPressure: 200 }],
|
|
83
|
+
})).toEqual({ kind: 'unknown', reason })
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
it('fails closed when the exact request count does not match the provider key tokenizer revision', () => {
|
|
87
|
+
expect(deriveAdaptiveTokenBounds({
|
|
88
|
+
exactReclaimedTokens: 100,
|
|
89
|
+
earliestChangedSeq: 2,
|
|
90
|
+
previousPromptTokens: 200,
|
|
91
|
+
expectedTokenizerRevision: 'DeepSeek-V4-Pro-0813',
|
|
92
|
+
previousRequestMeasurement: exact(200, 'DeepSeek-V4-Flash-0731'),
|
|
93
|
+
measuredNodes: [{ seq: 0, count: exact(50), characterPressure: 200 }],
|
|
94
|
+
})).toEqual({ kind: 'unknown', reason: 'request-tokenizer-revision-mismatch' })
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
it('fails closed before subtracting an exact prefix from another tokenizer revision', () => {
|
|
98
|
+
expect(deriveAdaptiveTokenBounds({
|
|
99
|
+
exactReclaimedTokens: 100,
|
|
100
|
+
earliestChangedSeq: 2,
|
|
101
|
+
previousPromptTokens: 200,
|
|
102
|
+
expectedTokenizerRevision: 'DeepSeek-V4-Pro-0813',
|
|
103
|
+
previousRequestMeasurement: exact(200),
|
|
104
|
+
measuredNodes: [{ seq: 0, count: exact(50, 'DeepSeek-V4-Flash-0731'), characterPressure: 200 }],
|
|
105
|
+
})).toEqual({ kind: 'unknown', reason: 'exact-prefix-tokenizer-revision-mismatch' })
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
it('allows routine History only when minimum removal value beats maximum cache-loss penalty', () => {
|
|
109
|
+
expect(decideConservativeAdaptive({
|
|
110
|
+
capacityPressure: false,
|
|
111
|
+
bounds: {
|
|
112
|
+
kind: 'available', measurementKind: 'exact-tokenizer',
|
|
113
|
+
reclaimedLowerBoundTokens: 20_000,
|
|
114
|
+
affectedRetainedSuffixUpperBoundTokens: 500,
|
|
115
|
+
exactPrefixLowerBoundTokens: 10_000,
|
|
116
|
+
},
|
|
117
|
+
inputCacheHitRate: '0.014',
|
|
118
|
+
inputCacheMissRate: '0.44',
|
|
119
|
+
})).toMatchObject({
|
|
120
|
+
allowHistory: true,
|
|
121
|
+
reason: 'cost-interval-clearly-favourable',
|
|
122
|
+
})
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
it.each([
|
|
126
|
+
[19_000, 1_000, 'cache-risk-not-clearly-paid-back'],
|
|
127
|
+
[20_000, 500, 'adaptive-unknown-price'],
|
|
128
|
+
] as const)('fails closed for overlap or unknown price', (reclaimed, suffix, expected) => {
|
|
129
|
+
expect(decideConservativeAdaptive({
|
|
130
|
+
capacityPressure: false,
|
|
131
|
+
bounds: {
|
|
132
|
+
kind: 'available', measurementKind: 'exact-tokenizer',
|
|
133
|
+
reclaimedLowerBoundTokens: reclaimed,
|
|
134
|
+
affectedRetainedSuffixUpperBoundTokens: suffix,
|
|
135
|
+
exactPrefixLowerBoundTokens: 0,
|
|
136
|
+
},
|
|
137
|
+
...(expected === 'adaptive-unknown-price'
|
|
138
|
+
? {}
|
|
139
|
+
: { inputCacheHitRate: '0.014', inputCacheMissRate: '0.44' }),
|
|
140
|
+
})).toMatchObject({ allowHistory: false, reason: expected })
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
it('lets capacity safety override missing price and telemetry', () => {
|
|
144
|
+
expect(decideConservativeAdaptive({
|
|
145
|
+
capacityPressure: true,
|
|
146
|
+
bounds: { kind: 'unknown', reason: 'usage-unavailable' },
|
|
147
|
+
})).toEqual({ allowHistory: true, reason: 'capacity-override' })
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
it('uses request-level cache hits only as a conservative exposure cap', () => {
|
|
151
|
+
expect(decideConservativeAdaptive({
|
|
152
|
+
capacityPressure: false,
|
|
153
|
+
bounds: {
|
|
154
|
+
kind: 'available', measurementKind: 'exact-tokenizer',
|
|
155
|
+
reclaimedLowerBoundTokens: 20_000,
|
|
156
|
+
affectedRetainedSuffixUpperBoundTokens: 10_000,
|
|
157
|
+
exactPrefixLowerBoundTokens: 0,
|
|
158
|
+
},
|
|
159
|
+
observedCacheReadTokens: 100,
|
|
160
|
+
inputCacheHitRate: '0.014',
|
|
161
|
+
inputCacheMissRate: '0.44',
|
|
162
|
+
})).toMatchObject({
|
|
163
|
+
allowHistory: true,
|
|
164
|
+
maximumCacheLossPenalty: '42600000000',
|
|
165
|
+
})
|
|
166
|
+
})
|
|
167
|
+
})
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The retired review gate's replacement, pinned at the host integration level.
|
|
3
|
+
*
|
|
4
|
+
* This spec exists to make the OLD failure impossible to reintroduce: with the
|
|
5
|
+
* exact settings that used to divert 100% of a fresh batch into the human-gated
|
|
6
|
+
* review queue (`reviewMode: true` + a `reviewHighImpactTokens` threshold far
|
|
7
|
+
* below the batch), the batch must LAND, and the benefit model must publish its
|
|
8
|
+
* opinion as a `reduction-advice` audit instead of withholding anything.
|
|
9
|
+
*
|
|
10
|
+
* "缩减不阻断自动处理": advice describes a landing, it never gates one.
|
|
11
|
+
*/
|
|
12
|
+
import { describe, expect, it, vi } from 'vitest'
|
|
13
|
+
import { Context } from '@deepseek-ai/cordis'
|
|
14
|
+
import {
|
|
15
|
+
ToolCallId as CallId,
|
|
16
|
+
createMessage,
|
|
17
|
+
createUserMessage,
|
|
18
|
+
createToolResultMessage,
|
|
19
|
+
} from '@deepseek-ai/dsh-llm'
|
|
20
|
+
import SessionStore, {
|
|
21
|
+
Session,
|
|
22
|
+
SessionId,
|
|
23
|
+
canonicalHeader,
|
|
24
|
+
} from '@deepseek-ai/dsh-session'
|
|
25
|
+
import { type SettingsNamespace } from '@deepseek-ai/dsh-settings'
|
|
26
|
+
import { LegacySettingsProvider } from '../helpers/legacy-settings/index.ts'
|
|
27
|
+
import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
|
|
28
|
+
import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection'
|
|
29
|
+
import TokenMeter from '@deepseek-ai/dsh-token-meter'
|
|
30
|
+
import ToolRuntime from '@deepseek-ai/dsh-tools'
|
|
31
|
+
import { LegacyNamespaceRegistrar } from '../helpers/legacy-namespace.ts'
|
|
32
|
+
import ToolResultPruner, {
|
|
33
|
+
CONTEXT_COMPRESSION_SETTINGS_NAMESPACE,
|
|
34
|
+
} from '../../src/pruner.ts'
|
|
35
|
+
import { measureForCompaction } from '../../src/runtime/measurement.ts'
|
|
36
|
+
import {
|
|
37
|
+
COMPRESSION_AUDIT_PREFIX,
|
|
38
|
+
type CompressionAuditRecord,
|
|
39
|
+
type CompressionRewriteAuditRecord,
|
|
40
|
+
type ReductionAdviceAuditRecord,
|
|
41
|
+
} from '../../src/runtime/audit.ts'
|
|
42
|
+
|
|
43
|
+
const MODEL = 'deepseek-v4-flash'
|
|
44
|
+
|
|
45
|
+
class TestSettings extends LegacySettingsProvider {
|
|
46
|
+
readonly writable = true
|
|
47
|
+
private readonly stored: Record<string, unknown> = {}
|
|
48
|
+
|
|
49
|
+
protected override load(): Promise<Record<string, unknown>> {
|
|
50
|
+
return Promise.resolve(structuredClone(this.stored))
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
protected override persist(namespace: SettingsNamespace, section: Record<string, unknown>): Promise<void> {
|
|
54
|
+
this.stored[namespace] = structuredClone(section)
|
|
55
|
+
return Promise.resolve()
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async function runtimeContext(): Promise<Context> {
|
|
60
|
+
const ctx = new Context()
|
|
61
|
+
await ctx.plugin(SessionStore).await()
|
|
62
|
+
await ctx.plugin(SystemPrompt).await()
|
|
63
|
+
await ctx.plugin(ToolRuntime).await()
|
|
64
|
+
await ctx.plugin(SessionProjectionRegistry).await()
|
|
65
|
+
await ctx.plugin(TokenMeter).await()
|
|
66
|
+
return ctx
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function captureAudit(ctx: Context): { records(): CompressionAuditRecord[] } {
|
|
70
|
+
const info = vi.spyOn(ctx.logger, 'info').mockImplementation(() => ctx.logger)
|
|
71
|
+
return {
|
|
72
|
+
records: () => info.mock.calls.flatMap((call) => {
|
|
73
|
+
const line = String(call[0])
|
|
74
|
+
return line.startsWith(COMPRESSION_AUDIT_PREFIX)
|
|
75
|
+
? [JSON.parse(line.slice(COMPRESSION_AUDIT_PREFIX.length)) as CompressionAuditRecord]
|
|
76
|
+
: []
|
|
77
|
+
}),
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function rewrites(records: readonly CompressionAuditRecord[]): CompressionRewriteAuditRecord[] {
|
|
82
|
+
return records.filter((record): record is CompressionRewriteAuditRecord => record.kind === 'rewrite')
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function adviceOf(records: readonly CompressionAuditRecord[]): ReductionAdviceAuditRecord[] {
|
|
86
|
+
return records.filter((record): record is ReductionAdviceAuditRecord => record.kind === 'reduction-advice')
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const nsBrand = (value: string): SettingsNamespace => value as unknown as SettingsNamespace
|
|
90
|
+
|
|
91
|
+
function appendToolTurn(session: Session, turn: number, text: string): void {
|
|
92
|
+
const callId = CallId(`call-${String(turn)}`)
|
|
93
|
+
session.append('turn/start', { turn })
|
|
94
|
+
if (session.requestHeader() === undefined) {
|
|
95
|
+
session.append('request/header', {
|
|
96
|
+
reason: 'initial',
|
|
97
|
+
header: canonicalHeader({ config: { provider: 'deepseek', model: MODEL } }),
|
|
98
|
+
})
|
|
99
|
+
}
|
|
100
|
+
session.append('user/message', createUserMessage({
|
|
101
|
+
content: [{ type: 'text', text: `user turn ${String(turn)}` }],
|
|
102
|
+
source: { kind: 'user' },
|
|
103
|
+
}), { surfaceOp: 'append' })
|
|
104
|
+
session.append('step/start', { turn, step: 1 })
|
|
105
|
+
session.append('assistant/message', {
|
|
106
|
+
stream: [],
|
|
107
|
+
turn,
|
|
108
|
+
step: 1,
|
|
109
|
+
message: createMessage({
|
|
110
|
+
role: 'assistant',
|
|
111
|
+
content: [{ type: 'tool-call', id: callId, name: 'bash', arguments: '{}' }],
|
|
112
|
+
source: { kind: 'model', provider: 'deepseek', model: MODEL },
|
|
113
|
+
}),
|
|
114
|
+
}, { surfaceOp: 'append' })
|
|
115
|
+
session.append('tool/call', { turn, step: 1, callId, name: 'bash', arguments: '{}' })
|
|
116
|
+
session.append('tool/result', {
|
|
117
|
+
turn,
|
|
118
|
+
step: 1,
|
|
119
|
+
message: createToolResultMessage({
|
|
120
|
+
callId,
|
|
121
|
+
content: [{ type: 'text', text }],
|
|
122
|
+
isError: false,
|
|
123
|
+
}),
|
|
124
|
+
}, { surfaceOp: 'append' })
|
|
125
|
+
session.append('step/end', { turn, step: 1 })
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* The settings that used to withhold everything: review mode ON with a
|
|
130
|
+
* high-impact threshold of 1 token. History pricing is kept production-like
|
|
131
|
+
* (64,000 protected tail tokens) so the batch is only landable through the
|
|
132
|
+
* fresh stage.
|
|
133
|
+
*/
|
|
134
|
+
async function gatedSettings(ctx: Context): Promise<void> {
|
|
135
|
+
await ctx.plugin(TestSettings).await()
|
|
136
|
+
await ctx.plugin(LegacyNamespaceRegistrar).await()
|
|
137
|
+
await ctx.settings.update(nsBrand(CONTEXT_COMPRESSION_SETTINGS_NAMESPACE), {
|
|
138
|
+
profile: 'tokenpilot-inspired',
|
|
139
|
+
presetOptions: { reviewMode: true, reviewHighImpactTokens: 1 },
|
|
140
|
+
})
|
|
141
|
+
await ctx.plugin(ToolResultPruner, {
|
|
142
|
+
profile: 'tokenpilot-inspired',
|
|
143
|
+
freshTriggerTokens: 200,
|
|
144
|
+
freshTargetTokens: 100,
|
|
145
|
+
aggregateTriggerTokens: 1_000_000,
|
|
146
|
+
aggregateTargetTokens: 900_000,
|
|
147
|
+
historyTriggerTokens: 400,
|
|
148
|
+
historyKeepRecentToolCalls: 0,
|
|
149
|
+
historyKeepRecentTokens: 64_000,
|
|
150
|
+
historyMinReclaimTokens: 1,
|
|
151
|
+
}).await()
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function freshSession(ctx: Context, id: string, text: string): Session {
|
|
155
|
+
const session = Session.create(SessionId(id))
|
|
156
|
+
appendToolTurn(session, 1, text)
|
|
157
|
+
const total = measureForCompaction(ctx, session).totalTokens
|
|
158
|
+
session.append('request/context', {
|
|
159
|
+
provider: 'deepseek',
|
|
160
|
+
model: MODEL,
|
|
161
|
+
contextWindow: Math.floor(total / 0.6),
|
|
162
|
+
})
|
|
163
|
+
// Fresh landing publishes a surface replacement, which requires an open turn.
|
|
164
|
+
session.append('turn/start', { turn: 2 })
|
|
165
|
+
return session
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** ~36,400 characters. The shipped advice threshold is 4,000 tokens, and this
|
|
169
|
+
* fixture is repetitive enough that the exact tokenizer packs it denser than
|
|
170
|
+
* the conservative 4-chars-per-token estimate (an 18,200-character variant
|
|
171
|
+
* measured under 4,000 tokens), so the volume is doubled to keep the
|
|
172
|
+
* `high-impact` label deterministic. */
|
|
173
|
+
const HIGH_IMPACT_TEXT = 'fresh reviewable evidence '.repeat(1_400)
|
|
174
|
+
|
|
175
|
+
describe('advice never withholds (retired review gate)', () => {
|
|
176
|
+
it('lands the fresh batch the gate used to divert, and advises instead', async () => {
|
|
177
|
+
const ctx = await runtimeContext()
|
|
178
|
+
await gatedSettings(ctx)
|
|
179
|
+
const audit = captureAudit(ctx)
|
|
180
|
+
const session = freshSession(ctx, 'advice-lands', HIGH_IMPACT_TEXT)
|
|
181
|
+
|
|
182
|
+
const result = ctx.toolResultPruner.pruneSession(session, { stage: 'fresh', freshTurn: 1, freshStep: 1 })
|
|
183
|
+
const records = audit.records()
|
|
184
|
+
|
|
185
|
+
// 1. The reduction LANDED. Under the retired gate this was the assertion
|
|
186
|
+
// that failed: the batch was withheld pending human approval.
|
|
187
|
+
expect(result.pruned).toHaveLength(1)
|
|
188
|
+
expect(rewrites(records).some(entry => entry.component === 'fresh')).toBe(true)
|
|
189
|
+
|
|
190
|
+
// 2. The benefit model still speaks — as advice about the landing.
|
|
191
|
+
const advice = adviceOf(records)
|
|
192
|
+
expect(advice).toHaveLength(1)
|
|
193
|
+
expect(advice[0]!.stage).toBe('fresh')
|
|
194
|
+
expect(advice[0]!.band).toBe('high-impact')
|
|
195
|
+
expect(advice[0]!.maxTokensBefore).toBeGreaterThanOrEqual(4_000)
|
|
196
|
+
expect(advice[0]!.recoveredTokens).toBeGreaterThan(0)
|
|
197
|
+
// The batch it describes is exactly the batch that landed.
|
|
198
|
+
const landedSeqs = rewrites(records).flatMap(entry => [...entry.sourceSeqs])
|
|
199
|
+
expect(advice[0]!.itemSeqs.every(seq => landedSeqs.includes(seq))).toBe(true)
|
|
200
|
+
|
|
201
|
+
// 3. Every advice record describes a landing, never a pending decision.
|
|
202
|
+
expect(advice.length).toBeLessThanOrEqual(rewrites(records).length)
|
|
203
|
+
})
|
|
204
|
+
|
|
205
|
+
it('keeps the automatic path identical when no legacy gate key is present', async () => {
|
|
206
|
+
const ctx = await runtimeContext()
|
|
207
|
+
await ctx.plugin(TestSettings).await()
|
|
208
|
+
await ctx.plugin(LegacyNamespaceRegistrar).await()
|
|
209
|
+
await ctx.settings.update(nsBrand(CONTEXT_COMPRESSION_SETTINGS_NAMESPACE), {
|
|
210
|
+
profile: 'tokenpilot-inspired',
|
|
211
|
+
})
|
|
212
|
+
await ctx.plugin(ToolResultPruner, {
|
|
213
|
+
profile: 'tokenpilot-inspired',
|
|
214
|
+
freshTriggerTokens: 200,
|
|
215
|
+
freshTargetTokens: 100,
|
|
216
|
+
aggregateTriggerTokens: 1_000_000,
|
|
217
|
+
aggregateTargetTokens: 900_000,
|
|
218
|
+
historyTriggerTokens: 400,
|
|
219
|
+
historyKeepRecentToolCalls: 0,
|
|
220
|
+
historyKeepRecentTokens: 64_000,
|
|
221
|
+
historyMinReclaimTokens: 1,
|
|
222
|
+
}).await()
|
|
223
|
+
const audit = captureAudit(ctx)
|
|
224
|
+
const session = freshSession(ctx, 'advice-default', HIGH_IMPACT_TEXT)
|
|
225
|
+
|
|
226
|
+
const result = ctx.toolResultPruner.pruneSession(session, { stage: 'fresh', freshTurn: 1, freshStep: 1 })
|
|
227
|
+
expect(result.pruned).toHaveLength(1)
|
|
228
|
+
expect(adviceOf(audit.records())).toHaveLength(1)
|
|
229
|
+
})
|
|
230
|
+
})
|