Repository navigation
Persist status code and cause on provider error events (#674) #882
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Svector-anu
wants to merge
12
commits into
Twigpine:main
from
Svector-anu:fix/674-provider-error-diagnostics
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1d3e263
Persist status code and cause on provider error events
Svector-anu 8f712b6
Address CodeRabbit review: upstream-unreachable metadata + test coverage
Svector-anu c762013
fix(zeroruntime): gofmt struct alignment in CollectedStream
Svector-anu 08e6a61
test(sessions): assert error event round-trips statusCode/cause throu…
Svector-anu ef00dd6
Merge branch 'main' into fix/674-provider-error-diagnostics
Svector-anu 031b841
Merge branch 'main' into fix/674-provider-error-diagnostics
Svector-anu ba92df9
fix(agent): keep StreamError through image-rejection recovery
Svector-anu 571675d
fix(providers): stop persisting a bucketed 500 and bound error cause
Svector-anu b7f6368
Merge remote-tracking branch 'origin/main' into fix/674-provider-erro…
Vasanthdev2004 ad8686e
fix(sessions): keep a truncated error cause within its cap
Vasanthdev2004 8cbf108
Merge remote-tracking branch 'origin/main' into fix/674-provider-erro…
Vasanthdev2004 eacfa46
fix(sessions): bound the error cause as it reads back, and pin the ca…
Vasanthdev2004 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| "github.com/Twigpine/zero/internal/config" | ||
| "github.com/Twigpine/zero/internal/sandbox" | ||
| "github.com/Twigpine/zero/internal/sessions" | ||
| "github.com/Twigpine/zero/internal/zeroruntime" | ||
| ) | ||
|
|
||
| // streamErrorExecProvider fails every request with one provider error event. | ||
| type streamErrorExecProvider struct { | ||
| event zeroruntime.StreamEvent | ||
| } | ||
|
|
||
| func (provider streamErrorExecProvider) StreamCompletion(ctx context.Context, _ zeroruntime.CompletionRequest) (<-chan zeroruntime.StreamEvent, error) { | ||
| ch := make(chan zeroruntime.StreamEvent, 1) | ||
| ch <- provider.event | ||
| close(ch) | ||
| return ch, nil | ||
| } | ||
|
|
||
| func providerErrorExecDeps(t *testing.T, store *sessions.Store, workspaceRoot string) appDeps { | ||
| t.Helper() | ||
| return appDeps{ | ||
| getwd: func() (string, error) { return workspaceRoot, nil }, | ||
| resolveConfig: func(_ string, _ config.Overrides) (config.ResolvedConfig, error) { | ||
| return config.ResolvedConfig{ | ||
| ActiveProvider: "test", | ||
| Provider: config.ProviderProfile{ | ||
| Name: "test", | ||
| ProviderKind: config.ProviderKindOpenAICompatible, | ||
| BaseURL: "http://127.0.0.1/v1", | ||
| Model: "test-model", | ||
| }, | ||
| MaxTurns: 3, | ||
| }, nil | ||
| }, | ||
| resolveMCPConfig: func(string, bool) (config.MCPConfig, error) { return config.MCPConfig{}, nil }, | ||
| newProvider: func(config.ProviderProfile) (zeroruntime.Provider, error) { | ||
| return streamErrorExecProvider{event: zeroruntime.StreamEvent{ | ||
| Type: zeroruntime.StreamEventError, | ||
| Error: "provider request error: model not found", | ||
| StatusCode: 404, | ||
| Cause: "upstream said: no such model", | ||
| }}, nil | ||
| }, | ||
| newSessionStore: func() *sessions.Store { return store }, | ||
| newSandboxStore: func() (*sandbox.GrantStore, error) { | ||
| return sandbox.NewGrantStore(sandbox.StoreOptions{FilePath: t.TempDir() + "/grants.json"}) | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| // recordedProviderError returns the payload of the one error event the run | ||
| // recorded, failing unless there is exactly one session with exactly one. | ||
| func recordedProviderError(t *testing.T, store *sessions.Store) map[string]any { | ||
| t.Helper() | ||
| items, err := store.List() | ||
| if err != nil || len(items) != 1 { | ||
| t.Fatalf("sessions = %d, %v; want one", len(items), err) | ||
| } | ||
| events, err := store.ReadEvents(items[0].SessionID) | ||
| if err != nil { | ||
| t.Fatalf("ReadEvents returned error: %v", err) | ||
| } | ||
| var found []map[string]any | ||
| for _, event := range events { | ||
| if event.Type != sessions.EventError { | ||
| continue | ||
| } | ||
| var payload map[string]any | ||
| if err := json.Unmarshal(event.Payload, &payload); err != nil { | ||
| t.Fatalf("unmarshal error payload: %v", err) | ||
| } | ||
| found = append(found, payload) | ||
| } | ||
| if len(found) != 1 { | ||
| t.Fatalf("recorded %d error events, want 1: %#v", len(found), found) | ||
| } | ||
| return found[0] | ||
| } | ||
|
|
||
| func assertProviderErrorDetail(t *testing.T, payload map[string]any) { | ||
| t.Helper() | ||
| if got, want := payload["message"], "provider request error: model not found"; got != want { | ||
| t.Errorf("message = %#v, want %q", got, want) | ||
| } | ||
| if got, want := payload["statusCode"], float64(404); got != want { | ||
| t.Errorf("statusCode = %#v, want %v: the run recorded only the flattened message", got, want) | ||
| } | ||
| if got, want := payload["cause"], "upstream said: no such model"; got != want { | ||
| t.Errorf("cause = %#v, want %q: the run recorded only the flattened message", got, want) | ||
| } | ||
| } | ||
|
|
||
| // A provider error reaches the exec session log with its HTTP status and the | ||
| // upstream cause, not only the flattened message (#674). | ||
| func TestRunExecRecordsTheProviderErrorStatusAndCause(t *testing.T) { | ||
| isolateCLIUserState(t) | ||
| workspaceRoot := t.TempDir() | ||
| // exec records through the default store, which isolateCLIUserState has | ||
| // moved under the test's temp root; this opens the same one. | ||
| store := sessions.NewStore(sessions.StoreOptions{}) | ||
| var stdout, stderr bytes.Buffer | ||
|
|
||
| exitCode := runWithDeps([]string{"exec", "--cwd", workspaceRoot, "--init-session-id", "provider_error_run", "hello"}, &stdout, &stderr, | ||
| providerErrorExecDeps(t, store, workspaceRoot)) | ||
|
|
||
| if exitCode != exitProvider { | ||
| t.Fatalf("exit = %d, want %d; stderr=%s", exitCode, exitProvider, stderr.String()) | ||
| } | ||
| assertProviderErrorDetail(t, recordedProviderError(t, store)) | ||
| } | ||
|
|
||
| // The spec draft run records its provider error through its own call site, so | ||
| // it is checked on its own. | ||
| func TestRunExecUseSpecRecordsTheProviderErrorStatusAndCause(t *testing.T) { | ||
| isolateCLIUserState(t) | ||
| workspaceRoot := t.TempDir() | ||
| store := sessions.NewStore(sessions.StoreOptions{RootDir: t.TempDir()}) | ||
| var stdout, stderr bytes.Buffer | ||
|
|
||
| exitCode := runWithDeps([]string{"exec", "--cwd", workspaceRoot, "--use-spec", "-o", "json", "plan the review flow"}, &stdout, &stderr, | ||
| providerErrorExecDeps(t, store, workspaceRoot)) | ||
|
|
||
| if exitCode != exitProvider { | ||
| t.Fatalf("exit = %d, want %d; stderr=%s stdout=%s", exitCode, exitProvider, stderr.String(), stdout.String()) | ||
| } | ||
| assertProviderErrorDetail(t, recordedProviderError(t, store)) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.