dsh-context-compression-improved 0.8.0 → 0.9.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/.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 +40 -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 +23 -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 +178 -21
- 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 +41 -43
- 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 +109 -31
- 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 +45 -40
- 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 +431 -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/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,7 +12,7 @@ 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'
|
|
15
|
+
import type { ContentBlock, ToolCallId, UserMessage } from '@deepseek-ai/dsh-llm'
|
|
16
16
|
import { SessionSeq } from '@deepseek-ai/dsh-session'
|
|
17
17
|
|
|
18
18
|
// 0.1.7-rc.2 removed the shared 'plugin' source kind — each producer declares
|
|
@@ -98,6 +98,7 @@ import { INTENT_FOLD_MARKER, renderIntentFoldBlock } from './runtime/tokenpilot/
|
|
|
98
98
|
import { buildIntentSummarySystemPrompt, buildIntentSummaryUserPrompt, parseIntentSummaryAnswer } from './runtime/tokenpilot/advisor-prompt.ts'
|
|
99
99
|
import { getSummaryOverride, observeIntentEnabled, recordIntentFold } from './runtime/tokenpilot/advisor-state.ts'
|
|
100
100
|
import { observeContextUsage } from './runtime/monitor.ts'
|
|
101
|
+
import { compressionMessageSource, viewToolResult } from './compat/tool-result.ts'
|
|
101
102
|
|
|
102
103
|
import {
|
|
103
104
|
DedupeTable,
|
|
@@ -109,7 +110,7 @@ import { getSavingsLedger } from './runtime/savings.ts'
|
|
|
109
110
|
|
|
110
111
|
/** 节省统计配对键用原文(纯文本块才可配对;富内容返回 undefined)。 */
|
|
111
112
|
function originalTextForSavings(candidate: SnapshotCandidate): string | undefined {
|
|
112
|
-
return flattenPlainText(candidate.event.data.message.
|
|
113
|
+
return flattenPlainText(viewToolResult(candidate.event.data.message).blocks as ContentBlock[])
|
|
113
114
|
}
|
|
114
115
|
import {
|
|
115
116
|
charsForTokens,
|
|
@@ -601,7 +602,8 @@ export class ToolResultPruner extends Service {
|
|
|
601
602
|
// carrying the marker would fail the host's closed-transaction validation.
|
|
602
603
|
const replacement = createUserMessage({
|
|
603
604
|
content,
|
|
604
|
-
|
|
605
|
+
// Generation-gated: 0.1.x hosts only accept the shared plugin kind.
|
|
606
|
+
source: compressionMessageSource() as { kind: 'dsh-context-compression' },
|
|
605
607
|
})
|
|
606
608
|
session.append('user/message', replacement, {
|
|
607
609
|
surfaceOp: { op: 'replace', startSeq: SessionSeq(checkpointSeq), endSeq: SessionSeq(checkpointSeq) },
|
|
@@ -757,7 +759,7 @@ export class ToolResultPruner extends Service {
|
|
|
757
759
|
.map(candidate => ({
|
|
758
760
|
seq: candidate.seq,
|
|
759
761
|
characterPressure: candidate.characterPressure,
|
|
760
|
-
preview: advisorCandidatePreview(candidate.call.name, candidate.event.data.message.
|
|
762
|
+
preview: advisorCandidatePreview(candidate.call.name, viewToolResult(candidate.event.data.message).blocks),
|
|
761
763
|
}))
|
|
762
764
|
|
|
763
765
|
let sawFailure = false
|
|
@@ -865,8 +867,7 @@ export class ToolResultPruner extends Service {
|
|
|
865
867
|
for (const seq of range.seqs) {
|
|
866
868
|
const candidate = bySeq.get(seq)
|
|
867
869
|
if (candidate === undefined) continue
|
|
868
|
-
const
|
|
869
|
-
const text = onlyTextBlock(result.content)?.text ?? ''
|
|
870
|
+
const text = onlyTextBlock(viewToolResult(candidate.event.data.message).blocks as ContentBlock[])?.text ?? ''
|
|
870
871
|
const masked = maskCandidateForSummary(seq, candidate.call.name, candidate.call.arguments, text)
|
|
871
872
|
records.push({ seq, role: masked.role, toolName: candidate.call.name, line: masked.record })
|
|
872
873
|
for (const line of extractErrorLines(text)) errorLines.add(line)
|
|
@@ -961,8 +962,7 @@ export class ToolResultPruner extends Service {
|
|
|
961
962
|
let charsAfter = 0
|
|
962
963
|
for (const seq of foldable) {
|
|
963
964
|
const candidate = bySeq.get(seq)!
|
|
964
|
-
const
|
|
965
|
-
const block = onlyTextBlock(result.content)
|
|
965
|
+
const block = onlyTextBlock(viewToolResult(candidate.event.data.message).blocks as ContentBlock[])
|
|
966
966
|
if (block === null) continue
|
|
967
967
|
const text = renderIntentFoldBlock(pending, seq)
|
|
968
968
|
const plan = this.plan(
|
|
@@ -1475,7 +1475,7 @@ export class ToolResultPruner extends Service {
|
|
|
1475
1475
|
const ledger = getSavingsLedger()
|
|
1476
1476
|
const sid = String(session.id)
|
|
1477
1477
|
for (const candidate of candidates) {
|
|
1478
|
-
const text = flattenPlainText(candidate.event.data.message.
|
|
1478
|
+
const text = flattenPlainText(viewToolResult(candidate.event.data.message).blocks as ContentBlock[])
|
|
1479
1479
|
if (text === undefined) continue
|
|
1480
1480
|
ledger.noteFullText({
|
|
1481
1481
|
sessionId: sid,
|
|
@@ -1603,11 +1603,14 @@ export class ToolResultPruner extends Service {
|
|
|
1603
1603
|
if (shadowedHeuristicTokenCount === undefined) {
|
|
1604
1604
|
throw new Error(`surface node ${String(seq)} is absent from the atomic legacy projection`)
|
|
1605
1605
|
}
|
|
1606
|
-
|
|
1606
|
+
// Generation-gated triple view: block set / error flag / call id move
|
|
1607
|
+
// together across the 0.1.x envelope and the flat 0.2.0 message.
|
|
1608
|
+
const resultView = viewToolResult(event.data.message)
|
|
1609
|
+
const content = resultView.blocks as ContentBlock[]
|
|
1607
1610
|
candidates.push({
|
|
1608
1611
|
seq,
|
|
1609
1612
|
event,
|
|
1610
|
-
call: calls.get(
|
|
1613
|
+
call: calls.get(resultView.callId ?? '') ?? { name: 'unknown', arguments: '{}' },
|
|
1611
1614
|
count: onlyTextBlocks(content) === null
|
|
1612
1615
|
? unavailableCount(`surface node ${String(seq)} contains unsupported rich tool-result content`)
|
|
1613
1616
|
: measured.get(seq) ?? unavailableCount(`surface node ${String(seq)} is absent from the atomic token view`),
|
|
@@ -1627,8 +1630,8 @@ export class ToolResultPruner extends Service {
|
|
|
1627
1630
|
): PlannedReplacement | null {
|
|
1628
1631
|
if (this.isRecoveryExempt(session, candidate)) return null
|
|
1629
1632
|
if (candidate.characterPressure <= charsForTokens(policy.nativeTriggerTokens)) return null
|
|
1630
|
-
const
|
|
1631
|
-
if (onlyTextBlocks(
|
|
1633
|
+
const resultContent = viewToolResult(candidate.event.data.message).blocks as ContentBlock[]
|
|
1634
|
+
if (onlyTextBlocks(resultContent) === null) return null
|
|
1632
1635
|
const sourceSeq = rootToolResultSeq(session, candidate.seq)
|
|
1633
1636
|
// R9b site: the marker's retrieve hint starts at the event line right
|
|
1634
1637
|
// after the retained head, computed inside nativePruneContent.
|
|
@@ -1637,7 +1640,7 @@ export class ToolResultPruner extends Service {
|
|
|
1637
1640
|
let tail = this.state.config.tailChars
|
|
1638
1641
|
for (let attempt = 0; attempt < 10; attempt += 1) {
|
|
1639
1642
|
const threshold = head + codePointLength(marker(1)) + tail
|
|
1640
|
-
const content = nativePruneContent(
|
|
1643
|
+
const content = nativePruneContent(resultContent, threshold, head, tail, marker)
|
|
1641
1644
|
if (content !== null) {
|
|
1642
1645
|
const plan = this.plan(
|
|
1643
1646
|
candidate,
|
|
@@ -1679,8 +1682,7 @@ export class ToolResultPruner extends Service {
|
|
|
1679
1682
|
view: CompactionTokenView,
|
|
1680
1683
|
): PlannedReplacement | null {
|
|
1681
1684
|
if (typeof candidate.event.surfaceOp === 'object') return null
|
|
1682
|
-
const
|
|
1683
|
-
const text = flattenPlainText(result.content)
|
|
1685
|
+
const text = flattenPlainText(viewToolResult(candidate.event.data.message).blocks as ContentBlock[])
|
|
1684
1686
|
if (text === undefined) return null
|
|
1685
1687
|
if (candidate.characterPressure <= charsForTokens(policy.freshTriggerTokens)) return null
|
|
1686
1688
|
let table = this.state.dedupeTables.get(session)
|
|
@@ -1727,11 +1729,11 @@ export class ToolResultPruner extends Service {
|
|
|
1727
1729
|
// pre-step coordinate filter prevents previously-kept originals from ever
|
|
1728
1730
|
// being reconsidered after their first request.
|
|
1729
1731
|
if (typeof candidate.event.surfaceOp === 'object') return null
|
|
1730
|
-
const
|
|
1732
|
+
const resultView = viewToolResult(candidate.event.data.message)
|
|
1731
1733
|
if (candidate.characterPressure <= charsForTokens(policy.freshTriggerTokens)) return null
|
|
1732
1734
|
const sourceSeq = candidate.seq
|
|
1733
1735
|
const sourceRef = sourceRefFn(session, sourceSeq)
|
|
1734
|
-
const textBlock = onlyTextBlock(
|
|
1736
|
+
const textBlock = onlyTextBlock(resultView.blocks as ContentBlock[])
|
|
1735
1737
|
if (textBlock !== null) {
|
|
1736
1738
|
let budgetChars = Math.max(1, Math.floor(codePointLength(textBlock.text) * 0.75))
|
|
1737
1739
|
const codeSkeleton = this.activeSettings(session).codeSkeleton.enabled
|
|
@@ -1742,7 +1744,7 @@ export class ToolResultPruner extends Service {
|
|
|
1742
1744
|
text: textBlock.text,
|
|
1743
1745
|
budgetChars,
|
|
1744
1746
|
sourceRef,
|
|
1745
|
-
isError:
|
|
1747
|
+
isError: resultView.isError || candidate.event.data.error !== undefined,
|
|
1746
1748
|
codeSkeleton,
|
|
1747
1749
|
})
|
|
1748
1750
|
if (output !== null) {
|
|
@@ -1805,8 +1807,8 @@ export class ToolResultPruner extends Service {
|
|
|
1805
1807
|
// example one carrying an image) must never reach it. The character basis
|
|
1806
1808
|
// no longer inherits the exact-tokenizer precondition that used to reject
|
|
1807
1809
|
// this path implicitly, so the guard has to be explicit.
|
|
1808
|
-
const
|
|
1809
|
-
if (onlyTextBlocks(
|
|
1810
|
+
const redactedContent = viewToolResult(candidate.event.data.message).blocks as ContentBlock[]
|
|
1811
|
+
if (onlyTextBlocks(redactedContent) === null) return null
|
|
1810
1812
|
const text = [
|
|
1811
1813
|
'[Tool result reduced to satisfy the completed-step aggregate budget]',
|
|
1812
1814
|
`tool: ${candidate.call.name}`,
|
|
@@ -1839,8 +1841,7 @@ export class ToolResultPruner extends Service {
|
|
|
1839
1841
|
historyMode?: HistoryMode,
|
|
1840
1842
|
): PlannedReplacement | null {
|
|
1841
1843
|
if (!isError(candidate)) return null
|
|
1842
|
-
const
|
|
1843
|
-
const blocks = onlyTextBlocks(result.content)
|
|
1844
|
+
const blocks = onlyTextBlocks(viewToolResult(candidate.event.data.message).blocks as ContentBlock[])
|
|
1844
1845
|
if (blocks === null) return null
|
|
1845
1846
|
const text = blocks.map(block => block.text).join('\n')
|
|
1846
1847
|
const sourceSeq = rootToolResultSeq(session, candidate.seq)
|
|
@@ -1897,8 +1898,7 @@ export class ToolResultPruner extends Service {
|
|
|
1897
1898
|
const protectedSeqs = this.protectedHistoryCandidateSeqs(candidates, policy)
|
|
1898
1899
|
const isUnsafe = (candidate: SnapshotCandidate): boolean => {
|
|
1899
1900
|
if (this.isRecoveryExempt(session, candidate)) return true
|
|
1900
|
-
const
|
|
1901
|
-
const block = onlyTextBlock(result.content)
|
|
1901
|
+
const block = onlyTextBlock(viewToolResult(candidate.event.data.message).blocks as ContentBlock[])
|
|
1902
1902
|
// Fold-once: an already-folded intent block is never re-candidated.
|
|
1903
1903
|
if (block?.text.includes(INTENT_FOLD_MARKER) === true) return true
|
|
1904
1904
|
return block?.text.includes('[Old tool result content cleared from active context]') === true
|
|
@@ -1961,8 +1961,8 @@ export class ToolResultPruner extends Service {
|
|
|
1961
1961
|
? minReclaimChars
|
|
1962
1962
|
: required
|
|
1963
1963
|
for (const candidate of eligible) {
|
|
1964
|
-
const
|
|
1965
|
-
const block = onlyTextBlock(
|
|
1964
|
+
const resultView = viewToolResult(candidate.event.data.message)
|
|
1965
|
+
const block = onlyTextBlock(resultView.blocks as ContentBlock[])
|
|
1966
1966
|
// TokenPilot-inspired R2: a read output whose file was later mutated is
|
|
1967
1967
|
// superseded — its text can no longer match the file — so it takes the
|
|
1968
1968
|
// small whole-result placeholder before the ordinary reducer runs.
|
|
@@ -2010,7 +2010,7 @@ export class ToolResultPruner extends Service {
|
|
|
2010
2010
|
toolName: candidate.call.name,
|
|
2011
2011
|
sourceRef: sourceRefFn(session, sourceSeq),
|
|
2012
2012
|
charsBefore: codePointLength(block.text),
|
|
2013
|
-
isError:
|
|
2013
|
+
isError: resultView.isError || candidate.event.data.error !== undefined,
|
|
2014
2014
|
text: block.text,
|
|
2015
2015
|
compact: false,
|
|
2016
2016
|
})
|
|
@@ -2020,7 +2020,7 @@ export class ToolResultPruner extends Service {
|
|
|
2020
2020
|
text: block.text,
|
|
2021
2021
|
budgetChars: 1_200,
|
|
2022
2022
|
sourceRef: sourceRefFn(session, sourceSeq),
|
|
2023
|
-
isError:
|
|
2023
|
+
isError: resultView.isError || candidate.event.data.error !== undefined,
|
|
2024
2024
|
}
|
|
2025
2025
|
if (!verifyReduction(verifyInput, output)) continue
|
|
2026
2026
|
// TokenPilot-inspired R3: when read-state semantics are on, append an
|
|
@@ -2166,22 +2166,25 @@ export class ToolResultPruner extends Service {
|
|
|
2166
2166
|
const resultSeqs = nodes.slice(index + 1, index + 1 + calls.length)
|
|
2167
2167
|
if (resultSeqs.length !== calls.length || resultSeqs.some(seq => protectedResults.has(seq))) continue
|
|
2168
2168
|
const results = resultSeqs.map(seq => events[seq])
|
|
2169
|
-
|
|
2170
|
-
|
|
2169
|
+
// One generation-gated triple view per result message, shared by the
|
|
2170
|
+
// safety gate below and the call-id pairing.
|
|
2171
|
+
const resultViews = results.map(event =>
|
|
2172
|
+
event?.type === 'tool/result' ? viewToolResult(event.data.message) : undefined)
|
|
2173
|
+
if (results.some((event, resultIndex): boolean => {
|
|
2174
|
+
const view = resultViews[resultIndex]
|
|
2175
|
+
if (event?.type !== 'tool/result' || view === undefined
|
|
2171
2176
|
|| event.data.turn !== assistant.data.turn || event.data.step !== assistant.data.step
|
|
2172
2177
|
|| event.data.error !== undefined) return true
|
|
2173
|
-
|
|
2174
|
-
if (block.isError === true) return true
|
|
2178
|
+
if (view.isError) return true
|
|
2175
2179
|
// Images and other rich inner blocks stay fail-open: a TailTrim stub
|
|
2176
2180
|
// would silently delete them from the active context.
|
|
2177
|
-
return
|
|
2181
|
+
return (view.blocks as ContentBlock[]).some((contentBlock: ContentBlock) => contentBlock.type !== 'text')
|
|
2178
2182
|
})) continue
|
|
2179
2183
|
const next = events[nodes[index + 1 + calls.length] ?? -1]
|
|
2180
2184
|
if (next?.type === 'tool/result'
|
|
2181
2185
|
&& next.data.turn === assistant.data.turn
|
|
2182
2186
|
&& next.data.step === assistant.data.step) continue
|
|
2183
|
-
const resultIds =
|
|
2184
|
-
? String(event.data.message.source.callId) : '')
|
|
2187
|
+
const resultIds = resultViews.map(view => view === undefined ? '' : String(view.callId))
|
|
2185
2188
|
if (new Set(resultIds).size !== resultIds.length
|
|
2186
2189
|
|| resultIds.some((id, resultIndex) => id !== callIds[resultIndex])) continue
|
|
2187
2190
|
const shadowedSeqs = [assistantSeq, ...resultSeqs]
|
|
@@ -2294,7 +2297,7 @@ export class ToolResultPruner extends Service {
|
|
|
2294
2297
|
const groupText = shadowedSeqs
|
|
2295
2298
|
.map(seq => sessionEvents(session)[seq])
|
|
2296
2299
|
.filter((event): event is SessionEvent<'tool/result'> => event?.type === 'tool/result')
|
|
2297
|
-
.map(event => flattenPlainText(event.data.message.
|
|
2300
|
+
.map(event => flattenPlainText(viewToolResult(event.data.message).blocks as ContentBlock[]))
|
|
2298
2301
|
.filter((text): text is string => text !== undefined)
|
|
2299
2302
|
.join('\n')
|
|
2300
2303
|
if (groupText.length > 0) {
|
|
@@ -2380,7 +2383,7 @@ export class ToolResultPruner extends Service {
|
|
|
2380
2383
|
// noise without reclaiming context. Text-level because the placeholder
|
|
2381
2384
|
// guidance lines (source refs, retrieval hints) must pay for themselves.
|
|
2382
2385
|
if (options.noNetSavingsGuard === true) {
|
|
2383
|
-
const originalBlocks = onlyTextBlocks(candidate.event.data.message.
|
|
2386
|
+
const originalBlocks = onlyTextBlocks(viewToolResult(candidate.event.data.message).blocks as ContentBlock[])
|
|
2384
2387
|
const replacementBlocks = onlyTextBlocks(content)
|
|
2385
2388
|
if (originalBlocks !== null && replacementBlocks !== null) {
|
|
2386
2389
|
const originalChars = originalBlocks.reduce((sum, block) => sum + codePointLength(block.text), 0)
|
|
@@ -2410,7 +2413,9 @@ export class ToolResultPruner extends Service {
|
|
|
2410
2413
|
private land(session: Session, plan: PlannedReplacement): PrunedEntry | null {
|
|
2411
2414
|
const { candidate } = plan
|
|
2412
2415
|
// 0.1.7-rc.2: the tool message's own `content` is the result block set —
|
|
2413
|
-
// no wrapping result block anymore.
|
|
2416
|
+
// no wrapping result block anymore. The triple view still resolves the
|
|
2417
|
+
// call id axis on a legacy envelope.
|
|
2418
|
+
const resultView = viewToolResult(candidate.event.data.message)
|
|
2414
2419
|
const message = freezeMessage<ToolResultMessage>({
|
|
2415
2420
|
...candidate.event.data.message,
|
|
2416
2421
|
content: plan.content,
|
|
@@ -2478,7 +2483,7 @@ export class ToolResultPruner extends Service {
|
|
|
2478
2483
|
originalSeq: candidate.seq,
|
|
2479
2484
|
sourceSeq: plan.sourceSeq,
|
|
2480
2485
|
replacementSeq: replacement.seq,
|
|
2481
|
-
callId:
|
|
2486
|
+
callId: resultView.callId as ToolCallId,
|
|
2482
2487
|
reducer: plan.reducer,
|
|
2483
2488
|
stage: plan.stage,
|
|
2484
2489
|
charsBefore: plan.charsBefore,
|