Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/context-compact.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ context:
- `auto_compact.enabled`
控制是否启用基于 token 阈值的自动压缩;默认关闭。
- `auto_compact.input_token_threshold`
当会话累计输入 token 数达到此阈值时触发自动压缩;默认 100000。
当会话累计输入 token 数达到此阈值时触发自动压缩;默认 `0`(自动推导),推导失败时回退到 `fallback_input_token_threshold`(默认 `100000`)

## 自动压缩

Expand Down
2 changes: 2 additions & 0 deletions docs/guides/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ context:
| `context.compact.micro_compact_disabled` | 是否关闭默认启用的 micro compact |
| `context.auto_compact.enabled` | 是否启用自动压缩 |
| `context.auto_compact.input_token_threshold` | 自动压缩输入 token 阈值 |
| `context.auto_compact.reserve_tokens` | 自动阈值推导时预留 token 缓冲(`resolved_threshold = context_window - reserve_tokens`) |
| `context.auto_compact.fallback_input_token_threshold` | 自动推导失败时使用的保底阈值 |

### `runtime` 字段

Expand Down
2 changes: 1 addition & 1 deletion internal/runtime/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ func (s *Service) autoCompactThreshold(ctx context.Context, cfg config.Config) i
}
if s != nil && s.autoCompactThresholdResolver != nil {
threshold, err := s.autoCompactThresholdResolver.ResolveAutoCompactThreshold(ctx, cfg)
if err == nil {
if err == nil && threshold > 0 {
return threshold
}
}
Expand Down
52 changes: 52 additions & 0 deletions internal/runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4297,6 +4297,58 @@ func TestAutoCompactThresholdFallsBackWhenResolverErrors(t *testing.T) {
}
}

func TestAutoCompactThresholdFallsBackWhenResolverReturnsZeroWithoutError(t *testing.T) {
t.Parallel()

service := &Service{}
service.SetAutoCompactThresholdResolver(autoCompactThresholdResolverFunc(
func(ctx context.Context, cfg config.Config) (int, error) {
return 0, nil
},
))

cfg := config.Config{
Context: config.ContextConfig{
AutoCompact: config.AutoCompactConfig{
Enabled: true,
InputTokenThreshold: 0,
FallbackInputTokenThreshold: 88000,
},
},
}

threshold := service.autoCompactThreshold(context.Background(), cfg)
if threshold != 88000 {
t.Fatalf("expected fallback threshold == 88000, got %d", threshold)
}
}

func TestAutoCompactThresholdFallsBackWhenResolverReturnsNegativeWithoutError(t *testing.T) {
t.Parallel()

service := &Service{}
service.SetAutoCompactThresholdResolver(autoCompactThresholdResolverFunc(
func(ctx context.Context, cfg config.Config) (int, error) {
return -1, nil
},
))

cfg := config.Config{
Context: config.ContextConfig{
AutoCompact: config.AutoCompactConfig{
Enabled: true,
InputTokenThreshold: 0,
FallbackInputTokenThreshold: 88000,
},
},
}

threshold := service.autoCompactThreshold(context.Background(), cfg)
if threshold != 88000 {
t.Fatalf("expected fallback threshold == 88000, got %d", threshold)
}
}

func TestAutoCompactThresholdImplicitModeWithoutResolverUsesFallback(t *testing.T) {
t.Parallel()

Expand Down
Loading