dsh-context-compression-improved 0.4.0-beta.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/CHANGELOG.ja.md +68 -36
  2. package/CHANGELOG.ko.md +67 -35
  3. package/CHANGELOG.md +195 -134
  4. package/CHANGELOG.zh.md +64 -36
  5. package/README.ja.md +1 -1
  6. package/README.ko.md +1 -1
  7. package/README.md +1 -1
  8. package/README.zh.md +1 -1
  9. package/docs/installation.ja.md +2 -2
  10. package/docs/installation.ko.md +2 -2
  11. package/docs/installation.md +103 -78
  12. package/docs/installation.zh.md +100 -77
  13. package/docs/repair-log.md +54 -0
  14. package/package.json +1 -1
  15. package/packages/selector/lib/{config.js → advisor-state.js} +329 -5
  16. package/packages/selector/lib/client.d.ts +7 -0
  17. package/packages/selector/lib/client.js +33 -3
  18. package/packages/selector/lib/index.d.ts +7 -0
  19. package/packages/selector/lib/index.js +112 -3
  20. package/packages/selector/lib/pruner.d.ts +128 -1
  21. package/packages/selector/lib/pruner.js +2802 -1374
  22. package/packages/selector/src/client/ReviewOverlay.tsx +1 -1
  23. package/packages/selector/src/client/index.ts +1 -1
  24. package/packages/selector/src/client/preset-options.ts +2 -0
  25. package/packages/selector/src/index.ts +129 -49
  26. package/packages/selector/src/profiles.ts +48 -0
  27. package/packages/selector/src/pruner/content.ts +18 -5
  28. package/packages/selector/src/pruner/state.ts +3 -0
  29. package/packages/selector/src/pruner/types.ts +23 -5
  30. package/packages/selector/src/pruner.ts +297 -162
  31. package/packages/selector/src/runtime/adaptive-cost.ts +23 -12
  32. package/packages/selector/src/runtime/audit.ts +40 -2
  33. package/packages/selector/src/runtime/config.ts +88 -1
  34. package/packages/selector/src/runtime/measurement.ts +31 -2
  35. package/packages/selector/src/runtime/reducers.ts +1115 -97
  36. package/packages/selector/src/runtime/tokenpilot/advisor-prompt.ts +188 -0
  37. package/packages/selector/src/runtime/tokenpilot/advisor-state.ts +133 -0
  38. package/packages/selector/src/runtime/tokenpilot/advisor.ts +419 -0
  39. package/packages/selector/src/runtime/tokenpilot/dedup.ts +1 -1
  40. package/packages/selector/src/runtime/tokenpilot/estimator.ts +8 -118
  41. package/packages/selector/src/runtime/tokenpilot/locator.ts +1 -1
  42. package/packages/selector/src/runtime/tokenpilot/proposal.ts +76 -32
  43. package/packages/selector/src/runtime/tokenpilot/read-state.ts +23 -2
  44. package/packages/selector/src/runtime/tokenpilot/review-registry.ts +117 -0
  45. package/packages/selector/src/runtime/tokenpilot/sidechannel.ts +303 -0
  46. package/packages/selector/src/runtime/toolclass.ts +103 -0
  47. package/packages/selector/src/runtime/types.ts +37 -0
  48. package/packages/selector/tests/advisor-report.host.spec.ts +223 -0
  49. package/packages/selector/tests/public/package-contract.client.spec.ts +2 -1
  50. package/packages/selector/tests/review-routes-registry.host.spec.ts +142 -0
  51. package/packages/selector/tests/runtime/adaptive-cost.spec.ts +7 -7
  52. package/packages/selector/tests/runtime/advisor-invariant.spec.ts +272 -0
  53. package/packages/selector/tests/runtime/advisor.spec.ts +226 -0
  54. package/packages/selector/tests/runtime/audit.spec.ts +88 -1
  55. package/packages/selector/tests/runtime/char-basis.spec.ts +30 -0
  56. package/packages/selector/tests/runtime/code-skeleton.spec.ts +14 -3
  57. package/packages/selector/tests/runtime/frequency-longstrings.spec.ts +74 -0
  58. package/packages/selector/tests/runtime/html-reducer.spec.ts +212 -0
  59. package/packages/selector/tests/runtime/line-mapping.spec.ts +153 -0
  60. package/packages/selector/tests/runtime/prose-reducers.spec.ts +133 -0
  61. package/packages/selector/tests/runtime/public/public-runtime.spec.ts +198 -27
  62. package/packages/selector/tests/runtime/read-input-cap.spec.ts +33 -0
  63. package/packages/selector/tests/runtime/search-reducer.spec.ts +110 -0
  64. package/packages/selector/tests/runtime/sidechannel.spec.ts +241 -0
  65. package/packages/selector/tests/runtime/toc-and-bundled.spec.ts +159 -0
  66. package/packages/selector/tests/runtime/tokenpilot/profile-baseline.spec.ts +12 -0
  67. package/packages/selector/tests/runtime/tokenpilot/proposal.spec.ts +194 -0
  68. package/packages/selector/tests/runtime/tokenpilot/pruner-review.spec.ts +70 -1
  69. package/packages/selector/tests/runtime/tokenpilot/read-state.spec.ts +24 -0
  70. package/packages/selector/tests/runtime/toolclass.spec.ts +156 -0
  71. package/scripts/toolclass-corpus-replay.mjs +281 -0
@@ -82,6 +82,20 @@ interface PresetOptions {
82
82
  readonly estimator: {
83
83
  readonly mode: '' | 'host' | 'direct';
84
84
  };
85
+ /**
86
+ * Advisory relevance advisor: statistics and suggestions only — every output
87
+ * (summaries, scores, decay, recertification) is observational and must never
88
+ * suppress, delay, or rewrite any reduction that would land. `''` (the
89
+ * default) keeps the advisor fully off.
90
+ */
91
+ readonly advisor: {
92
+ readonly mode: '' | 'host' | 'direct';
93
+ readonly timeoutMs: number;
94
+ readonly refreshTurns: number;
95
+ readonly scoreThreshold: number;
96
+ readonly sampleLimit: number;
97
+ readonly minTokens: number;
98
+ };
85
99
  /**
86
100
  * Human-gated review pipeline (beta): edge/high-impact candidates queue for
87
101
  * manual approval and execute in one merged batch at the next turn boundary
@@ -158,6 +172,13 @@ interface PresetOptionsSettings {
158
172
  readonly estimatorBaseUrl?: string;
159
173
  readonly estimatorApiKey?: string;
160
174
  readonly estimatorTimeoutMs?: number;
175
+ /** Advisory advisor channel; `''` (the default) keeps the advisor off. */
176
+ readonly advisorMode?: '' | 'host' | 'direct';
177
+ readonly advisorTimeoutMs?: number;
178
+ readonly advisorRefreshTurns?: number;
179
+ readonly advisorScoreThreshold?: number;
180
+ readonly advisorSampleLimit?: number;
181
+ readonly advisorMinTokens?: number;
161
182
  }
162
183
  /** Durable global preference exposed through `ctx.settings`. */
163
184
  interface ContextCompressionSettings {
@@ -200,6 +221,14 @@ interface ToolResultPruneConfig {
200
221
  historyKeepRecentTokens?: number;
201
222
  /** Minimum reclaim required before historical aging is worth a cache break. Profile default when omitted. */
202
223
  historyMinReclaimTokens?: number;
224
+ /**
225
+ * Read-class input cap in characters. Invariant (startup-asserted in
226
+ * `resolvePolicy`): when set it must exceed `freshTriggerTokens × 4.0` —
227
+ * the conservative chars/token upper bound — otherwise the cap sits below
228
+ * the fresh trigger and silently silences the fresh path for every read
229
+ * result. Unset profiles (host cap 50k–59.5k chars observed) stay untouched.
230
+ */
231
+ readInputCapChars?: number;
203
232
  /**
204
233
  * Auto Compact threshold percent frozen into this deployment by the preset
205
234
  * overlay generation (50–90 integer). When present it supersedes the live
@@ -228,6 +257,12 @@ interface CompressionPolicy {
228
257
  readonly historyKeepRecentToolCalls: number;
229
258
  readonly historyKeepRecentTokens: number;
230
259
  readonly historyMinReclaimTokens: number;
260
+ /**
261
+ * Read-class input cap in characters when configured; absent otherwise.
262
+ * Startup-asserted to exceed `freshTriggerTokens × 4.0` so it can never
263
+ * silently silence the fresh path (G2 invariant).
264
+ */
265
+ readonly readInputCapChars?: number;
231
266
  /**
232
267
  * Auto Compact token watermark `A = floor(C × a)` when the standard-profile
233
268
  * History linkage resolved for this Session; absent for Custom, Off, Native,
@@ -266,6 +301,8 @@ interface ResolvedConfig {
266
301
  readonly historyKeepRecentToolCalls?: number;
267
302
  readonly historyKeepRecentTokens?: number;
268
303
  readonly historyMinReclaimTokens?: number;
304
+ /** Read-class input cap in characters when configured; absent otherwise. */
305
+ readonly readInputCapChars?: number;
269
306
  /**
270
307
  * Auto Compact threshold percent frozen into this deployment by the preset
271
308
  * overlay generation (50-90 integer). Supersedes the live Host setting.
@@ -320,6 +357,56 @@ interface PruneResult {
320
357
  readonly tokensRemoved: number;
321
358
  }
322
359
  //#endregion
360
+ //#region src/runtime/tokenpilot/sidechannel.d.ts
361
+ /** Audit record of one side-channel call (Phase 13). All fields optional-safe. */
362
+ interface SideChannelAudit {
363
+ readonly ok: boolean;
364
+ readonly latencyMs: number;
365
+ /** host|direct plus the resolved provider/model identity. */
366
+ readonly channel?: string;
367
+ /** L2 coverage: content shown N of node content total M. */
368
+ readonly coverage?: {
369
+ readonly shown: number;
370
+ readonly total: number;
371
+ };
372
+ readonly reason?: string;
373
+ }
374
+ interface SideChannelRequest {
375
+ readonly system: string;
376
+ readonly user: string;
377
+ readonly signal: AbortSignal;
378
+ }
379
+ /** One bound side channel. `ask` resolves `undefined` on ANY failure. */
380
+ declare class SideChannel {
381
+ private readonly ctx;
382
+ private readonly options;
383
+ private readonly overrides?;
384
+ /**
385
+ * @param overrides - per-consumer overrides of the estimator-named options.
386
+ * The estimator itself never passes them (byte-identical behavior); the
387
+ * advisory advisor passes its own mode/timeout/output budget so both
388
+ * consumers share one transport without sharing one configuration.
389
+ */
390
+ constructor(ctx: Context, options: PresetOptionsSettings, overrides?: {
391
+ readonly mode?: "" | "host" | "direct";
392
+ readonly timeoutMs?: number;
393
+ readonly maxTokens?: number;
394
+ } | undefined);
395
+ private get mode();
396
+ get enabled(): boolean;
397
+ ask(request: SideChannelRequest): Promise<string | undefined>;
398
+ /** Failure-open wrapper that also records one audit record per call. */
399
+ askAudited(request: SideChannelRequest): Promise<{
400
+ text?: string;
401
+ audit: SideChannelAudit;
402
+ }>;
403
+ identity(): string | undefined;
404
+ /** Same host-route resolution as the estimator: explicit, then host default. */
405
+ private resolveHostRoute;
406
+ private askHost;
407
+ private askDirect;
408
+ }
409
+ //#endregion
323
410
  //#region src/runtime/tokenpilot/estimator.d.ts
324
411
  /** Per-session estimator failure bookkeeping for exponential backoff. */
325
412
  interface EstimatorFailures {
@@ -516,6 +603,8 @@ interface PrunerState {
516
603
  readonly reviewClocks: WeakMap<Session, number>;
517
604
  /** Estimator-reported remaining turns Ŝ per Session; advisory only. */
518
605
  readonly estimatorRemainingTurns: WeakMap<Session, number>;
606
+ /** Per-session advisor side channel, constructed once with the advisor overrides. */
607
+ readonly advisorChannels: WeakMap<Session, SideChannel>;
519
608
  /** Four-state outcome counters per Session (floating-window summary row). */
520
609
  readonly reviewSummaries: WeakMap<Session, ReviewSessionSummary>;
521
610
  }
@@ -622,13 +711,32 @@ interface ReducerOutput {
622
711
  readonly text: string;
623
712
  readonly reducer: string;
624
713
  readonly lossy: boolean;
714
+ /**
715
+ * Structured telemetry (task_4c/G7): how many ORIGINAL-event lines the
716
+ * reducer elided, when the reducer knows it. Never printed into `text` —
717
+ * host-side logging is what turns this into the compress→retrieve M/N ratio.
718
+ */
719
+ readonly elidedLines?: number;
720
+ }
721
+ /**
722
+ * Optional side-channel ranking (S1a/S1b) handed to the form-dispatched
723
+ * reducers. Selection and order ONLY — the mechanical fold stays the sole
724
+ * content authority, and `undefined` reproduces the mechanical output
725
+ * byte-for-byte. The ranking itself always comes from outside the reducers:
726
+ * the mechanical layer never calls a model.
727
+ */
728
+ interface ReductionRanking {
729
+ /** Search file paths, most relevant first (S1a). Unknown paths are ignored. */
730
+ readonly files?: readonly string[];
731
+ /** Document section heading texts, most relevant first (S1b). */
732
+ readonly sections?: readonly string[];
625
733
  }
626
734
  /**
627
735
  * Select a reducer from verified tool, command, and content evidence.
628
736
  * @param input - original result text, recovery source, and output budget.
629
737
  * @returns a verified candidate, or `null` when every reducer fails open.
630
738
  */
631
- declare function reduceFreshToolResult(input: ReducerInput): ReducerOutput | null;
739
+ declare function reduceFreshToolResult(input: ReducerInput, ranking?: ReductionRanking): ReducerOutput | null;
632
740
  /**
633
741
  * Build a recoverable placeholder for an old tool result.
634
742
  * @param input - tool identity, source reference, size, status, and retained evidence.
@@ -729,6 +837,11 @@ interface IntrinsicImageBlockDiagnostic {
729
837
  interface MeasuredTokenSurfaceNode {
730
838
  readonly seq: number;
731
839
  readonly count: TokenCount;
840
+ /**
841
+ * Authoritative decision metric for this node: character pressure in
842
+ * Unicode code points (same convention as SnapshotCandidate.characterPressure).
843
+ */
844
+ readonly characterPressure: number;
732
845
  /** Intrinsic-grid diagnostic when usable image dimensions were available. */
733
846
  readonly intrinsicImageBlockEstimate?: IntrinsicImageBlockDiagnostic;
734
847
  }
@@ -738,6 +851,11 @@ interface CompactionTokenView extends TokenMeasurement {
738
851
  readonly modelId?: string;
739
852
  readonly measuredNodes: readonly MeasuredTokenSurfaceNode[];
740
853
  readonly currentSurface: TokenCount;
854
+ /**
855
+ * Exact character analogue of `currentSurface`: sum of per-node
856
+ * `characterPressure` over the same nodes `currentSurface` covers.
857
+ */
858
+ readonly currentSurfaceChars: number;
741
859
  /** Sum of per-node intrinsic padding minima; a diagnostic, not a token bound. */
742
860
  readonly intrinsicImageBlockEstimateTokens: number;
743
861
  readonly latestEnvelopeKey?: ProviderMeasurementKey;
@@ -802,6 +920,15 @@ declare class ToolResultPruner extends Service {
802
920
  * superseded classification.
803
921
  */
804
922
  private postflightEstimatorPass;
923
+ /**
924
+ * Advisory advisor pass at the turn boundary, strictly fire-and-forget.
925
+ * Produces todolist-bound tail-task summaries, incremental relevance
926
+ * scores, and a prefix-decay figure — all observational. Every short
927
+ * circuit below (mode off, re-entry, cooldown, no task semantics, no
928
+ * direct endpoint) returns without touching any state the pruning chain
929
+ * reads, so the default configuration adds exactly zero behavior.
930
+ */
931
+ private postflightAdvisorPass;
805
932
  /**
806
933
  * The per-session review queue, or `undefined` while review mode is off
807
934
  * (every review path must then behave exactly like before).