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.
- package/.github/workflows/ci.yml +39 -39
- package/CHANGELOG.ja.md +217 -220
- package/CHANGELOG.ko.md +211 -214
- package/CHANGELOG.md +401 -376
- package/CHANGELOG.zh.md +196 -199
- package/README.md +97 -97
- package/README.zh.md +86 -86
- package/package.json +1 -1
- package/packages/selector/dsh.plugin.json +1 -1
- package/packages/selector/lib/client.d.ts +44 -0
- package/packages/selector/lib/client.js +1733 -6
- package/packages/selector/lib/{intent-gate.js → config.js} +799 -136
- package/packages/selector/lib/index.d.ts +13 -1
- package/packages/selector/lib/index.js +204 -3
- package/packages/selector/lib/profiles.d.ts +6 -0
- package/packages/selector/lib/pruner.d.ts +6 -0
- package/packages/selector/lib/pruner.js +90 -258
- package/packages/selector/lib/style.css +88 -46
- package/packages/selector/src/client/CompressionProfileControls.tsx +293 -226
- package/packages/selector/src/client/CompressionProfileSelector.module.css +199 -170
- package/packages/selector/src/client/CompressionProfileSelector.tsx +85 -83
- package/packages/selector/src/client/decode.ts +57 -53
- package/packages/selector/src/client/index.ts +307 -213
- package/packages/selector/src/client/locales/de.ts +141 -0
- package/packages/selector/src/client/locales/es.ts +141 -0
- package/packages/selector/src/client/locales/fr.ts +141 -0
- package/packages/selector/src/client/locales/it.ts +141 -0
- package/packages/selector/src/client/locales/ja.ts +141 -0
- package/packages/selector/src/client/locales/ko.ts +141 -0
- package/packages/selector/src/client/locales/ru.ts +141 -0
- package/packages/selector/src/client/locales.ts +282 -196
- package/packages/selector/src/client/monitor-fab.ts +336 -0
- package/packages/selector/src/client/savings-card.tsx +150 -0
- package/packages/selector/src/client/settings-section.tsx +140 -124
- package/packages/selector/src/deepseek-v4-tokenizer.ts +205 -205
- package/packages/selector/src/index.ts +207 -5
- package/packages/selector/src/preset-overlay.ts +629 -629
- package/packages/selector/src/profiles.ts +431 -414
- package/packages/selector/src/pruner/content.ts +198 -198
- package/packages/selector/src/pruner/state.ts +57 -57
- package/packages/selector/src/pruner.ts +2759 -2665
- package/packages/selector/src/runtime/audit.ts +303 -303
- package/packages/selector/src/runtime/config.ts +756 -731
- package/packages/selector/src/runtime/monitor.ts +235 -0
- package/packages/selector/src/runtime/reducers.ts +1674 -1674
- package/packages/selector/src/runtime/retrieve.ts +479 -457
- package/packages/selector/src/runtime/savings.ts +337 -0
- package/packages/selector/src/runtime/summary-command.ts +133 -133
- package/packages/selector/src/runtime/tokenpilot/advisor-prompt.ts +233 -233
- package/packages/selector/src/runtime/tokenpilot/advisor-state.ts +204 -204
- package/packages/selector/src/runtime/tokenpilot/intent-fold.ts +53 -53
- package/packages/selector/src/runtime/tokenpilot/intent-gate.ts +65 -65
- package/packages/selector/src/runtime/tokenpilot/intent-input-mask.ts +125 -125
- package/packages/selector/src/runtime/tokenpilot/intent-range.ts +37 -37
- package/packages/selector/src/runtime/tokenpilot/locator.ts +128 -128
- package/packages/selector/src/runtime/tokenpilot/sidechannel.ts +306 -306
- package/packages/selector/src/runtime/types.ts +365 -358
- package/packages/selector/tests/built/client-artifact.spec.ts +61 -61
- package/packages/selector/tests/code-skeleton.client.spec.ts +2 -2
- package/packages/selector/tests/custom-contract.client.spec.ts +2 -2
- package/packages/selector/tests/estimator-channel.client.spec.tsx +2 -0
- package/packages/selector/tests/estimator-route-registration.host.spec.ts +194 -188
- package/packages/selector/tests/helpers/legacy-settings/index.ts +15 -15
- package/packages/selector/tests/helpers/legacy-settings/legacy/provider.d.ts +41 -41
- package/packages/selector/tests/helpers/legacy-settings/legacy/provider.js +647 -647
- package/packages/selector/tests/helpers/legacy-settings/legacy/redact.js +78 -78
- package/packages/selector/tests/helpers/legacy-settings/legacy/util-values.js +234 -234
- package/packages/selector/tests/host-preset-overlay.host.spec.ts +203 -203
- package/packages/selector/tests/monitor-fab.client.spec.ts +138 -0
- package/packages/selector/tests/runtime/auto-compact-config.spec.ts +523 -523
- package/packages/selector/tests/runtime/deepseek-v4-tokenizer.spec.ts +122 -122
- package/packages/selector/tests/runtime/intent-gate.spec.ts +62 -62
- package/packages/selector/tests/runtime/intent-summary.spec.ts +156 -156
- package/packages/selector/tests/runtime/monitor.spec.ts +301 -0
- package/packages/selector/tests/runtime/savings.spec.ts +120 -0
- package/packages/selector/tests/runtime/summary-command.spec.ts +154 -154
- package/packages/selector/tests/settings-seat.client.spec.ts +177 -165
- package/scripts/packed-components-smoke.mjs +734 -734
- package/scripts/packed-install-e2e.mjs +1 -1
- package/scripts/verify-release.mjs +1 -1
- package/tests/TEST_INVENTORY.md +41 -41
- package/tsconfig.tests.json +17 -17
- package/vitest.config.ts +57 -57
|
@@ -1,204 +1,204 @@
|
|
|
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
|
-
}
|
|
150
|
-
|
|
151
|
-
// ─────────── Turn-tail intent summary: session-scoped overrides and records ───────────
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* `/ctx-summary off|on` overrides, keyed by session id (string). The command
|
|
155
|
-
* handler only sees `invocation.agent.session.id`, not the pruner's Session
|
|
156
|
-
* object, so these are id-keyed rather than WeakMap'd. Entries are two-char
|
|
157
|
-
* strings per session and cleared whenever the override is lifted.
|
|
158
|
-
*/
|
|
159
|
-
const summaryOverrides = new Map<string, 'on' | 'off'>()
|
|
160
|
-
|
|
161
|
-
/** Read the session's `/ctx-summary` override; `undefined` = default (settings-driven). */
|
|
162
|
-
export function getSummaryOverride(sessionId: string): 'on' | 'off' | undefined {
|
|
163
|
-
return summaryOverrides.get(sessionId)
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/** Set or clear (`undefined`) the session's `/ctx-summary` override. */
|
|
167
|
-
export function setSummaryOverride(sessionId: string, value: 'on' | 'off' | undefined): void {
|
|
168
|
-
if (value === undefined) summaryOverrides.delete(sessionId)
|
|
169
|
-
else summaryOverrides.set(sessionId, value)
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/** Last observed `intentSummary.enabled` for a session, for `/ctx-summary status`. */
|
|
173
|
-
const lastObservedIntentEnabled = new Map<string, boolean>()
|
|
174
|
-
|
|
175
|
-
/** Record the gate input the pass last evaluated for this session. */
|
|
176
|
-
export function observeIntentEnabled(sessionId: string, enabled: boolean): void {
|
|
177
|
-
lastObservedIntentEnabled.set(sessionId, enabled)
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/** The settings-side enabled flag last observed for the session, if any. */
|
|
181
|
-
export function getObservedIntentEnabled(sessionId: string): boolean | undefined {
|
|
182
|
-
return lastObservedIntentEnabled.get(sessionId)
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/** One landed intent fold, kept for `/ctx-summary status` (one per session). */
|
|
186
|
-
export interface IntentFoldRecord {
|
|
187
|
-
readonly turn: number
|
|
188
|
-
readonly startSeq: number
|
|
189
|
-
readonly endSeq: number
|
|
190
|
-
readonly liveTokensBefore: number
|
|
191
|
-
readonly at: number
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
const lastIntentFolds = new Map<string, IntentFoldRecord>()
|
|
195
|
-
|
|
196
|
-
/** Record a landed intent fold for the session (newest wins). */
|
|
197
|
-
export function recordIntentFold(sessionId: string, record: IntentFoldRecord): void {
|
|
198
|
-
lastIntentFolds.set(sessionId, record)
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/** The most recent landed intent fold for the session, if any. */
|
|
202
|
-
export function getLastIntentFold(sessionId: string): IntentFoldRecord | undefined {
|
|
203
|
-
return lastIntentFolds.get(sessionId)
|
|
204
|
-
}
|
|
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
|
+
}
|
|
150
|
+
|
|
151
|
+
// ─────────── Turn-tail intent summary: session-scoped overrides and records ───────────
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* `/ctx-summary off|on` overrides, keyed by session id (string). The command
|
|
155
|
+
* handler only sees `invocation.agent.session.id`, not the pruner's Session
|
|
156
|
+
* object, so these are id-keyed rather than WeakMap'd. Entries are two-char
|
|
157
|
+
* strings per session and cleared whenever the override is lifted.
|
|
158
|
+
*/
|
|
159
|
+
const summaryOverrides = new Map<string, 'on' | 'off'>()
|
|
160
|
+
|
|
161
|
+
/** Read the session's `/ctx-summary` override; `undefined` = default (settings-driven). */
|
|
162
|
+
export function getSummaryOverride(sessionId: string): 'on' | 'off' | undefined {
|
|
163
|
+
return summaryOverrides.get(sessionId)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Set or clear (`undefined`) the session's `/ctx-summary` override. */
|
|
167
|
+
export function setSummaryOverride(sessionId: string, value: 'on' | 'off' | undefined): void {
|
|
168
|
+
if (value === undefined) summaryOverrides.delete(sessionId)
|
|
169
|
+
else summaryOverrides.set(sessionId, value)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Last observed `intentSummary.enabled` for a session, for `/ctx-summary status`. */
|
|
173
|
+
const lastObservedIntentEnabled = new Map<string, boolean>()
|
|
174
|
+
|
|
175
|
+
/** Record the gate input the pass last evaluated for this session. */
|
|
176
|
+
export function observeIntentEnabled(sessionId: string, enabled: boolean): void {
|
|
177
|
+
lastObservedIntentEnabled.set(sessionId, enabled)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** The settings-side enabled flag last observed for the session, if any. */
|
|
181
|
+
export function getObservedIntentEnabled(sessionId: string): boolean | undefined {
|
|
182
|
+
return lastObservedIntentEnabled.get(sessionId)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** One landed intent fold, kept for `/ctx-summary status` (one per session). */
|
|
186
|
+
export interface IntentFoldRecord {
|
|
187
|
+
readonly turn: number
|
|
188
|
+
readonly startSeq: number
|
|
189
|
+
readonly endSeq: number
|
|
190
|
+
readonly liveTokensBefore: number
|
|
191
|
+
readonly at: number
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const lastIntentFolds = new Map<string, IntentFoldRecord>()
|
|
195
|
+
|
|
196
|
+
/** Record a landed intent fold for the session (newest wins). */
|
|
197
|
+
export function recordIntentFold(sessionId: string, record: IntentFoldRecord): void {
|
|
198
|
+
lastIntentFolds.set(sessionId, record)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** The most recent landed intent fold for the session, if any. */
|
|
202
|
+
export function getLastIntentFold(sessionId: string): IntentFoldRecord | undefined {
|
|
203
|
+
return lastIntentFolds.get(sessionId)
|
|
204
|
+
}
|
|
@@ -1,53 +1,53 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Turn-tail intent summary: pending-fold types and pure block builders
|
|
3
|
-
* (task_2.4/2.5). The pass orchestration lives in the pruner (it owns the
|
|
4
|
-
* channel, gate inputs, and landing machinery); this module owns the data
|
|
5
|
-
* contract and the deterministic intent-block rendering. The model writes
|
|
6
|
-
* ONLY the summary segment — block rendering here is fully deterministic.
|
|
7
|
-
*/
|
|
8
|
-
import type { IntentRole } from './intent-input-mask.ts'
|
|
9
|
-
|
|
10
|
-
/** One staged fold awaiting the next pressure round. */
|
|
11
|
-
export interface PendingIntentFold {
|
|
12
|
-
readonly createdAt: number
|
|
13
|
-
readonly turn: number
|
|
14
|
-
/** Inclusive seq span of the folded candidates. */
|
|
15
|
-
readonly startSeq: number
|
|
16
|
-
readonly endSeq: number
|
|
17
|
-
/** LLM-written summary segment (fail-open parsed, char-capped). */
|
|
18
|
-
readonly summary: string
|
|
19
|
-
/** Verbatim error lines collected across the batch (never LLM-revoiced). */
|
|
20
|
-
readonly errorLines: readonly string[]
|
|
21
|
-
/** Per-candidate faces: read-mask single lines ride the block, keep-class lines prompt-only. */
|
|
22
|
-
readonly records: ReadonlyArray<{ readonly seq: number, readonly role: IntentRole, readonly toolName: string, readonly line: string }>
|
|
23
|
-
/** Characters consumed by the summary-writer call (credit accounting). */
|
|
24
|
-
readonly summaryCallChars: number
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/** Marker written into every folded block; its presence makes a seq unfoldable again (fold-once). */
|
|
28
|
-
export const INTENT_FOLD_MARKER = '[Intent summary'
|
|
29
|
-
|
|
30
|
-
/** Render the replacement text for one folded candidate. Pure. */
|
|
31
|
-
export function renderIntentFoldBlock(pending: PendingIntentFold, seq: number): string {
|
|
32
|
-
const start = pending.startSeq
|
|
33
|
-
const end = pending.endSeq
|
|
34
|
-
if (seq === start) {
|
|
35
|
-
const readLines = pending.records
|
|
36
|
-
.filter(record => record.role === 'read-mask')
|
|
37
|
-
.map(record => record.line)
|
|
38
|
-
const consulted = readLines.length > 0 ? `Consulted: ${readLines.join('; ')}` : ''
|
|
39
|
-
const errorBlock = pending.errorLines.length > 0
|
|
40
|
-
? `\nError lines (verbatim):\n${pending.errorLines.join('\n')}`
|
|
41
|
-
: ''
|
|
42
|
-
return [
|
|
43
|
-
`${INTENT_FOLD_MARKER} of folded tool results seq ${start}..${end}]`,
|
|
44
|
-
pending.summary,
|
|
45
|
-
consulted,
|
|
46
|
-
errorBlock,
|
|
47
|
-
'[Original content remains in the session log at the listed seq values.]',
|
|
48
|
-
].filter(part => part.length > 0).join('\n')
|
|
49
|
-
}
|
|
50
|
-
const record = pending.records.find(entry => entry.seq === seq)
|
|
51
|
-
const face = record === undefined ? `${seq}` : record.role === 'read-mask' ? record.line : `${record.toolName} result folded into the batch intent summary`
|
|
52
|
-
return `${INTENT_FOLD_MARKER} folded into the batch summary above (seq ${start}..${end})] ${face}`
|
|
53
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Turn-tail intent summary: pending-fold types and pure block builders
|
|
3
|
+
* (task_2.4/2.5). The pass orchestration lives in the pruner (it owns the
|
|
4
|
+
* channel, gate inputs, and landing machinery); this module owns the data
|
|
5
|
+
* contract and the deterministic intent-block rendering. The model writes
|
|
6
|
+
* ONLY the summary segment — block rendering here is fully deterministic.
|
|
7
|
+
*/
|
|
8
|
+
import type { IntentRole } from './intent-input-mask.ts'
|
|
9
|
+
|
|
10
|
+
/** One staged fold awaiting the next pressure round. */
|
|
11
|
+
export interface PendingIntentFold {
|
|
12
|
+
readonly createdAt: number
|
|
13
|
+
readonly turn: number
|
|
14
|
+
/** Inclusive seq span of the folded candidates. */
|
|
15
|
+
readonly startSeq: number
|
|
16
|
+
readonly endSeq: number
|
|
17
|
+
/** LLM-written summary segment (fail-open parsed, char-capped). */
|
|
18
|
+
readonly summary: string
|
|
19
|
+
/** Verbatim error lines collected across the batch (never LLM-revoiced). */
|
|
20
|
+
readonly errorLines: readonly string[]
|
|
21
|
+
/** Per-candidate faces: read-mask single lines ride the block, keep-class lines prompt-only. */
|
|
22
|
+
readonly records: ReadonlyArray<{ readonly seq: number, readonly role: IntentRole, readonly toolName: string, readonly line: string }>
|
|
23
|
+
/** Characters consumed by the summary-writer call (credit accounting). */
|
|
24
|
+
readonly summaryCallChars: number
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Marker written into every folded block; its presence makes a seq unfoldable again (fold-once). */
|
|
28
|
+
export const INTENT_FOLD_MARKER = '[Intent summary'
|
|
29
|
+
|
|
30
|
+
/** Render the replacement text for one folded candidate. Pure. */
|
|
31
|
+
export function renderIntentFoldBlock(pending: PendingIntentFold, seq: number): string {
|
|
32
|
+
const start = pending.startSeq
|
|
33
|
+
const end = pending.endSeq
|
|
34
|
+
if (seq === start) {
|
|
35
|
+
const readLines = pending.records
|
|
36
|
+
.filter(record => record.role === 'read-mask')
|
|
37
|
+
.map(record => record.line)
|
|
38
|
+
const consulted = readLines.length > 0 ? `Consulted: ${readLines.join('; ')}` : ''
|
|
39
|
+
const errorBlock = pending.errorLines.length > 0
|
|
40
|
+
? `\nError lines (verbatim):\n${pending.errorLines.join('\n')}`
|
|
41
|
+
: ''
|
|
42
|
+
return [
|
|
43
|
+
`${INTENT_FOLD_MARKER} of folded tool results seq ${start}..${end}]`,
|
|
44
|
+
pending.summary,
|
|
45
|
+
consulted,
|
|
46
|
+
errorBlock,
|
|
47
|
+
'[Original content remains in the session log at the listed seq values.]',
|
|
48
|
+
].filter(part => part.length > 0).join('\n')
|
|
49
|
+
}
|
|
50
|
+
const record = pending.records.find(entry => entry.seq === seq)
|
|
51
|
+
const face = record === undefined ? `${seq}` : record.role === 'read-mask' ? record.line : `${record.toolName} result folded into the batch intent summary`
|
|
52
|
+
return `${INTENT_FOLD_MARKER} folded into the batch summary above (seq ${start}..${end})] ${face}`
|
|
53
|
+
}
|
|
@@ -1,65 +1,65 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Turn-tail intent-summary growth gate (TokenPilot-inspired E2).
|
|
3
|
-
*
|
|
4
|
-
* Pure decision helper evaluated at the turn-boundary postflight: it decides
|
|
5
|
-
* whether this turn may spend a summary-writer LLM call and stage a fold for
|
|
6
|
-
* the next pressure round. The gate is deliberately conservative — every
|
|
7
|
-
* unresolved input (unknown context window, non-finite counters) fails closed
|
|
8
|
-
* toward "do not run", because a skipped fold is free while a wasted summary
|
|
9
|
-
* call is not. Content safety is unaffected either way: fail-open semantics
|
|
10
|
-
* live in the fold landing path, not here.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
/** Floor: the live surface must exceed this fraction of the context window. */
|
|
14
|
-
export const INTENT_GATE_FLOOR_FRACTION = 0.45
|
|
15
|
-
|
|
16
|
-
/** Growth: the live surface must have grown by more than this since the last landed fold. */
|
|
17
|
-
export const INTENT_GATE_GROWTH_TOKENS = 50_000
|
|
18
|
-
|
|
19
|
-
/** Session-scoped user override set through `/ctx-summary off|on`. */
|
|
20
|
-
export type SummaryOverride = 'on' | 'off' | undefined
|
|
21
|
-
|
|
22
|
-
export interface IntentGateInput {
|
|
23
|
-
/** Frozen `intentSummary.enabled` setting for the session. */
|
|
24
|
-
enabled: boolean
|
|
25
|
-
/** `/ctx-summary` override; `'off'` wins over everything, `'on'` never bypasses the numeric gates. */
|
|
26
|
-
override: SummaryOverride
|
|
27
|
-
/** Exact live-surface token estimate from the compaction view. */
|
|
28
|
-
liveTokens: number
|
|
29
|
-
/** Host context window in tokens; `undefined` fails the floor check. */
|
|
30
|
-
contextWindow: number | undefined
|
|
31
|
-
/** Live-surface tokens at the time the last fold landed (0 = never folded). */
|
|
32
|
-
baselineTokens: number
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export interface IntentGateSnapshot extends IntentGateInput {
|
|
36
|
-
readonly decision: boolean
|
|
37
|
-
readonly reason: 'disabled' | 'override-off' | 'no-window' | 'below-floor' | 'below-growth' | 'passed'
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function isUsableNumber(value: number): boolean {
|
|
41
|
-
return Number.isFinite(value) && value >= 0
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/** Evaluate the growth gate. Boundary-exact: equality never triggers. */
|
|
45
|
-
export function shouldRunIntentFold(input: IntentGateInput): boolean {
|
|
46
|
-
return evaluateIntentGate(input).decision === true
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** Same evaluation with the reason attached, for audits and `/ctx-summary status`. */
|
|
50
|
-
export function evaluateIntentGate(input: IntentGateInput): IntentGateSnapshot {
|
|
51
|
-
const snapshot = (decision: boolean, reason: IntentGateSnapshot['reason']): IntentGateSnapshot => ({
|
|
52
|
-
...input,
|
|
53
|
-
decision,
|
|
54
|
-
reason,
|
|
55
|
-
})
|
|
56
|
-
if (!input.enabled) return snapshot(false, 'disabled')
|
|
57
|
-
if (input.override === 'off') return snapshot(false, 'override-off')
|
|
58
|
-
if (!isUsableNumber(input.liveTokens)) return snapshot(false, 'below-floor')
|
|
59
|
-
if (input.contextWindow === undefined || !isUsableNumber(input.contextWindow)) return snapshot(false, 'no-window')
|
|
60
|
-
if (!(input.liveTokens > input.contextWindow * INTENT_GATE_FLOOR_FRACTION)) return snapshot(false, 'below-floor')
|
|
61
|
-
if (!isUsableNumber(input.baselineTokens)) return snapshot(false, 'below-growth')
|
|
62
|
-
const growth = input.liveTokens - input.baselineTokens
|
|
63
|
-
if (!(growth > INTENT_GATE_GROWTH_TOKENS)) return snapshot(false, 'below-growth')
|
|
64
|
-
return snapshot(true, 'passed')
|
|
65
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Turn-tail intent-summary growth gate (TokenPilot-inspired E2).
|
|
3
|
+
*
|
|
4
|
+
* Pure decision helper evaluated at the turn-boundary postflight: it decides
|
|
5
|
+
* whether this turn may spend a summary-writer LLM call and stage a fold for
|
|
6
|
+
* the next pressure round. The gate is deliberately conservative — every
|
|
7
|
+
* unresolved input (unknown context window, non-finite counters) fails closed
|
|
8
|
+
* toward "do not run", because a skipped fold is free while a wasted summary
|
|
9
|
+
* call is not. Content safety is unaffected either way: fail-open semantics
|
|
10
|
+
* live in the fold landing path, not here.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** Floor: the live surface must exceed this fraction of the context window. */
|
|
14
|
+
export const INTENT_GATE_FLOOR_FRACTION = 0.45
|
|
15
|
+
|
|
16
|
+
/** Growth: the live surface must have grown by more than this since the last landed fold. */
|
|
17
|
+
export const INTENT_GATE_GROWTH_TOKENS = 50_000
|
|
18
|
+
|
|
19
|
+
/** Session-scoped user override set through `/ctx-summary off|on`. */
|
|
20
|
+
export type SummaryOverride = 'on' | 'off' | undefined
|
|
21
|
+
|
|
22
|
+
export interface IntentGateInput {
|
|
23
|
+
/** Frozen `intentSummary.enabled` setting for the session. */
|
|
24
|
+
enabled: boolean
|
|
25
|
+
/** `/ctx-summary` override; `'off'` wins over everything, `'on'` never bypasses the numeric gates. */
|
|
26
|
+
override: SummaryOverride
|
|
27
|
+
/** Exact live-surface token estimate from the compaction view. */
|
|
28
|
+
liveTokens: number
|
|
29
|
+
/** Host context window in tokens; `undefined` fails the floor check. */
|
|
30
|
+
contextWindow: number | undefined
|
|
31
|
+
/** Live-surface tokens at the time the last fold landed (0 = never folded). */
|
|
32
|
+
baselineTokens: number
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface IntentGateSnapshot extends IntentGateInput {
|
|
36
|
+
readonly decision: boolean
|
|
37
|
+
readonly reason: 'disabled' | 'override-off' | 'no-window' | 'below-floor' | 'below-growth' | 'passed'
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function isUsableNumber(value: number): boolean {
|
|
41
|
+
return Number.isFinite(value) && value >= 0
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Evaluate the growth gate. Boundary-exact: equality never triggers. */
|
|
45
|
+
export function shouldRunIntentFold(input: IntentGateInput): boolean {
|
|
46
|
+
return evaluateIntentGate(input).decision === true
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Same evaluation with the reason attached, for audits and `/ctx-summary status`. */
|
|
50
|
+
export function evaluateIntentGate(input: IntentGateInput): IntentGateSnapshot {
|
|
51
|
+
const snapshot = (decision: boolean, reason: IntentGateSnapshot['reason']): IntentGateSnapshot => ({
|
|
52
|
+
...input,
|
|
53
|
+
decision,
|
|
54
|
+
reason,
|
|
55
|
+
})
|
|
56
|
+
if (!input.enabled) return snapshot(false, 'disabled')
|
|
57
|
+
if (input.override === 'off') return snapshot(false, 'override-off')
|
|
58
|
+
if (!isUsableNumber(input.liveTokens)) return snapshot(false, 'below-floor')
|
|
59
|
+
if (input.contextWindow === undefined || !isUsableNumber(input.contextWindow)) return snapshot(false, 'no-window')
|
|
60
|
+
if (!(input.liveTokens > input.contextWindow * INTENT_GATE_FLOOR_FRACTION)) return snapshot(false, 'below-floor')
|
|
61
|
+
if (!isUsableNumber(input.baselineTokens)) return snapshot(false, 'below-growth')
|
|
62
|
+
const growth = input.liveTokens - input.baselineTokens
|
|
63
|
+
if (!(growth > INTENT_GATE_GROWTH_TOKENS)) return snapshot(false, 'below-growth')
|
|
64
|
+
return snapshot(true, 'passed')
|
|
65
|
+
}
|