dsh-context-compression-improved 0.6.4 → 0.7.0-beta.1

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 (75) hide show
  1. package/CHANGELOG.ja.md +51 -0
  2. package/CHANGELOG.ko.md +47 -0
  3. package/CHANGELOG.md +72 -0
  4. package/CHANGELOG.zh.md +47 -0
  5. package/README.ja.md +2 -2
  6. package/README.ko.md +2 -2
  7. package/README.md +6 -3
  8. package/README.zh.md +3 -2
  9. package/eslint.config.js +76 -30
  10. package/package.json +164 -162
  11. package/packages/selector/dsh.plugin.json +16 -16
  12. package/packages/selector/lib/client.js +16 -4
  13. package/packages/selector/lib/index.js +92 -1
  14. package/packages/selector/lib/{advisor-state.js → intent-gate.js} +91 -3
  15. package/packages/selector/lib/profiles.d.ts +9 -0
  16. package/packages/selector/lib/pruner.d.ts +59 -0
  17. package/packages/selector/lib/pruner.js +410 -8
  18. package/packages/selector/package.json +114 -114
  19. package/packages/selector/src/client/decode.ts +6 -2
  20. package/packages/selector/src/client/index.ts +213 -232
  21. package/packages/selector/src/client/scope-face.ts +24 -24
  22. package/packages/selector/src/deepseek-v4-tokenizer.ts +1 -1
  23. package/packages/selector/src/index.ts +418 -412
  24. package/packages/selector/src/preset-overlay.ts +3 -0
  25. package/packages/selector/src/profiles.ts +30 -10
  26. package/packages/selector/src/pruner/content.ts +1 -1
  27. package/packages/selector/src/pruner/state.ts +7 -0
  28. package/packages/selector/src/pruner.ts +253 -10
  29. package/packages/selector/src/runtime/audit.ts +17 -0
  30. package/packages/selector/src/runtime/config.ts +36 -11
  31. package/packages/selector/src/runtime/reducers.ts +1 -1
  32. package/packages/selector/src/runtime/summary-command.ts +133 -0
  33. package/packages/selector/src/runtime/tokenpilot/advisor-prompt.ts +46 -1
  34. package/packages/selector/src/runtime/tokenpilot/advisor-state.ts +55 -0
  35. package/packages/selector/src/runtime/tokenpilot/intent-fold.ts +53 -0
  36. package/packages/selector/src/runtime/tokenpilot/intent-gate.ts +65 -0
  37. package/packages/selector/src/runtime/tokenpilot/intent-input-mask.ts +125 -0
  38. package/packages/selector/src/runtime/tokenpilot/intent-range.ts +37 -0
  39. package/packages/selector/src/runtime/tokenpilot/locator.ts +2 -2
  40. package/packages/selector/src/runtime/tokenpilot/sidechannel.ts +4 -1
  41. package/packages/selector/src/runtime/types.ts +12 -0
  42. package/packages/selector/tests/auto-compact.client.spec.tsx +214 -213
  43. package/packages/selector/tests/built/client-artifact.spec.ts +2 -0
  44. package/packages/selector/tests/code-skeleton.client.spec.ts +94 -93
  45. package/packages/selector/tests/custom-contract.client.spec.ts +3 -2
  46. package/packages/selector/tests/estimator-channel.client.spec.tsx +252 -252
  47. package/packages/selector/tests/helpers/legacy-namespace.ts +8 -4
  48. package/packages/selector/tests/helpers/legacy-settings/index.ts +15 -0
  49. package/packages/selector/tests/helpers/legacy-settings/legacy/provider.d.ts +41 -0
  50. package/packages/selector/tests/helpers/legacy-settings/legacy/provider.js +647 -0
  51. package/packages/selector/tests/helpers/legacy-settings/legacy/redact.js +78 -0
  52. package/packages/selector/tests/helpers/legacy-settings/legacy/util-values.js +234 -0
  53. package/packages/selector/tests/host-preset-overlay.host.spec.ts +204 -206
  54. package/packages/selector/tests/preset-options-write.client.spec.ts +188 -187
  55. package/packages/selector/tests/preset-overlay-loader.e2e.host.spec.ts +200 -206
  56. package/packages/selector/tests/profiles.client.spec.tsx +436 -434
  57. package/packages/selector/tests/public/package-contract.client.spec.ts +78 -78
  58. package/packages/selector/tests/runtime/advice-never-withholds.host.spec.ts +230 -233
  59. package/packages/selector/tests/runtime/audit.spec.ts +275 -274
  60. package/packages/selector/tests/runtime/auto-compact-config.spec.ts +3 -3
  61. package/packages/selector/tests/runtime/deepseek-v4-tokenizer.spec.ts +4 -4
  62. package/packages/selector/tests/runtime/intent-gate.spec.ts +62 -0
  63. package/packages/selector/tests/runtime/intent-summary.spec.ts +156 -0
  64. package/packages/selector/tests/runtime/public/public-runtime.spec.ts +2736 -2727
  65. package/packages/selector/tests/runtime/summary-command.spec.ts +154 -0
  66. package/packages/selector/tests/settings-seat.client.spec.ts +165 -165
  67. package/packages/selector/tests/standing-generation.host.spec.ts +704 -704
  68. package/packages/selector/tests/subagent-cache-reuse.host.spec.ts +18 -2
  69. package/packages/selector/tests/support/ui-primitives.tsx +34 -34
  70. package/pnpm-workspace.yaml +1 -1
  71. package/scripts/packed-components-smoke.mjs +2 -0
  72. package/scripts/packed-install-e2e.mjs +1275 -1273
  73. package/scripts/verify-release.mjs +318 -317
  74. package/tsconfig.tests.json +3 -1
  75. package/vitest.config.ts +14 -0
@@ -373,6 +373,20 @@ function parseCodeSkeletonSettings(value) {
373
373
  return { enabled };
374
374
  }
375
375
  /**
376
+ * Strictly parse the persisted intentSummary section. Mirrors the codeSkeleton
377
+ * section semantics: absent inherits the lossless `false` default, while a
378
+ * present-but-invalid section is an explicitly invalid document.
379
+ */
380
+ function parseIntentSummarySettings(value) {
381
+ if (value === void 0) return { enabled: false };
382
+ if (!isPlainRecord(value)) throw new TypeError("Context-compression intentSummary must be a plain object");
383
+ const keys = Object.keys(value);
384
+ if (keys.length !== 1 || keys[0] !== "enabled") throw new TypeError(`Context-compression intentSummary: expected exactly "enabled", got "${keys.join("\", \"")}"`);
385
+ const enabled = value.enabled;
386
+ if (typeof enabled !== "boolean") throw new TypeError("Context-compression intentSummary.enabled must be a boolean");
387
+ return { enabled };
388
+ }
389
+ /**
376
390
  * Parse the optional tokenpilot-inspired preset sub-capability section. Absent
377
391
  * inherits the preset defaults; present-but-invalid is rejected, mirroring the
378
392
  * codeSkeleton section semantics.
@@ -479,7 +493,8 @@ const DEFAULT_CONTEXT_COMPRESSION_SETTINGS = {
479
493
  profile: "balanced",
480
494
  custom: structuredClone(DEFAULT_CUSTOM_COMPRESSION_POLICY),
481
495
  autoCompact: { thresholdPercent: AUTO_COMPACT_THRESHOLD_LIMITS.default },
482
- codeSkeleton: { enabled: false }
496
+ codeSkeleton: { enabled: false },
497
+ intentSummary: { enabled: false }
483
498
  };
484
499
  /**
485
500
  * Parse one settings document with the persisted-section semantics: `undefined`
@@ -497,17 +512,19 @@ const ContextCompressionSettingsSchema = z.transform(z.any().required(), (value)
497
512
  if (!isPlainRecord(value)) throw new TypeError("Context-compression settings must be a plain object");
498
513
  assertPlainDataTree(value);
499
514
  const candidate = structuredClone(value);
500
- const unknown = Object.keys(candidate).find((key) => key !== "profile" && key !== "custom" && key !== "autoCompact" && key !== "codeSkeleton" && key !== "presetOptions");
515
+ const unknown = Object.keys(candidate).find((key) => key !== "profile" && key !== "custom" && key !== "autoCompact" && key !== "codeSkeleton" && key !== "intentSummary" && key !== "presetOptions");
501
516
  if (unknown !== void 0) throw new TypeError(`Context-compression settings: unknown key "${unknown}"`);
502
517
  assertPresentSection(candidate, "profile", isSupportedProfile);
503
518
  assertPresentSection(candidate, "custom", isUsableCustomDocument);
504
519
  const autoCompact = parseAutoCompactSettings(candidate.autoCompact);
505
520
  const codeSkeleton = parseCodeSkeletonSettings(candidate.codeSkeleton);
521
+ const intentSummary = parseIntentSummarySettings(candidate.intentSummary);
506
522
  const presetOptions = parsePresetOptionsSettings(candidate.presetOptions);
507
523
  return {
508
524
  ...contextCompressionSettingsInputSchema(candidate),
509
525
  autoCompact,
510
526
  codeSkeleton,
527
+ intentSummary,
511
528
  ...presetOptions === void 0 ? {} : { presetOptions }
512
529
  };
513
530
  }).default(DEFAULT_CONTEXT_COMPRESSION_SETTINGS);
@@ -958,5 +975,76 @@ function invalidateOnTaskChange(state, todoVersion) {
958
975
  state.lastSummaryTurn = -1;
959
976
  return true;
960
977
  }
978
+ /**
979
+ * `/ctx-summary off|on` overrides, keyed by session id (string). The command
980
+ * handler only sees `invocation.agent.session.id`, not the pruner's Session
981
+ * object, so these are id-keyed rather than WeakMap'd. Entries are two-char
982
+ * strings per session and cleared whenever the override is lifted.
983
+ */
984
+ const summaryOverrides = /* @__PURE__ */ new Map();
985
+ /** Read the session's `/ctx-summary` override; `undefined` = default (settings-driven). */
986
+ function getSummaryOverride(sessionId) {
987
+ return summaryOverrides.get(sessionId);
988
+ }
989
+ /** Set or clear (`undefined`) the session's `/ctx-summary` override. */
990
+ function setSummaryOverride(sessionId, value) {
991
+ if (value === void 0) summaryOverrides.delete(sessionId);
992
+ else summaryOverrides.set(sessionId, value);
993
+ }
994
+ /** Last observed `intentSummary.enabled` for a session, for `/ctx-summary status`. */
995
+ const lastObservedIntentEnabled = /* @__PURE__ */ new Map();
996
+ /** Record the gate input the pass last evaluated for this session. */
997
+ function observeIntentEnabled(sessionId, enabled) {
998
+ lastObservedIntentEnabled.set(sessionId, enabled);
999
+ }
1000
+ /** The settings-side enabled flag last observed for the session, if any. */
1001
+ function getObservedIntentEnabled(sessionId) {
1002
+ return lastObservedIntentEnabled.get(sessionId);
1003
+ }
1004
+ const lastIntentFolds = /* @__PURE__ */ new Map();
1005
+ /** Record a landed intent fold for the session (newest wins). */
1006
+ function recordIntentFold(sessionId, record) {
1007
+ lastIntentFolds.set(sessionId, record);
1008
+ }
1009
+ /** The most recent landed intent fold for the session, if any. */
1010
+ function getLastIntentFold(sessionId) {
1011
+ return lastIntentFolds.get(sessionId);
1012
+ }
1013
+ //#endregion
1014
+ //#region src/runtime/tokenpilot/intent-gate.ts
1015
+ /**
1016
+ * Turn-tail intent-summary growth gate (TokenPilot-inspired E2).
1017
+ *
1018
+ * Pure decision helper evaluated at the turn-boundary postflight: it decides
1019
+ * whether this turn may spend a summary-writer LLM call and stage a fold for
1020
+ * the next pressure round. The gate is deliberately conservative — every
1021
+ * unresolved input (unknown context window, non-finite counters) fails closed
1022
+ * toward "do not run", because a skipped fold is free while a wasted summary
1023
+ * call is not. Content safety is unaffected either way: fail-open semantics
1024
+ * live in the fold landing path, not here.
1025
+ */
1026
+ /** Floor: the live surface must exceed this fraction of the context window. */
1027
+ const INTENT_GATE_FLOOR_FRACTION = .45;
1028
+ /** Growth: the live surface must have grown by more than this since the last landed fold. */
1029
+ const INTENT_GATE_GROWTH_TOKENS = 5e4;
1030
+ function isUsableNumber(value) {
1031
+ return Number.isFinite(value) && value >= 0;
1032
+ }
1033
+ /** Same evaluation with the reason attached, for audits and `/ctx-summary status`. */
1034
+ function evaluateIntentGate(input) {
1035
+ const snapshot = (decision, reason) => ({
1036
+ ...input,
1037
+ decision,
1038
+ reason
1039
+ });
1040
+ if (!input.enabled) return snapshot(false, "disabled");
1041
+ if (input.override === "off") return snapshot(false, "override-off");
1042
+ if (!isUsableNumber(input.liveTokens)) return snapshot(false, "below-floor");
1043
+ if (input.contextWindow === void 0 || !isUsableNumber(input.contextWindow)) return snapshot(false, "no-window");
1044
+ if (!(input.liveTokens > input.contextWindow * .45)) return snapshot(false, "below-floor");
1045
+ if (!isUsableNumber(input.baselineTokens)) return snapshot(false, "below-growth");
1046
+ if (!(input.liveTokens - input.baselineTokens > 5e4)) return snapshot(false, "below-growth");
1047
+ return snapshot(true, "passed");
1048
+ }
961
1049
  //#endregion
962
- export { deepFreeze as C, assertNever as S, resolveConfig as _, AUTO_COMPACT_THRESHOLD_LIMITS as a, DEFAULT_CUSTOM_COMPRESSION_POLICY as b, DEFAULTS as c, charsForTokens as d, charsToTokens as f, parseContextCompressionSettings as g, isValidAutoCompactThresholdPercent as h, recordScore as i, DEFAULT_CONTEXT_COMPRESSION_SETTINGS as l, isCompressionProfile as m, invalidateOnTaskChange as n, CONTEXT_COMPRESSION_SETTINGS_NAMESPACE as o, codePointLength as p, recordRecertified as r, ContextCompressionSettingsSchema as s, getAdvisorState as t, PRUNE_MARKER as u, resolvePolicy as v, COMPRESSION_PROFILES as w, resolveCustomPolicy as x, CustomCompressionPolicySchema as y };
1050
+ export { resolveCustomPolicy as A, isCompressionProfile as C, resolvePolicy as D, resolveConfig as E, deepFreeze as M, COMPRESSION_PROFILES as N, CustomCompressionPolicySchema as O, codePointLength as S, parseContextCompressionSettings as T, DEFAULTS as _, getLastIntentFold as a, charsForTokens as b, invalidateOnTaskChange as c, recordRecertified as d, recordScore as f, ContextCompressionSettingsSchema as g, CONTEXT_COMPRESSION_SETTINGS_NAMESPACE as h, getAdvisorState as i, assertNever as j, DEFAULT_CUSTOM_COMPRESSION_POLICY as k, observeIntentEnabled as l, AUTO_COMPACT_THRESHOLD_LIMITS as m, INTENT_GATE_GROWTH_TOKENS as n, getObservedIntentEnabled as o, setSummaryOverride as p, evaluateIntentGate as r, getSummaryOverride as s, INTENT_GATE_FLOOR_FRACTION as t, recordIntentFold as u, DEFAULT_CONTEXT_COMPRESSION_SETTINGS as v, isValidAutoCompactThresholdPercent as w, charsToTokens as x, PRUNE_MARKER as y };
@@ -68,6 +68,13 @@ interface AutoCompactSettings {
68
68
  interface CodeSkeletonSettings {
69
69
  enabled: boolean;
70
70
  }
71
+ /**
72
+ * Orthogonal turn-tail intent-summary gate, mirrored browser-safe from the
73
+ * runtime: independent of every profile, default off.
74
+ */
75
+ interface IntentSummarySettings {
76
+ enabled: boolean;
77
+ }
71
78
  /** Browser-safe mirror of the runtime presetOptions section. */
72
79
  interface PresetOptionsSettings {
73
80
  readonly dedupeToolResults?: boolean;
@@ -98,6 +105,8 @@ interface ContextCompressionSettings {
98
105
  autoCompact: AutoCompactSettings;
99
106
  /** Code-skeleton reducer gate captured independently of `profile`. */
100
107
  codeSkeleton: CodeSkeletonSettings;
108
+ /** Turn-tail intent-summary gate captured independently of `profile`. */
109
+ intentSummary: IntentSummarySettings;
101
110
  /** Optional tokenpilot-inspired sub-capability overrides (presence-validated only). */
102
111
  presetOptions?: PresetOptionsSettings;
103
112
  }
@@ -134,6 +134,15 @@ interface AutoCompactSettings {
134
134
  interface CodeSkeletonSettings {
135
135
  enabled: boolean;
136
136
  }
137
+ /**
138
+ * Orthogonal turn-tail intent-summary gate. Independent of every profile:
139
+ * when enabled, the turn-boundary growth gate may fold consumed-increment
140
+ * tool results into an LLM-written intent block (skeleton + verbatim error
141
+ * lines are kept; the model only ever writes the summary segment).
142
+ */
143
+ interface IntentSummarySettings {
144
+ enabled: boolean;
145
+ }
137
146
  /**
138
147
  * Persisted sub-capability overrides for the `tokenpilot-inspired` preset.
139
148
  * Absent fields inherit the preset defaults; the section is only meaningful
@@ -173,6 +182,8 @@ interface ContextCompressionSettings {
173
182
  autoCompact: AutoCompactSettings;
174
183
  /** Code-skeleton reducer gate snapped independently of `profile`. */
175
184
  codeSkeleton: CodeSkeletonSettings;
185
+ /** Turn-tail intent-summary gate snapped independently of `profile`. */
186
+ intentSummary: IntentSummarySettings;
176
187
  /** Optional tokenpilot-inspired sub-capability overrides; absent inherits preset defaults. */
177
188
  presetOptions?: PresetOptionsSettings;
178
189
  }
@@ -397,6 +408,32 @@ interface EstimatorFailures {
397
408
  cooldownUntil: number;
398
409
  }
399
410
  //#endregion
411
+ //#region src/runtime/tokenpilot/intent-input-mask.d.ts
412
+ type IntentRole = 'read-mask' | 'write-keep' | 'conservative';
413
+ //#endregion
414
+ //#region src/runtime/tokenpilot/intent-fold.d.ts
415
+ /** One staged fold awaiting the next pressure round. */
416
+ interface PendingIntentFold {
417
+ readonly createdAt: number;
418
+ readonly turn: number;
419
+ /** Inclusive seq span of the folded candidates. */
420
+ readonly startSeq: number;
421
+ readonly endSeq: number;
422
+ /** LLM-written summary segment (fail-open parsed, char-capped). */
423
+ readonly summary: string;
424
+ /** Verbatim error lines collected across the batch (never LLM-revoiced). */
425
+ readonly errorLines: readonly string[];
426
+ /** Per-candidate faces: read-mask single lines ride the block, keep-class lines prompt-only. */
427
+ readonly records: ReadonlyArray<{
428
+ readonly seq: number;
429
+ readonly role: IntentRole;
430
+ readonly toolName: string;
431
+ readonly line: string;
432
+ }>;
433
+ /** Characters consumed by the summary-writer call (credit accounting). */
434
+ readonly summaryCallChars: number;
435
+ }
436
+ //#endregion
400
437
  //#region src/pruner/state.d.ts
401
438
  /** Mutable per-session state bag used inside {@link ToolResultPruner}. */
402
439
  interface PrunerState {
@@ -430,6 +467,12 @@ interface PrunerState {
430
467
  readonly estimatorRemainingTurns: WeakMap<Session, number>;
431
468
  /** Per-session advisor side channel, constructed once with the advisor overrides. */
432
469
  readonly advisorChannels: WeakMap<Session, SideChannel>;
470
+ /** Staged intent fold awaiting the next pressure round (at most one per session). */
471
+ readonly pendingIntentFolds: WeakMap<Session, PendingIntentFold>;
472
+ /** Landed intent-fold seqs: fold-once, never reconsidered by any later stage. */
473
+ readonly intentFoldedSeqs: WeakMap<Session, Set<number>>;
474
+ /** Live-surface tokens when the last intent fold landed (growth-gate baseline). */
475
+ readonly intentBaselines: WeakMap<Session, number>;
433
476
  }
434
477
  //#endregion
435
478
  //#region src/runtime/custom-policy.d.ts
@@ -759,6 +802,22 @@ declare class ToolResultPruner extends Service {
759
802
  * reads, so the default configuration adds exactly zero behavior.
760
803
  */
761
804
  private postflightAdvisorPass;
805
+ /**
806
+ * Fire-and-forget postflight: evaluate the growth gate, and only when it
807
+ * passes, select the consumed increment OUTSIDE the protected working set,
808
+ * mask it to semantic-role records, and spend ONE summary-writer call. The
809
+ * verdict stages as a pending fold that the NEXT pressure round lands via
810
+ * the ordinary plan/apply machinery — the synchronous decision path never
811
+ * waits on an LLM.
812
+ */
813
+ private postflightIntentFoldPass;
814
+ /**
815
+ * Convert a staged pending fold into per-candidate planned replacements
816
+ * (reducer `intent-summary`). Fail-open: a candidate that would grow, is
817
+ * rich-content, or no longer exists keeps its original text. Returns
818
+ * `undefined` when there is nothing (or no longer anything) to fold.
819
+ */
820
+ private applyPendingIntentFold;
762
821
  /**
763
822
  * Monotonic per-session turn clock for advisory records. Bumped by the agent
764
823
  * loop payloads (`pre-step`); passes without a turn coordinate reuse the last