|
6 | 6 | type AuthEnvironmentScope, |
7 | 7 | } from "./auth.ts"; |
8 | 8 | import * as Effect from "effect/Effect"; |
| 9 | +import * as Option from "effect/Option"; |
9 | 10 | import * as Duration from "effect/Duration"; |
10 | 11 | import * as Schema from "effect/Schema"; |
11 | 12 | import * as SchemaTransformation from "effect/SchemaTransformation"; |
@@ -643,6 +644,34 @@ export type CodexSettings = typeof CodexSettings.Type; |
643 | 644 |
|
644 | 645 | const CLAUDE_AUTO_COMPACT_WINDOW_PATTERN = /^(?:|[1-9]\d{5}|1000000)$/; |
645 | 646 |
|
| 647 | +export const IDLE_COMPACTION_MODES = [ |
| 648 | + { value: "onReturn", label: "When you return" }, |
| 649 | + { value: "beforeCacheExpires", label: "Before the cache expires (beta)" }, |
| 650 | + { value: "off", label: "Never" }, |
| 651 | +] as const; |
| 652 | +export const IdleCompactionMode = Schema.Literals(IDLE_COMPACTION_MODES.map((mode) => mode.value)); |
| 653 | +export type IdleCompactionMode = typeof IdleCompactionMode.Type; |
| 654 | + |
| 655 | +// `compactIdleThreads` was a beta on/off switch for compacting before the cache expires. |
| 656 | +const LegacyCompactIdleThreads = Schema.Boolean.pipe( |
| 657 | + Schema.decodeTo( |
| 658 | + IdleCompactionMode, |
| 659 | + SchemaTransformation.transform({ |
| 660 | + decode: (enabled): IdleCompactionMode => (enabled ? "beforeCacheExpires" : "onReturn"), |
| 661 | + encode: (mode) => mode === "beforeCacheExpires", |
| 662 | + }), |
| 663 | + ), |
| 664 | +); |
| 665 | +const IdleCompactionModeSetting = Schema.Union([IdleCompactionMode, LegacyCompactIdleThreads]); |
| 666 | + |
| 667 | +const INSTANCE_IDLE_COMPACTION_MODES = [ |
| 668 | + { value: "default", label: "Use default" }, |
| 669 | + ...IDLE_COMPACTION_MODES, |
| 670 | +] as const; |
| 671 | +const InstanceIdleCompaction = Schema.Literals( |
| 672 | + INSTANCE_IDLE_COMPACTION_MODES.map((mode) => mode.value), |
| 673 | +); |
| 674 | + |
646 | 675 | export const ClaudeSettings = makeProviderSettingsSchema( |
647 | 676 | { |
648 | 677 | enabled: Schema.Boolean.pipe( |
@@ -694,9 +723,22 @@ export const ClaudeSettings = makeProviderSettingsSchema( |
694 | 723 | }, |
695 | 724 | }), |
696 | 725 | ), |
| 726 | + idleCompaction: InstanceIdleCompaction.pipe( |
| 727 | + Schema.withDecodingDefault(Effect.succeed("default" as const)), |
| 728 | + Schema.annotateKey({ |
| 729 | + title: "Compact idle threads", |
| 730 | + description: |
| 731 | + "When to compact an idle thread over 200,000 tokens: after you return, just before its prompt cache expires, or never. Overrides the default in General settings.", |
| 732 | + providerSettingsForm: { |
| 733 | + control: "select", |
| 734 | + options: INSTANCE_IDLE_COMPACTION_MODES, |
| 735 | + clearWhenEmpty: "omit", |
| 736 | + }, |
| 737 | + }), |
| 738 | + ), |
697 | 739 | }, |
698 | 740 | { |
699 | | - order: ["binaryPath", "homePath", "autoCompactWindow", "launchArgs"], |
| 741 | + order: ["binaryPath", "homePath", "autoCompactWindow", "idleCompaction", "launchArgs"], |
700 | 742 | }, |
701 | 743 | ); |
702 | 744 | export type ClaudeSettings = typeof ClaudeSettings.Type; |
@@ -1160,7 +1202,9 @@ export const ServerSettings = Schema.Struct({ |
1160 | 1202 | sidebarAutoSettleAfterDays: Schema.NullOr(SidebarAutoSettleAfterDays).pipe( |
1161 | 1203 | Schema.withDecodingDefault(Effect.succeed(DEFAULT_SIDEBAR_AUTO_SETTLE_AFTER_DAYS)), |
1162 | 1204 | ), |
1163 | | - compactIdleThreads: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))), |
| 1205 | + compactIdleThreads: IdleCompactionModeSetting.pipe( |
| 1206 | + Schema.withDecodingDefault(Effect.succeed("onReturn" as const)), |
| 1207 | + ), |
1164 | 1208 | snoozeLimitedThreads: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))), |
1165 | 1209 | autoResumeLimitedThreads: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))), |
1166 | 1210 | sidebarAutoSettleKeepPullRequests: Schema.Boolean.pipe( |
@@ -1334,6 +1378,22 @@ export const resolveProjectProviderInstanceEnabled = ( |
1334 | 1378 | : isUnconfiguredDefaultInstanceEnabled(instanceId); |
1335 | 1379 | }; |
1336 | 1380 |
|
| 1381 | +const decodeClaudeSettings = Schema.decodeUnknownOption(ClaudeSettings); |
| 1382 | + |
| 1383 | +export const resolveIdleCompaction = ( |
| 1384 | + settings: Pick<ServerSettings, "providerInstances" | "compactIdleThreads">, |
| 1385 | + instanceId: ProviderInstanceId, |
| 1386 | +): IdleCompactionMode => { |
| 1387 | + const instance = Object.hasOwn(settings.providerInstances, instanceId) |
| 1388 | + ? settings.providerInstances[instanceId] |
| 1389 | + : undefined; |
| 1390 | + const override = decodeClaudeSettings(instance?.config ?? {}).pipe( |
| 1391 | + Option.map((config) => config.idleCompaction), |
| 1392 | + Option.getOrElse(() => "default" as const), |
| 1393 | + ); |
| 1394 | + return override === "default" ? settings.compactIdleThreads : override; |
| 1395 | +}; |
| 1396 | + |
1337 | 1397 | export const ServerSettingsOperation = Schema.Literals([ |
1338 | 1398 | "normalize", |
1339 | 1399 | "check-exists", |
@@ -1475,7 +1535,7 @@ export const ServerSettingsPatch = Schema.Struct({ |
1475 | 1535 | deviceHosts: Schema.optionalKey(SshDeviceHostConfigs), |
1476 | 1536 | easDeviceHosts: Schema.optionalKey(EasDeviceHostConfigs), |
1477 | 1537 | sidebarAutoSettleAfterDays: Schema.optionalKey(Schema.NullOr(SidebarAutoSettleAfterDays)), |
1478 | | - compactIdleThreads: Schema.optionalKey(Schema.Boolean), |
| 1538 | + compactIdleThreads: Schema.optionalKey(IdleCompactionMode), |
1479 | 1539 | sidebarAutoSettleKeepPullRequests: Schema.optionalKey(Schema.Boolean), |
1480 | 1540 | sidebarAutoSettleOnMerge: Schema.optionalKey(Schema.Boolean), |
1481 | 1541 | autoResumeLimitedThreads: Schema.optionalKey(Schema.Boolean), |
|
0 commit comments