dsh-context-compression-improved 0.9.3 → 0.9.7

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.
Files changed (66) hide show
  1. package/README.de.md +3 -3
  2. package/README.es.md +3 -3
  3. package/README.fr.md +3 -3
  4. package/README.it.md +3 -3
  5. package/README.ja.md +16 -3
  6. package/README.ko.md +16 -3
  7. package/README.md +16 -3
  8. package/README.ru.md +3 -3
  9. package/README.zh.md +16 -3
  10. package/assets/board.png +0 -0
  11. package/assets/context.png +0 -0
  12. package/docs/installation.de.md +2 -2
  13. package/docs/installation.es.md +2 -2
  14. package/docs/installation.fr.md +2 -2
  15. package/docs/installation.it.md +2 -2
  16. package/docs/installation.ja.md +2 -2
  17. package/docs/installation.ko.md +2 -2
  18. package/docs/installation.md +2 -2
  19. package/docs/installation.ru.md +2 -2
  20. package/docs/installation.zh.md +2 -2
  21. package/package.json +7 -6
  22. package/packages/selector/dsh.plugin.json +2 -2
  23. package/packages/selector/lib/client.d.ts +1 -0
  24. package/packages/selector/lib/client.js +63 -10
  25. package/packages/selector/lib/config.js +71 -7
  26. package/packages/selector/lib/index.js +145 -20
  27. package/packages/selector/lib/pruner.d.ts +158 -107
  28. package/packages/selector/lib/pruner.js +332 -22
  29. package/packages/selector/lib/tail-trim.js +25 -2
  30. package/packages/selector/package.json +22 -21
  31. package/packages/selector/src/client/index.ts +29 -2
  32. package/packages/selector/src/client/locales/de.ts +1 -0
  33. package/packages/selector/src/client/locales/es.ts +1 -0
  34. package/packages/selector/src/client/locales/fr.ts +1 -0
  35. package/packages/selector/src/client/locales/it.ts +1 -0
  36. package/packages/selector/src/client/locales/ja.ts +1 -0
  37. package/packages/selector/src/client/locales/ko.ts +1 -0
  38. package/packages/selector/src/client/locales/ru.ts +1 -0
  39. package/packages/selector/src/client/locales.ts +2 -0
  40. package/packages/selector/src/client/monitor-fab.ts +19 -4
  41. package/packages/selector/src/compat/llm-messages.ts +50 -0
  42. package/packages/selector/src/index.ts +27 -0
  43. package/packages/selector/src/preset-overlay.ts +145 -22
  44. package/packages/selector/src/pruner/content.ts +1 -1
  45. package/packages/selector/src/pruner/state.ts +4 -0
  46. package/packages/selector/src/pruner.ts +109 -16
  47. package/packages/selector/src/runtime/config.ts +1 -1
  48. package/packages/selector/src/runtime/reducers.ts +81 -1
  49. package/packages/selector/src/runtime/retrieve.ts +4 -3
  50. package/packages/selector/src/runtime/savings-projection.ts +226 -0
  51. package/packages/selector/src/runtime/savings.ts +83 -3
  52. package/packages/selector/src/runtime/tail-trim.ts +2 -1
  53. package/packages/selector/tests/public/package-contract.client.spec.ts +4 -4
  54. package/packages/selector/tests/runtime/fresh-gate.spec.ts +43 -0
  55. package/packages/selector/tests/runtime/jsonl-reducer.spec.ts +79 -0
  56. package/packages/selector/tests/runtime/public/public-runtime.spec.ts +48 -0
  57. package/packages/selector/tests/runtime/savings-projection.spec.ts +89 -0
  58. package/packages/selector/tests/runtime/savings.spec.ts +22 -3
  59. package/packages/selector/tests/runtime/settings-unwrap.spec.ts +58 -0
  60. package/packages/selector/tests/runtime/tokenpilot/profile-baseline.spec.ts +5 -2
  61. package/pnpm-workspace.yaml +6 -0
  62. package/screenshots.json +6 -0
  63. package/scripts/hosts.mjs +5 -2
  64. package/scripts/packed-install-e2e.mjs +19 -3
  65. package/scripts/sync-hosts.mjs +2 -2
  66. package/scripts/verify-release.mjs +6 -6
@@ -1,7 +1,7 @@
1
1
  import z from "@deepseek-ai/schemastery";
2
2
  import { Context, Service } from "@deepseek-ai/cordis";
3
+ import { Session, SessionEvent } from "@deepseek-ai/dsh-session";
3
4
  import { ContentBlock, ToolCallId } from "@deepseek-ai/dsh-llm";
4
- import { Session } 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 };