dsh-context-compression-improved 0.8.0 → 0.9.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.
- package/.gitattributes +2 -2
- package/.githooks/pre-push +37 -37
- package/.github/workflows/ci.yml +39 -39
- package/CHANGELOG.de.md +41 -0
- package/CHANGELOG.es.md +41 -0
- package/CHANGELOG.fr.md +42 -0
- package/CHANGELOG.it.md +41 -0
- package/CHANGELOG.ja.md +35 -0
- package/CHANGELOG.ko.md +34 -0
- package/CHANGELOG.md +96 -0
- package/CHANGELOG.ru.md +37 -0
- package/CHANGELOG.zh.md +32 -0
- package/CONTRIBUTING.md +22 -22
- package/LICENSE +21 -21
- package/README.de.md +7 -9
- package/README.es.md +7 -9
- package/README.fr.md +7 -9
- package/README.it.md +7 -9
- package/README.ja.md +6 -2
- package/README.ko.md +6 -2
- package/README.md +96 -98
- package/README.ru.md +7 -9
- package/README.zh.md +89 -85
- package/SECURITY.md +18 -18
- package/THIRD_PARTY_NOTICES.md +14 -14
- package/docs/compat-0.1.5-report.md +77 -77
- package/docs/installation.de.md +5 -5
- package/docs/installation.es.md +5 -5
- package/docs/installation.fr.md +5 -5
- package/docs/installation.it.md +5 -5
- package/docs/installation.ja.md +5 -5
- package/docs/installation.ko.md +5 -5
- package/docs/installation.md +5 -5
- package/docs/installation.ru.md +5 -5
- package/docs/installation.zh.md +5 -5
- package/docs/repair-log.md +657 -657
- package/package.json +24 -22
- package/packages/selector/LICENSE +21 -21
- package/packages/selector/README.md +26 -26
- package/packages/selector/README.zh.md +24 -24
- package/packages/selector/THIRD_PARTY_NOTICES.md +32 -32
- package/packages/selector/cordis.patch.yml +27 -27
- package/packages/selector/docs/history-tool-call-working-set-spec.md +112 -112
- package/packages/selector/docs/native-tool-result-selector-spec.md +34 -34
- package/packages/selector/docs/subagent-cache-reuse-spec.md +46 -46
- package/packages/selector/dsh.plugin.json +16 -16
- package/packages/selector/lib/client.js +180 -22
- package/packages/selector/lib/host-generation.js +62 -0
- package/packages/selector/lib/index.js +208 -1
- package/packages/selector/lib/invariant.js +1 -1
- package/packages/selector/lib/pruner.js +46 -44
- package/packages/selector/lib/style.css +53 -53
- package/packages/selector/lib/tail-trim.js +73 -4
- package/packages/selector/package.json +80 -21
- package/packages/selector/screenshots.json +3 -3
- package/packages/selector/src/client/CustomPolicyEditor.tsx +216 -216
- package/packages/selector/src/client/EstimatorControls.tsx +277 -277
- package/packages/selector/src/client/bridge-scope.ts +125 -0
- package/packages/selector/src/client/index.ts +122 -36
- package/packages/selector/src/client/monitor-fab.ts +0 -2
- package/packages/selector/src/client/preset-options.ts +88 -88
- package/packages/selector/src/client/scope-face.ts +24 -24
- package/packages/selector/src/compat/host-generation.ts +96 -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 -6
- package/packages/selector/src/deepseek-v4-tokenizer.ts +205 -205
- package/packages/selector/src/estimator-catalog.ts +104 -104
- package/packages/selector/src/index.ts +8 -0
- package/packages/selector/src/invariant.ts +113 -113
- package/packages/selector/src/preset-overlay.ts +626 -626
- package/packages/selector/src/pruner/content.ts +198 -198
- package/packages/selector/src/pruner/session.ts +97 -96
- package/packages/selector/src/pruner/tuning.ts +23 -23
- package/packages/selector/src/pruner/types.ts +78 -78
- package/packages/selector/src/pruner.ts +61 -41
- package/packages/selector/src/runtime/adaptive-cost.ts +205 -205
- package/packages/selector/src/runtime/custom-policy.ts +278 -278
- package/packages/selector/src/runtime/deepseek-official-pricing.ts +298 -298
- package/packages/selector/src/runtime/deepseek-v4-vision-tokens.ts +254 -254
- package/packages/selector/src/runtime/measurement.ts +444 -431
- package/packages/selector/src/runtime/reducers.ts +1674 -1674
- package/packages/selector/src/runtime/retrieve.ts +7 -3
- package/packages/selector/src/runtime/session-events.ts +30 -30
- package/packages/selector/src/runtime/tail-trim.ts +175 -165
- package/packages/selector/src/runtime/token-count.ts +72 -72
- package/packages/selector/src/runtime/tokenpilot/advisor.ts +419 -419
- package/packages/selector/src/runtime/tokenpilot/benefit.ts +200 -200
- package/packages/selector/src/runtime/tokenpilot/dedup.ts +81 -81
- package/packages/selector/src/runtime/tokenpilot/estimator.ts +151 -151
- package/packages/selector/src/runtime/tokenpilot/locator.ts +128 -128
- package/packages/selector/src/runtime/tokenpilot/read-state.ts +98 -98
- package/packages/selector/src/runtime/tokenpilot/sidechannel.ts +303 -303
- package/packages/selector/src/runtime/toolclass.ts +103 -103
- package/packages/selector/src/runtime/value.ts +48 -48
- package/packages/selector/src/settings-bridge.ts +175 -0
- package/packages/selector/tests/advisor-report.host.spec.ts +223 -223
- package/packages/selector/tests/built/client-artifact.spec.ts +6 -9
- package/packages/selector/tests/cache-prefix-audit.spec.ts +123 -123
- package/packages/selector/tests/code-skeleton.client.spec.ts +98 -94
- package/packages/selector/tests/compat/compat-narrow-waist.spec.ts +150 -0
- package/packages/selector/tests/custom-contract.client.spec.ts +226 -223
- package/packages/selector/tests/estimator-catalog.spec.ts +70 -70
- package/packages/selector/tests/estimator-route-registration.host.spec.ts +199 -194
- package/packages/selector/tests/helpers/legacy-namespace.ts +35 -35
- 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 +204 -204
- package/packages/selector/tests/preset-options-write.client.spec.ts +192 -188
- package/packages/selector/tests/preset-overlay-loader.e2e.host.spec.ts +200 -200
- package/packages/selector/tests/preset-overlay.host.spec.ts +246 -246
- package/packages/selector/tests/public/package-contract.client.spec.ts +11 -3
- package/packages/selector/tests/runtime/adaptive-cost.spec.ts +167 -167
- package/packages/selector/tests/runtime/advice-never-withholds.host.spec.ts +230 -230
- package/packages/selector/tests/runtime/advisor-invariant.spec.ts +272 -272
- package/packages/selector/tests/runtime/advisor.spec.ts +226 -226
- package/packages/selector/tests/runtime/auto-compact-config.spec.ts +523 -523
- package/packages/selector/tests/runtime/char-basis.spec.ts +30 -30
- package/packages/selector/tests/runtime/code-skeleton.spec.ts +152 -152
- package/packages/selector/tests/runtime/deepseek-official-pricing.spec.ts +186 -186
- package/packages/selector/tests/runtime/deepseek-v4-tokenizer.spec.ts +122 -122
- package/packages/selector/tests/runtime/deepseek-v4-vision-tokens.spec.ts +122 -122
- package/packages/selector/tests/runtime/deprecated-preset-options.spec.ts +96 -96
- package/packages/selector/tests/runtime/fixtures/profile-baseline.json +273 -273
- package/packages/selector/tests/runtime/fixtures/tokenizer-golden.json +106 -106
- package/packages/selector/tests/runtime/fixtures/vision-golden.json +459 -459
- package/packages/selector/tests/runtime/frequency-longstrings.spec.ts +74 -74
- package/packages/selector/tests/runtime/html-reducer.spec.ts +212 -212
- package/packages/selector/tests/runtime/line-mapping.spec.ts +153 -153
- package/packages/selector/tests/runtime/prose-reducers.spec.ts +133 -133
- package/packages/selector/tests/runtime/public/public-runtime.spec.ts +3 -3
- package/packages/selector/tests/runtime/read-input-cap.spec.ts +33 -33
- package/packages/selector/tests/runtime/search-reducer.spec.ts +110 -110
- package/packages/selector/tests/runtime/session-events.spec.ts +27 -27
- package/packages/selector/tests/runtime/sidechannel.spec.ts +241 -241
- package/packages/selector/tests/runtime/toc-and-bundled.spec.ts +159 -159
- package/packages/selector/tests/runtime/tokenizer-golden.spec.ts +53 -53
- package/packages/selector/tests/runtime/tokenpilot/benefit.spec.ts +217 -217
- package/packages/selector/tests/runtime/tokenpilot/dedup.spec.ts +52 -52
- package/packages/selector/tests/runtime/tokenpilot/estimator.spec.ts +79 -79
- package/packages/selector/tests/runtime/tokenpilot/locator.spec.ts +76 -76
- package/packages/selector/tests/runtime/tokenpilot/profile-baseline.spec.ts +116 -116
- package/packages/selector/tests/runtime/tokenpilot/read-state.spec.ts +82 -82
- package/packages/selector/tests/runtime/toolclass.spec.ts +156 -156
- package/packages/selector/tests/runtime/value.spec.ts +23 -23
- package/packages/selector/tests/settings-seat.client.spec.ts +45 -7
- package/packages/selector/tests/standing-generation.host.spec.ts +724 -724
- package/packages/selector/tests/subagent-cache-reuse.host.spec.ts +269 -269
- package/packages/selector/tests/support/cache-prefix-audit.ts +105 -105
- package/packages/selector/tests/support/mock-adapter.ts +37 -37
- package/packages/selector/tests/support/ui-primitives.tsx +34 -34
- package/packages/selector/tsconfig.json +11 -11
- package/packages/selector/tsdown.client.config.ts +102 -102
- package/packages/selector/tsdown.config.ts +20 -20
- package/scripts/capture-profile-baseline.mjs +42 -42
- package/scripts/cell-browser-e2e.mjs +354 -0
- package/scripts/generate-tokenizer-fixtures.py +81 -81
- package/scripts/generate-vision-fixtures.py +208 -208
- package/scripts/hosts.mjs +74 -0
- package/scripts/packed-components-smoke.mjs +763 -732
- package/scripts/packed-install-e2e.mjs +1330 -1273
- package/scripts/sync-hosts.mjs +174 -0
- package/scripts/toolclass-corpus-replay.mjs +281 -281
- package/scripts/verify-release.mjs +335 -318
- package/tsconfig.base.json +18 -18
- package/tsconfig.json +7 -7
- package/tsconfig.tests.json +15 -15
- package/vitest.built.config.ts +9 -9
- package/vitest.config.ts +46 -43
|
@@ -1,78 +1,78 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Pruner-internal type definitions.
|
|
3
|
-
*
|
|
4
|
-
* These types are private to the pruner module tree and are NOT re-exported
|
|
5
|
-
* through the package's public API.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import type { ContentBlock } from '@deepseek-ai/dsh-llm'
|
|
9
|
-
import type { SessionEvent } from '@deepseek-ai/dsh-session'
|
|
10
|
-
import type { TokenCount } from '../runtime/measurement.ts'
|
|
11
|
-
import type { CompressionAuditComponent } from '../runtime/audit.ts'
|
|
12
|
-
import type { HistoryMode, PruneStage } from '../runtime/types.ts'
|
|
13
|
-
|
|
14
|
-
export interface ToolCallInfo {
|
|
15
|
-
readonly name: string
|
|
16
|
-
readonly arguments: string
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export interface SnapshotCandidate {
|
|
20
|
-
readonly seq: number
|
|
21
|
-
readonly event: SessionEvent<'tool/result'>
|
|
22
|
-
readonly call: ToolCallInfo
|
|
23
|
-
/** Authoritative same-revision exact canonical content count (telemetry only). */
|
|
24
|
-
readonly count: TokenCount
|
|
25
|
-
/** Same-revision legacy heuristic price used only by bounded projections. */
|
|
26
|
-
readonly shadowedHeuristicTokenCount: number
|
|
27
|
-
/**
|
|
28
|
-
* Authoritative decision metric: character pressure in Unicode code points.
|
|
29
|
-
* Every eligibility and acceptance gate decides on this number so the plugin
|
|
30
|
-
* never depends on the routed model id owning a bundled exact tokenizer;
|
|
31
|
-
* `count` is retained as diagnostic telemetry only.
|
|
32
|
-
*/
|
|
33
|
-
readonly characterPressure: number
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export interface PlannedReplacement {
|
|
37
|
-
readonly candidate: SnapshotCandidate
|
|
38
|
-
readonly content: ContentBlock[]
|
|
39
|
-
readonly sourceSeq: number
|
|
40
|
-
readonly reducer: string
|
|
41
|
-
readonly stage: PruneStage
|
|
42
|
-
readonly component: CompressionAuditComponent
|
|
43
|
-
readonly historyMode?: HistoryMode
|
|
44
|
-
readonly charsBefore: number
|
|
45
|
-
readonly charsAfter: number
|
|
46
|
-
readonly tokensBefore: number
|
|
47
|
-
readonly tokensAfter: number
|
|
48
|
-
/**
|
|
49
|
-
* Which measurement produced this plan's decision: 'exact-tokenizer' when
|
|
50
|
-
* both sides of the reduction carry one bundled tokenizer identity,
|
|
51
|
-
* 'characters' when the proof ran on code points alone.
|
|
52
|
-
*/
|
|
53
|
-
readonly measurementBasis: 'exact-tokenizer' | 'characters'
|
|
54
|
-
readonly tokenizerId: string
|
|
55
|
-
readonly tokenizerRevision: string
|
|
56
|
-
/**
|
|
57
|
-
* Original-event lines the reducer elided (task_4c/G7 telemetry, from
|
|
58
|
-
* `ReducerOutput.elidedLines`). Rides the rewrite AUDIT record only — it is
|
|
59
|
-
* never printed into replacement content.
|
|
60
|
-
*/
|
|
61
|
-
readonly elidedLines?: number
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Discriminated History planning outcome: every skip path carries its own
|
|
66
|
-
* reason instead of collapsing into one merged "no eligible minimum reclaim"
|
|
67
|
-
* audit, so operators can tell a protected working set from an unreachable
|
|
68
|
-
* reclaim target. The former 'exact-tokenizer-unavailable' member is gone:
|
|
69
|
-
* planning decides on the character basis and can no longer refuse for a
|
|
70
|
-
* missing exact count.
|
|
71
|
-
*/
|
|
72
|
-
export type HistoryPlanOutcome =
|
|
73
|
-
| { readonly kind: 'planned', readonly plans: PlannedReplacement[] }
|
|
74
|
-
| { readonly kind: 'below-profile-trigger' }
|
|
75
|
-
| { readonly kind: 'no-safe-candidates' }
|
|
76
|
-
| { readonly kind: 'protected-working-set' }
|
|
77
|
-
| { readonly kind: 'insufficient-reclaim', readonly reclaim: number, readonly required: number }
|
|
78
|
-
| { readonly kind: 'cannot-reach-deadline-target', readonly reclaim: number, readonly required: number }
|
|
1
|
+
/**
|
|
2
|
+
* Pruner-internal type definitions.
|
|
3
|
+
*
|
|
4
|
+
* These types are private to the pruner module tree and are NOT re-exported
|
|
5
|
+
* through the package's public API.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { ContentBlock } from '@deepseek-ai/dsh-llm'
|
|
9
|
+
import type { SessionEvent } from '@deepseek-ai/dsh-session'
|
|
10
|
+
import type { TokenCount } from '../runtime/measurement.ts'
|
|
11
|
+
import type { CompressionAuditComponent } from '../runtime/audit.ts'
|
|
12
|
+
import type { HistoryMode, PruneStage } from '../runtime/types.ts'
|
|
13
|
+
|
|
14
|
+
export interface ToolCallInfo {
|
|
15
|
+
readonly name: string
|
|
16
|
+
readonly arguments: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface SnapshotCandidate {
|
|
20
|
+
readonly seq: number
|
|
21
|
+
readonly event: SessionEvent<'tool/result'>
|
|
22
|
+
readonly call: ToolCallInfo
|
|
23
|
+
/** Authoritative same-revision exact canonical content count (telemetry only). */
|
|
24
|
+
readonly count: TokenCount
|
|
25
|
+
/** Same-revision legacy heuristic price used only by bounded projections. */
|
|
26
|
+
readonly shadowedHeuristicTokenCount: number
|
|
27
|
+
/**
|
|
28
|
+
* Authoritative decision metric: character pressure in Unicode code points.
|
|
29
|
+
* Every eligibility and acceptance gate decides on this number so the plugin
|
|
30
|
+
* never depends on the routed model id owning a bundled exact tokenizer;
|
|
31
|
+
* `count` is retained as diagnostic telemetry only.
|
|
32
|
+
*/
|
|
33
|
+
readonly characterPressure: number
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface PlannedReplacement {
|
|
37
|
+
readonly candidate: SnapshotCandidate
|
|
38
|
+
readonly content: ContentBlock[]
|
|
39
|
+
readonly sourceSeq: number
|
|
40
|
+
readonly reducer: string
|
|
41
|
+
readonly stage: PruneStage
|
|
42
|
+
readonly component: CompressionAuditComponent
|
|
43
|
+
readonly historyMode?: HistoryMode
|
|
44
|
+
readonly charsBefore: number
|
|
45
|
+
readonly charsAfter: number
|
|
46
|
+
readonly tokensBefore: number
|
|
47
|
+
readonly tokensAfter: number
|
|
48
|
+
/**
|
|
49
|
+
* Which measurement produced this plan's decision: 'exact-tokenizer' when
|
|
50
|
+
* both sides of the reduction carry one bundled tokenizer identity,
|
|
51
|
+
* 'characters' when the proof ran on code points alone.
|
|
52
|
+
*/
|
|
53
|
+
readonly measurementBasis: 'exact-tokenizer' | 'characters'
|
|
54
|
+
readonly tokenizerId: string
|
|
55
|
+
readonly tokenizerRevision: string
|
|
56
|
+
/**
|
|
57
|
+
* Original-event lines the reducer elided (task_4c/G7 telemetry, from
|
|
58
|
+
* `ReducerOutput.elidedLines`). Rides the rewrite AUDIT record only — it is
|
|
59
|
+
* never printed into replacement content.
|
|
60
|
+
*/
|
|
61
|
+
readonly elidedLines?: number
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Discriminated History planning outcome: every skip path carries its own
|
|
66
|
+
* reason instead of collapsing into one merged "no eligible minimum reclaim"
|
|
67
|
+
* audit, so operators can tell a protected working set from an unreachable
|
|
68
|
+
* reclaim target. The former 'exact-tokenizer-unavailable' member is gone:
|
|
69
|
+
* planning decides on the character basis and can no longer refuse for a
|
|
70
|
+
* missing exact count.
|
|
71
|
+
*/
|
|
72
|
+
export type HistoryPlanOutcome =
|
|
73
|
+
| { readonly kind: 'planned', readonly plans: PlannedReplacement[] }
|
|
74
|
+
| { readonly kind: 'below-profile-trigger' }
|
|
75
|
+
| { readonly kind: 'no-safe-candidates' }
|
|
76
|
+
| { readonly kind: 'protected-working-set' }
|
|
77
|
+
| { readonly kind: 'insufficient-reclaim', readonly reclaim: number, readonly required: number }
|
|
78
|
+
| { readonly kind: 'cannot-reach-deadline-target', readonly reclaim: number, readonly required: number }
|
|
@@ -12,8 +12,23 @@ import { Service } from '@deepseek-ai/cordis'
|
|
|
12
12
|
import type { Context } from '@deepseek-ai/cordis'
|
|
13
13
|
import z from '@deepseek-ai/schemastery'
|
|
14
14
|
import { createUserMessage, freezeMessage } from '@deepseek-ai/dsh-llm'
|
|
15
|
-
import type { ContentBlock, UserMessage } from '@deepseek-ai/dsh-llm'
|
|
16
|
-
|
|
15
|
+
import type { ContentBlock, ToolCallId, UserMessage } from '@deepseek-ai/dsh-llm'
|
|
16
|
+
// ≤0.1.1 宿主 loader 模块表的 dsh-session 没有 SessionSeq 运行时导出——静态
|
|
17
|
+
// named import 在 ESM 链接期即炸,loader entry(tool-result-pruner)mount 失败
|
|
18
|
+
// 连累整个 standard 预设(会话 resume 全挂,2026-10-08 五格 0.1.0/0.1.1 实证)。
|
|
19
|
+
// namespace 软取(链接零失败)+ 恒等回退:老线 session 事件 seq 本就是裸 number,
|
|
20
|
+
// 身份封印仅是新线防呆。CJS interop 形态(named 挂 default 下)一并兼容。
|
|
21
|
+
import * as dshSessionNamespace from '@deepseek-ai/dsh-session'
|
|
22
|
+
import type { SessionSeq as SessionSeqBrand } from '@deepseek-ai/dsh-session'
|
|
23
|
+
const dshSessionSoft = dshSessionNamespace as unknown as {
|
|
24
|
+
SessionSeq?: (value: number) => SessionSeqBrand
|
|
25
|
+
default?: { SessionSeq?: (value: number) => SessionSeqBrand }
|
|
26
|
+
}
|
|
27
|
+
const SessionSeqImpl =
|
|
28
|
+
dshSessionSoft.SessionSeq ??
|
|
29
|
+
dshSessionSoft.default?.SessionSeq ??
|
|
30
|
+
((value: number) => value as SessionSeqBrand)
|
|
31
|
+
const SessionSeq = SessionSeqImpl as (value: number) => SessionSeqBrand
|
|
17
32
|
|
|
18
33
|
// 0.1.7-rc.2 removed the shared 'plugin' source kind — each producer declares
|
|
19
34
|
// its own kind in its own module (official pattern: compaction's
|
|
@@ -98,6 +113,7 @@ import { INTENT_FOLD_MARKER, renderIntentFoldBlock } from './runtime/tokenpilot/
|
|
|
98
113
|
import { buildIntentSummarySystemPrompt, buildIntentSummaryUserPrompt, parseIntentSummaryAnswer } from './runtime/tokenpilot/advisor-prompt.ts'
|
|
99
114
|
import { getSummaryOverride, observeIntentEnabled, recordIntentFold } from './runtime/tokenpilot/advisor-state.ts'
|
|
100
115
|
import { observeContextUsage } from './runtime/monitor.ts'
|
|
116
|
+
import { compressionMessageSource, viewToolResult } from './compat/tool-result.ts'
|
|
101
117
|
|
|
102
118
|
import {
|
|
103
119
|
DedupeTable,
|
|
@@ -109,7 +125,7 @@ import { getSavingsLedger } from './runtime/savings.ts'
|
|
|
109
125
|
|
|
110
126
|
/** 节省统计配对键用原文(纯文本块才可配对;富内容返回 undefined)。 */
|
|
111
127
|
function originalTextForSavings(candidate: SnapshotCandidate): string | undefined {
|
|
112
|
-
return flattenPlainText(candidate.event.data.message.
|
|
128
|
+
return flattenPlainText(viewToolResult(candidate.event.data.message).blocks as ContentBlock[])
|
|
113
129
|
}
|
|
114
130
|
import {
|
|
115
131
|
charsForTokens,
|
|
@@ -601,7 +617,8 @@ export class ToolResultPruner extends Service {
|
|
|
601
617
|
// carrying the marker would fail the host's closed-transaction validation.
|
|
602
618
|
const replacement = createUserMessage({
|
|
603
619
|
content,
|
|
604
|
-
|
|
620
|
+
// Generation-gated: 0.1.x hosts only accept the shared plugin kind.
|
|
621
|
+
source: compressionMessageSource() as { kind: 'dsh-context-compression' },
|
|
605
622
|
})
|
|
606
623
|
session.append('user/message', replacement, {
|
|
607
624
|
surfaceOp: { op: 'replace', startSeq: SessionSeq(checkpointSeq), endSeq: SessionSeq(checkpointSeq) },
|
|
@@ -757,7 +774,7 @@ export class ToolResultPruner extends Service {
|
|
|
757
774
|
.map(candidate => ({
|
|
758
775
|
seq: candidate.seq,
|
|
759
776
|
characterPressure: candidate.characterPressure,
|
|
760
|
-
preview: advisorCandidatePreview(candidate.call.name, candidate.event.data.message.
|
|
777
|
+
preview: advisorCandidatePreview(candidate.call.name, viewToolResult(candidate.event.data.message).blocks),
|
|
761
778
|
}))
|
|
762
779
|
|
|
763
780
|
let sawFailure = false
|
|
@@ -865,8 +882,7 @@ export class ToolResultPruner extends Service {
|
|
|
865
882
|
for (const seq of range.seqs) {
|
|
866
883
|
const candidate = bySeq.get(seq)
|
|
867
884
|
if (candidate === undefined) continue
|
|
868
|
-
const
|
|
869
|
-
const text = onlyTextBlock(result.content)?.text ?? ''
|
|
885
|
+
const text = onlyTextBlock(viewToolResult(candidate.event.data.message).blocks as ContentBlock[])?.text ?? ''
|
|
870
886
|
const masked = maskCandidateForSummary(seq, candidate.call.name, candidate.call.arguments, text)
|
|
871
887
|
records.push({ seq, role: masked.role, toolName: candidate.call.name, line: masked.record })
|
|
872
888
|
for (const line of extractErrorLines(text)) errorLines.add(line)
|
|
@@ -961,8 +977,7 @@ export class ToolResultPruner extends Service {
|
|
|
961
977
|
let charsAfter = 0
|
|
962
978
|
for (const seq of foldable) {
|
|
963
979
|
const candidate = bySeq.get(seq)!
|
|
964
|
-
const
|
|
965
|
-
const block = onlyTextBlock(result.content)
|
|
980
|
+
const block = onlyTextBlock(viewToolResult(candidate.event.data.message).blocks as ContentBlock[])
|
|
966
981
|
if (block === null) continue
|
|
967
982
|
const text = renderIntentFoldBlock(pending, seq)
|
|
968
983
|
const plan = this.plan(
|
|
@@ -1475,7 +1490,7 @@ export class ToolResultPruner extends Service {
|
|
|
1475
1490
|
const ledger = getSavingsLedger()
|
|
1476
1491
|
const sid = String(session.id)
|
|
1477
1492
|
for (const candidate of candidates) {
|
|
1478
|
-
const text = flattenPlainText(candidate.event.data.message.
|
|
1493
|
+
const text = flattenPlainText(viewToolResult(candidate.event.data.message).blocks as ContentBlock[])
|
|
1479
1494
|
if (text === undefined) continue
|
|
1480
1495
|
ledger.noteFullText({
|
|
1481
1496
|
sessionId: sid,
|
|
@@ -1603,11 +1618,14 @@ export class ToolResultPruner extends Service {
|
|
|
1603
1618
|
if (shadowedHeuristicTokenCount === undefined) {
|
|
1604
1619
|
throw new Error(`surface node ${String(seq)} is absent from the atomic legacy projection`)
|
|
1605
1620
|
}
|
|
1606
|
-
|
|
1621
|
+
// Generation-gated triple view: block set / error flag / call id move
|
|
1622
|
+
// together across the 0.1.x envelope and the flat 0.2.0 message.
|
|
1623
|
+
const resultView = viewToolResult(event.data.message)
|
|
1624
|
+
const content = resultView.blocks as ContentBlock[]
|
|
1607
1625
|
candidates.push({
|
|
1608
1626
|
seq,
|
|
1609
1627
|
event,
|
|
1610
|
-
call: calls.get(
|
|
1628
|
+
call: calls.get(resultView.callId ?? '') ?? { name: 'unknown', arguments: '{}' },
|
|
1611
1629
|
count: onlyTextBlocks(content) === null
|
|
1612
1630
|
? unavailableCount(`surface node ${String(seq)} contains unsupported rich tool-result content`)
|
|
1613
1631
|
: measured.get(seq) ?? unavailableCount(`surface node ${String(seq)} is absent from the atomic token view`),
|
|
@@ -1627,8 +1645,8 @@ export class ToolResultPruner extends Service {
|
|
|
1627
1645
|
): PlannedReplacement | null {
|
|
1628
1646
|
if (this.isRecoveryExempt(session, candidate)) return null
|
|
1629
1647
|
if (candidate.characterPressure <= charsForTokens(policy.nativeTriggerTokens)) return null
|
|
1630
|
-
const
|
|
1631
|
-
if (onlyTextBlocks(
|
|
1648
|
+
const resultContent = viewToolResult(candidate.event.data.message).blocks as ContentBlock[]
|
|
1649
|
+
if (onlyTextBlocks(resultContent) === null) return null
|
|
1632
1650
|
const sourceSeq = rootToolResultSeq(session, candidate.seq)
|
|
1633
1651
|
// R9b site: the marker's retrieve hint starts at the event line right
|
|
1634
1652
|
// after the retained head, computed inside nativePruneContent.
|
|
@@ -1637,7 +1655,7 @@ export class ToolResultPruner extends Service {
|
|
|
1637
1655
|
let tail = this.state.config.tailChars
|
|
1638
1656
|
for (let attempt = 0; attempt < 10; attempt += 1) {
|
|
1639
1657
|
const threshold = head + codePointLength(marker(1)) + tail
|
|
1640
|
-
const content = nativePruneContent(
|
|
1658
|
+
const content = nativePruneContent(resultContent, threshold, head, tail, marker)
|
|
1641
1659
|
if (content !== null) {
|
|
1642
1660
|
const plan = this.plan(
|
|
1643
1661
|
candidate,
|
|
@@ -1679,8 +1697,7 @@ export class ToolResultPruner extends Service {
|
|
|
1679
1697
|
view: CompactionTokenView,
|
|
1680
1698
|
): PlannedReplacement | null {
|
|
1681
1699
|
if (typeof candidate.event.surfaceOp === 'object') return null
|
|
1682
|
-
const
|
|
1683
|
-
const text = flattenPlainText(result.content)
|
|
1700
|
+
const text = flattenPlainText(viewToolResult(candidate.event.data.message).blocks as ContentBlock[])
|
|
1684
1701
|
if (text === undefined) return null
|
|
1685
1702
|
if (candidate.characterPressure <= charsForTokens(policy.freshTriggerTokens)) return null
|
|
1686
1703
|
let table = this.state.dedupeTables.get(session)
|
|
@@ -1727,11 +1744,11 @@ export class ToolResultPruner extends Service {
|
|
|
1727
1744
|
// pre-step coordinate filter prevents previously-kept originals from ever
|
|
1728
1745
|
// being reconsidered after their first request.
|
|
1729
1746
|
if (typeof candidate.event.surfaceOp === 'object') return null
|
|
1730
|
-
const
|
|
1747
|
+
const resultView = viewToolResult(candidate.event.data.message)
|
|
1731
1748
|
if (candidate.characterPressure <= charsForTokens(policy.freshTriggerTokens)) return null
|
|
1732
1749
|
const sourceSeq = candidate.seq
|
|
1733
1750
|
const sourceRef = sourceRefFn(session, sourceSeq)
|
|
1734
|
-
const textBlock = onlyTextBlock(
|
|
1751
|
+
const textBlock = onlyTextBlock(resultView.blocks as ContentBlock[])
|
|
1735
1752
|
if (textBlock !== null) {
|
|
1736
1753
|
let budgetChars = Math.max(1, Math.floor(codePointLength(textBlock.text) * 0.75))
|
|
1737
1754
|
const codeSkeleton = this.activeSettings(session).codeSkeleton.enabled
|
|
@@ -1742,7 +1759,7 @@ export class ToolResultPruner extends Service {
|
|
|
1742
1759
|
text: textBlock.text,
|
|
1743
1760
|
budgetChars,
|
|
1744
1761
|
sourceRef,
|
|
1745
|
-
isError:
|
|
1762
|
+
isError: resultView.isError || candidate.event.data.error !== undefined,
|
|
1746
1763
|
codeSkeleton,
|
|
1747
1764
|
})
|
|
1748
1765
|
if (output !== null) {
|
|
@@ -1805,8 +1822,8 @@ export class ToolResultPruner extends Service {
|
|
|
1805
1822
|
// example one carrying an image) must never reach it. The character basis
|
|
1806
1823
|
// no longer inherits the exact-tokenizer precondition that used to reject
|
|
1807
1824
|
// this path implicitly, so the guard has to be explicit.
|
|
1808
|
-
const
|
|
1809
|
-
if (onlyTextBlocks(
|
|
1825
|
+
const redactedContent = viewToolResult(candidate.event.data.message).blocks as ContentBlock[]
|
|
1826
|
+
if (onlyTextBlocks(redactedContent) === null) return null
|
|
1810
1827
|
const text = [
|
|
1811
1828
|
'[Tool result reduced to satisfy the completed-step aggregate budget]',
|
|
1812
1829
|
`tool: ${candidate.call.name}`,
|
|
@@ -1839,8 +1856,7 @@ export class ToolResultPruner extends Service {
|
|
|
1839
1856
|
historyMode?: HistoryMode,
|
|
1840
1857
|
): PlannedReplacement | null {
|
|
1841
1858
|
if (!isError(candidate)) return null
|
|
1842
|
-
const
|
|
1843
|
-
const blocks = onlyTextBlocks(result.content)
|
|
1859
|
+
const blocks = onlyTextBlocks(viewToolResult(candidate.event.data.message).blocks as ContentBlock[])
|
|
1844
1860
|
if (blocks === null) return null
|
|
1845
1861
|
const text = blocks.map(block => block.text).join('\n')
|
|
1846
1862
|
const sourceSeq = rootToolResultSeq(session, candidate.seq)
|
|
@@ -1897,8 +1913,7 @@ export class ToolResultPruner extends Service {
|
|
|
1897
1913
|
const protectedSeqs = this.protectedHistoryCandidateSeqs(candidates, policy)
|
|
1898
1914
|
const isUnsafe = (candidate: SnapshotCandidate): boolean => {
|
|
1899
1915
|
if (this.isRecoveryExempt(session, candidate)) return true
|
|
1900
|
-
const
|
|
1901
|
-
const block = onlyTextBlock(result.content)
|
|
1916
|
+
const block = onlyTextBlock(viewToolResult(candidate.event.data.message).blocks as ContentBlock[])
|
|
1902
1917
|
// Fold-once: an already-folded intent block is never re-candidated.
|
|
1903
1918
|
if (block?.text.includes(INTENT_FOLD_MARKER) === true) return true
|
|
1904
1919
|
return block?.text.includes('[Old tool result content cleared from active context]') === true
|
|
@@ -1961,8 +1976,8 @@ export class ToolResultPruner extends Service {
|
|
|
1961
1976
|
? minReclaimChars
|
|
1962
1977
|
: required
|
|
1963
1978
|
for (const candidate of eligible) {
|
|
1964
|
-
const
|
|
1965
|
-
const block = onlyTextBlock(
|
|
1979
|
+
const resultView = viewToolResult(candidate.event.data.message)
|
|
1980
|
+
const block = onlyTextBlock(resultView.blocks as ContentBlock[])
|
|
1966
1981
|
// TokenPilot-inspired R2: a read output whose file was later mutated is
|
|
1967
1982
|
// superseded — its text can no longer match the file — so it takes the
|
|
1968
1983
|
// small whole-result placeholder before the ordinary reducer runs.
|
|
@@ -2010,7 +2025,7 @@ export class ToolResultPruner extends Service {
|
|
|
2010
2025
|
toolName: candidate.call.name,
|
|
2011
2026
|
sourceRef: sourceRefFn(session, sourceSeq),
|
|
2012
2027
|
charsBefore: codePointLength(block.text),
|
|
2013
|
-
isError:
|
|
2028
|
+
isError: resultView.isError || candidate.event.data.error !== undefined,
|
|
2014
2029
|
text: block.text,
|
|
2015
2030
|
compact: false,
|
|
2016
2031
|
})
|
|
@@ -2020,7 +2035,7 @@ export class ToolResultPruner extends Service {
|
|
|
2020
2035
|
text: block.text,
|
|
2021
2036
|
budgetChars: 1_200,
|
|
2022
2037
|
sourceRef: sourceRefFn(session, sourceSeq),
|
|
2023
|
-
isError:
|
|
2038
|
+
isError: resultView.isError || candidate.event.data.error !== undefined,
|
|
2024
2039
|
}
|
|
2025
2040
|
if (!verifyReduction(verifyInput, output)) continue
|
|
2026
2041
|
// TokenPilot-inspired R3: when read-state semantics are on, append an
|
|
@@ -2166,22 +2181,25 @@ export class ToolResultPruner extends Service {
|
|
|
2166
2181
|
const resultSeqs = nodes.slice(index + 1, index + 1 + calls.length)
|
|
2167
2182
|
if (resultSeqs.length !== calls.length || resultSeqs.some(seq => protectedResults.has(seq))) continue
|
|
2168
2183
|
const results = resultSeqs.map(seq => events[seq])
|
|
2169
|
-
|
|
2170
|
-
|
|
2184
|
+
// One generation-gated triple view per result message, shared by the
|
|
2185
|
+
// safety gate below and the call-id pairing.
|
|
2186
|
+
const resultViews = results.map(event =>
|
|
2187
|
+
event?.type === 'tool/result' ? viewToolResult(event.data.message) : undefined)
|
|
2188
|
+
if (results.some((event, resultIndex): boolean => {
|
|
2189
|
+
const view = resultViews[resultIndex]
|
|
2190
|
+
if (event?.type !== 'tool/result' || view === undefined
|
|
2171
2191
|
|| event.data.turn !== assistant.data.turn || event.data.step !== assistant.data.step
|
|
2172
2192
|
|| event.data.error !== undefined) return true
|
|
2173
|
-
|
|
2174
|
-
if (block.isError === true) return true
|
|
2193
|
+
if (view.isError) return true
|
|
2175
2194
|
// Images and other rich inner blocks stay fail-open: a TailTrim stub
|
|
2176
2195
|
// would silently delete them from the active context.
|
|
2177
|
-
return
|
|
2196
|
+
return (view.blocks as ContentBlock[]).some((contentBlock: ContentBlock) => contentBlock.type !== 'text')
|
|
2178
2197
|
})) continue
|
|
2179
2198
|
const next = events[nodes[index + 1 + calls.length] ?? -1]
|
|
2180
2199
|
if (next?.type === 'tool/result'
|
|
2181
2200
|
&& next.data.turn === assistant.data.turn
|
|
2182
2201
|
&& next.data.step === assistant.data.step) continue
|
|
2183
|
-
const resultIds =
|
|
2184
|
-
? String(event.data.message.source.callId) : '')
|
|
2202
|
+
const resultIds = resultViews.map(view => view === undefined ? '' : String(view.callId))
|
|
2185
2203
|
if (new Set(resultIds).size !== resultIds.length
|
|
2186
2204
|
|| resultIds.some((id, resultIndex) => id !== callIds[resultIndex])) continue
|
|
2187
2205
|
const shadowedSeqs = [assistantSeq, ...resultSeqs]
|
|
@@ -2294,7 +2312,7 @@ export class ToolResultPruner extends Service {
|
|
|
2294
2312
|
const groupText = shadowedSeqs
|
|
2295
2313
|
.map(seq => sessionEvents(session)[seq])
|
|
2296
2314
|
.filter((event): event is SessionEvent<'tool/result'> => event?.type === 'tool/result')
|
|
2297
|
-
.map(event => flattenPlainText(event.data.message.
|
|
2315
|
+
.map(event => flattenPlainText(viewToolResult(event.data.message).blocks as ContentBlock[]))
|
|
2298
2316
|
.filter((text): text is string => text !== undefined)
|
|
2299
2317
|
.join('\n')
|
|
2300
2318
|
if (groupText.length > 0) {
|
|
@@ -2380,7 +2398,7 @@ export class ToolResultPruner extends Service {
|
|
|
2380
2398
|
// noise without reclaiming context. Text-level because the placeholder
|
|
2381
2399
|
// guidance lines (source refs, retrieval hints) must pay for themselves.
|
|
2382
2400
|
if (options.noNetSavingsGuard === true) {
|
|
2383
|
-
const originalBlocks = onlyTextBlocks(candidate.event.data.message.
|
|
2401
|
+
const originalBlocks = onlyTextBlocks(viewToolResult(candidate.event.data.message).blocks as ContentBlock[])
|
|
2384
2402
|
const replacementBlocks = onlyTextBlocks(content)
|
|
2385
2403
|
if (originalBlocks !== null && replacementBlocks !== null) {
|
|
2386
2404
|
const originalChars = originalBlocks.reduce((sum, block) => sum + codePointLength(block.text), 0)
|
|
@@ -2410,7 +2428,9 @@ export class ToolResultPruner extends Service {
|
|
|
2410
2428
|
private land(session: Session, plan: PlannedReplacement): PrunedEntry | null {
|
|
2411
2429
|
const { candidate } = plan
|
|
2412
2430
|
// 0.1.7-rc.2: the tool message's own `content` is the result block set —
|
|
2413
|
-
// no wrapping result block anymore.
|
|
2431
|
+
// no wrapping result block anymore. The triple view still resolves the
|
|
2432
|
+
// call id axis on a legacy envelope.
|
|
2433
|
+
const resultView = viewToolResult(candidate.event.data.message)
|
|
2414
2434
|
const message = freezeMessage<ToolResultMessage>({
|
|
2415
2435
|
...candidate.event.data.message,
|
|
2416
2436
|
content: plan.content,
|
|
@@ -2478,7 +2498,7 @@ export class ToolResultPruner extends Service {
|
|
|
2478
2498
|
originalSeq: candidate.seq,
|
|
2479
2499
|
sourceSeq: plan.sourceSeq,
|
|
2480
2500
|
replacementSeq: replacement.seq,
|
|
2481
|
-
callId:
|
|
2501
|
+
callId: resultView.callId as ToolCallId,
|
|
2482
2502
|
reducer: plan.reducer,
|
|
2483
2503
|
stage: plan.stage,
|
|
2484
2504
|
charsBefore: plan.charsBefore,
|