dsh-context-compression-improved 0.7.0-beta.1 → 0.7.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 (83) hide show
  1. package/.github/workflows/ci.yml +39 -39
  2. package/CHANGELOG.ja.md +217 -220
  3. package/CHANGELOG.ko.md +211 -214
  4. package/CHANGELOG.md +401 -376
  5. package/CHANGELOG.zh.md +196 -199
  6. package/README.md +97 -97
  7. package/README.zh.md +86 -86
  8. package/package.json +1 -1
  9. package/packages/selector/dsh.plugin.json +1 -1
  10. package/packages/selector/lib/client.d.ts +44 -0
  11. package/packages/selector/lib/client.js +1733 -6
  12. package/packages/selector/lib/{intent-gate.js → config.js} +799 -136
  13. package/packages/selector/lib/index.d.ts +13 -1
  14. package/packages/selector/lib/index.js +204 -3
  15. package/packages/selector/lib/profiles.d.ts +6 -0
  16. package/packages/selector/lib/pruner.d.ts +6 -0
  17. package/packages/selector/lib/pruner.js +90 -258
  18. package/packages/selector/lib/style.css +88 -46
  19. package/packages/selector/src/client/CompressionProfileControls.tsx +293 -226
  20. package/packages/selector/src/client/CompressionProfileSelector.module.css +199 -170
  21. package/packages/selector/src/client/CompressionProfileSelector.tsx +85 -83
  22. package/packages/selector/src/client/decode.ts +57 -53
  23. package/packages/selector/src/client/index.ts +307 -213
  24. package/packages/selector/src/client/locales/de.ts +141 -0
  25. package/packages/selector/src/client/locales/es.ts +141 -0
  26. package/packages/selector/src/client/locales/fr.ts +141 -0
  27. package/packages/selector/src/client/locales/it.ts +141 -0
  28. package/packages/selector/src/client/locales/ja.ts +141 -0
  29. package/packages/selector/src/client/locales/ko.ts +141 -0
  30. package/packages/selector/src/client/locales/ru.ts +141 -0
  31. package/packages/selector/src/client/locales.ts +282 -196
  32. package/packages/selector/src/client/monitor-fab.ts +336 -0
  33. package/packages/selector/src/client/savings-card.tsx +150 -0
  34. package/packages/selector/src/client/settings-section.tsx +140 -124
  35. package/packages/selector/src/deepseek-v4-tokenizer.ts +205 -205
  36. package/packages/selector/src/index.ts +207 -5
  37. package/packages/selector/src/preset-overlay.ts +629 -629
  38. package/packages/selector/src/profiles.ts +431 -414
  39. package/packages/selector/src/pruner/content.ts +198 -198
  40. package/packages/selector/src/pruner/state.ts +57 -57
  41. package/packages/selector/src/pruner.ts +2759 -2665
  42. package/packages/selector/src/runtime/audit.ts +303 -303
  43. package/packages/selector/src/runtime/config.ts +756 -731
  44. package/packages/selector/src/runtime/monitor.ts +235 -0
  45. package/packages/selector/src/runtime/reducers.ts +1674 -1674
  46. package/packages/selector/src/runtime/retrieve.ts +479 -457
  47. package/packages/selector/src/runtime/savings.ts +337 -0
  48. package/packages/selector/src/runtime/summary-command.ts +133 -133
  49. package/packages/selector/src/runtime/tokenpilot/advisor-prompt.ts +233 -233
  50. package/packages/selector/src/runtime/tokenpilot/advisor-state.ts +204 -204
  51. package/packages/selector/src/runtime/tokenpilot/intent-fold.ts +53 -53
  52. package/packages/selector/src/runtime/tokenpilot/intent-gate.ts +65 -65
  53. package/packages/selector/src/runtime/tokenpilot/intent-input-mask.ts +125 -125
  54. package/packages/selector/src/runtime/tokenpilot/intent-range.ts +37 -37
  55. package/packages/selector/src/runtime/tokenpilot/locator.ts +128 -128
  56. package/packages/selector/src/runtime/tokenpilot/sidechannel.ts +306 -306
  57. package/packages/selector/src/runtime/types.ts +365 -358
  58. package/packages/selector/tests/built/client-artifact.spec.ts +61 -61
  59. package/packages/selector/tests/code-skeleton.client.spec.ts +2 -2
  60. package/packages/selector/tests/custom-contract.client.spec.ts +2 -2
  61. package/packages/selector/tests/estimator-channel.client.spec.tsx +2 -0
  62. package/packages/selector/tests/estimator-route-registration.host.spec.ts +194 -188
  63. package/packages/selector/tests/helpers/legacy-settings/index.ts +15 -15
  64. package/packages/selector/tests/helpers/legacy-settings/legacy/provider.d.ts +41 -41
  65. package/packages/selector/tests/helpers/legacy-settings/legacy/provider.js +647 -647
  66. package/packages/selector/tests/helpers/legacy-settings/legacy/redact.js +78 -78
  67. package/packages/selector/tests/helpers/legacy-settings/legacy/util-values.js +234 -234
  68. package/packages/selector/tests/host-preset-overlay.host.spec.ts +203 -203
  69. package/packages/selector/tests/monitor-fab.client.spec.ts +138 -0
  70. package/packages/selector/tests/runtime/auto-compact-config.spec.ts +523 -523
  71. package/packages/selector/tests/runtime/deepseek-v4-tokenizer.spec.ts +122 -122
  72. package/packages/selector/tests/runtime/intent-gate.spec.ts +62 -62
  73. package/packages/selector/tests/runtime/intent-summary.spec.ts +156 -156
  74. package/packages/selector/tests/runtime/monitor.spec.ts +301 -0
  75. package/packages/selector/tests/runtime/savings.spec.ts +120 -0
  76. package/packages/selector/tests/runtime/summary-command.spec.ts +154 -154
  77. package/packages/selector/tests/settings-seat.client.spec.ts +177 -165
  78. package/scripts/packed-components-smoke.mjs +734 -734
  79. package/scripts/packed-install-e2e.mjs +1 -1
  80. package/scripts/verify-release.mjs +1 -1
  81. package/tests/TEST_INVENTORY.md +41 -41
  82. package/tsconfig.tests.json +17 -17
  83. package/vitest.config.ts +57 -57
@@ -1,122 +1,122 @@
1
- /** Registry behavior for the bundled official DeepSeek tokenizer artifacts. */
2
-
3
- import { readFileSync } from 'node:fs'
4
- import { fileURLToPath } from 'node:url'
5
- import { describe, expect, it } from 'vitest'
6
- import {
7
- DEEPSEEK_V4_TOKENIZER_ARTIFACT,
8
- DEEPSEEK_VISION_TOKENIZER_ARTIFACT,
9
- createDeepSeekV4TokenizerFromAssets,
10
- deepSeekV4TokenizerFailureReason,
11
- deepSeekV4TokenizerForModel,
12
- } from '../../src/deepseek-v4-tokenizer.ts'
13
-
14
- const RUNTIME_ROOT = new URL('../../', import.meta.url)
15
-
16
- describe('bundled DeepSeek tokenizer artifact registry', () => {
17
- it('resolves each exact API model id to its pinned artifact identity', () => {
18
- for (const modelId of ['deepseek-v4-flash', 'deepseek-v4-pro']) {
19
- expect(deepSeekV4TokenizerForModel(modelId)?.countText('identity probe')).toMatchObject({
20
- kind: 'exact-tokenizer',
21
- tokenizerId: 'deepseek-ai/DeepSeek-V4-Pro',
22
- tokenizerRevision: DEEPSEEK_V4_TOKENIZER_ARTIFACT.revision,
23
- })
24
- }
25
- expect(deepSeekV4TokenizerForModel('deepseek-v4-flash-vision-exp')?.countText('identity probe')).toMatchObject({
26
- kind: 'exact-tokenizer',
27
- tokenizerId: 'deepseek-ai/DeepSeek-V4-Flash-Vision-Exp',
28
- tokenizerRevision: '6821d6ad3681a4b137b066b76094fa82ebd0a380',
29
- })
30
- })
31
-
32
- it('ships a vision tokenizer that is not an alias of the V4 Pro tokenizer', () => {
33
- expect(DEEPSEEK_VISION_TOKENIZER_ARTIFACT.modelIds).toEqual(['deepseek-v4-flash-vision-exp'])
34
- expect(DEEPSEEK_VISION_TOKENIZER_ARTIFACT.license).toBe('MIT')
35
- expect(DEEPSEEK_VISION_TOKENIZER_ARTIFACT.tokenizerSha256)
36
- .not.toBe(DEEPSEEK_V4_TOKENIZER_ARTIFACT.tokenizerSha256)
37
- // The official vision placeholder special token exists only in the vision
38
- // vocabulary; the text tokenizer must not silently accept it as one token.
39
- expect(deepSeekV4TokenizerForModel('deepseek-v4-flash-vision-exp')?.countText('<|deepseek_image|>').tokens).toBe(1)
40
- expect(deepSeekV4TokenizerForModel('deepseek-v4-pro')?.countText('<|deepseek_image|>').tokens).toBeGreaterThan(1)
41
- })
42
-
43
- it.each([
44
- 'deepseek-v4-flash-vision',
45
- 'deepseek-v4-flash-vision-exp-2',
46
- 'DeepSeek-V4-Flash-Vision-Exp',
47
- 'deepseek-v4-vision-exp',
48
- 'deepseek-v4-pro-vision',
49
- 'deepseek-vision',
50
- 'gpt-4o',
51
- '',
52
- ])('refuses the non-exact model id %s', (modelId) => {
53
- expect(deepSeekV4TokenizerForModel(modelId)).toBeUndefined()
54
- expect(deepSeekV4TokenizerFailureReason(modelId)).toBeUndefined()
55
- })
56
-
57
- it('reports the cached failure reason for the artifact serving one model', () => {
58
- // Both text models resolve successfully today, so no failure is recorded.
59
- expect(deepSeekV4TokenizerFailureReason('deepseek-v4-pro')).toBeUndefined()
60
- expect(deepSeekV4TokenizerFailureReason('deepseek-v4-flash-vision-exp')).toBeUndefined()
61
- })
62
-
63
- it('fails closed on byte-length, SHA-256, and JSON corruption for the vision artifact without touching the text artifact', () => {
64
- const visionRoot = new URL('assets/deepseek-v4-vision-exp/', RUNTIME_ROOT)
65
- const tokenizerBytes = readFileSync(fileURLToPath(new URL('tokenizer.json', visionRoot)))
66
- const good = {
67
- tokenizer: { bytes: tokenizerBytes.byteLength, sha256: DEEPSEEK_VISION_TOKENIZER_ARTIFACT.tokenizerSha256 },
68
- config: { bytes: 801, sha256: DEEPSEEK_VISION_TOKENIZER_ARTIFACT.tokenizerConfigSha256 },
69
- }
70
- expect(() => createDeepSeekV4TokenizerFromAssets(visionRoot, {
71
- tokenizer: { ...good.tokenizer, bytes: good.tokenizer.bytes + 1 },
72
- config: good.config,
73
- })).toThrow(/bytes/)
74
- expect(() => createDeepSeekV4TokenizerFromAssets(visionRoot, {
75
- tokenizer: { ...good.tokenizer, sha256: '0'.repeat(64) },
76
- config: good.config,
77
- })).toThrow(/SHA-256/)
78
- expect(() => createDeepSeekV4TokenizerFromAssets(visionRoot, good)).not.toThrow()
79
- // The shared registry still serves the independent text artifact.
80
- expect(deepSeekV4TokenizerForModel('deepseek-v4-flash')?.countText('still exact').kind).toBe('exact-tokenizer')
81
- })
82
-
83
- it('fails closed when a byte-and-hash-valid asset is not JSON', async () => {
84
- const { mkdtemp, writeFile } = await import('node:fs/promises')
85
- const { tmpdir } = await import('node:os')
86
- const { createHash } = await import('node:crypto')
87
- const path = await import('node:path')
88
- const root = await mkdtemp(path.join(tmpdir(), 'dsh-tokenizer-json-corrupt-'))
89
- try {
90
- const payload = 'this is definitely not json'
91
- const bytes = Buffer.from(payload, 'utf8')
92
- await writeFile(path.join(root, 'tokenizer.json'), bytes)
93
- await writeFile(path.join(root, 'tokenizer_config.json'), bytes)
94
- const sha256 = createHash('sha256').update(bytes).digest('hex')
95
- const integrity = {
96
- tokenizer: { bytes: bytes.byteLength, sha256 },
97
- config: { bytes: bytes.byteLength, sha256 },
98
- }
99
- await expect(Promise.resolve().then(() =>
100
- createDeepSeekV4TokenizerFromAssets(new URL(`file://${root}/`), integrity),
101
- )).rejects.toThrow(/JSON|json/)
102
- } finally {
103
- await import('node:fs/promises').then(fs => fs.rm(root, { recursive: true, force: true }))
104
- }
105
- })
106
-
107
- it('verifies the committed asset manifests against the shipped files', () => {
108
- for (const [directory, artifact] of [
109
- ['deepseek-v4', DEEPSEEK_V4_TOKENIZER_ARTIFACT],
110
- ['deepseek-v4-vision-exp', DEEPSEEK_VISION_TOKENIZER_ARTIFACT],
111
- ] as const) {
112
- const manifest = JSON.parse(
113
- readFileSync(fileURLToPath(new URL(`assets/${directory}/manifest.json`, RUNTIME_ROOT)), 'utf8'),
114
- ) as { repository: string, revision: string, modelIds: string[], files: Record<string, { bytes: number, sha256: string }> }
115
- expect(manifest.repository).toBe(artifact.repository)
116
- expect(manifest.revision).toBe(artifact.revision)
117
- expect(manifest.modelIds).toEqual([...artifact.modelIds])
118
- expect(manifest.files['tokenizer.json']?.sha256).toBe(artifact.tokenizerSha256)
119
- expect(manifest.files['tokenizer_config.json']?.sha256).toBe(artifact.tokenizerConfigSha256)
120
- }
121
- })
122
- })
1
+ /** Registry behavior for the bundled official DeepSeek tokenizer artifacts. */
2
+
3
+ import { readFileSync } from 'node:fs'
4
+ import { fileURLToPath } from 'node:url'
5
+ import { describe, expect, it } from 'vitest'
6
+ import {
7
+ DEEPSEEK_V4_TOKENIZER_ARTIFACT,
8
+ DEEPSEEK_VISION_TOKENIZER_ARTIFACT,
9
+ createDeepSeekV4TokenizerFromAssets,
10
+ deepSeekV4TokenizerFailureReason,
11
+ deepSeekV4TokenizerForModel,
12
+ } from '../../src/deepseek-v4-tokenizer.ts'
13
+
14
+ const RUNTIME_ROOT = new URL('../../', import.meta.url)
15
+
16
+ describe('bundled DeepSeek tokenizer artifact registry', () => {
17
+ it('resolves each exact API model id to its pinned artifact identity', () => {
18
+ for (const modelId of ['deepseek-v4-flash', 'deepseek-v4-pro']) {
19
+ expect(deepSeekV4TokenizerForModel(modelId)?.countText('identity probe')).toMatchObject({
20
+ kind: 'exact-tokenizer',
21
+ tokenizerId: 'deepseek-ai/DeepSeek-V4-Pro',
22
+ tokenizerRevision: DEEPSEEK_V4_TOKENIZER_ARTIFACT.revision,
23
+ })
24
+ }
25
+ expect(deepSeekV4TokenizerForModel('deepseek-v4-flash-vision-exp')?.countText('identity probe')).toMatchObject({
26
+ kind: 'exact-tokenizer',
27
+ tokenizerId: 'deepseek-ai/DeepSeek-V4-Flash-Vision-Exp',
28
+ tokenizerRevision: '6821d6ad3681a4b137b066b76094fa82ebd0a380',
29
+ })
30
+ })
31
+
32
+ it('ships a vision tokenizer that is not an alias of the V4 Pro tokenizer', () => {
33
+ expect(DEEPSEEK_VISION_TOKENIZER_ARTIFACT.modelIds).toEqual(['deepseek-v4-flash-vision-exp'])
34
+ expect(DEEPSEEK_VISION_TOKENIZER_ARTIFACT.license).toBe('MIT')
35
+ expect(DEEPSEEK_VISION_TOKENIZER_ARTIFACT.tokenizerSha256)
36
+ .not.toBe(DEEPSEEK_V4_TOKENIZER_ARTIFACT.tokenizerSha256)
37
+ // The official vision placeholder special token exists only in the vision
38
+ // vocabulary; the text tokenizer must not silently accept it as one token.
39
+ expect(deepSeekV4TokenizerForModel('deepseek-v4-flash-vision-exp')?.countText('<|deepseek_image|>').tokens).toBe(1)
40
+ expect(deepSeekV4TokenizerForModel('deepseek-v4-pro')?.countText('<|deepseek_image|>').tokens).toBeGreaterThan(1)
41
+ })
42
+
43
+ it.each([
44
+ 'deepseek-v4-flash-vision',
45
+ 'deepseek-v4-flash-vision-exp-2',
46
+ 'DeepSeek-V4-Flash-Vision-Exp',
47
+ 'deepseek-v4-vision-exp',
48
+ 'deepseek-v4-pro-vision',
49
+ 'deepseek-vision',
50
+ 'gpt-4o',
51
+ '',
52
+ ])('refuses the non-exact model id %s', (modelId) => {
53
+ expect(deepSeekV4TokenizerForModel(modelId)).toBeUndefined()
54
+ expect(deepSeekV4TokenizerFailureReason(modelId)).toBeUndefined()
55
+ })
56
+
57
+ it('reports the cached failure reason for the artifact serving one model', () => {
58
+ // Both text models resolve successfully today, so no failure is recorded.
59
+ expect(deepSeekV4TokenizerFailureReason('deepseek-v4-pro')).toBeUndefined()
60
+ expect(deepSeekV4TokenizerFailureReason('deepseek-v4-flash-vision-exp')).toBeUndefined()
61
+ })
62
+
63
+ it('fails closed on byte-length, SHA-256, and JSON corruption for the vision artifact without touching the text artifact', () => {
64
+ const visionRoot = new URL('assets/deepseek-v4-vision-exp/', RUNTIME_ROOT)
65
+ const tokenizerBytes = readFileSync(fileURLToPath(new URL('tokenizer.json', visionRoot)))
66
+ const good = {
67
+ tokenizer: { bytes: tokenizerBytes.byteLength, sha256: DEEPSEEK_VISION_TOKENIZER_ARTIFACT.tokenizerSha256 },
68
+ config: { bytes: 801, sha256: DEEPSEEK_VISION_TOKENIZER_ARTIFACT.tokenizerConfigSha256 },
69
+ }
70
+ expect(() => createDeepSeekV4TokenizerFromAssets(visionRoot, {
71
+ tokenizer: { ...good.tokenizer, bytes: good.tokenizer.bytes + 1 },
72
+ config: good.config,
73
+ })).toThrow(/bytes/)
74
+ expect(() => createDeepSeekV4TokenizerFromAssets(visionRoot, {
75
+ tokenizer: { ...good.tokenizer, sha256: '0'.repeat(64) },
76
+ config: good.config,
77
+ })).toThrow(/SHA-256/)
78
+ expect(() => createDeepSeekV4TokenizerFromAssets(visionRoot, good)).not.toThrow()
79
+ // The shared registry still serves the independent text artifact.
80
+ expect(deepSeekV4TokenizerForModel('deepseek-v4-flash')?.countText('still exact').kind).toBe('exact-tokenizer')
81
+ })
82
+
83
+ it('fails closed when a byte-and-hash-valid asset is not JSON', async () => {
84
+ const { mkdtemp, writeFile } = await import('node:fs/promises')
85
+ const { tmpdir } = await import('node:os')
86
+ const { createHash } = await import('node:crypto')
87
+ const path = await import('node:path')
88
+ const root = await mkdtemp(path.join(tmpdir(), 'dsh-tokenizer-json-corrupt-'))
89
+ try {
90
+ const payload = 'this is definitely not json'
91
+ const bytes = Buffer.from(payload, 'utf8')
92
+ await writeFile(path.join(root, 'tokenizer.json'), bytes)
93
+ await writeFile(path.join(root, 'tokenizer_config.json'), bytes)
94
+ const sha256 = createHash('sha256').update(bytes).digest('hex')
95
+ const integrity = {
96
+ tokenizer: { bytes: bytes.byteLength, sha256 },
97
+ config: { bytes: bytes.byteLength, sha256 },
98
+ }
99
+ await expect(Promise.resolve().then(() =>
100
+ createDeepSeekV4TokenizerFromAssets(new URL(`file://${root}/`), integrity),
101
+ )).rejects.toThrow(/JSON|json/)
102
+ } finally {
103
+ await import('node:fs/promises').then(fs => fs.rm(root, { recursive: true, force: true }))
104
+ }
105
+ })
106
+
107
+ it('verifies the committed asset manifests against the shipped files', () => {
108
+ for (const [directory, artifact] of [
109
+ ['deepseek-v4', DEEPSEEK_V4_TOKENIZER_ARTIFACT],
110
+ ['deepseek-v4-vision-exp', DEEPSEEK_VISION_TOKENIZER_ARTIFACT],
111
+ ] as const) {
112
+ const manifest = JSON.parse(
113
+ readFileSync(fileURLToPath(new URL(`assets/${directory}/manifest.json`, RUNTIME_ROOT)), 'utf8'),
114
+ ) as { repository: string, revision: string, modelIds: string[], files: Record<string, { bytes: number, sha256: string }> }
115
+ expect(manifest.repository).toBe(artifact.repository)
116
+ expect(manifest.revision).toBe(artifact.revision)
117
+ expect(manifest.modelIds).toEqual([...artifact.modelIds])
118
+ expect(manifest.files['tokenizer.json']?.sha256).toBe(artifact.tokenizerSha256)
119
+ expect(manifest.files['tokenizer_config.json']?.sha256).toBe(artifact.tokenizerConfigSha256)
120
+ }
121
+ })
122
+ })
@@ -1,62 +1,62 @@
1
- import { describe, expect, it } from 'vitest'
2
- import {
3
- INTENT_GATE_FLOOR_FRACTION,
4
- INTENT_GATE_GROWTH_TOKENS,
5
- evaluateIntentGate,
6
- shouldRunIntentFold,
7
- } from '../../src/runtime/tokenpilot/intent-gate.ts'
8
-
9
- const BASE = {
10
- enabled: true,
11
- override: undefined,
12
- liveTokens: 100_000,
13
- contextWindow: 200_000,
14
- baselineTokens: 0,
15
- } as const
16
-
17
- describe('intent growth gate', () => {
18
- it('passes when every condition holds strictly', () => {
19
- expect(shouldRunIntentFold({ ...BASE })).toBe(true)
20
- })
21
-
22
- it('is disabled when the frozen setting is off', () => {
23
- const out = evaluateIntentGate({ ...BASE, enabled: false })
24
- expect(out.decision).toBe(false)
25
- expect(out.reason).toBe('disabled')
26
- })
27
-
28
- it('override off wins over everything, including a passing numeric profile', () => {
29
- const out = evaluateIntentGate({ ...BASE, override: 'off' })
30
- expect(out.decision).toBe(false)
31
- expect(out.reason).toBe('override-off')
32
- })
33
-
34
- it('override on does NOT bypass the numeric gates', () => {
35
- expect(shouldRunIntentFold({ ...BASE, override: 'on', liveTokens: 1_000 })).toBe(false)
36
- })
37
-
38
- it('unknown context window fails closed toward no-window', () => {
39
- const out = evaluateIntentGate({ ...BASE, contextWindow: undefined })
40
- expect(out.decision).toBe(false)
41
- expect(out.reason).toBe('no-window')
42
- })
43
-
44
- it('floor boundary is exclusive: exactly at 45% does not trigger', () => {
45
- const atFloor = BASE.contextWindow * INTENT_GATE_FLOOR_FRACTION
46
- expect(shouldRunIntentFold({ ...BASE, liveTokens: atFloor })).toBe(false)
47
- expect(shouldRunIntentFold({ ...BASE, liveTokens: atFloor + 1 })).toBe(true)
48
- })
49
-
50
- it('growth boundary is exclusive: exactly at the threshold does not trigger', () => {
51
- const baseline = 90_000
52
- const atGrowth = baseline + INTENT_GATE_GROWTH_TOKENS
53
- expect(shouldRunIntentFold({ ...BASE, liveTokens: atGrowth, baselineTokens: baseline })).toBe(false)
54
- expect(shouldRunIntentFold({ ...BASE, liveTokens: atGrowth + 1, baselineTokens: baseline })).toBe(true)
55
- })
56
-
57
- it('non-finite or negative counters fail closed', () => {
58
- expect(shouldRunIntentFold({ ...BASE, liveTokens: Number.NaN })).toBe(false)
59
- expect(shouldRunIntentFold({ ...BASE, liveTokens: Number.POSITIVE_INFINITY })).toBe(false)
60
- expect(shouldRunIntentFold({ ...BASE, baselineTokens: -1 })).toBe(false)
61
- })
62
- })
1
+ import { describe, expect, it } from 'vitest'
2
+ import {
3
+ INTENT_GATE_FLOOR_FRACTION,
4
+ INTENT_GATE_GROWTH_TOKENS,
5
+ evaluateIntentGate,
6
+ shouldRunIntentFold,
7
+ } from '../../src/runtime/tokenpilot/intent-gate.ts'
8
+
9
+ const BASE = {
10
+ enabled: true,
11
+ override: undefined,
12
+ liveTokens: 100_000,
13
+ contextWindow: 200_000,
14
+ baselineTokens: 0,
15
+ } as const
16
+
17
+ describe('intent growth gate', () => {
18
+ it('passes when every condition holds strictly', () => {
19
+ expect(shouldRunIntentFold({ ...BASE })).toBe(true)
20
+ })
21
+
22
+ it('is disabled when the frozen setting is off', () => {
23
+ const out = evaluateIntentGate({ ...BASE, enabled: false })
24
+ expect(out.decision).toBe(false)
25
+ expect(out.reason).toBe('disabled')
26
+ })
27
+
28
+ it('override off wins over everything, including a passing numeric profile', () => {
29
+ const out = evaluateIntentGate({ ...BASE, override: 'off' })
30
+ expect(out.decision).toBe(false)
31
+ expect(out.reason).toBe('override-off')
32
+ })
33
+
34
+ it('override on does NOT bypass the numeric gates', () => {
35
+ expect(shouldRunIntentFold({ ...BASE, override: 'on', liveTokens: 1_000 })).toBe(false)
36
+ })
37
+
38
+ it('unknown context window fails closed toward no-window', () => {
39
+ const out = evaluateIntentGate({ ...BASE, contextWindow: undefined })
40
+ expect(out.decision).toBe(false)
41
+ expect(out.reason).toBe('no-window')
42
+ })
43
+
44
+ it('floor boundary is exclusive: exactly at 45% does not trigger', () => {
45
+ const atFloor = BASE.contextWindow * INTENT_GATE_FLOOR_FRACTION
46
+ expect(shouldRunIntentFold({ ...BASE, liveTokens: atFloor })).toBe(false)
47
+ expect(shouldRunIntentFold({ ...BASE, liveTokens: atFloor + 1 })).toBe(true)
48
+ })
49
+
50
+ it('growth boundary is exclusive: exactly at the threshold does not trigger', () => {
51
+ const baseline = 90_000
52
+ const atGrowth = baseline + INTENT_GATE_GROWTH_TOKENS
53
+ expect(shouldRunIntentFold({ ...BASE, liveTokens: atGrowth, baselineTokens: baseline })).toBe(false)
54
+ expect(shouldRunIntentFold({ ...BASE, liveTokens: atGrowth + 1, baselineTokens: baseline })).toBe(true)
55
+ })
56
+
57
+ it('non-finite or negative counters fail closed', () => {
58
+ expect(shouldRunIntentFold({ ...BASE, liveTokens: Number.NaN })).toBe(false)
59
+ expect(shouldRunIntentFold({ ...BASE, liveTokens: Number.POSITIVE_INFINITY })).toBe(false)
60
+ expect(shouldRunIntentFold({ ...BASE, baselineTokens: -1 })).toBe(false)
61
+ })
62
+ })