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
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,12 @@ github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66D
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmSxCcxctByoQdvwPiA7DTK7jaaFDBTtu0ic=
github.com/go-git/go-billy/v5 v5.9.1 h1:8U73XiOTfINdItHVa6z4Gv7ToObcZ6grkqQbLryLCdA=
github.com/go-git/go-billy/v5 v5.9.1/go.mod h1:ExsU+jcGwXTBOnyilvAnEM1wug1IxHr4yP2ZXsNRtV0=
github.com/go-git/go-git/v5 v5.19.1 h1:nX27AnaU43/K5bKktKwgBmR9lawoYVe1Ckg0rgzzN00=
github.com/go-git/go-git/v5 v5.19.1/go.mod h1:Pb1v0c7/g8aGQJwx9Us09W85yGoyvSwuhEGMH7zjDKQ=
github.com/go-git/go-git/v5 v5.19.2 h1:wkfn7vOlUBu8ivAWKBWisTiwJK4jYHzTF8Ndv1LyGqY=
github.com/go-git/go-git/v5 v5.19.2/go.mod h1:QqCBE1EFN5ddFmrliLQ3/ntRCUjZU3EJuwuB/jWEHjk=
github.com/go-quicktest/qt v1.101.0 h1:O1K29Txy5P2OK0dGo59b7b0LR6wKfIhttaAhHUyn7eI=
github.com/go-quicktest/qt v1.101.0/go.mod h1:14Bz/f7NwaXPtdYEgzsx46kqSxVwTbzVZsDC26tQJow=
github.com/go-viper/mapstructure/v2 v2.5.0 h1:vM5IJoUAy3d7zRSVtIwQgBj7BiWtMPfmPEgAXnvj1Ro=
github.com/go-viper/mapstructure/v2 v2.5.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
github.com/gofrs/uuid/v5 v5.4.0 h1:EfbpCTjqMuGyq5ZJwxqzn3Cbr2d0rUZU7v5ycAk/e/0=
github.com/gofrs/uuid/v5 v5.4.0/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV+9bD8=
github.com/gofrs/uuid/v5 v5.5.0 h1:FkPv6jYQRbZtH3bD8yC7106u+CedTCLF8+t7CLHSZNo=
github.com/gofrs/uuid/v5 v5.5.0/go.mod h1:bbAA98EoIlxyRHIVg6ektCSsZ5n8mSbwgEhvhMYlZgg=
github.com/golang-jwt/jwt/v5 v5.3.1 h1:kYf81DTWFe7t+1VvL7eS+jKFVWaUnK9cB1qbwn63YCY=
Expand Down Expand Up @@ -399,8 +395,6 @@ modernc.org/opt v0.2.0/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns=
modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w=
modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE=
modernc.org/sqlite v1.26.0/go.mod h1:FL3pVXie73rg3Rii6V/u5BoHlSoyeZeIgKZEgHARyCU=
modernc.org/sqlite v1.54.0 h1:JCxR4qwkJvOaqAoYcgDoO25Nc+ROg6EJ2LfBVzdrgog=
modernc.org/sqlite v1.54.0/go.mod h1:4ntCLuNmnH8+GNqjka1wNg7KJd5/Hi5FYp8K+XQ7GZw=
modernc.org/sqlite v1.55.0 h1:hIFh0MCH0rGinQ/4KYb5/UbCkRkb+UP+OkLCVWa5MTM=
modernc.org/sqlite v1.55.0/go.mod h1:4ntCLuNmnH8+GNqjka1wNg7KJd5/Hi5FYp8K+XQ7GZw=
modernc.org/strutil v1.1.3/go.mod h1:MEHNA7PdEnEwLvspRMtWTNnp2nnyvMfkimT1NKNAGbw=
Expand Down
31 changes: 16 additions & 15 deletions internal/assistant/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,22 @@ type ToolExecutor func(context.Context, []ToolCall, func(StreamEvent)) ([]ToolEv

// CompletionRequest describes one assistant-owned model completion request.
type CompletionRequest struct {
OnEvent func(StreamEvent) `json:"-"`
OnProviderObserve func(context.Context, *CompletionRequest, int) `json:"-"`
OnProviderRequest llm.ProviderRequestHook `json:"-"`
ToolRegistry *tool.Registry `json:"-"`
ExecuteTools ToolExecutor `json:"-"`
SessionID string `json:"session_id"`
SystemPrompt string `json:"system_prompt"`
ThinkingLevel string `json:"thinking_level"`
CWD string `json:"cwd"`
Auth model.RequestAuth `json:"auth"`
Messages []database.MessageEntity `json:"messages"`
Usage model.TokenUsage `json:"usage"`
Model model.Model `json:"model"`
ProviderAttempt int `json:"-"`
DisableTools bool `json:"-"`
OnEvent func(StreamEvent) `json:"-"`
OnProviderObserve func(context.Context, *CompletionRequest, int) `json:"-"`
OnProviderRequest llm.ProviderRequestHook `json:"-"`
ToolRegistry *tool.Registry `json:"-"`
ExecuteTools ToolExecutor `json:"-"`
SessionID string `json:"session_id"`
SystemPrompt string `json:"system_prompt"`
ThinkingLevel string `json:"thinking_level"`
CWD string `json:"cwd"`
Auth model.RequestAuth `json:"auth"`
Messages []database.MessageEntity `json:"messages"`
Usage model.TokenUsage `json:"usage"`
Model model.Model `json:"model"`
ProviderAttempt int `json:"-"`
DisableTools bool `json:"-"`
ToolSideEffectsStarted bool `json:"-"`
}

// CompletionResult is an assistant-owned provider response plus model-visible side effects.
Expand Down
31 changes: 16 additions & 15 deletions internal/assistant/client_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,22 @@ func completionRequestFromHookInput(input *llm.HookInput) *CompletionRequest {
}

return &CompletionRequest{
OnEvent: nil,
OnProviderObserve: nil,
OnProviderRequest: nil,
ToolRegistry: nil,
ExecuteTools: nil,
SessionID: input.SessionID,
SystemPrompt: "",
ThinkingLevel: input.ThinkingLevel,
CWD: stringFromOptions(input.ProviderOptions, "cwd"),
Auth: requestAuthFromHookInput(input),
Messages: nil,
Usage: model.EmptyTokenUsage(),
Model: modelFromLLMRef(&input.Model),
ProviderAttempt: input.Attempt,
DisableTools: false,
OnEvent: nil,
OnProviderObserve: nil,
OnProviderRequest: nil,
ToolRegistry: nil,
ExecuteTools: nil,
SessionID: input.SessionID,
SystemPrompt: "",
ThinkingLevel: input.ThinkingLevel,
CWD: stringFromOptions(input.ProviderOptions, "cwd"),
Auth: requestAuthFromHookInput(input),
Messages: nil,
Usage: model.EmptyTokenUsage(),
Model: modelFromLLMRef(&input.Model),
ProviderAttempt: input.Attempt,
DisableTools: false,
ToolSideEffectsStarted: false,
}
}

Expand Down
5 changes: 3 additions & 2 deletions internal/assistant/client_adapter_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ func TestProviderRequestFromCompletionRequestAdaptsCallbacksAndRequest(t *testin
MaxTokens: 50,
Reasoning: true,
},
ProviderAttempt: 3,
DisableTools: false,
ProviderAttempt: 3,
DisableTools: false,
ToolSideEffectsStarted: false,
}

converted := providerRequestFromCompletionRequest(request)
Expand Down
31 changes: 16 additions & 15 deletions internal/assistant/context_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,22 @@ func (runtime *Runtime) ContextUsage(ctx context.Context, sessionID, cwd string)
}

request := &CompletionRequest{
OnEvent: nil,
OnProviderObserve: nil,
OnProviderRequest: nil,
ToolRegistry: registry,
ExecuteTools: nil,
SessionID: sessionID,
SystemPrompt: "",
ThinkingLevel: "",
CWD: cwd,
Auth: model.RequestAuth{Headers: nil, APIKey: "", Error: "", OK: false},
Messages: nil,
Usage: model.EmptyTokenUsage(),
Model: selectedModel,
ProviderAttempt: 0,
DisableTools: false,
OnEvent: nil,
OnProviderObserve: nil,
OnProviderRequest: nil,
ToolRegistry: registry,
ExecuteTools: nil,
SessionID: sessionID,
SystemPrompt: "",
ThinkingLevel: "",
CWD: cwd,
Auth: model.RequestAuth{Headers: nil, APIKey: "", Error: "", OK: false},
Messages: nil,
Usage: model.EmptyTokenUsage(),
Model: selectedModel,
ProviderAttempt: 0,
DisableTools: false,
ToolSideEffectsStarted: false,
}

messages := []database.MessageEntity{}
Expand Down
31 changes: 16 additions & 15 deletions internal/assistant/context_compaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,21 +326,22 @@ func (runtime *Runtime) summarizeCompaction(
) (string, error) {
systemPrompt := compaction.SystemPrompt(plan.PreviousSummary, plan.SplitTurnSummary)
request := &CompletionRequest{
OnEvent: nil,
OnProviderObserve: runtime.emitProviderRequest,
OnProviderRequest: runtime.dispatchProviderRequestHook,
ToolRegistry: tool.NewRegistry(cwd),
ExecuteTools: nil,
DisableTools: true,
SessionID: sessionID,
SystemPrompt: systemPrompt,
ThinkingLevel: thinkingOff,
CWD: cwd,
Auth: auth,
Messages: plan.Messages,
Usage: compactionRequestUsage(selectedModel, systemPrompt, plan.Messages),
Model: *selectedModel,
ProviderAttempt: 0,
OnEvent: nil,
OnProviderObserve: runtime.emitProviderRequest,
OnProviderRequest: runtime.dispatchProviderRequestHook,
ToolRegistry: tool.NewRegistry(cwd),
ExecuteTools: nil,
DisableTools: true,
ToolSideEffectsStarted: false,
SessionID: sessionID,
SystemPrompt: systemPrompt,
ThinkingLevel: thinkingOff,
CWD: cwd,
Auth: auth,
Messages: plan.Messages,
Usage: compactionRequestUsage(selectedModel, systemPrompt, plan.Messages),
Model: *selectedModel,
ProviderAttempt: 0,
}

result, err := runtime.completeWithRetry(ctx, request, nil)
Expand Down
2 changes: 1 addition & 1 deletion internal/assistant/context_overflow_compaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (runtime *Runtime) completeWithProviderOverflowRecovery(
return input.build, input.compactionEntry, result, nil
}

if !IsContextWindowError(err) {
if !IsContextWindowError(err) || input.build.Request.ToolSideEffectsStarted {
return input.build, input.compactionEntry, nil, err
}

Expand Down
23 changes: 23 additions & 0 deletions internal/assistant/context_overflow_compaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,29 @@ func TestRuntime_ProviderContextOverflowRecoveryScenarios(t *testing.T) {
}
}

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

client := &recordingCompleter{
complete: func(_ int, request *assistant.CompletionRequest) (*assistant.CompletionResult, error) {
_, err := request.ExecuteTools(context.Background(), nil, nil)
require.NoError(t, err)

return nil, testContextWindowError()
},
requests: nil,
disableToolsByCall: nil,
}
runtime := newProviderOverflowRecoveryRuntime(t, client)

response, _, _, err := runProviderOverflowPrompt(t, runtime, t.Name())

require.Nil(t, response)
require.Error(t, err)
assert.True(t, assistant.IsContextWindowError(err))
assert.Len(t, client.requests, 1)
}

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

Expand Down
5 changes: 3 additions & 2 deletions internal/assistant/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,9 @@ func newZeroCompletionRequest(auth model.RequestAuth) *CompletionRequest {
MaxTokens: 0,
Reasoning: false,
},
ProviderAttempt: 0,
DisableTools: false,
ProviderAttempt: 0,
DisableTools: false,
ToolSideEffectsStarted: false,
}
}

Expand Down
36 changes: 19 additions & 17 deletions internal/assistant/llm_conversion_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ func TestLLMRequestFromCompletionRequestConvertsAssistantState(t *testing.T) {
MaxTokens: 4096,
Reasoning: true,
},
ProviderAttempt: 0,
DisableTools: false,
ProviderAttempt: 0,
DisableTools: false,
ToolSideEffectsStarted: false,
}

converted := llmRequestFromCompletionRequest(request)
Expand Down Expand Up @@ -92,21 +93,22 @@ func TestLLMRequestFromCompletionRequestNilAndDisabledTools(t *testing.T) {
assert.Empty(t, empty.Tools)

converted := llmRequestFromCompletionRequest(&CompletionRequest{
OnEvent: nil,
OnProviderObserve: nil,
OnProviderRequest: nil,
ToolRegistry: tool.NewRegistry(t.TempDir()),
ExecuteTools: nil,
SessionID: "",
SystemPrompt: "",
ThinkingLevel: "",
CWD: "",
Auth: model.RequestAuth{Headers: nil, APIKey: "", Error: "", OK: false},
Messages: nil,
Usage: model.EmptyTokenUsage(),
Model: emptyTestModel(),
ProviderAttempt: 0,
DisableTools: true,
OnEvent: nil,
OnProviderObserve: nil,
OnProviderRequest: nil,
ToolRegistry: tool.NewRegistry(t.TempDir()),
ExecuteTools: nil,
SessionID: "",
SystemPrompt: "",
ThinkingLevel: "",
CWD: "",
Auth: model.RequestAuth{Headers: nil, APIKey: "", Error: "", OK: false},
Messages: nil,
Usage: model.EmptyTokenUsage(),
Model: emptyTestModel(),
ProviderAttempt: 0,
DisableTools: true,
ToolSideEffectsStarted: false,
})
assert.Empty(t, converted.Tools)
assert.True(t, converted.DisableTools)
Expand Down
5 changes: 3 additions & 2 deletions internal/assistant/provider_hooks_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,9 @@ func providerHookTestRequest() *CompletionRequest {
MaxTokens: 0,
Reasoning: false,
},
ProviderAttempt: 1,
DisableTools: false,
ProviderAttempt: 1,
DisableTools: false,
ToolSideEffectsStarted: false,
}
}

Expand Down
Loading
Loading