dsh-context-compression-improved 0.5.2 → 0.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (28) hide show
  1. package/.gitattributes +1 -0
  2. package/CHANGELOG.ja.md +144 -119
  3. package/CHANGELOG.ko.md +143 -118
  4. package/CHANGELOG.md +278 -250
  5. package/CHANGELOG.zh.md +131 -109
  6. package/docs/installation.md +103 -103
  7. package/docs/installation.zh.md +100 -100
  8. package/package.json +1 -1
  9. package/packages/selector/cordis.patch.yml +5 -6
  10. package/packages/selector/src/client/EstimatorControls.tsx +277 -277
  11. package/packages/selector/src/client/locales.ts +196 -196
  12. package/packages/selector/src/index.ts +463 -463
  13. package/packages/selector/src/pruner/state.ts +50 -50
  14. package/packages/selector/src/pruner.ts +2402 -2402
  15. package/packages/selector/src/runtime/tokenpilot/advisor-prompt.ts +188 -188
  16. package/packages/selector/src/runtime/tokenpilot/advisor-state.ts +149 -149
  17. package/packages/selector/src/runtime/tokenpilot/advisor.ts +419 -419
  18. package/packages/selector/src/runtime/tokenpilot/benefit.ts +200 -200
  19. package/packages/selector/tests/advisor-report.host.spec.ts +223 -223
  20. package/packages/selector/tests/public/package-contract.client.spec.ts +20 -0
  21. package/packages/selector/tests/runtime/advice-never-withholds.host.spec.ts +232 -232
  22. package/packages/selector/tests/runtime/advisor-invariant.spec.ts +272 -272
  23. package/packages/selector/tests/runtime/advisor.spec.ts +226 -226
  24. package/packages/selector/tests/runtime/char-basis.spec.ts +30 -30
  25. package/packages/selector/tests/runtime/deprecated-preset-options.spec.ts +96 -96
  26. package/packages/selector/tests/runtime/tokenpilot/benefit.spec.ts +217 -217
  27. package/packages/selector/tests/settings-seat.client.spec.ts +29 -4
  28. package/scripts/toolclass-corpus-replay.mjs +281 -281
@@ -1,149 +1,149 @@
1
- /**
2
- * Per-session state for the advisory relevance advisor.
3
- *
4
- * The advisor is statistics and suggestions only — its outputs (summaries,
5
- * scores, decay, recertification marks) are observational and must never
6
- * suppress, delay, or rewrite any reduction that would land. All state lives
7
- * in WeakMaps keyed by the Session object so it is collected with the session
8
- * and never leaks across sessions; every collection inside the state is
9
- * bounded.
10
- *
11
- * Module-level `getAdvisorState` mirrors the PrunerState consolidation pattern
12
- * (GC semantics identical) so `pruner.ts` reaches advisor state through a
13
- * plain import rather than constructor injection.
14
- */
15
- import type { Session } from '@deepseek-ai/dsh-session'
16
- import type { AdviceBand } from './benefit.ts'
17
-
18
- /** The most recent benefit-model label of a landed batch. Observational only:
19
- * no decision path reads it — the report route serves it verbatim. */
20
- export interface AdvisorAdviceSnapshot {
21
- readonly band: AdviceBand
22
- /** Turn index the advised batch landed at. */
23
- readonly turn: number
24
- readonly itemSeqs: readonly number[]
25
- readonly recoveredTokens: number
26
- readonly penaltyTokens: number
27
- readonly paybackTurns?: number
28
- }
29
-
30
- /** One relevance score for one surface seq, with the turn it was scored at. */
31
- export interface AdvisorScoreEntry {
32
- readonly score: number
33
- /** Turn index the score was produced at. */
34
- readonly turn: number
35
- }
36
-
37
- /** LLM-summarized tail-task semantics bound to the session todolist. */
38
- export interface AdvisorSummary {
39
- /** The session's overall task, one sentence. */
40
- readonly overallTask: string
41
- /** Subtasks currently being pushed forward. */
42
- readonly activeSubtasks: readonly string[]
43
- /** Short task keywords used by the local relevance prescreen. */
44
- readonly keywords: readonly string[]
45
- /** Task-semantics version the summary was derived from. */
46
- readonly todoVersion: string
47
- /** Turn index the summary was produced at. */
48
- readonly turn: number
49
- }
50
-
51
- /** Per-session estimator-style failure bookkeeping for exponential backoff. */
52
- export interface AdvisorFailures {
53
- failures: number
54
- cooldownUntil: number
55
- }
56
-
57
- /** Bounded per-session advisor state bag. */
58
- export interface AdvisorState {
59
- /** Version of the task semantics the cached summary and scores were built against. */
60
- todoVersion: string | undefined
61
- summary: AdvisorSummary | undefined
62
- /** Turn index the last summary pass ran at. */
63
- lastSummaryTurn: number
64
- /** Highest candidate seq the scoring pass has examined (incremental watermark). */
65
- watermarkSeq: number
66
- /** LRU-bounded seq → score map (most recent touch wins, oldest evicted). */
67
- readonly scores: Map<number, AdvisorScoreEntry>
68
- /** Bounded seq → turn marks for old low-relevance segments (suggestions only). */
69
- readonly recertified: Map<number, number>
70
- failures: AdvisorFailures | undefined
71
- /** Re-entry guard: summary + scoring are two LLM calls, so the overlap window is wide. */
72
- inFlight: boolean
73
- /** Most recent decay computation, shared verbatim by audits and the report route. */
74
- lastDecay: { readonly decay: number, readonly weightedChars: number, readonly turn: number } | undefined
75
- /** Most recent benefit-model advice; replaced by every advised batch. */
76
- lastAdvice: AdvisorAdviceSnapshot | undefined
77
- }
78
-
79
- /** Upper bound of the per-session scores LRU. */
80
- export const ADVISOR_SCORES_LIMIT = 64
81
- /** Upper bound of the per-session recertification marks. */
82
- export const ADVISOR_RECERTIFIED_LIMIT = 64
83
-
84
- const advisorStates = new WeakMap<Session, AdvisorState>()
85
-
86
- /**
87
- * The per-session advisor state, created on first touch.
88
- * @param session - the session to key the state on (by object identity).
89
- */
90
- export function getAdvisorState(session: Session): AdvisorState {
91
- let state = advisorStates.get(session)
92
- if (state === undefined) {
93
- state = {
94
- todoVersion: undefined,
95
- summary: undefined,
96
- lastSummaryTurn: -1,
97
- watermarkSeq: 0,
98
- scores: new Map(),
99
- recertified: new Map(),
100
- failures: undefined,
101
- inFlight: false,
102
- lastDecay: undefined,
103
- lastAdvice: undefined,
104
- }
105
- advisorStates.set(session, state)
106
- }
107
- return state
108
- }
109
-
110
- /**
111
- * Insert or refresh one score with LRU semantics: a re-touched seq moves to
112
- * the newest position, and the oldest entry is evicted once the map exceeds
113
- * {@link ADVISOR_SCORES_LIMIT}.
114
- */
115
- export function recordScore(state: AdvisorState, seq: number, entry: AdvisorScoreEntry): void {
116
- state.scores.delete(seq)
117
- state.scores.set(seq, entry)
118
- if (state.scores.size > ADVISOR_SCORES_LIMIT) {
119
- const oldest = state.scores.keys().next()
120
- if (oldest.done !== true) state.scores.delete(oldest.value)
121
- }
122
- }
123
-
124
- /**
125
- * Mark one seq as LLM-recertified low relevance (a suggestion for later
126
- * history-aggressiveness decisions, consumed by nothing in this round).
127
- * Bounded at {@link ADVISOR_RECERTIFIED_LIMIT} with the same LRU eviction.
128
- */
129
- export function recordRecertified(state: AdvisorState, seq: number, turn: number): void {
130
- state.recertified.delete(seq)
131
- state.recertified.set(seq, turn)
132
- if (state.recertified.size > ADVISOR_RECERTIFIED_LIMIT) {
133
- const oldest = state.recertified.keys().next()
134
- if (oldest.done !== true) state.recertified.delete(oldest.value)
135
- }
136
- }
137
-
138
- /**
139
- * Drop every cached artifact that depends on the task semantics: a changed
140
- * todo version invalidates the summary and makes all eligible candidates
141
- * rescore-worthy (the watermark alone would otherwise hide them).
142
- */
143
- export function invalidateOnTaskChange(state: AdvisorState, todoVersion: string): boolean {
144
- if (state.todoVersion === todoVersion) return false
145
- state.todoVersion = todoVersion
146
- state.summary = undefined
147
- state.lastSummaryTurn = -1
148
- return true
149
- }
1
+ /**
2
+ * Per-session state for the advisory relevance advisor.
3
+ *
4
+ * The advisor is statistics and suggestions only — its outputs (summaries,
5
+ * scores, decay, recertification marks) are observational and must never
6
+ * suppress, delay, or rewrite any reduction that would land. All state lives
7
+ * in WeakMaps keyed by the Session object so it is collected with the session
8
+ * and never leaks across sessions; every collection inside the state is
9
+ * bounded.
10
+ *
11
+ * Module-level `getAdvisorState` mirrors the PrunerState consolidation pattern
12
+ * (GC semantics identical) so `pruner.ts` reaches advisor state through a
13
+ * plain import rather than constructor injection.
14
+ */
15
+ import type { Session } from '@deepseek-ai/dsh-session'
16
+ import type { AdviceBand } from './benefit.ts'
17
+
18
+ /** The most recent benefit-model label of a landed batch. Observational only:
19
+ * no decision path reads it — the report route serves it verbatim. */
20
+ export interface AdvisorAdviceSnapshot {
21
+ readonly band: AdviceBand
22
+ /** Turn index the advised batch landed at. */
23
+ readonly turn: number
24
+ readonly itemSeqs: readonly number[]
25
+ readonly recoveredTokens: number
26
+ readonly penaltyTokens: number
27
+ readonly paybackTurns?: number
28
+ }
29
+
30
+ /** One relevance score for one surface seq, with the turn it was scored at. */
31
+ export interface AdvisorScoreEntry {
32
+ readonly score: number
33
+ /** Turn index the score was produced at. */
34
+ readonly turn: number
35
+ }
36
+
37
+ /** LLM-summarized tail-task semantics bound to the session todolist. */
38
+ export interface AdvisorSummary {
39
+ /** The session's overall task, one sentence. */
40
+ readonly overallTask: string
41
+ /** Subtasks currently being pushed forward. */
42
+ readonly activeSubtasks: readonly string[]
43
+ /** Short task keywords used by the local relevance prescreen. */
44
+ readonly keywords: readonly string[]
45
+ /** Task-semantics version the summary was derived from. */
46
+ readonly todoVersion: string
47
+ /** Turn index the summary was produced at. */
48
+ readonly turn: number
49
+ }
50
+
51
+ /** Per-session estimator-style failure bookkeeping for exponential backoff. */
52
+ export interface AdvisorFailures {
53
+ failures: number
54
+ cooldownUntil: number
55
+ }
56
+
57
+ /** Bounded per-session advisor state bag. */
58
+ export interface AdvisorState {
59
+ /** Version of the task semantics the cached summary and scores were built against. */
60
+ todoVersion: string | undefined
61
+ summary: AdvisorSummary | undefined
62
+ /** Turn index the last summary pass ran at. */
63
+ lastSummaryTurn: number
64
+ /** Highest candidate seq the scoring pass has examined (incremental watermark). */
65
+ watermarkSeq: number
66
+ /** LRU-bounded seq → score map (most recent touch wins, oldest evicted). */
67
+ readonly scores: Map<number, AdvisorScoreEntry>
68
+ /** Bounded seq → turn marks for old low-relevance segments (suggestions only). */
69
+ readonly recertified: Map<number, number>
70
+ failures: AdvisorFailures | undefined
71
+ /** Re-entry guard: summary + scoring are two LLM calls, so the overlap window is wide. */
72
+ inFlight: boolean
73
+ /** Most recent decay computation, shared verbatim by audits and the report route. */
74
+ lastDecay: { readonly decay: number, readonly weightedChars: number, readonly turn: number } | undefined
75
+ /** Most recent benefit-model advice; replaced by every advised batch. */
76
+ lastAdvice: AdvisorAdviceSnapshot | undefined
77
+ }
78
+
79
+ /** Upper bound of the per-session scores LRU. */
80
+ export const ADVISOR_SCORES_LIMIT = 64
81
+ /** Upper bound of the per-session recertification marks. */
82
+ export const ADVISOR_RECERTIFIED_LIMIT = 64
83
+
84
+ const advisorStates = new WeakMap<Session, AdvisorState>()
85
+
86
+ /**
87
+ * The per-session advisor state, created on first touch.
88
+ * @param session - the session to key the state on (by object identity).
89
+ */
90
+ export function getAdvisorState(session: Session): AdvisorState {
91
+ let state = advisorStates.get(session)
92
+ if (state === undefined) {
93
+ state = {
94
+ todoVersion: undefined,
95
+ summary: undefined,
96
+ lastSummaryTurn: -1,
97
+ watermarkSeq: 0,
98
+ scores: new Map(),
99
+ recertified: new Map(),
100
+ failures: undefined,
101
+ inFlight: false,
102
+ lastDecay: undefined,
103
+ lastAdvice: undefined,
104
+ }
105
+ advisorStates.set(session, state)
106
+ }
107
+ return state
108
+ }
109
+
110
+ /**
111
+ * Insert or refresh one score with LRU semantics: a re-touched seq moves to
112
+ * the newest position, and the oldest entry is evicted once the map exceeds
113
+ * {@link ADVISOR_SCORES_LIMIT}.
114
+ */
115
+ export function recordScore(state: AdvisorState, seq: number, entry: AdvisorScoreEntry): void {
116
+ state.scores.delete(seq)
117
+ state.scores.set(seq, entry)
118
+ if (state.scores.size > ADVISOR_SCORES_LIMIT) {
119
+ const oldest = state.scores.keys().next()
120
+ if (oldest.done !== true) state.scores.delete(oldest.value)
121
+ }
122
+ }
123
+
124
+ /**
125
+ * Mark one seq as LLM-recertified low relevance (a suggestion for later
126
+ * history-aggressiveness decisions, consumed by nothing in this round).
127
+ * Bounded at {@link ADVISOR_RECERTIFIED_LIMIT} with the same LRU eviction.
128
+ */
129
+ export function recordRecertified(state: AdvisorState, seq: number, turn: number): void {
130
+ state.recertified.delete(seq)
131
+ state.recertified.set(seq, turn)
132
+ if (state.recertified.size > ADVISOR_RECERTIFIED_LIMIT) {
133
+ const oldest = state.recertified.keys().next()
134
+ if (oldest.done !== true) state.recertified.delete(oldest.value)
135
+ }
136
+ }
137
+
138
+ /**
139
+ * Drop every cached artifact that depends on the task semantics: a changed
140
+ * todo version invalidates the summary and makes all eligible candidates
141
+ * rescore-worthy (the watermark alone would otherwise hide them).
142
+ */
143
+ export function invalidateOnTaskChange(state: AdvisorState, todoVersion: string): boolean {
144
+ if (state.todoVersion === todoVersion) return false
145
+ state.todoVersion = todoVersion
146
+ state.summary = undefined
147
+ state.lastSummaryTurn = -1
148
+ return true
149
+ }