tool-result-compaction-config.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. export interface ToolResultCompactionSettings {
  2. /** Enables cumulative ToolResult archive/trim during automatic compaction. */
  3. readonly enabled: boolean;
  4. /** Per-result safety fuse applied before the result enters History. */
  5. readonly maxInlineKiB: number;
  6. /** Raw MCP detail safety fuse applied after Plugin hooks and before History. */
  7. readonly mcpDetailsMaxInlineKiB: number;
  8. /** Cumulative ToolResult text watermark for direct compaction. */
  9. readonly watermarkKiB: number;
  10. /** Minimum actual savings required before committing direct compaction. */
  11. readonly minSavingsKiB: number;
  12. /** Minimum size for one ToolResult to become a direct-compaction candidate. */
  13. readonly minCandidateKiB: number;
  14. /** Number of recent complete tool rounds protected from cumulative compaction. */
  15. readonly keepRecentRounds: number;
  16. }
  17. export const TOOL_RESULT_COMPACTION_DEFAULTS: Readonly<ToolResultCompactionSettings> = {
  18. enabled: true,
  19. maxInlineKiB: 64,
  20. mcpDetailsMaxInlineKiB: 32,
  21. watermarkKiB: 256,
  22. minSavingsKiB: 256,
  23. minCandidateKiB: 2,
  24. keepRecentRounds: 5,
  25. };
  26. const MAX_SAFE_KIB = Math.floor(Number.MAX_SAFE_INTEGER / 1_024);
  27. /** Resolves the user-facing config.yaml block without allowing unsafe byte conversion. */
  28. export function parseToolResultCompactionConfig(raw: unknown): ToolResultCompactionSettings {
  29. const value = isRecord(raw) ? raw : {};
  30. return {
  31. enabled:
  32. typeof value.enabled === 'boolean' ? value.enabled : TOOL_RESULT_COMPACTION_DEFAULTS.enabled,
  33. maxInlineKiB: positiveSafeKiB(value.maxInlineKiB, TOOL_RESULT_COMPACTION_DEFAULTS.maxInlineKiB),
  34. mcpDetailsMaxInlineKiB: positiveSafeKiB(
  35. value.mcpDetailsMaxInlineKiB,
  36. TOOL_RESULT_COMPACTION_DEFAULTS.mcpDetailsMaxInlineKiB,
  37. ),
  38. watermarkKiB: positiveSafeKiB(value.watermarkKiB, TOOL_RESULT_COMPACTION_DEFAULTS.watermarkKiB),
  39. minSavingsKiB: positiveSafeKiB(
  40. value.minSavingsKiB,
  41. TOOL_RESULT_COMPACTION_DEFAULTS.minSavingsKiB,
  42. ),
  43. minCandidateKiB: positiveSafeKiB(
  44. value.minCandidateKiB,
  45. TOOL_RESULT_COMPACTION_DEFAULTS.minCandidateKiB,
  46. ),
  47. keepRecentRounds: positiveSafeInteger(
  48. value.keepRecentRounds,
  49. TOOL_RESULT_COMPACTION_DEFAULTS.keepRecentRounds,
  50. ),
  51. };
  52. }
  53. function positiveSafeKiB(value: unknown, fallback: number): number {
  54. return typeof value === 'number' &&
  55. Number.isSafeInteger(value) &&
  56. value > 0 &&
  57. value <= MAX_SAFE_KIB
  58. ? value
  59. : fallback;
  60. }
  61. function positiveSafeInteger(value: unknown, fallback: number): number {
  62. return typeof value === 'number' && Number.isSafeInteger(value) && value > 0 ? value : fallback;
  63. }
  64. function isRecord(value: unknown): value is Readonly<Record<string, unknown>> {
  65. return typeof value === 'object' && value !== null && !Array.isArray(value);
  66. }