dsh-context-compression-improved 0.9.0 → 0.9.5
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 +56 -0
- package/package.json +2 -1
- package/packages/selector/dsh.plugin.json +1 -1
- package/packages/selector/lib/client.d.ts +1 -0
- package/packages/selector/lib/client.js +86 -32
- package/packages/selector/lib/config.js +71 -7
- package/packages/selector/lib/index.js +120 -20
- package/packages/selector/lib/pruner.d.ts +158 -107
- package/packages/selector/lib/pruner.js +336 -21
- package/packages/selector/package.json +1 -1
- package/packages/selector/src/client/index.ts +55 -20
- package/packages/selector/src/client/locales/de.ts +1 -0
- package/packages/selector/src/client/locales/es.ts +1 -0
- package/packages/selector/src/client/locales/fr.ts +1 -0
- package/packages/selector/src/client/locales/it.ts +1 -0
- package/packages/selector/src/client/locales/ja.ts +1 -0
- package/packages/selector/src/client/locales/ko.ts +1 -0
- package/packages/selector/src/client/locales/ru.ts +1 -0
- package/packages/selector/src/client/locales.ts +2 -0
- package/packages/selector/src/client/monitor-fab.ts +19 -4
- package/packages/selector/src/preset-overlay.ts +145 -22
- package/packages/selector/src/pruner/content.ts +1 -1
- package/packages/selector/src/pruner/state.ts +4 -0
- package/packages/selector/src/pruner.ts +120 -16
- package/packages/selector/src/runtime/config.ts +1 -1
- package/packages/selector/src/runtime/measurement.ts +14 -1
- package/packages/selector/src/runtime/reducers.ts +81 -1
- package/packages/selector/src/runtime/retrieve.ts +4 -3
- package/packages/selector/src/runtime/savings-projection.ts +226 -0
- package/packages/selector/src/runtime/savings.ts +83 -3
- package/packages/selector/tests/runtime/fresh-gate.spec.ts +43 -0
- package/packages/selector/tests/runtime/jsonl-reducer.spec.ts +79 -0
- package/packages/selector/tests/runtime/public/public-runtime.spec.ts +48 -0
- package/packages/selector/tests/runtime/savings-projection.spec.ts +89 -0
- package/packages/selector/tests/runtime/savings.spec.ts +22 -3
- package/packages/selector/tests/runtime/settings-unwrap.spec.ts +58 -0
- package/packages/selector/tests/runtime/tokenpilot/profile-baseline.spec.ts +5 -2
- package/scripts/cell-browser-e2e.mjs +354 -0
|
@@ -373,6 +373,17 @@ function wireSettingsBridge(ctx) {
|
|
|
373
373
|
//#endregion
|
|
374
374
|
//#region src/preset-overlay.ts
|
|
375
375
|
/** Plugin-owned, reversible compression overlays for native agent presets. */
|
|
376
|
+
/** Cordis 把未被代理的原始 service 暴露在该 symbol 下(见 cordis createTraceable 的 get 陷阱)。 */
|
|
377
|
+
const CORDIS_ORIGINAL = Symbol.for("cordis.original");
|
|
378
|
+
/**
|
|
379
|
+
* 取得 Cordis traceable 代理背后的原始 service。原始对象是宿主内部调用
|
|
380
|
+
* (`this.resolveMountable → this.resolve`) 时的接收者;只有装饰它,压缩栈补丁才能
|
|
381
|
+
* 真正注入(实证:装饰代理/原型代理均失败)。
|
|
382
|
+
*/
|
|
383
|
+
function rawService(presets) {
|
|
384
|
+
const raw = presets[CORDIS_ORIGINAL];
|
|
385
|
+
return raw !== null && typeof raw === "object" ? raw : presets;
|
|
386
|
+
}
|
|
376
387
|
/**
|
|
377
388
|
* Fixed base for deterministic standing mtimes. The stamp is derived from the
|
|
378
389
|
* FULL content identity (source + modules + threshold), not from the write
|
|
@@ -658,6 +669,75 @@ function renderPatchWarning(message, args) {
|
|
|
658
669
|
let index = 0;
|
|
659
670
|
return `context-compression selector: ${message.replace(/%C/g, () => JSON.stringify(args[index++]))}`;
|
|
660
671
|
}
|
|
672
|
+
/** Read a definition's preset id when the shape is a plain object. */
|
|
673
|
+
function readDefinitionId(definition) {
|
|
674
|
+
if (typeof definition !== "object" || definition === null) return void 0;
|
|
675
|
+
const id = definition.id;
|
|
676
|
+
return typeof id === "string" ? id : void 0;
|
|
677
|
+
}
|
|
678
|
+
/**
|
|
679
|
+
* Inject the canonical compression stack into one preset definition's rows.
|
|
680
|
+
*
|
|
681
|
+
* The 0.2.0 `dsh-agent-preset-registry` PRE-BUILDS a generation per definition
|
|
682
|
+
* (`activate` → `mountPreset(ctx, id, definition.plugins)`) and `mount` only
|
|
683
|
+
* `retain`s it; the composition never calls `resolve`. So the stack must land in
|
|
684
|
+
* the definition's own `plugins` before activation, not in a resolved preset
|
|
685
|
+
* file. Best-effort: a failed injection leaves the original rows untouched.
|
|
686
|
+
*/
|
|
687
|
+
function injectCompressionRows(definition, options, excluded) {
|
|
688
|
+
const id = readDefinitionId(definition);
|
|
689
|
+
if (id === void 0 || excluded.has(id)) return;
|
|
690
|
+
const plugins = definition.plugins;
|
|
691
|
+
if (!Array.isArray(plugins)) return;
|
|
692
|
+
try {
|
|
693
|
+
const thresholdPercent = options.autoCompactThresholdPercent?.();
|
|
694
|
+
definition.plugins = applyEntryPatches(stripCompressionRows(plugins), [{ insert: compressionRowsBySpecifier(thresholdPercent) }], (message, ...args) => {
|
|
695
|
+
throw new Error(renderPatchWarning(message, args));
|
|
696
|
+
});
|
|
697
|
+
} catch {}
|
|
698
|
+
}
|
|
699
|
+
/**
|
|
700
|
+
* The canonical compression stack addressed by BARE SPECIFIERS. The 0.2.0
|
|
701
|
+
* `dsh-agent-preset-registry` preset files declare this exact group by package
|
|
702
|
+
* name, and its loader resolves bare specifiers (`@deepseek-ai/...`,
|
|
703
|
+
* `dsh-context-compression-improved/pruner`) — absolute filesystem paths from
|
|
704
|
+
* {@link resolveCompressionModulePaths} are NOT accepted there (empirically:
|
|
705
|
+
* every injected row reports "never started").
|
|
706
|
+
*/
|
|
707
|
+
function compressionRowsBySpecifier(thresholdPercent) {
|
|
708
|
+
return [{
|
|
709
|
+
id: "compaction",
|
|
710
|
+
name: "cordis:group",
|
|
711
|
+
group: true,
|
|
712
|
+
isolate: {
|
|
713
|
+
compaction: true,
|
|
714
|
+
toolResultPruner: true
|
|
715
|
+
},
|
|
716
|
+
config: [
|
|
717
|
+
{
|
|
718
|
+
id: "compaction-basic",
|
|
719
|
+
name: "@deepseek-ai/dsh-compaction-basic",
|
|
720
|
+
...thresholdPercent === void 0 ? {} : { config: {
|
|
721
|
+
thresholdRatio: thresholdPercent / 100,
|
|
722
|
+
retainRatio: .16
|
|
723
|
+
} }
|
|
724
|
+
},
|
|
725
|
+
{
|
|
726
|
+
id: "command-compact",
|
|
727
|
+
name: "@deepseek-ai/dsh-command-compact"
|
|
728
|
+
},
|
|
729
|
+
{
|
|
730
|
+
id: "tool-result-pruner",
|
|
731
|
+
name: "dsh-context-compression-improved/pruner",
|
|
732
|
+
config: {
|
|
733
|
+
headChars: 4096,
|
|
734
|
+
tailChars: 1024,
|
|
735
|
+
...thresholdPercent === void 0 ? {} : { autoCompactThresholdPercent: thresholdPercent }
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
]
|
|
739
|
+
}];
|
|
740
|
+
}
|
|
661
741
|
/**
|
|
662
742
|
* Cordis can hand two callers different traceable proxies for one service.
|
|
663
743
|
* Symbol properties forward to the shared target, unlike proxy identity.
|
|
@@ -734,47 +814,67 @@ function installAgentPresetsDecoration(presets, options) {
|
|
|
734
814
|
const excluded = new Set(options.excludedPresetIds ?? ["minimal"]);
|
|
735
815
|
const operations = new AsyncLocalStorage();
|
|
736
816
|
const store = new PresetOverlayStore(options);
|
|
817
|
+
const raw = rawService(presets);
|
|
737
818
|
const snapshots = snapshotMethods(presets);
|
|
738
819
|
const resolveSnapshot = snapshotFor(snapshots, "resolve");
|
|
739
820
|
const resolveWrapped = async (id) => {
|
|
740
|
-
const preset = await Reflect.apply(resolveSnapshot.original,
|
|
741
|
-
if (operations.getStore()?.composing
|
|
821
|
+
const preset = await Reflect.apply(resolveSnapshot.original, raw, [id]);
|
|
822
|
+
if (!(operations.getStore()?.composing === true) || excluded.has(preset.id)) return preset;
|
|
742
823
|
return await store.overlay(preset);
|
|
743
824
|
};
|
|
744
|
-
installMethod(
|
|
825
|
+
installMethod(resolveSnapshot, resolveWrapped);
|
|
745
826
|
for (const method of [
|
|
746
827
|
"mount",
|
|
747
828
|
"recompose",
|
|
748
829
|
"standingKeyFor"
|
|
749
830
|
]) {
|
|
750
|
-
const snapshot =
|
|
751
|
-
|
|
752
|
-
|
|
831
|
+
const snapshot = snapshots.find((candidate) => candidate.name === method);
|
|
832
|
+
if (snapshot === void 0) continue;
|
|
833
|
+
const wrapped = (...args) => {
|
|
834
|
+
return operations.run({ composing: true }, () => Reflect.apply(snapshot.original, raw, args));
|
|
835
|
+
};
|
|
836
|
+
installMethod(snapshot, wrapped);
|
|
837
|
+
}
|
|
838
|
+
const registerSnapshot = snapshots.find((candidate) => candidate.name === "register");
|
|
839
|
+
if (registerSnapshot !== void 0) {
|
|
840
|
+
const registerWrapped = async (definition) => {
|
|
841
|
+
injectCompressionRows(definition, options, excluded);
|
|
842
|
+
return await Reflect.apply(registerSnapshot.original, raw, [definition]);
|
|
843
|
+
};
|
|
844
|
+
installMethod(registerSnapshot, registerWrapped);
|
|
753
845
|
}
|
|
754
846
|
let disposed = false;
|
|
755
847
|
return { async dispose() {
|
|
756
848
|
if (disposed) return;
|
|
757
849
|
disposed = true;
|
|
758
|
-
for (const snapshot of [...snapshots].reverse()) restoreMethod(
|
|
850
|
+
for (const snapshot of [...snapshots].reverse()) restoreMethod(snapshot);
|
|
759
851
|
await store.dispose();
|
|
760
852
|
} };
|
|
761
853
|
}
|
|
762
854
|
/** Capture callable methods and whether each was inherited or owned. */
|
|
763
855
|
function snapshotMethods(presets) {
|
|
764
|
-
|
|
856
|
+
const methods = [];
|
|
857
|
+
for (const name of [
|
|
765
858
|
"resolve",
|
|
766
859
|
"mount",
|
|
767
860
|
"recompose",
|
|
768
|
-
"standingKeyFor"
|
|
769
|
-
|
|
861
|
+
"standingKeyFor",
|
|
862
|
+
"register"
|
|
863
|
+
]) {
|
|
770
864
|
const original = presets[name];
|
|
771
|
-
if (typeof original !== "function")
|
|
772
|
-
|
|
865
|
+
if (typeof original !== "function") {
|
|
866
|
+
if (name === "resolve") throw new TypeError(`context-compression selector: AgentPresets.${name} is unavailable`);
|
|
867
|
+
continue;
|
|
868
|
+
}
|
|
869
|
+
const home = rawService(presets);
|
|
870
|
+
methods.push({
|
|
773
871
|
name,
|
|
774
|
-
|
|
872
|
+
home,
|
|
873
|
+
own: Object.getOwnPropertyDescriptor(home, name),
|
|
775
874
|
original
|
|
776
|
-
};
|
|
777
|
-
}
|
|
875
|
+
});
|
|
876
|
+
}
|
|
877
|
+
return methods;
|
|
778
878
|
}
|
|
779
879
|
/** Return the captured method or fail loudly if the snapshot set is corrupt. */
|
|
780
880
|
function snapshotFor(snapshots, name) {
|
|
@@ -783,18 +883,18 @@ function snapshotFor(snapshots, name) {
|
|
|
783
883
|
return snapshot;
|
|
784
884
|
}
|
|
785
885
|
/** Install one own method while retaining its identity for safe disposal. */
|
|
786
|
-
function installMethod(
|
|
886
|
+
function installMethod(snapshot, wrapped) {
|
|
787
887
|
snapshot.wrapped = wrapped;
|
|
788
|
-
Object.defineProperty(
|
|
888
|
+
Object.defineProperty(snapshot.home, snapshot.name, {
|
|
789
889
|
configurable: true,
|
|
790
890
|
writable: true,
|
|
791
891
|
value: wrapped
|
|
792
892
|
});
|
|
793
893
|
}
|
|
794
894
|
/** Restore the captured own/prototype state after the final shared lease. */
|
|
795
|
-
function restoreMethod(
|
|
796
|
-
if (snapshot.own === void 0) Reflect.deleteProperty(
|
|
797
|
-
else Object.defineProperty(
|
|
895
|
+
function restoreMethod(snapshot) {
|
|
896
|
+
if (snapshot.own === void 0) Reflect.deleteProperty(snapshot.home, snapshot.name);
|
|
897
|
+
else Object.defineProperty(snapshot.home, snapshot.name, snapshot.own);
|
|
798
898
|
}
|
|
799
899
|
//#endregion
|
|
800
900
|
//#region src/index.ts
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import z from "@deepseek-ai/schemastery";
|
|
2
2
|
import { Context, Service } from "@deepseek-ai/cordis";
|
|
3
3
|
import { ContentBlock, ToolCallId } from "@deepseek-ai/dsh-llm";
|
|
4
|
-
import { Session } from "@deepseek-ai/dsh-session";
|
|
4
|
+
import { Session, SessionEvent } from "@deepseek-ai/dsh-session";
|
|
5
5
|
import { TokenMeasurement } from "@deepseek-ai/dsh-token-meter";
|
|
6
6
|
//#region src/runtime/tokenpilot/dedup.d.ts
|
|
7
7
|
/** Entry stored per first-seen content hash. */
|
|
@@ -473,6 +473,10 @@ interface PrunerState {
|
|
|
473
473
|
readonly estimatorRemainingTurns: WeakMap<Session, number>;
|
|
474
474
|
/** Per-session advisor side channel, constructed once with the advisor overrides. */
|
|
475
475
|
readonly advisorChannels: WeakMap<Session, SideChannel>;
|
|
476
|
+
/** Per-session intent-summary writer channel, constructed once with the estimator channel mode. */
|
|
477
|
+
readonly intentChannels: WeakMap<Session, SideChannel>;
|
|
478
|
+
/** Sessions whose savings ledger has been seeded from the session-log projection. */
|
|
479
|
+
readonly projectedSavings: WeakSet<Session>;
|
|
476
480
|
/** Staged intent fold awaiting the next pressure round (at most one per session). */
|
|
477
481
|
readonly pendingIntentFolds: WeakMap<Session, PendingIntentFold>;
|
|
478
482
|
/** Landed intent-fold seqs: fold-once, never reconsidered by any later stage. */
|
|
@@ -481,6 +485,133 @@ interface PrunerState {
|
|
|
481
485
|
readonly intentBaselines: WeakMap<Session, number>;
|
|
482
486
|
}
|
|
483
487
|
//#endregion
|
|
488
|
+
//#region src/runtime/token-count.d.ts
|
|
489
|
+
/** Token counts owned by the standalone compression runtime. */
|
|
490
|
+
/** Exact count of one canonical value under a pinned tokenizer artifact. */
|
|
491
|
+
interface ExactTokenizerTokenCount {
|
|
492
|
+
readonly kind: 'exact-tokenizer';
|
|
493
|
+
readonly tokens: number;
|
|
494
|
+
readonly tokenizerId: string;
|
|
495
|
+
readonly tokenizerRevision: string;
|
|
496
|
+
}
|
|
497
|
+
/** A value that the bundled tokenizer cannot safely count. */
|
|
498
|
+
interface UnavailableTokenCount {
|
|
499
|
+
readonly kind: 'unavailable';
|
|
500
|
+
readonly reason: string;
|
|
501
|
+
}
|
|
502
|
+
/** Best-effort estimate carrying a conservative upper value when available. */
|
|
503
|
+
interface TokenizerEstimateTokenCount {
|
|
504
|
+
readonly kind: 'tokenizer-estimate';
|
|
505
|
+
readonly tokens: number;
|
|
506
|
+
readonly upperBoundTokens: number;
|
|
507
|
+
readonly estimatorId: string;
|
|
508
|
+
readonly estimatorRevision: string;
|
|
509
|
+
readonly calibration?: Readonly<{
|
|
510
|
+
readonly sampleCount: number;
|
|
511
|
+
readonly conservativeMarginTokens: number;
|
|
512
|
+
}>;
|
|
513
|
+
}
|
|
514
|
+
/** Exact, conservative request estimate, or an explicit refusal. */
|
|
515
|
+
type TokenCount = ExactTokenizerTokenCount | TokenizerEstimateTokenCount | UnavailableTokenCount;
|
|
516
|
+
//#endregion
|
|
517
|
+
//#region src/runtime/measurement.d.ts
|
|
518
|
+
/** Request identity retained only when every dimension is publicly known. */
|
|
519
|
+
interface ProviderMeasurementKey {
|
|
520
|
+
readonly provider: string;
|
|
521
|
+
readonly baseUrlClass: string;
|
|
522
|
+
readonly apiRoute: string;
|
|
523
|
+
readonly modelId: string;
|
|
524
|
+
readonly requestTemplateRevision: string;
|
|
525
|
+
readonly tokenizerRevision: string;
|
|
526
|
+
readonly modality: string;
|
|
527
|
+
}
|
|
528
|
+
/** Rich request observation used by Adaptive when a future public API supplies it. */
|
|
529
|
+
interface ObservedPromptUsage {
|
|
530
|
+
readonly attemptId: string;
|
|
531
|
+
readonly providerRequestOrdinal: number;
|
|
532
|
+
readonly startedAtMs: number;
|
|
533
|
+
readonly completedAtMs: number;
|
|
534
|
+
readonly measurement: TokenCount;
|
|
535
|
+
readonly observedPromptTokens: number;
|
|
536
|
+
readonly observedOutputTokens?: number;
|
|
537
|
+
readonly responseModelId?: string;
|
|
538
|
+
readonly cacheStatus?: 'complete' | 'unknown';
|
|
539
|
+
readonly cacheReadTokens?: number;
|
|
540
|
+
readonly cacheMissTokens?: number;
|
|
541
|
+
readonly key?: ProviderMeasurementKey;
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* Intrinsic-grid diagnostic attached to nodes whose count estimates images.
|
|
545
|
+
* It reports ONLY the official block
|
|
546
|
+
* arithmetic evaluated on the attachment's intrinsic dimensions at the two
|
|
547
|
+
* alignment-padding extremes (compress-pad 0 and 3). It is NOT a request-token
|
|
548
|
+
* bound: the adapter may still re-project the image (per-route pixel-budget
|
|
549
|
+
* or image-detail overrides, byte-cap reprojection), which can move the real
|
|
550
|
+
* count below the diagnostic minimum. It never participates in exact gates,
|
|
551
|
+
* rewrite proofs, or any lossy decision.
|
|
552
|
+
*/
|
|
553
|
+
interface IntrinsicImageBlockDiagnostic {
|
|
554
|
+
readonly paddingMinimumTokens: number;
|
|
555
|
+
readonly paddingMaximumTokens: number;
|
|
556
|
+
}
|
|
557
|
+
/** One same-revision surface node with exact, estimated, or unavailable count. */
|
|
558
|
+
interface MeasuredTokenSurfaceNode {
|
|
559
|
+
readonly seq: number;
|
|
560
|
+
readonly count: TokenCount;
|
|
561
|
+
/**
|
|
562
|
+
* Authoritative decision metric for this node: character pressure in
|
|
563
|
+
* Unicode code points (same convention as SnapshotCandidate.characterPressure).
|
|
564
|
+
*/
|
|
565
|
+
readonly characterPressure: number;
|
|
566
|
+
/** Intrinsic-grid diagnostic when usable image dimensions were available. */
|
|
567
|
+
readonly intrinsicImageBlockEstimate?: IntrinsicImageBlockDiagnostic;
|
|
568
|
+
}
|
|
569
|
+
/** Compression view derived only from published Session and TokenMeter methods. */
|
|
570
|
+
interface CompactionTokenView extends TokenMeasurement {
|
|
571
|
+
readonly providerRoute?: string;
|
|
572
|
+
readonly modelId?: string;
|
|
573
|
+
readonly measuredNodes: readonly MeasuredTokenSurfaceNode[];
|
|
574
|
+
readonly currentSurface: TokenCount;
|
|
575
|
+
/**
|
|
576
|
+
* Exact character analogue of `currentSurface`: sum of per-node
|
|
577
|
+
* `characterPressure` over the same nodes `currentSurface` covers.
|
|
578
|
+
*/
|
|
579
|
+
readonly currentSurfaceChars: number;
|
|
580
|
+
/** Sum of per-node intrinsic padding minima; a diagnostic, not a token bound. */
|
|
581
|
+
readonly intrinsicImageBlockEstimateTokens: number;
|
|
582
|
+
readonly latestEnvelopeKey?: ProviderMeasurementKey;
|
|
583
|
+
readonly lastCompletedUsage?: ObservedPromptUsage;
|
|
584
|
+
countCanonicalText(text: string): TokenCount;
|
|
585
|
+
}
|
|
586
|
+
/**
|
|
587
|
+
* Capture one route-bound view without calling patched Harness methods.
|
|
588
|
+
* Official `measure()` remains authoritative for request pressure; the bundled
|
|
589
|
+
* tokenizer supplies exact canonical content counts used by safe rewrites.
|
|
590
|
+
*/
|
|
591
|
+
declare function measureForCompaction(ctx: Context, session: Session): CompactionTokenView;
|
|
592
|
+
//#endregion
|
|
593
|
+
//#region src/pruner/types.d.ts
|
|
594
|
+
interface ToolCallInfo {
|
|
595
|
+
readonly name: string;
|
|
596
|
+
readonly arguments: string;
|
|
597
|
+
}
|
|
598
|
+
interface SnapshotCandidate {
|
|
599
|
+
readonly seq: number;
|
|
600
|
+
readonly event: SessionEvent<'tool/result'>;
|
|
601
|
+
readonly call: ToolCallInfo;
|
|
602
|
+
/** Authoritative same-revision exact canonical content count (telemetry only). */
|
|
603
|
+
readonly count: TokenCount;
|
|
604
|
+
/** Same-revision legacy heuristic price used only by bounded projections. */
|
|
605
|
+
readonly shadowedHeuristicTokenCount: number;
|
|
606
|
+
/**
|
|
607
|
+
* Authoritative decision metric: character pressure in Unicode code points.
|
|
608
|
+
* Every eligibility and acceptance gate decides on this number so the plugin
|
|
609
|
+
* never depends on the routed model id owning a bundled exact tokenizer;
|
|
610
|
+
* `count` is retained as diagnostic telemetry only.
|
|
611
|
+
*/
|
|
612
|
+
readonly characterPressure: number;
|
|
613
|
+
}
|
|
614
|
+
//#endregion
|
|
484
615
|
//#region src/runtime/custom-policy.d.ts
|
|
485
616
|
/** Canonical Custom document accepted by Host settings and the runtime resolver. */
|
|
486
617
|
declare const CustomCompressionPolicySchema: z<CustomCompressionPolicy>;
|
|
@@ -636,111 +767,6 @@ declare function verifyReduction(input: ReducerInput, output: ReducerOutput): bo
|
|
|
636
767
|
*/
|
|
637
768
|
declare function normalizeTerminalText(text: string): string;
|
|
638
769
|
//#endregion
|
|
639
|
-
//#region src/runtime/token-count.d.ts
|
|
640
|
-
/** Token counts owned by the standalone compression runtime. */
|
|
641
|
-
/** Exact count of one canonical value under a pinned tokenizer artifact. */
|
|
642
|
-
interface ExactTokenizerTokenCount {
|
|
643
|
-
readonly kind: 'exact-tokenizer';
|
|
644
|
-
readonly tokens: number;
|
|
645
|
-
readonly tokenizerId: string;
|
|
646
|
-
readonly tokenizerRevision: string;
|
|
647
|
-
}
|
|
648
|
-
/** A value that the bundled tokenizer cannot safely count. */
|
|
649
|
-
interface UnavailableTokenCount {
|
|
650
|
-
readonly kind: 'unavailable';
|
|
651
|
-
readonly reason: string;
|
|
652
|
-
}
|
|
653
|
-
/** Best-effort estimate carrying a conservative upper value when available. */
|
|
654
|
-
interface TokenizerEstimateTokenCount {
|
|
655
|
-
readonly kind: 'tokenizer-estimate';
|
|
656
|
-
readonly tokens: number;
|
|
657
|
-
readonly upperBoundTokens: number;
|
|
658
|
-
readonly estimatorId: string;
|
|
659
|
-
readonly estimatorRevision: string;
|
|
660
|
-
readonly calibration?: Readonly<{
|
|
661
|
-
readonly sampleCount: number;
|
|
662
|
-
readonly conservativeMarginTokens: number;
|
|
663
|
-
}>;
|
|
664
|
-
}
|
|
665
|
-
/** Exact, conservative request estimate, or an explicit refusal. */
|
|
666
|
-
type TokenCount = ExactTokenizerTokenCount | TokenizerEstimateTokenCount | UnavailableTokenCount;
|
|
667
|
-
//#endregion
|
|
668
|
-
//#region src/runtime/measurement.d.ts
|
|
669
|
-
/** Request identity retained only when every dimension is publicly known. */
|
|
670
|
-
interface ProviderMeasurementKey {
|
|
671
|
-
readonly provider: string;
|
|
672
|
-
readonly baseUrlClass: string;
|
|
673
|
-
readonly apiRoute: string;
|
|
674
|
-
readonly modelId: string;
|
|
675
|
-
readonly requestTemplateRevision: string;
|
|
676
|
-
readonly tokenizerRevision: string;
|
|
677
|
-
readonly modality: string;
|
|
678
|
-
}
|
|
679
|
-
/** Rich request observation used by Adaptive when a future public API supplies it. */
|
|
680
|
-
interface ObservedPromptUsage {
|
|
681
|
-
readonly attemptId: string;
|
|
682
|
-
readonly providerRequestOrdinal: number;
|
|
683
|
-
readonly startedAtMs: number;
|
|
684
|
-
readonly completedAtMs: number;
|
|
685
|
-
readonly measurement: TokenCount;
|
|
686
|
-
readonly observedPromptTokens: number;
|
|
687
|
-
readonly observedOutputTokens?: number;
|
|
688
|
-
readonly responseModelId?: string;
|
|
689
|
-
readonly cacheStatus?: 'complete' | 'unknown';
|
|
690
|
-
readonly cacheReadTokens?: number;
|
|
691
|
-
readonly cacheMissTokens?: number;
|
|
692
|
-
readonly key?: ProviderMeasurementKey;
|
|
693
|
-
}
|
|
694
|
-
/**
|
|
695
|
-
* Intrinsic-grid diagnostic attached to nodes whose count estimates images.
|
|
696
|
-
* It reports ONLY the official block
|
|
697
|
-
* arithmetic evaluated on the attachment's intrinsic dimensions at the two
|
|
698
|
-
* alignment-padding extremes (compress-pad 0 and 3). It is NOT a request-token
|
|
699
|
-
* bound: the adapter may still re-project the image (per-route pixel-budget
|
|
700
|
-
* or image-detail overrides, byte-cap reprojection), which can move the real
|
|
701
|
-
* count below the diagnostic minimum. It never participates in exact gates,
|
|
702
|
-
* rewrite proofs, or any lossy decision.
|
|
703
|
-
*/
|
|
704
|
-
interface IntrinsicImageBlockDiagnostic {
|
|
705
|
-
readonly paddingMinimumTokens: number;
|
|
706
|
-
readonly paddingMaximumTokens: number;
|
|
707
|
-
}
|
|
708
|
-
/** One same-revision surface node with exact, estimated, or unavailable count. */
|
|
709
|
-
interface MeasuredTokenSurfaceNode {
|
|
710
|
-
readonly seq: number;
|
|
711
|
-
readonly count: TokenCount;
|
|
712
|
-
/**
|
|
713
|
-
* Authoritative decision metric for this node: character pressure in
|
|
714
|
-
* Unicode code points (same convention as SnapshotCandidate.characterPressure).
|
|
715
|
-
*/
|
|
716
|
-
readonly characterPressure: number;
|
|
717
|
-
/** Intrinsic-grid diagnostic when usable image dimensions were available. */
|
|
718
|
-
readonly intrinsicImageBlockEstimate?: IntrinsicImageBlockDiagnostic;
|
|
719
|
-
}
|
|
720
|
-
/** Compression view derived only from published Session and TokenMeter methods. */
|
|
721
|
-
interface CompactionTokenView extends TokenMeasurement {
|
|
722
|
-
readonly providerRoute?: string;
|
|
723
|
-
readonly modelId?: string;
|
|
724
|
-
readonly measuredNodes: readonly MeasuredTokenSurfaceNode[];
|
|
725
|
-
readonly currentSurface: TokenCount;
|
|
726
|
-
/**
|
|
727
|
-
* Exact character analogue of `currentSurface`: sum of per-node
|
|
728
|
-
* `characterPressure` over the same nodes `currentSurface` covers.
|
|
729
|
-
*/
|
|
730
|
-
readonly currentSurfaceChars: number;
|
|
731
|
-
/** Sum of per-node intrinsic padding minima; a diagnostic, not a token bound. */
|
|
732
|
-
readonly intrinsicImageBlockEstimateTokens: number;
|
|
733
|
-
readonly latestEnvelopeKey?: ProviderMeasurementKey;
|
|
734
|
-
readonly lastCompletedUsage?: ObservedPromptUsage;
|
|
735
|
-
countCanonicalText(text: string): TokenCount;
|
|
736
|
-
}
|
|
737
|
-
/**
|
|
738
|
-
* Capture one route-bound view without calling patched Harness methods.
|
|
739
|
-
* Official `measure()` remains authoritative for request pressure; the bundled
|
|
740
|
-
* tokenizer supplies exact canonical content counts used by safe rewrites.
|
|
741
|
-
*/
|
|
742
|
-
declare function measureForCompaction(ctx: Context, session: Session): CompactionTokenView;
|
|
743
|
-
//#endregion
|
|
744
770
|
//#region src/pruner.d.ts
|
|
745
771
|
declare module '@deepseek-ai/dsh-llm' {
|
|
746
772
|
interface MessageSourceMap {
|
|
@@ -754,6 +780,25 @@ declare module '@deepseek-ai/cordis' {
|
|
|
754
780
|
toolResultPruner: ToolResultPruner;
|
|
755
781
|
}
|
|
756
782
|
}
|
|
783
|
+
/**
|
|
784
|
+
* Cross-generation adaptation (0.2.0): the host `settings` service hands back
|
|
785
|
+
* the WHOLE entry config `{ settings: <subtree> }` instead of the settings
|
|
786
|
+
* subtree directly (older lines surfaced the subtree at the top level). The
|
|
787
|
+
* parser requires `profile`+`custom` at the root, so the wrapped shape threw
|
|
788
|
+
* "missing its complete shape" → the session failed open to `profile:'off'`,
|
|
789
|
+
* silently disabling fresh/aggregate on the desktop. Descend exactly one
|
|
790
|
+
* `{settings:<obj>}` wrapper ONLY when the root has no `profile`; every other
|
|
791
|
+
* shape is passed through untouched so the "exotic Host value must fail open
|
|
792
|
+
* to off" safety contract still holds.
|
|
793
|
+
*/
|
|
794
|
+
declare function unwrapSettingsEntry(value: unknown): unknown;
|
|
795
|
+
/**
|
|
796
|
+
* Whether a fresh candidate is BELOW the fresh trigger. Uses the real token
|
|
797
|
+
* count when an exact tokenizer is available (DeepSeek routes), and otherwise
|
|
798
|
+
* falls back to the 4.0 chars/token estimate — byte-for-byte the pre-existing
|
|
799
|
+
* behavior on routes without a bundled tokenizer (e.g. local Qwen).
|
|
800
|
+
*/
|
|
801
|
+
declare function freshBelowTrigger(candidate: SnapshotCandidate, policy: CompressionPolicy): boolean;
|
|
757
802
|
/** Mixed deterministic selector behind the existing `ctx.toolResultPruner` seam. */
|
|
758
803
|
declare class ToolResultPruner extends Service {
|
|
759
804
|
static inject: string[];
|
|
@@ -808,6 +853,12 @@ declare class ToolResultPruner extends Service {
|
|
|
808
853
|
* reads, so the default configuration adds exactly zero behavior.
|
|
809
854
|
*/
|
|
810
855
|
private postflightAdvisorPass;
|
|
856
|
+
/**
|
|
857
|
+
* 重进/重启会话的懒重建:首次观测某会话时,若账本尚无该会话条目,则按会话
|
|
858
|
+
* 日志投影播种(当前生效毛节省 + 每次重读惩罚 + 原文配对键)。只做一次,
|
|
859
|
+
* fail-open;同一进程内的实时增量随后在同一条目集合上继续。
|
|
860
|
+
*/
|
|
861
|
+
private ensureSavingsProjection;
|
|
811
862
|
/**
|
|
812
863
|
* Fire-and-forget postflight: evaluate the growth gate, and only when it
|
|
813
864
|
* passes, select the consumed increment OUTSIDE the protected working set,
|
|
@@ -908,4 +959,4 @@ declare class ToolResultPruner extends Service {
|
|
|
908
959
|
private warnOnce;
|
|
909
960
|
}
|
|
910
961
|
//#endregion
|
|
911
|
-
export { AUTO_COMPACT_THRESHOLD_LIMITS, type AutoCompactSettings, COMPRESSION_PROFILES, CONTEXT_COMPRESSION_SETTINGS_NAMESPACE, type CodeSkeletonSettings, type CompactionTokenView, type CompressionPolicy, type CompressionProfile, type ContextCompressionSettings, ContextCompressionSettingsSchema, type CustomCompressionBudget, type CustomCompressionPolicy, CustomCompressionPolicySchema, type CustomCompressionPolicyV1, type CustomCompressionPolicyV2, type CustomCompressionPolicyV3, type CustomCompressionUnit, type CustomHistoryPolicy, type CustomPolicyResolutionOptions, type CustomPrefixPolicy, type CustomTailTrimPolicy, DEFAULTS, DEFAULT_CUSTOM_COMPRESSION_POLICY, type HistoryMode, type MeasuredTokenSurfaceNode, PRUNE_MARKER, type PruneResult, type PruneSessionOptions, type PruneStage, type PrunedEntry, type ToolResultPruneConfig, ToolResultPruner, ToolResultPruner as default, codePointLength, historicalPlaceholder, isCompressionProfile, isValidAutoCompactThresholdPercent, measureForCompaction, normalizeTerminalText, parseContextCompressionSettings, reduceFreshToolResult, resolveConfig, resolveCustomPolicy, resolvePolicy, verifyReduction };
|
|
962
|
+
export { AUTO_COMPACT_THRESHOLD_LIMITS, type AutoCompactSettings, COMPRESSION_PROFILES, CONTEXT_COMPRESSION_SETTINGS_NAMESPACE, type CodeSkeletonSettings, type CompactionTokenView, type CompressionPolicy, type CompressionProfile, type ContextCompressionSettings, ContextCompressionSettingsSchema, type CustomCompressionBudget, type CustomCompressionPolicy, CustomCompressionPolicySchema, type CustomCompressionPolicyV1, type CustomCompressionPolicyV2, type CustomCompressionPolicyV3, type CustomCompressionUnit, type CustomHistoryPolicy, type CustomPolicyResolutionOptions, type CustomPrefixPolicy, type CustomTailTrimPolicy, DEFAULTS, DEFAULT_CUSTOM_COMPRESSION_POLICY, type HistoryMode, type MeasuredTokenSurfaceNode, PRUNE_MARKER, type PruneResult, type PruneSessionOptions, type PruneStage, type PrunedEntry, type ToolResultPruneConfig, ToolResultPruner, ToolResultPruner as default, codePointLength, freshBelowTrigger, historicalPlaceholder, isCompressionProfile, isValidAutoCompactThresholdPercent, measureForCompaction, normalizeTerminalText, parseContextCompressionSettings, reduceFreshToolResult, resolveConfig, resolveCustomPolicy, resolvePolicy, unwrapSettingsEntry, verifyReduction };
|