dsh-context-compression-improved 0.8.0-beta.4 → 0.8.0-beta.6
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/CHANGELOG.md +29 -0
- package/package.json +1 -1
- package/packages/selector/dsh.plugin.json +1 -1
- package/packages/selector/lib/client.d.ts +23 -0
- package/packages/selector/lib/client.js +567 -3
- package/packages/selector/lib/{intent-gate.js → config.js} +285 -136
- package/packages/selector/lib/index.d.ts +13 -1
- package/packages/selector/lib/index.js +131 -50
- package/packages/selector/lib/profiles.d.ts +6 -0
- package/packages/selector/lib/pruner.d.ts +6 -0
- package/packages/selector/lib/pruner.js +5 -3
- package/packages/selector/src/client/CompressionProfileControls.tsx +33 -0
- package/packages/selector/src/client/CompressionProfileSelector.tsx +1 -0
- package/packages/selector/src/client/decode.ts +5 -1
- package/packages/selector/src/client/index.ts +60 -0
- package/packages/selector/src/client/locales/de.ts +22 -0
- package/packages/selector/src/client/locales/es.ts +22 -0
- package/packages/selector/src/client/locales/fr.ts +22 -0
- package/packages/selector/src/client/locales/it.ts +22 -0
- package/packages/selector/src/client/locales/ja.ts +22 -0
- package/packages/selector/src/client/locales/ko.ts +22 -0
- package/packages/selector/src/client/locales/ru.ts +22 -0
- package/packages/selector/src/client/locales.ts +44 -0
- package/packages/selector/src/client/monitor-fab.ts +320 -0
- package/packages/selector/src/client/settings-section.tsx +9 -2
- package/packages/selector/src/index.ts +126 -60
- package/packages/selector/src/profiles.ts +17 -0
- package/packages/selector/src/pruner.ts +2 -0
- package/packages/selector/src/runtime/config.ts +26 -1
- package/packages/selector/src/runtime/monitor.ts +211 -0
- package/packages/selector/src/runtime/types.ts +7 -0
- package/packages/selector/tests/code-skeleton.client.spec.ts +2 -2
- package/packages/selector/tests/custom-contract.client.spec.ts +2 -2
- package/packages/selector/tests/estimator-route-registration.host.spec.ts +8 -6
- package/packages/selector/tests/monitor-fab.client.spec.ts +117 -0
- package/packages/selector/tests/runtime/monitor.spec.ts +274 -0
- package/packages/selector/tests/settings-seat.client.spec.ts +19 -7
- package/scripts/packed-install-e2e.mjs +1 -1
- package/scripts/verify-release.mjs +1 -1
|
@@ -500,6 +500,271 @@ function unpriced(reason) {
|
|
|
500
500
|
reason
|
|
501
501
|
};
|
|
502
502
|
}
|
|
503
|
+
const advisorStates = /* @__PURE__ */ new WeakMap();
|
|
504
|
+
/**
|
|
505
|
+
* The per-session advisor state, created on first touch.
|
|
506
|
+
* @param session - the session to key the state on (by object identity).
|
|
507
|
+
*/
|
|
508
|
+
function getAdvisorState(session) {
|
|
509
|
+
let state = advisorStates.get(session);
|
|
510
|
+
if (state === void 0) {
|
|
511
|
+
state = {
|
|
512
|
+
todoVersion: void 0,
|
|
513
|
+
summary: void 0,
|
|
514
|
+
lastSummaryTurn: -1,
|
|
515
|
+
watermarkSeq: 0,
|
|
516
|
+
scores: /* @__PURE__ */ new Map(),
|
|
517
|
+
recertified: /* @__PURE__ */ new Map(),
|
|
518
|
+
failures: void 0,
|
|
519
|
+
inFlight: false,
|
|
520
|
+
lastDecay: void 0,
|
|
521
|
+
lastAdvice: void 0
|
|
522
|
+
};
|
|
523
|
+
advisorStates.set(session, state);
|
|
524
|
+
}
|
|
525
|
+
return state;
|
|
526
|
+
}
|
|
527
|
+
/**
|
|
528
|
+
* Insert or refresh one score with LRU semantics: a re-touched seq moves to
|
|
529
|
+
* the newest position, and the oldest entry is evicted once the map exceeds
|
|
530
|
+
* {@link ADVISOR_SCORES_LIMIT}.
|
|
531
|
+
*/
|
|
532
|
+
function recordScore(state, seq, entry) {
|
|
533
|
+
state.scores.delete(seq);
|
|
534
|
+
state.scores.set(seq, entry);
|
|
535
|
+
if (state.scores.size > 64) {
|
|
536
|
+
const oldest = state.scores.keys().next();
|
|
537
|
+
if (oldest.done !== true) state.scores.delete(oldest.value);
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
/**
|
|
541
|
+
* Mark one seq as LLM-recertified low relevance (a suggestion for later
|
|
542
|
+
* history-aggressiveness decisions, consumed by nothing in this round).
|
|
543
|
+
* Bounded at {@link ADVISOR_RECERTIFIED_LIMIT} with the same LRU eviction.
|
|
544
|
+
*/
|
|
545
|
+
function recordRecertified(state, seq, turn) {
|
|
546
|
+
state.recertified.delete(seq);
|
|
547
|
+
state.recertified.set(seq, turn);
|
|
548
|
+
if (state.recertified.size > 64) {
|
|
549
|
+
const oldest = state.recertified.keys().next();
|
|
550
|
+
if (oldest.done !== true) state.recertified.delete(oldest.value);
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
/**
|
|
554
|
+
* Drop every cached artifact that depends on the task semantics: a changed
|
|
555
|
+
* todo version invalidates the summary and makes all eligible candidates
|
|
556
|
+
* rescore-worthy (the watermark alone would otherwise hide them).
|
|
557
|
+
*/
|
|
558
|
+
function invalidateOnTaskChange(state, todoVersion) {
|
|
559
|
+
if (state.todoVersion === todoVersion) return false;
|
|
560
|
+
state.todoVersion = todoVersion;
|
|
561
|
+
state.summary = void 0;
|
|
562
|
+
state.lastSummaryTurn = -1;
|
|
563
|
+
return true;
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* `/ctx-summary off|on` overrides, keyed by session id (string). The command
|
|
567
|
+
* handler only sees `invocation.agent.session.id`, not the pruner's Session
|
|
568
|
+
* object, so these are id-keyed rather than WeakMap'd. Entries are two-char
|
|
569
|
+
* strings per session and cleared whenever the override is lifted.
|
|
570
|
+
*/
|
|
571
|
+
const summaryOverrides = /* @__PURE__ */ new Map();
|
|
572
|
+
/** Read the session's `/ctx-summary` override; `undefined` = default (settings-driven). */
|
|
573
|
+
function getSummaryOverride(sessionId) {
|
|
574
|
+
return summaryOverrides.get(sessionId);
|
|
575
|
+
}
|
|
576
|
+
/** Set or clear (`undefined`) the session's `/ctx-summary` override. */
|
|
577
|
+
function setSummaryOverride(sessionId, value) {
|
|
578
|
+
if (value === void 0) summaryOverrides.delete(sessionId);
|
|
579
|
+
else summaryOverrides.set(sessionId, value);
|
|
580
|
+
}
|
|
581
|
+
/** Last observed `intentSummary.enabled` for a session, for `/ctx-summary status`. */
|
|
582
|
+
const lastObservedIntentEnabled = /* @__PURE__ */ new Map();
|
|
583
|
+
/** Record the gate input the pass last evaluated for this session. */
|
|
584
|
+
function observeIntentEnabled(sessionId, enabled) {
|
|
585
|
+
lastObservedIntentEnabled.set(sessionId, enabled);
|
|
586
|
+
}
|
|
587
|
+
/** The settings-side enabled flag last observed for the session, if any. */
|
|
588
|
+
function getObservedIntentEnabled(sessionId) {
|
|
589
|
+
return lastObservedIntentEnabled.get(sessionId);
|
|
590
|
+
}
|
|
591
|
+
const lastIntentFolds = /* @__PURE__ */ new Map();
|
|
592
|
+
/** Record a landed intent fold for the session (newest wins). */
|
|
593
|
+
function recordIntentFold(sessionId, record) {
|
|
594
|
+
lastIntentFolds.set(sessionId, record);
|
|
595
|
+
}
|
|
596
|
+
/** The most recent landed intent fold for the session, if any. */
|
|
597
|
+
function getLastIntentFold(sessionId) {
|
|
598
|
+
return lastIntentFolds.get(sessionId);
|
|
599
|
+
}
|
|
600
|
+
//#endregion
|
|
601
|
+
//#region src/runtime/tokenpilot/intent-gate.ts
|
|
602
|
+
/**
|
|
603
|
+
* Turn-tail intent-summary growth gate (TokenPilot-inspired E2).
|
|
604
|
+
*
|
|
605
|
+
* Pure decision helper evaluated at the turn-boundary postflight: it decides
|
|
606
|
+
* whether this turn may spend a summary-writer LLM call and stage a fold for
|
|
607
|
+
* the next pressure round. The gate is deliberately conservative — every
|
|
608
|
+
* unresolved input (unknown context window, non-finite counters) fails closed
|
|
609
|
+
* toward "do not run", because a skipped fold is free while a wasted summary
|
|
610
|
+
* call is not. Content safety is unaffected either way: fail-open semantics
|
|
611
|
+
* live in the fold landing path, not here.
|
|
612
|
+
*/
|
|
613
|
+
/** Floor: the live surface must exceed this fraction of the context window. */
|
|
614
|
+
const INTENT_GATE_FLOOR_FRACTION = .45;
|
|
615
|
+
/** Growth: the live surface must have grown by more than this since the last landed fold. */
|
|
616
|
+
const INTENT_GATE_GROWTH_TOKENS = 5e4;
|
|
617
|
+
function isUsableNumber(value) {
|
|
618
|
+
return Number.isFinite(value) && value >= 0;
|
|
619
|
+
}
|
|
620
|
+
/** Same evaluation with the reason attached, for audits and `/ctx-summary status`. */
|
|
621
|
+
function evaluateIntentGate(input) {
|
|
622
|
+
const snapshot = (decision, reason) => ({
|
|
623
|
+
...input,
|
|
624
|
+
decision,
|
|
625
|
+
reason
|
|
626
|
+
});
|
|
627
|
+
if (!input.enabled) return snapshot(false, "disabled");
|
|
628
|
+
if (input.override === "off") return snapshot(false, "override-off");
|
|
629
|
+
if (!isUsableNumber(input.liveTokens)) return snapshot(false, "below-floor");
|
|
630
|
+
if (input.contextWindow === void 0 || !isUsableNumber(input.contextWindow)) return snapshot(false, "no-window");
|
|
631
|
+
if (!(input.liveTokens > input.contextWindow * .45)) return snapshot(false, "below-floor");
|
|
632
|
+
if (!isUsableNumber(input.baselineTokens)) return snapshot(false, "below-growth");
|
|
633
|
+
if (!(input.liveTokens - input.baselineTokens > 5e4)) return snapshot(false, "below-growth");
|
|
634
|
+
return snapshot(true, "passed");
|
|
635
|
+
}
|
|
636
|
+
//#endregion
|
|
637
|
+
//#region src/runtime/monitor.ts
|
|
638
|
+
/**
|
|
639
|
+
* Savings snapshot plus the intent-summary control block. Ledger is
|
|
640
|
+
* injectable so tests seed a private instance instead of the process
|
|
641
|
+
* singleton.
|
|
642
|
+
*/
|
|
643
|
+
function buildMonitorSnapshot(sessionId, ledger = getSavingsLedger()) {
|
|
644
|
+
const scopeKey = sessionId ?? "";
|
|
645
|
+
const override = getSummaryOverride(scopeKey);
|
|
646
|
+
const observedContext = scopeKey.length > 0 ? getContextUsage(scopeKey) : latestObservedContextUsage();
|
|
647
|
+
const contextBlock = observedContext === void 0 ? void 0 : {
|
|
648
|
+
liveTokens: observedContext.liveTokens,
|
|
649
|
+
contextWindow: observedContext.contextWindow,
|
|
650
|
+
pct: observedContext.contextWindow !== void 0 && observedContext.contextWindow > 0 ? Math.round(observedContext.liveTokens / observedContext.contextWindow * 1e3) / 1e3 : null,
|
|
651
|
+
sessionId: observedContext.sessionId
|
|
652
|
+
};
|
|
653
|
+
return {
|
|
654
|
+
...ledger.snapshot(sessionId),
|
|
655
|
+
...contextBlock === void 0 ? {} : { context: contextBlock },
|
|
656
|
+
intent: {
|
|
657
|
+
override,
|
|
658
|
+
observedEnabled: getObservedIntentEnabled(scopeKey),
|
|
659
|
+
gate: {
|
|
660
|
+
floorFraction: INTENT_GATE_FLOOR_FRACTION,
|
|
661
|
+
growthTokens: INTENT_GATE_GROWTH_TOKENS
|
|
662
|
+
},
|
|
663
|
+
lastFold: getLastIntentFold(scopeKey)
|
|
664
|
+
},
|
|
665
|
+
sessionScope: sessionId ?? null
|
|
666
|
+
};
|
|
667
|
+
}
|
|
668
|
+
/** 每会话最近一次上下文占用观测;上限 64 会话,超出淘汰最旧。 */
|
|
669
|
+
const contextUsageBySession = /* @__PURE__ */ new Map();
|
|
670
|
+
const CONTEXT_USAGE_CAP = 64;
|
|
671
|
+
/**
|
|
672
|
+
* 由 pruner 的 turn-tail postflight 调用:记录该会话最近的 liveTokens/
|
|
673
|
+
* contextWindow,供 monitor 快照的占用条消费。空会话 id 忽略。
|
|
674
|
+
*/
|
|
675
|
+
function observeContextUsage(sessionId, liveTokens, contextWindow) {
|
|
676
|
+
if (sessionId.length === 0 || !Number.isFinite(liveTokens)) return;
|
|
677
|
+
if (contextUsageBySession.size >= CONTEXT_USAGE_CAP) {
|
|
678
|
+
const oldest = contextUsageBySession.keys().next().value;
|
|
679
|
+
if (oldest !== void 0) contextUsageBySession.delete(oldest);
|
|
680
|
+
}
|
|
681
|
+
contextUsageBySession.set(sessionId, {
|
|
682
|
+
liveTokens,
|
|
683
|
+
contextWindow,
|
|
684
|
+
at: Date.now(),
|
|
685
|
+
seq: ++observeSeq
|
|
686
|
+
});
|
|
687
|
+
}
|
|
688
|
+
function getContextUsage(sessionId) {
|
|
689
|
+
const entry = contextUsageBySession.get(sessionId);
|
|
690
|
+
return entry === void 0 ? void 0 : {
|
|
691
|
+
liveTokens: entry.liveTokens,
|
|
692
|
+
contextWindow: entry.contextWindow,
|
|
693
|
+
sessionId
|
|
694
|
+
};
|
|
695
|
+
}
|
|
696
|
+
let observeSeq = 0;
|
|
697
|
+
function latestObservedContextUsage() {
|
|
698
|
+
let latest;
|
|
699
|
+
for (const [sessionId, entry] of contextUsageBySession) if (latest === void 0 || entry.seq > latest.seq) latest = {
|
|
700
|
+
sessionId,
|
|
701
|
+
liveTokens: entry.liveTokens,
|
|
702
|
+
contextWindow: entry.contextWindow,
|
|
703
|
+
seq: entry.seq
|
|
704
|
+
};
|
|
705
|
+
return latest;
|
|
706
|
+
}
|
|
707
|
+
function isSessionOverrideAction(value) {
|
|
708
|
+
return value === "on" || value === "off" || value === "clear";
|
|
709
|
+
}
|
|
710
|
+
/**
|
|
711
|
+
* Drive the `/ctx-summary` override state machine: `on`/`off` pin the
|
|
712
|
+
* session, `clear` returns it to settings-driven. The resulting override is
|
|
713
|
+
* handed back so the route can echo the post-action truth.
|
|
714
|
+
*/
|
|
715
|
+
function applySessionOverride(sessionId, action) {
|
|
716
|
+
setSummaryOverride(sessionId, action === "clear" ? void 0 : action);
|
|
717
|
+
return { override: getSummaryOverride(sessionId) };
|
|
718
|
+
}
|
|
719
|
+
/**
|
|
720
|
+
* 金额估算(monitor 口径的 token/金额估算,结合官方牌价):
|
|
721
|
+
* - actualCost:本进程累计真实 usage 的官方牌价(exact/range);
|
|
722
|
+
* - estimatedSavedCost:净节省(精确口径)按 cache-miss 输入价折算——
|
|
723
|
+
* 基线假设"这些 token 不压缩就要全价进上下文",标注估算。
|
|
724
|
+
* 仅官方 deepseek-official 路由可解;第三方/未知模型 fail-closed 为 undefined。
|
|
725
|
+
* (自入口迁入: savings 路由与 monitor 路由共享,避免 runtime → entry 回环。)
|
|
726
|
+
*/
|
|
727
|
+
function estimateSavingsPricing(readService, snapshot) {
|
|
728
|
+
if (snapshot.usage.requests === 0 && snapshot.net.exact === 0) return void 0;
|
|
729
|
+
const selection = readService("agentDefaultModel")?.currentSelection?.();
|
|
730
|
+
const provider = typeof selection?.provider === "string" ? selection.provider : "";
|
|
731
|
+
const modelId = typeof selection?.model === "string" ? selection.model : "";
|
|
732
|
+
if (provider !== "deepseek-official") return void 0;
|
|
733
|
+
const now = /* @__PURE__ */ new Date();
|
|
734
|
+
const startedAt = new Date(snapshot.startedAt);
|
|
735
|
+
const base = {
|
|
736
|
+
provider,
|
|
737
|
+
baseUrlClass: "official-public",
|
|
738
|
+
apiRoute: "chat-completions",
|
|
739
|
+
modelId,
|
|
740
|
+
currency: "USD",
|
|
741
|
+
startedAt,
|
|
742
|
+
completedAt: now.getTime() > startedAt.getTime() ? now : new Date(startedAt.getTime() + 1)
|
|
743
|
+
};
|
|
744
|
+
const actual = priceOfficialDeepSeekUsage({
|
|
745
|
+
...base,
|
|
746
|
+
usage: {
|
|
747
|
+
cacheReadTokens: Math.max(0, Math.round(snapshot.usage.cacheReadTokens)),
|
|
748
|
+
cacheMissTokens: Math.max(0, Math.round(snapshot.usage.inputTokens)),
|
|
749
|
+
outputTokens: Math.max(0, Math.round(snapshot.usage.outputTokens))
|
|
750
|
+
}
|
|
751
|
+
});
|
|
752
|
+
const saved = snapshot.net.exact > 0 ? priceOfficialDeepSeekUsage({
|
|
753
|
+
...base,
|
|
754
|
+
usage: {
|
|
755
|
+
cacheReadTokens: 0,
|
|
756
|
+
cacheMissTokens: Math.round(snapshot.net.exact),
|
|
757
|
+
outputTokens: 0
|
|
758
|
+
}
|
|
759
|
+
}) : void 0;
|
|
760
|
+
const decimalOf = (cost) => cost.kind === "exact" ? cost.decimal : cost.kind === "range" ? cost.minimum.decimal : void 0;
|
|
761
|
+
if (actual.kind === "unpriced" && (saved === void 0 || saved.kind === "unpriced")) return void 0;
|
|
762
|
+
return {
|
|
763
|
+
currency: "USD",
|
|
764
|
+
...actual.kind === "unpriced" ? {} : { actualCost: decimalOf(actual) },
|
|
765
|
+
...saved === void 0 || saved.kind === "unpriced" ? {} : { estimatedSavedCost: decimalOf(saved) }
|
|
766
|
+
};
|
|
767
|
+
}
|
|
503
768
|
//#endregion
|
|
504
769
|
//#region src/runtime/types.ts
|
|
505
770
|
/** User-facing mixed strategy profile. */
|
|
@@ -889,6 +1154,20 @@ function parseIntentSummarySettings(value) {
|
|
|
889
1154
|
return { enabled };
|
|
890
1155
|
}
|
|
891
1156
|
/**
|
|
1157
|
+
* Strictly parse the persisted monitorPanel section. Mirrors the intentSummary
|
|
1158
|
+
* section semantics: absent inherits the hidden default, while a
|
|
1159
|
+
* present-but-invalid section is an explicitly invalid document.
|
|
1160
|
+
*/
|
|
1161
|
+
function parseMonitorPanelSettings(value) {
|
|
1162
|
+
if (value === void 0) return { enabled: false };
|
|
1163
|
+
if (!isPlainRecord(value)) throw new TypeError("Context-compression monitorPanel must be a plain object");
|
|
1164
|
+
const keys = Object.keys(value);
|
|
1165
|
+
if (keys.length !== 1 || keys[0] !== "enabled") throw new TypeError(`Context-compression monitorPanel: expected exactly "enabled", got "${keys.join("\", \"")}"`);
|
|
1166
|
+
const enabled = value.enabled;
|
|
1167
|
+
if (typeof enabled !== "boolean") throw new TypeError("Context-compression monitorPanel.enabled must be a boolean");
|
|
1168
|
+
return { enabled };
|
|
1169
|
+
}
|
|
1170
|
+
/**
|
|
892
1171
|
* Parse the optional tokenpilot-inspired preset sub-capability section. Absent
|
|
893
1172
|
* inherits the preset defaults; present-but-invalid is rejected, mirroring the
|
|
894
1173
|
* codeSkeleton section semantics.
|
|
@@ -996,7 +1275,8 @@ const DEFAULT_CONTEXT_COMPRESSION_SETTINGS = {
|
|
|
996
1275
|
custom: structuredClone(DEFAULT_CUSTOM_COMPRESSION_POLICY),
|
|
997
1276
|
autoCompact: { thresholdPercent: AUTO_COMPACT_THRESHOLD_LIMITS.default },
|
|
998
1277
|
codeSkeleton: { enabled: false },
|
|
999
|
-
intentSummary: { enabled: false }
|
|
1278
|
+
intentSummary: { enabled: false },
|
|
1279
|
+
monitorPanel: { enabled: false }
|
|
1000
1280
|
};
|
|
1001
1281
|
/**
|
|
1002
1282
|
* Parse one settings document with the persisted-section semantics: `undefined`
|
|
@@ -1014,19 +1294,21 @@ const ContextCompressionSettingsSchema = z.transform(z.any().required(), (value)
|
|
|
1014
1294
|
if (!isPlainRecord(value)) throw new TypeError("Context-compression settings must be a plain object");
|
|
1015
1295
|
assertPlainDataTree(value);
|
|
1016
1296
|
const candidate = structuredClone(value);
|
|
1017
|
-
const unknown = Object.keys(candidate).find((key) => key !== "profile" && key !== "custom" && key !== "autoCompact" && key !== "codeSkeleton" && key !== "intentSummary" && key !== "presetOptions");
|
|
1297
|
+
const unknown = Object.keys(candidate).find((key) => key !== "profile" && key !== "custom" && key !== "autoCompact" && key !== "codeSkeleton" && key !== "intentSummary" && key !== "monitorPanel" && key !== "presetOptions");
|
|
1018
1298
|
if (unknown !== void 0) throw new TypeError(`Context-compression settings: unknown key "${unknown}"`);
|
|
1019
1299
|
assertPresentSection(candidate, "profile", isSupportedProfile);
|
|
1020
1300
|
assertPresentSection(candidate, "custom", isUsableCustomDocument);
|
|
1021
1301
|
const autoCompact = parseAutoCompactSettings(candidate.autoCompact);
|
|
1022
1302
|
const codeSkeleton = parseCodeSkeletonSettings(candidate.codeSkeleton);
|
|
1023
1303
|
const intentSummary = parseIntentSummarySettings(candidate.intentSummary);
|
|
1304
|
+
const monitorPanel = parseMonitorPanelSettings(candidate.monitorPanel);
|
|
1024
1305
|
const presetOptions = parsePresetOptionsSettings(candidate.presetOptions);
|
|
1025
1306
|
return {
|
|
1026
1307
|
...contextCompressionSettingsInputSchema(candidate),
|
|
1027
1308
|
autoCompact,
|
|
1028
1309
|
codeSkeleton,
|
|
1029
1310
|
intentSummary,
|
|
1311
|
+
monitorPanel,
|
|
1030
1312
|
...presetOptions === void 0 ? {} : { presetOptions }
|
|
1031
1313
|
};
|
|
1032
1314
|
}).default(DEFAULT_CONTEXT_COMPRESSION_SETTINGS);
|
|
@@ -1415,138 +1697,5 @@ function assertPositiveInteger(name, value) {
|
|
|
1415
1697
|
function assertNonNegativeInteger(name, value) {
|
|
1416
1698
|
if (!Number.isSafeInteger(value) || value < 0) throw new Error(`ToolResultPruneConfig: ${name} (${String(value)}) must be a non-negative safe integer`);
|
|
1417
1699
|
}
|
|
1418
|
-
const advisorStates = /* @__PURE__ */ new WeakMap();
|
|
1419
|
-
/**
|
|
1420
|
-
* The per-session advisor state, created on first touch.
|
|
1421
|
-
* @param session - the session to key the state on (by object identity).
|
|
1422
|
-
*/
|
|
1423
|
-
function getAdvisorState(session) {
|
|
1424
|
-
let state = advisorStates.get(session);
|
|
1425
|
-
if (state === void 0) {
|
|
1426
|
-
state = {
|
|
1427
|
-
todoVersion: void 0,
|
|
1428
|
-
summary: void 0,
|
|
1429
|
-
lastSummaryTurn: -1,
|
|
1430
|
-
watermarkSeq: 0,
|
|
1431
|
-
scores: /* @__PURE__ */ new Map(),
|
|
1432
|
-
recertified: /* @__PURE__ */ new Map(),
|
|
1433
|
-
failures: void 0,
|
|
1434
|
-
inFlight: false,
|
|
1435
|
-
lastDecay: void 0,
|
|
1436
|
-
lastAdvice: void 0
|
|
1437
|
-
};
|
|
1438
|
-
advisorStates.set(session, state);
|
|
1439
|
-
}
|
|
1440
|
-
return state;
|
|
1441
|
-
}
|
|
1442
|
-
/**
|
|
1443
|
-
* Insert or refresh one score with LRU semantics: a re-touched seq moves to
|
|
1444
|
-
* the newest position, and the oldest entry is evicted once the map exceeds
|
|
1445
|
-
* {@link ADVISOR_SCORES_LIMIT}.
|
|
1446
|
-
*/
|
|
1447
|
-
function recordScore(state, seq, entry) {
|
|
1448
|
-
state.scores.delete(seq);
|
|
1449
|
-
state.scores.set(seq, entry);
|
|
1450
|
-
if (state.scores.size > 64) {
|
|
1451
|
-
const oldest = state.scores.keys().next();
|
|
1452
|
-
if (oldest.done !== true) state.scores.delete(oldest.value);
|
|
1453
|
-
}
|
|
1454
|
-
}
|
|
1455
|
-
/**
|
|
1456
|
-
* Mark one seq as LLM-recertified low relevance (a suggestion for later
|
|
1457
|
-
* history-aggressiveness decisions, consumed by nothing in this round).
|
|
1458
|
-
* Bounded at {@link ADVISOR_RECERTIFIED_LIMIT} with the same LRU eviction.
|
|
1459
|
-
*/
|
|
1460
|
-
function recordRecertified(state, seq, turn) {
|
|
1461
|
-
state.recertified.delete(seq);
|
|
1462
|
-
state.recertified.set(seq, turn);
|
|
1463
|
-
if (state.recertified.size > 64) {
|
|
1464
|
-
const oldest = state.recertified.keys().next();
|
|
1465
|
-
if (oldest.done !== true) state.recertified.delete(oldest.value);
|
|
1466
|
-
}
|
|
1467
|
-
}
|
|
1468
|
-
/**
|
|
1469
|
-
* Drop every cached artifact that depends on the task semantics: a changed
|
|
1470
|
-
* todo version invalidates the summary and makes all eligible candidates
|
|
1471
|
-
* rescore-worthy (the watermark alone would otherwise hide them).
|
|
1472
|
-
*/
|
|
1473
|
-
function invalidateOnTaskChange(state, todoVersion) {
|
|
1474
|
-
if (state.todoVersion === todoVersion) return false;
|
|
1475
|
-
state.todoVersion = todoVersion;
|
|
1476
|
-
state.summary = void 0;
|
|
1477
|
-
state.lastSummaryTurn = -1;
|
|
1478
|
-
return true;
|
|
1479
|
-
}
|
|
1480
|
-
/**
|
|
1481
|
-
* `/ctx-summary off|on` overrides, keyed by session id (string). The command
|
|
1482
|
-
* handler only sees `invocation.agent.session.id`, not the pruner's Session
|
|
1483
|
-
* object, so these are id-keyed rather than WeakMap'd. Entries are two-char
|
|
1484
|
-
* strings per session and cleared whenever the override is lifted.
|
|
1485
|
-
*/
|
|
1486
|
-
const summaryOverrides = /* @__PURE__ */ new Map();
|
|
1487
|
-
/** Read the session's `/ctx-summary` override; `undefined` = default (settings-driven). */
|
|
1488
|
-
function getSummaryOverride(sessionId) {
|
|
1489
|
-
return summaryOverrides.get(sessionId);
|
|
1490
|
-
}
|
|
1491
|
-
/** Set or clear (`undefined`) the session's `/ctx-summary` override. */
|
|
1492
|
-
function setSummaryOverride(sessionId, value) {
|
|
1493
|
-
if (value === void 0) summaryOverrides.delete(sessionId);
|
|
1494
|
-
else summaryOverrides.set(sessionId, value);
|
|
1495
|
-
}
|
|
1496
|
-
/** Last observed `intentSummary.enabled` for a session, for `/ctx-summary status`. */
|
|
1497
|
-
const lastObservedIntentEnabled = /* @__PURE__ */ new Map();
|
|
1498
|
-
/** Record the gate input the pass last evaluated for this session. */
|
|
1499
|
-
function observeIntentEnabled(sessionId, enabled) {
|
|
1500
|
-
lastObservedIntentEnabled.set(sessionId, enabled);
|
|
1501
|
-
}
|
|
1502
|
-
/** The settings-side enabled flag last observed for the session, if any. */
|
|
1503
|
-
function getObservedIntentEnabled(sessionId) {
|
|
1504
|
-
return lastObservedIntentEnabled.get(sessionId);
|
|
1505
|
-
}
|
|
1506
|
-
const lastIntentFolds = /* @__PURE__ */ new Map();
|
|
1507
|
-
/** Record a landed intent fold for the session (newest wins). */
|
|
1508
|
-
function recordIntentFold(sessionId, record) {
|
|
1509
|
-
lastIntentFolds.set(sessionId, record);
|
|
1510
|
-
}
|
|
1511
|
-
/** The most recent landed intent fold for the session, if any. */
|
|
1512
|
-
function getLastIntentFold(sessionId) {
|
|
1513
|
-
return lastIntentFolds.get(sessionId);
|
|
1514
|
-
}
|
|
1515
|
-
//#endregion
|
|
1516
|
-
//#region src/runtime/tokenpilot/intent-gate.ts
|
|
1517
|
-
/**
|
|
1518
|
-
* Turn-tail intent-summary growth gate (TokenPilot-inspired E2).
|
|
1519
|
-
*
|
|
1520
|
-
* Pure decision helper evaluated at the turn-boundary postflight: it decides
|
|
1521
|
-
* whether this turn may spend a summary-writer LLM call and stage a fold for
|
|
1522
|
-
* the next pressure round. The gate is deliberately conservative — every
|
|
1523
|
-
* unresolved input (unknown context window, non-finite counters) fails closed
|
|
1524
|
-
* toward "do not run", because a skipped fold is free while a wasted summary
|
|
1525
|
-
* call is not. Content safety is unaffected either way: fail-open semantics
|
|
1526
|
-
* live in the fold landing path, not here.
|
|
1527
|
-
*/
|
|
1528
|
-
/** Floor: the live surface must exceed this fraction of the context window. */
|
|
1529
|
-
const INTENT_GATE_FLOOR_FRACTION = .45;
|
|
1530
|
-
/** Growth: the live surface must have grown by more than this since the last landed fold. */
|
|
1531
|
-
const INTENT_GATE_GROWTH_TOKENS = 5e4;
|
|
1532
|
-
function isUsableNumber(value) {
|
|
1533
|
-
return Number.isFinite(value) && value >= 0;
|
|
1534
|
-
}
|
|
1535
|
-
/** Same evaluation with the reason attached, for audits and `/ctx-summary status`. */
|
|
1536
|
-
function evaluateIntentGate(input) {
|
|
1537
|
-
const snapshot = (decision, reason) => ({
|
|
1538
|
-
...input,
|
|
1539
|
-
decision,
|
|
1540
|
-
reason
|
|
1541
|
-
});
|
|
1542
|
-
if (!input.enabled) return snapshot(false, "disabled");
|
|
1543
|
-
if (input.override === "off") return snapshot(false, "override-off");
|
|
1544
|
-
if (!isUsableNumber(input.liveTokens)) return snapshot(false, "below-floor");
|
|
1545
|
-
if (input.contextWindow === void 0 || !isUsableNumber(input.contextWindow)) return snapshot(false, "no-window");
|
|
1546
|
-
if (!(input.liveTokens > input.contextWindow * .45)) return snapshot(false, "below-floor");
|
|
1547
|
-
if (!isUsableNumber(input.baselineTokens)) return snapshot(false, "below-growth");
|
|
1548
|
-
if (!(input.liveTokens - input.baselineTokens > 5e4)) return snapshot(false, "below-growth");
|
|
1549
|
-
return snapshot(true, "passed");
|
|
1550
|
-
}
|
|
1551
1700
|
//#endregion
|
|
1552
|
-
export {
|
|
1701
|
+
export { getLastIntentFold as A, decimalRateNanoUnits as B, estimateSavingsPricing as C, INTENT_GATE_GROWTH_TOKENS as D, INTENT_GATE_FLOOR_FRACTION as E, recordIntentFold as F, resolveOfficialDeepSeekPrice as H, recordRecertified as I, recordScore as L, getSummaryOverride as M, invalidateOnTaskChange as N, evaluateIntentGate as O, observeIntentEnabled as P, setSummaryOverride as R, buildMonitorSnapshot as S, observeContextUsage as T, getSavingsLedger as U, priceOfficialDeepSeekUsage as V, resolveCustomPolicy as _, DEFAULT_CONTEXT_COMPRESSION_SETTINGS as a, COMPRESSION_PROFILES as b, charsToTokens as c, isValidAutoCompactThresholdPercent as d, parseContextCompressionSettings as f, DEFAULT_CUSTOM_COMPRESSION_POLICY as g, CustomCompressionPolicySchema as h, DEFAULTS as i, getObservedIntentEnabled as j, getAdvisorState as k, codePointLength as l, resolvePolicy as m, CONTEXT_COMPRESSION_SETTINGS_NAMESPACE as n, PRUNE_MARKER as o, resolveConfig as p, ContextCompressionSettingsSchema as r, charsForTokens as s, AUTO_COMPACT_THRESHOLD_LIMITS as t, isCompressionProfile as u, assertNever as v, isSessionOverrideAction as w, applySessionOverride as x, deepFreeze as y, DEEPSEEK_OFFICIAL_PRICE_CATALOG_VERSION as z };
|
|
@@ -2,6 +2,18 @@ import { n as ContextCompressionSettings } from "./profiles.js";
|
|
|
2
2
|
import z from "@deepseek-ai/schemastery";
|
|
3
3
|
import { Context } from "@deepseek-ai/cordis";
|
|
4
4
|
//#region src/index.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Floating-panel monitor route (`GET|POST .../monitor?sessionId=…`):
|
|
7
|
+
* - GET → the monitor snapshot (savings aggregate + intent control block +
|
|
8
|
+
* pricing) — the payload shape the panel polls;
|
|
9
|
+
* - POST → session override action (`{"action":"on"|"off"|"clear"}`),
|
|
10
|
+
* driving the exact state machine `/ctx-summary` drives.
|
|
11
|
+
* Registration mirrors the savings route (webServer gate alone, dual channels,
|
|
12
|
+
* single guarded registration); the handler branches on `req.method` because
|
|
13
|
+
* the registration surface is method-agnostic.
|
|
14
|
+
*/
|
|
15
|
+
/** Exported for the runtime monitor spec; the entry owns all webServer wiring. */
|
|
16
|
+
declare function registerMonitorRoute(ctx: Context): void;
|
|
5
17
|
/** Standalone Bundle behavior; the settings/UI owner remains safe when false. */
|
|
6
18
|
interface Config {
|
|
7
19
|
/** Add the canonical compression stack to every non-Minimal preset. */
|
|
@@ -40,4 +52,4 @@ declare const Config: z<Config>;
|
|
|
40
52
|
/** Register the persisted default read by the currently mounted root pruner. */
|
|
41
53
|
declare function apply(ctx: Context, config?: Config): void;
|
|
42
54
|
//#endregion
|
|
43
|
-
export { Config, apply };
|
|
55
|
+
export { Config, apply, registerMonitorRoute };
|