dsh-context-compression-improved 0.5.6 → 0.5.8
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.ja.md +169 -169
- package/CHANGELOG.ko.md +167 -167
- package/CHANGELOG.md +323 -304
- package/CHANGELOG.zh.md +152 -152
- package/CONTRIBUTING.md +22 -22
- package/LICENSE +21 -21
- package/README.ja.md +85 -85
- package/README.ko.md +85 -85
- package/README.md +94 -94
- package/README.zh.md +85 -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.ja.md +78 -78
- package/docs/installation.ko.md +78 -78
- package/docs/installation.md +103 -103
- package/docs/installation.zh.md +100 -100
- package/docs/repair-log.md +657 -657
- package/eslint.config.js +30 -30
- package/package.json +163 -164
- 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.d.ts +59 -0
- package/packages/selector/lib/client.js +1738 -10
- package/packages/selector/lib/{advisor-state.js → config.js} +816 -65
- package/packages/selector/lib/index.d.ts +13 -1
- package/packages/selector/lib/index.js +295 -3
- package/packages/selector/lib/pruner.d.ts +65 -0
- package/packages/selector/lib/pruner.js +497 -262
- package/packages/selector/lib/style.css +88 -46
- package/packages/selector/package.json +114 -114
- package/packages/selector/screenshots.json +3 -3
- 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/CustomPolicyEditor.tsx +216 -216
- package/packages/selector/src/client/EstimatorControls.tsx +277 -277
- package/packages/selector/src/client/decode.ts +57 -49
- package/packages/selector/src/client/index.ts +240 -149
- 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/preset-options.ts +88 -88
- package/packages/selector/src/client/savings-card.tsx +150 -0
- package/packages/selector/src/client/settings-section.tsx +140 -124
- 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 +673 -463
- package/packages/selector/src/invariant.ts +113 -113
- package/packages/selector/src/preset-overlay.ts +626 -626
- package/packages/selector/src/profiles.ts +431 -394
- package/packages/selector/src/pruner/content.ts +201 -201
- package/packages/selector/src/pruner/session.ts +94 -94
- package/packages/selector/src/pruner/state.ts +57 -50
- package/packages/selector/src/pruner/tuning.ts +23 -23
- package/packages/selector/src/pruner/types.ts +78 -78
- package/packages/selector/src/pruner.ts +2736 -2402
- package/packages/selector/src/runtime/adaptive-cost.ts +205 -205
- package/packages/selector/src/runtime/audit.ts +303 -286
- package/packages/selector/src/runtime/config.ts +756 -706
- 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 +436 -436
- 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/session-events.ts +30 -30
- package/packages/selector/src/runtime/summary-command.ts +127 -0
- package/packages/selector/src/runtime/tail-trim.ts +166 -166
- package/packages/selector/src/runtime/token-count.ts +72 -72
- package/packages/selector/src/runtime/tokenpilot/advisor-prompt.ts +233 -188
- package/packages/selector/src/runtime/tokenpilot/advisor-state.ts +204 -149
- 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/intent-fold.ts +53 -0
- package/packages/selector/src/runtime/tokenpilot/intent-gate.ts +65 -0
- package/packages/selector/src/runtime/tokenpilot/intent-input-mask.ts +125 -0
- package/packages/selector/src/runtime/tokenpilot/intent-range.ts +37 -0
- 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/types.ts +365 -346
- package/packages/selector/src/runtime/value.ts +48 -48
- package/packages/selector/tests/advisor-report.host.spec.ts +223 -223
- package/packages/selector/tests/auto-compact.client.spec.tsx +214 -213
- package/packages/selector/tests/built/client-artifact.spec.ts +59 -59
- package/packages/selector/tests/cache-prefix-audit.spec.ts +123 -123
- package/packages/selector/tests/code-skeleton.client.spec.ts +91 -90
- package/packages/selector/tests/custom-contract.client.spec.ts +205 -204
- package/packages/selector/tests/estimator-catalog.spec.ts +70 -70
- package/packages/selector/tests/estimator-channel.client.spec.tsx +253 -252
- package/packages/selector/tests/estimator-route-registration.host.spec.ts +194 -188
- package/packages/selector/tests/host-preset-overlay.host.spec.ts +206 -206
- package/packages/selector/tests/monitor-fab.client.spec.ts +138 -0
- package/packages/selector/tests/preset-options-write.client.spec.ts +207 -206
- package/packages/selector/tests/preset-overlay-loader.e2e.host.spec.ts +201 -201
- package/packages/selector/tests/preset-overlay.host.spec.ts +246 -246
- package/packages/selector/tests/profiles.client.spec.tsx +436 -434
- package/packages/selector/tests/public/package-contract.client.spec.ts +78 -78
- package/packages/selector/tests/runtime/adaptive-cost.spec.ts +167 -167
- package/packages/selector/tests/runtime/advice-never-withholds.host.spec.ts +232 -232
- 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/audit.spec.ts +275 -274
- 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/intent-gate.spec.ts +62 -0
- package/packages/selector/tests/runtime/intent-summary.spec.ts +156 -0
- package/packages/selector/tests/runtime/line-mapping.spec.ts +153 -153
- package/packages/selector/tests/runtime/monitor.spec.ts +301 -0
- package/packages/selector/tests/runtime/prose-reducers.spec.ts +133 -133
- package/packages/selector/tests/runtime/public/public-runtime.spec.ts +2724 -2724
- package/packages/selector/tests/runtime/read-input-cap.spec.ts +33 -33
- package/packages/selector/tests/runtime/savings.spec.ts +120 -0
- 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/summary-command.spec.ts +154 -0
- 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 +130 -118
- package/packages/selector/tests/standing-generation.host.spec.ts +697 -697
- package/packages/selector/tests/subagent-cache-reuse.host.spec.ts +253 -253
- 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/pnpm-workspace.yaml +66 -66
- 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/packed-components-smoke.mjs +732 -732
- package/scripts/packed-install-e2e.mjs +1273 -1273
- package/scripts/toolclass-corpus-replay.mjs +281 -281
- package/scripts/verify-release.mjs +318 -317
- package/tests/TEST_INVENTORY.md +41 -41
- 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 +43 -43
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
import z from "@deepseek-ai/schemastery";
|
|
2
2
|
import { Context } from "@deepseek-ai/cordis";
|
|
3
3
|
//#region src/index.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Floating-panel monitor route (`GET|POST .../monitor?sessionId=…`):
|
|
6
|
+
* - GET → the monitor snapshot (savings aggregate + intent control block +
|
|
7
|
+
* pricing) — the payload shape the panel polls;
|
|
8
|
+
* - POST → session override action (`{"action":"on"|"off"|"clear"}`),
|
|
9
|
+
* driving the exact state machine `/ctx-summary` drives.
|
|
10
|
+
* Registration mirrors the savings route (webServer gate alone, dual channels,
|
|
11
|
+
* single guarded registration); the handler branches on `req.method` because
|
|
12
|
+
* the registration surface is method-agnostic.
|
|
13
|
+
*/
|
|
14
|
+
/** Exported for the runtime monitor spec; the entry owns all webServer wiring. */
|
|
15
|
+
declare function registerMonitorRoute(ctx: Context): void;
|
|
4
16
|
/** Standalone Bundle behavior; the settings/UI owner remains safe when false. */
|
|
5
17
|
interface Config {
|
|
6
18
|
/** Add the canonical compression stack to every non-Minimal preset. */
|
|
@@ -31,4 +43,4 @@ declare const Config: z<Config>;
|
|
|
31
43
|
/** Register the persisted default read by the currently mounted root pruner. */
|
|
32
44
|
declare function apply(ctx: Context, config?: Config): void;
|
|
33
45
|
//#endregion
|
|
34
|
-
export { Config, apply };
|
|
46
|
+
export { Config, apply, registerMonitorRoute };
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { A as getObservedIntentEnabled, C as isSessionOverrideAction, E as INTENT_GATE_GROWTH_TOKENS, H as getSavingsLedger, L as setSummaryOverride, O as getAdvisorState, S as estimateSavingsPricing, T as INTENT_GATE_FLOOR_FRACTION, b as applySessionOverride, j as getSummaryOverride, k as getLastIntentFold, n as CONTEXT_COMPRESSION_SETTINGS_NAMESPACE, r as ContextCompressionSettingsSchema, x as buildMonitorSnapshot } from "./config.js";
|
|
2
2
|
import z from "@deepseek-ai/schemastery";
|
|
3
3
|
import "@deepseek-ai/dsh-settings";
|
|
4
|
-
import { AsyncLocalStorage } from "node:async_hooks";
|
|
5
4
|
import { createHash } from "node:crypto";
|
|
5
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
6
6
|
import { chmod, mkdtemp, readFile, rename, rm, stat, utimes, writeFile } from "node:fs/promises";
|
|
7
7
|
import { tmpdir } from "node:os";
|
|
8
8
|
import { isAbsolute, join } from "node:path";
|
|
@@ -76,6 +76,96 @@ async function buildEstimatorCatalog(deps) {
|
|
|
76
76
|
};
|
|
77
77
|
}
|
|
78
78
|
//#endregion
|
|
79
|
+
//#region src/runtime/summary-command.ts
|
|
80
|
+
const SUMMARY_COMMAND_NAME = "ctx-summary";
|
|
81
|
+
const USAGE = "Usage: /ctx-summary off|on|status";
|
|
82
|
+
/** Parse the raw argument string; `undefined` = unrecognized (usage error). */
|
|
83
|
+
function parseSummaryCommandArgs(rawInput) {
|
|
84
|
+
const token = rawInput.trim().toLowerCase();
|
|
85
|
+
if (token === "off" || token === "on" || token === "status") return token;
|
|
86
|
+
}
|
|
87
|
+
function describeFold(sessionId) {
|
|
88
|
+
const fold = getLastIntentFold(sessionId);
|
|
89
|
+
if (fold === void 0) return "none";
|
|
90
|
+
return `turn ${fold.turn}, seq ${fold.startSeq}..${fold.endSeq}`;
|
|
91
|
+
}
|
|
92
|
+
/** Human-facing status line; observability only, never a decision input. */
|
|
93
|
+
function formatSummaryStatus(sessionId) {
|
|
94
|
+
const override = getSummaryOverride(sessionId);
|
|
95
|
+
const enabled = getObservedIntentEnabled(sessionId);
|
|
96
|
+
const enabledText = enabled === void 0 ? "unknown (gate not evaluated yet)" : enabled ? "yes" : "no";
|
|
97
|
+
return [
|
|
98
|
+
`ctx-summary status:`,
|
|
99
|
+
` override: ${override ?? "default (settings-driven)"}`,
|
|
100
|
+
` intentSummary enabled (settings): ${enabledText}`,
|
|
101
|
+
` gate: floor >${Math.round(INTENT_GATE_FLOOR_FRACTION * 100)}% of window AND growth >${INTENT_GATE_GROWTH_TOKENS} tokens since last fold`,
|
|
102
|
+
` last fold: ${describeFold(sessionId)}`
|
|
103
|
+
].join("\n");
|
|
104
|
+
}
|
|
105
|
+
/** Resolve the invoking agent's session id (`''` when the host exposes none). */
|
|
106
|
+
function sessionIdOf(agent) {
|
|
107
|
+
const session = agent?.session;
|
|
108
|
+
if (session === void 0 || session.id === void 0 || session.id === null) return "";
|
|
109
|
+
return String(session.id);
|
|
110
|
+
}
|
|
111
|
+
/** Best-effort `CommandDefinitionId` brand; `undefined` on stripped hosts (registered without it). */
|
|
112
|
+
async function loadCommandDefinitionId() {
|
|
113
|
+
try {
|
|
114
|
+
return (await import("@deepseek-ai/dsh-commands")).CommandDefinitionId;
|
|
115
|
+
} catch {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Mount `/ctx-summary` for every composed human-command adapter. Never
|
|
121
|
+
* throws: the command is a convenience surface, and a host without the
|
|
122
|
+
* commands service (or with an incompatible registry shape) simply runs
|
|
123
|
+
* without it.
|
|
124
|
+
*/
|
|
125
|
+
function registerSummaryCommand(ctx) {
|
|
126
|
+
ctx.inject(["commands"], (injected) => {
|
|
127
|
+
const commands = injected.commands;
|
|
128
|
+
if (commands === void 0 || typeof commands.register !== "function") return;
|
|
129
|
+
(async () => {
|
|
130
|
+
try {
|
|
131
|
+
const definitionId = await loadCommandDefinitionId();
|
|
132
|
+
await commands.register({
|
|
133
|
+
name: SUMMARY_COMMAND_NAME,
|
|
134
|
+
description: "Turn-tail intent summary: temporarily disable/resume or inspect status",
|
|
135
|
+
...definitionId === void 0 ? {} : { definitionId: definitionId("dsh-context-compression-improved/ctx-summary") },
|
|
136
|
+
handler: async (invocation) => {
|
|
137
|
+
const args = parseSummaryCommandArgs(typeof invocation.rawInput === "string" ? invocation.rawInput : "");
|
|
138
|
+
if (args === void 0) return {
|
|
139
|
+
kind: "error",
|
|
140
|
+
text: USAGE
|
|
141
|
+
};
|
|
142
|
+
const sessionId = sessionIdOf(invocation.agent);
|
|
143
|
+
if (args === "status") {
|
|
144
|
+
if (sessionId === "") return {
|
|
145
|
+
kind: "error",
|
|
146
|
+
text: "ctx-summary: no session is attached to this invocation"
|
|
147
|
+
};
|
|
148
|
+
return {
|
|
149
|
+
kind: "success",
|
|
150
|
+
text: formatSummaryStatus(sessionId)
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
if (sessionId === "") return {
|
|
154
|
+
kind: "error",
|
|
155
|
+
text: "ctx-summary: no session is attached to this invocation"
|
|
156
|
+
};
|
|
157
|
+
setSummaryOverride(sessionId, args);
|
|
158
|
+
return {
|
|
159
|
+
kind: "success",
|
|
160
|
+
text: args === "off" ? "ctx-summary: turn-tail intent summary paused for this session (until process restart or `/ctx-summary on`)" : "ctx-summary: turn-tail intent summary resumed for this session (numeric gates still apply)"
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
} catch {}
|
|
165
|
+
})();
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
//#endregion
|
|
79
169
|
//#region src/preset-overlay.ts
|
|
80
170
|
/** Plugin-owned, reversible compression overlays for native agent presets. */
|
|
81
171
|
/**
|
|
@@ -506,6 +596,8 @@ function restoreMethod(presets, snapshot) {
|
|
|
506
596
|
const CONTEXT_COMPRESSION_NAMESPACE = CONTEXT_COMPRESSION_SETTINGS_NAMESPACE;
|
|
507
597
|
const ESTIMATOR_CATALOG_ROUTES = ["/endpoint/dsh-context-compression-improved/estimator-catalog", "/api/dsh-context-compression-improved/estimator-catalog"];
|
|
508
598
|
const ADVISOR_REPORT_ROUTES = ["/endpoint/dsh-context-compression-improved/advisor-report", "/api/dsh-context-compression-improved/advisor-report"];
|
|
599
|
+
const SAVINGS_ROUTES = ["/endpoint/dsh-context-compression-improved/savings", "/api/dsh-context-compression-improved/savings"];
|
|
600
|
+
const MONITOR_ROUTES = ["/endpoint/dsh-context-compression-improved/monitor", "/api/dsh-context-compression-improved/monitor"];
|
|
509
601
|
function sessionFor(readService, sessionId) {
|
|
510
602
|
const agents = readService("agents");
|
|
511
603
|
return typeof agents?.get === "function" ? agents.get(sessionId)?.session : void 0;
|
|
@@ -628,6 +720,7 @@ function registerAdvisorReportRoute(ctx) {
|
|
|
628
720
|
* never reasons to withhold the route.
|
|
629
721
|
*/
|
|
630
722
|
const ESTIMATOR_CATALOG_ROUTE_DEPS = ["webServer"];
|
|
723
|
+
/** The estimator-side service the catalog handler enriches its response with. */
|
|
631
724
|
function asWebServer(value) {
|
|
632
725
|
if (typeof value !== "object" || value === null) return void 0;
|
|
633
726
|
return typeof value.register === "function" ? value : void 0;
|
|
@@ -720,6 +813,202 @@ function registerEstimatorCatalogRoute(ctx) {
|
|
|
720
813
|
});
|
|
721
814
|
log("warn", "context-compression webServer not active yet — estimator catalog route pending: %s", ESTIMATOR_CATALOG_ROUTES.join(", "));
|
|
722
815
|
}
|
|
816
|
+
/**
|
|
817
|
+
* Read-only savings snapshot route (`GET .../savings?sessionId=…`): gross saved,
|
|
818
|
+
* offsets (negative savings — compressed content later re-read in full), and net,
|
|
819
|
+
* with exact-tokenizer and chars/4 bases reported separately and never merged.
|
|
820
|
+
* Registration mirrors the estimator-catalog route (webServer gate alone, dual
|
|
821
|
+
* channels for both service arrival orders, single guarded registration).
|
|
822
|
+
*/
|
|
823
|
+
function registerSavingsRoute(ctx) {
|
|
824
|
+
const readService = (name) => {
|
|
825
|
+
try {
|
|
826
|
+
return ctx.get(name);
|
|
827
|
+
} catch {
|
|
828
|
+
return;
|
|
829
|
+
}
|
|
830
|
+
};
|
|
831
|
+
const log = (level, message, ...args) => {
|
|
832
|
+
console[level](message, ...args);
|
|
833
|
+
};
|
|
834
|
+
let registered = false;
|
|
835
|
+
const register = (webServer, channel) => {
|
|
836
|
+
if (registered) return;
|
|
837
|
+
const handler = (req, res) => {
|
|
838
|
+
const resTyped = res;
|
|
839
|
+
if (typeof resTyped?.writeHead !== "function" || typeof resTyped?.end !== "function") return;
|
|
840
|
+
let sessionId;
|
|
841
|
+
try {
|
|
842
|
+
const raw = new URL(String(req?.url ?? "/"), "http://localhost").searchParams.get("sessionId");
|
|
843
|
+
if (typeof raw === "string" && raw.length > 0 && raw.length <= 512) sessionId = raw;
|
|
844
|
+
} catch {}
|
|
845
|
+
const snapshot = getSavingsLedger().snapshot(sessionId);
|
|
846
|
+
const pricing = estimateSavingsPricing(readService, snapshot);
|
|
847
|
+
resTyped.writeHead(200, {
|
|
848
|
+
"content-type": "application/json; charset=utf-8",
|
|
849
|
+
"cache-control": "no-cache"
|
|
850
|
+
});
|
|
851
|
+
resTyped.end(JSON.stringify({
|
|
852
|
+
ok: true,
|
|
853
|
+
...snapshot,
|
|
854
|
+
...pricing === void 0 ? {} : { pricing }
|
|
855
|
+
}));
|
|
856
|
+
};
|
|
857
|
+
try {
|
|
858
|
+
const disposers = SAVINGS_ROUTES.map((path) => webServer.register({
|
|
859
|
+
kind: "exact",
|
|
860
|
+
path,
|
|
861
|
+
handler
|
|
862
|
+
})).filter((off) => typeof off === "function");
|
|
863
|
+
registered = true;
|
|
864
|
+
ctx.effect(() => () => {
|
|
865
|
+
for (const off of disposers) off();
|
|
866
|
+
}, "contextCompressionSelector.savings route");
|
|
867
|
+
log("info", "context-compression savings route registered (%s): %s", channel, SAVINGS_ROUTES.join(", "));
|
|
868
|
+
} catch (error) {
|
|
869
|
+
log("warn", "context-compression savings route registration failed (%s): %o", channel, error);
|
|
870
|
+
}
|
|
871
|
+
};
|
|
872
|
+
const active = asWebServer(readService("webServer"));
|
|
873
|
+
if (active !== void 0) {
|
|
874
|
+
register(active, "direct");
|
|
875
|
+
if (registered) return;
|
|
876
|
+
}
|
|
877
|
+
ctx.inject(["webServer"], (injected) => {
|
|
878
|
+
const webServer = asWebServer(injected.webServer);
|
|
879
|
+
if (webServer === void 0) {
|
|
880
|
+
log("warn", "context-compression webServer exposes no register() — savings route not registered");
|
|
881
|
+
return;
|
|
882
|
+
}
|
|
883
|
+
register(webServer, "inject");
|
|
884
|
+
});
|
|
885
|
+
}
|
|
886
|
+
/**
|
|
887
|
+
* Floating-panel monitor route (`GET|POST .../monitor?sessionId=…`):
|
|
888
|
+
* - GET → the monitor snapshot (savings aggregate + intent control block +
|
|
889
|
+
* pricing) — the payload shape the panel polls;
|
|
890
|
+
* - POST → session override action (`{"action":"on"|"off"|"clear"}`),
|
|
891
|
+
* driving the exact state machine `/ctx-summary` drives.
|
|
892
|
+
* Registration mirrors the savings route (webServer gate alone, dual channels,
|
|
893
|
+
* single guarded registration); the handler branches on `req.method` because
|
|
894
|
+
* the registration surface is method-agnostic.
|
|
895
|
+
*/
|
|
896
|
+
/** Exported for the runtime monitor spec; the entry owns all webServer wiring. */
|
|
897
|
+
function registerMonitorRoute(ctx) {
|
|
898
|
+
const readService = (name) => {
|
|
899
|
+
try {
|
|
900
|
+
return ctx.get(name);
|
|
901
|
+
} catch {
|
|
902
|
+
return;
|
|
903
|
+
}
|
|
904
|
+
};
|
|
905
|
+
const log = (level, message, ...args) => {
|
|
906
|
+
console[level](message, ...args);
|
|
907
|
+
};
|
|
908
|
+
let registered = false;
|
|
909
|
+
const register = (webServer, channel) => {
|
|
910
|
+
if (registered) return;
|
|
911
|
+
const handler = (req, res) => {
|
|
912
|
+
const resTyped = res;
|
|
913
|
+
if (typeof resTyped?.writeHead !== "function" || typeof resTyped?.end !== "function") return;
|
|
914
|
+
let sessionId;
|
|
915
|
+
try {
|
|
916
|
+
const raw = new URL(String(req?.url ?? "/"), "http://localhost").searchParams.get("sessionId");
|
|
917
|
+
if (typeof raw === "string" && raw.length > 0 && raw.length <= 512) sessionId = raw;
|
|
918
|
+
} catch {}
|
|
919
|
+
if (String(req?.method ?? "GET").toUpperCase() === "POST") {
|
|
920
|
+
const chunks = [];
|
|
921
|
+
let size = 0;
|
|
922
|
+
let done = false;
|
|
923
|
+
const respond = (status, body) => {
|
|
924
|
+
if (done) return;
|
|
925
|
+
done = true;
|
|
926
|
+
resTyped.writeHead(status, {
|
|
927
|
+
"content-type": "application/json; charset=utf-8",
|
|
928
|
+
"cache-control": "no-cache"
|
|
929
|
+
});
|
|
930
|
+
resTyped.end(JSON.stringify(body));
|
|
931
|
+
};
|
|
932
|
+
const reqTyped = req;
|
|
933
|
+
if (typeof reqTyped?.on !== "function") {
|
|
934
|
+
respond(400, {
|
|
935
|
+
ok: false,
|
|
936
|
+
error: "unreadable request body"
|
|
937
|
+
});
|
|
938
|
+
return;
|
|
939
|
+
}
|
|
940
|
+
reqTyped.on("data", (chunk) => {
|
|
941
|
+
size += chunk?.length ?? 0;
|
|
942
|
+
if (size <= 4096) chunks.push(Buffer.from(chunk ?? /* @__PURE__ */ new Uint8Array()));
|
|
943
|
+
});
|
|
944
|
+
reqTyped.on("end", () => {
|
|
945
|
+
let action;
|
|
946
|
+
try {
|
|
947
|
+
action = JSON.parse(Buffer.concat(chunks).toString("utf8") || "{}").action;
|
|
948
|
+
} catch {
|
|
949
|
+
respond(400, {
|
|
950
|
+
ok: false,
|
|
951
|
+
error: "monitor override action must be JSON"
|
|
952
|
+
});
|
|
953
|
+
return;
|
|
954
|
+
}
|
|
955
|
+
if (!isSessionOverrideAction(action)) {
|
|
956
|
+
respond(400, {
|
|
957
|
+
ok: false,
|
|
958
|
+
error: "monitor override action must be \"on\" | \"off\" | \"clear\""
|
|
959
|
+
});
|
|
960
|
+
return;
|
|
961
|
+
}
|
|
962
|
+
const { override } = applySessionOverride(sessionId ?? "", action);
|
|
963
|
+
respond(200, {
|
|
964
|
+
ok: true,
|
|
965
|
+
override: override ?? null,
|
|
966
|
+
sessionScope: sessionId ?? null
|
|
967
|
+
});
|
|
968
|
+
});
|
|
969
|
+
return;
|
|
970
|
+
}
|
|
971
|
+
const snapshot = buildMonitorSnapshot(sessionId);
|
|
972
|
+
const pricing = estimateSavingsPricing(readService, snapshot);
|
|
973
|
+
resTyped.writeHead(200, {
|
|
974
|
+
"content-type": "application/json; charset=utf-8",
|
|
975
|
+
"cache-control": "no-cache"
|
|
976
|
+
});
|
|
977
|
+
resTyped.end(JSON.stringify({
|
|
978
|
+
ok: true,
|
|
979
|
+
...snapshot,
|
|
980
|
+
...pricing === void 0 ? {} : { pricing }
|
|
981
|
+
}));
|
|
982
|
+
};
|
|
983
|
+
try {
|
|
984
|
+
const disposers = MONITOR_ROUTES.map((path) => webServer.register({
|
|
985
|
+
kind: "exact",
|
|
986
|
+
path,
|
|
987
|
+
handler
|
|
988
|
+
})).filter((off) => typeof off === "function");
|
|
989
|
+
registered = true;
|
|
990
|
+
ctx.effect(() => () => {
|
|
991
|
+
for (const off of disposers) off();
|
|
992
|
+
}, "contextCompressionSelector.monitor route");
|
|
993
|
+
log("info", "context-compression monitor route registered (%s): %s", channel, MONITOR_ROUTES.join(", "));
|
|
994
|
+
} catch (error) {
|
|
995
|
+
log("warn", "context-compression monitor route registration failed (%s): %o", channel, error);
|
|
996
|
+
}
|
|
997
|
+
};
|
|
998
|
+
const active = asWebServer(readService("webServer"));
|
|
999
|
+
if (active !== void 0) {
|
|
1000
|
+
register(active, "direct");
|
|
1001
|
+
if (registered) return;
|
|
1002
|
+
}
|
|
1003
|
+
ctx.inject(["webServer"], (injected) => {
|
|
1004
|
+
const webServer = asWebServer(injected.webServer);
|
|
1005
|
+
if (webServer === void 0) {
|
|
1006
|
+
log("warn", "context-compression webServer exposes no register() — monitor route not registered");
|
|
1007
|
+
return;
|
|
1008
|
+
}
|
|
1009
|
+
register(webServer, "inject");
|
|
1010
|
+
});
|
|
1011
|
+
}
|
|
723
1012
|
/** Symbol properties reach the shared service target through Cordis proxies. */
|
|
724
1013
|
const SHARED_SETTINGS = Symbol.for("dsh-context-compression-improved/settings-registration");
|
|
725
1014
|
/** Loader validation for the standalone Bundle opt-in. */
|
|
@@ -735,7 +1024,10 @@ function apply(ctx, config = {}) {
|
|
|
735
1024
|
acquireSettingsRegistration(settingsCtx);
|
|
736
1025
|
});
|
|
737
1026
|
if (config.estimatorCatalogRoute === true) registerEstimatorCatalogRoute(ctx);
|
|
1027
|
+
registerSavingsRoute(ctx);
|
|
1028
|
+
registerMonitorRoute(ctx);
|
|
738
1029
|
if (config.advisorReportRoute === true) registerAdvisorReportRoute(ctx);
|
|
1030
|
+
registerSummaryCommand(ctx);
|
|
739
1031
|
if (config.presetOverlay !== true) return;
|
|
740
1032
|
ctx.inject(["agentPresets"], (presetsCtx) => {
|
|
741
1033
|
const installation = decorateAgentPresets(presetsCtx.agentPresets, {
|
|
@@ -804,4 +1096,4 @@ function acquireSettingsRegistration(ctx) {
|
|
|
804
1096
|
if (state.owners.size === 1) state.scope = settings.register(CONTEXT_COMPRESSION_NAMESPACE, ContextCompressionSettingsSchema);
|
|
805
1097
|
}
|
|
806
1098
|
//#endregion
|
|
807
|
-
export { Config, apply };
|
|
1099
|
+
export { Config, apply, registerMonitorRoute };
|
|
@@ -134,6 +134,19 @@ interface AutoCompactSettings {
|
|
|
134
134
|
interface CodeSkeletonSettings {
|
|
135
135
|
enabled: boolean;
|
|
136
136
|
}
|
|
137
|
+
/**
|
|
138
|
+
* Orthogonal turn-tail intent-summary gate. Independent of every profile:
|
|
139
|
+
* when enabled, the turn-boundary growth gate may fold consumed-increment
|
|
140
|
+
* tool results into an LLM-written intent block (skeleton + verbatim error
|
|
141
|
+
* lines are kept; the model only ever writes the summary segment).
|
|
142
|
+
*/
|
|
143
|
+
interface IntentSummarySettings {
|
|
144
|
+
enabled: boolean;
|
|
145
|
+
}
|
|
146
|
+
/** Floating compression-monitor panel (FAB); visibility switch. */
|
|
147
|
+
interface MonitorPanelSettings {
|
|
148
|
+
enabled: boolean;
|
|
149
|
+
}
|
|
137
150
|
/**
|
|
138
151
|
* Persisted sub-capability overrides for the `tokenpilot-inspired` preset.
|
|
139
152
|
* Absent fields inherit the preset defaults; the section is only meaningful
|
|
@@ -173,6 +186,10 @@ interface ContextCompressionSettings {
|
|
|
173
186
|
autoCompact: AutoCompactSettings;
|
|
174
187
|
/** Code-skeleton reducer gate snapped independently of `profile`. */
|
|
175
188
|
codeSkeleton: CodeSkeletonSettings;
|
|
189
|
+
/** Turn-tail intent-summary gate snapped independently of `profile`. */
|
|
190
|
+
intentSummary: IntentSummarySettings;
|
|
191
|
+
/** Floating compression-monitor panel (FAB); absent inherits hidden. */
|
|
192
|
+
monitorPanel?: MonitorPanelSettings;
|
|
176
193
|
/** Optional tokenpilot-inspired sub-capability overrides; absent inherits preset defaults. */
|
|
177
194
|
presetOptions?: PresetOptionsSettings;
|
|
178
195
|
}
|
|
@@ -397,6 +414,32 @@ interface EstimatorFailures {
|
|
|
397
414
|
cooldownUntil: number;
|
|
398
415
|
}
|
|
399
416
|
//#endregion
|
|
417
|
+
//#region src/runtime/tokenpilot/intent-input-mask.d.ts
|
|
418
|
+
type IntentRole = 'read-mask' | 'write-keep' | 'conservative';
|
|
419
|
+
//#endregion
|
|
420
|
+
//#region src/runtime/tokenpilot/intent-fold.d.ts
|
|
421
|
+
/** One staged fold awaiting the next pressure round. */
|
|
422
|
+
interface PendingIntentFold {
|
|
423
|
+
readonly createdAt: number;
|
|
424
|
+
readonly turn: number;
|
|
425
|
+
/** Inclusive seq span of the folded candidates. */
|
|
426
|
+
readonly startSeq: number;
|
|
427
|
+
readonly endSeq: number;
|
|
428
|
+
/** LLM-written summary segment (fail-open parsed, char-capped). */
|
|
429
|
+
readonly summary: string;
|
|
430
|
+
/** Verbatim error lines collected across the batch (never LLM-revoiced). */
|
|
431
|
+
readonly errorLines: readonly string[];
|
|
432
|
+
/** Per-candidate faces: read-mask single lines ride the block, keep-class lines prompt-only. */
|
|
433
|
+
readonly records: ReadonlyArray<{
|
|
434
|
+
readonly seq: number;
|
|
435
|
+
readonly role: IntentRole;
|
|
436
|
+
readonly toolName: string;
|
|
437
|
+
readonly line: string;
|
|
438
|
+
}>;
|
|
439
|
+
/** Characters consumed by the summary-writer call (credit accounting). */
|
|
440
|
+
readonly summaryCallChars: number;
|
|
441
|
+
}
|
|
442
|
+
//#endregion
|
|
400
443
|
//#region src/pruner/state.d.ts
|
|
401
444
|
/** Mutable per-session state bag used inside {@link ToolResultPruner}. */
|
|
402
445
|
interface PrunerState {
|
|
@@ -430,6 +473,12 @@ interface PrunerState {
|
|
|
430
473
|
readonly estimatorRemainingTurns: WeakMap<Session, number>;
|
|
431
474
|
/** Per-session advisor side channel, constructed once with the advisor overrides. */
|
|
432
475
|
readonly advisorChannels: WeakMap<Session, SideChannel>;
|
|
476
|
+
/** Staged intent fold awaiting the next pressure round (at most one per session). */
|
|
477
|
+
readonly pendingIntentFolds: WeakMap<Session, PendingIntentFold>;
|
|
478
|
+
/** Landed intent-fold seqs: fold-once, never reconsidered by any later stage. */
|
|
479
|
+
readonly intentFoldedSeqs: WeakMap<Session, Set<number>>;
|
|
480
|
+
/** Live-surface tokens when the last intent fold landed (growth-gate baseline). */
|
|
481
|
+
readonly intentBaselines: WeakMap<Session, number>;
|
|
433
482
|
}
|
|
434
483
|
//#endregion
|
|
435
484
|
//#region src/runtime/custom-policy.d.ts
|
|
@@ -752,6 +801,22 @@ declare class ToolResultPruner extends Service {
|
|
|
752
801
|
* reads, so the default configuration adds exactly zero behavior.
|
|
753
802
|
*/
|
|
754
803
|
private postflightAdvisorPass;
|
|
804
|
+
/**
|
|
805
|
+
* Fire-and-forget postflight: evaluate the growth gate, and only when it
|
|
806
|
+
* passes, select the consumed increment OUTSIDE the protected working set,
|
|
807
|
+
* mask it to semantic-role records, and spend ONE summary-writer call. The
|
|
808
|
+
* verdict stages as a pending fold that the NEXT pressure round lands via
|
|
809
|
+
* the ordinary plan/apply machinery — the synchronous decision path never
|
|
810
|
+
* waits on an LLM.
|
|
811
|
+
*/
|
|
812
|
+
private postflightIntentFoldPass;
|
|
813
|
+
/**
|
|
814
|
+
* Convert a staged pending fold into per-candidate planned replacements
|
|
815
|
+
* (reducer `intent-summary`). Fail-open: a candidate that would grow, is
|
|
816
|
+
* rich-content, or no longer exists keeps its original text. Returns
|
|
817
|
+
* `undefined` when there is nothing (or no longer anything) to fold.
|
|
818
|
+
*/
|
|
819
|
+
private applyPendingIntentFold;
|
|
755
820
|
/**
|
|
756
821
|
* Monotonic per-session turn clock for advisory records. Bumped by the agent
|
|
757
822
|
* loop payloads (`pre-step`); passes without a turn coordinate reuse the last
|