diff --git a/auth-cursor/README.md b/auth-cursor/README.md index 3b8d823..dfc0cc1 100644 --- a/auth-cursor/README.md +++ b/auth-cursor/README.md @@ -185,6 +185,7 @@ Tools stay off unless the client sends them. The working directory is always emp Other limits: - No token-counting endpoint. `count_tokens` is a character estimate; billing uses counts Cursor reports after a run. +- Responses carry no `usage`. Cursor counts usage per run, and one run covers every tool round of a client turn, so the total re-counts the turn's context once per round. The run total goes only to the `cursor request completed` log line. - `executor.http_request` returns 501. - Remote images: only `http`/`https` to a non-loopback, non-private, non-metadata host. Credentials in the URL are refused. The error text never repeats the path or query. The host HTTP client still follows redirects, so a public URL that 302s onto loopback is not blocked here. - Region/plan refusals fail that one request without parking the credential. A 401 or 429 that arrives as an `SdkErrorCode` still cools or retires the key. diff --git a/auth-cursor/go/chatcompletions.go b/auth-cursor/go/chatcompletions.go index a763f0c..0d763bf 100644 --- a/auth-cursor/go/chatcompletions.go +++ b/auth-cursor/go/chatcompletions.go @@ -83,7 +83,6 @@ type chatCompletion struct { Created int64 `json:"created"` Model string `json:"model"` Choices []chatCompletionChoice `json:"choices"` - Usage *chatCompletionUsage `json:"usage,omitempty"` } func parseChatRequest(payload []byte) (chatRequest, error) { @@ -381,7 +380,11 @@ func convertUsage(usage *sdkv1.TokenUsage) *chatCompletionUsage { } } -func buildCompletion(id, model, text string, usage *sdkv1.TokenUsage) ([]byte, error) { +// Replies carry no usage. Cursor reports usage once per run, and a run spans every tool +// round of a client turn, so its total re-counts the whole turn's context on the last +// reply. Callers that size their context from usage would read that as overflow; without +// usage they estimate per request instead. +func buildCompletion(id, model, text string) ([]byte, error) { finish := "stop" return json.Marshal(chatCompletion{ ID: id, @@ -393,11 +396,10 @@ func buildCompletion(id, model, text string, usage *sdkv1.TokenUsage) ([]byte, e Message: &chatCompletionMessage{Role: "assistant", Content: text}, FinishReason: &finish, }}, - Usage: convertUsage(usage), }) } -func buildStreamChunk(id, model string, delta chatCompletionDelta, finishReason *string, usage *sdkv1.TokenUsage) []byte { +func buildStreamChunk(id, model string, delta chatCompletionDelta, finishReason *string) []byte { chunk := chatCompletion{ ID: id, Object: "chat.completion.chunk", @@ -408,7 +410,6 @@ func buildStreamChunk(id, model string, delta chatCompletionDelta, finishReason Delta: &delta, FinishReason: finishReason, }}, - Usage: convertUsage(usage), } raw, errMarshal := json.Marshal(chunk) if errMarshal != nil { diff --git a/auth-cursor/go/executor.go b/auth-cursor/go/executor.go index fa5f133..819c66b 100644 --- a/auth-cursor/go/executor.go +++ b/auth-cursor/go/executor.go @@ -52,7 +52,7 @@ func execute(raw []byte) ([]byte, error) { if len(result.toolCalls) > 0 { payload, errBody = buildToolCallCompletion(newCompletionID(), model, result.text, result.toolCalls) } else { - payload, errBody = buildCompletion(newCompletionID(), model, result.text, result.usage) + payload, errBody = buildCompletion(newCompletionID(), model, result.text) } if errBody != nil { logCtx.failed(errBody.Error()) @@ -125,7 +125,7 @@ func forwardStream(ctx context.Context, streamID, model string, framing streamFr delta.Role = "assistant" roleSent = true } - chunk := buildStreamChunk(completionID, model, delta, nil, nil) + chunk := buildStreamChunk(completionID, model, delta, nil) return emitPluginStreamChunk(streamID, framing.frame(chunk)) } @@ -145,7 +145,7 @@ func forwardStream(ctx context.Context, streamID, model string, framing streamFr return nil } finish := "stop" - final := buildStreamChunk(completionID, model, chatCompletionDelta{}, &finish, result.usage) + final := buildStreamChunk(completionID, model, chatCompletionDelta{}, &finish) if errEmit := emitPluginStreamChunk(streamID, framing.frame(final)); errEmit != nil { logCtx.failed(errEmit.Error()) return errEmit @@ -168,13 +168,13 @@ func emitToolCallStream(streamID, completionID, model string, framing streamFram delta.Role = "assistant" *roleSent = true } - chunk := buildStreamChunk(completionID, model, delta, nil, nil) + chunk := buildStreamChunk(completionID, model, delta, nil) if errEmit := emitPluginStreamChunk(streamID, framing.frame(chunk)); errEmit != nil { return errEmit } } finish := "tool_calls" - final := buildStreamChunk(completionID, model, chatCompletionDelta{}, &finish, nil) + final := buildStreamChunk(completionID, model, chatCompletionDelta{}, &finish) if errEmit := emitPluginStreamChunk(streamID, framing.frame(final)); errEmit != nil { return errEmit } diff --git a/auth-cursor/go/plugin_test.go b/auth-cursor/go/plugin_test.go index 8ba0418..09581fd 100644 --- a/auth-cursor/go/plugin_test.go +++ b/auth-cursor/go/plugin_test.go @@ -83,9 +83,9 @@ func TestExecuteAssemblesCompletion(t *testing.T) { if got := completion.Choices[0].Message.Content; got != "Hello world" { t.Errorf("content = %q, want %q", got, "Hello world") } - // Cache reads are prompt tokens served from cache, so they belong on the prompt side. - if completion.Usage == nil || completion.Usage.PromptTokens != 10 || completion.Usage.CompletionTokens != 2 { - t.Errorf("usage = %+v, want prompt=10 completion=2", completion.Usage) + // The run's usage total is not this request's usage, so the reply carries none. + if usage := gjson.GetBytes(response.Payload, "usage"); usage.Exists() { + t.Errorf("usage = %s, want none", usage.Raw) } // A non-streaming request has nothing to do with deltas, so it must not ask for them. if len(bridge.enableDeltas) != 1 || bridge.enableDeltas[0] { @@ -1047,7 +1047,7 @@ func TestExecuteReportsAnUnfetchableImageAsARequestFault(t *testing.T) { func TestBuildStreamChunkEmitsBareChunkJSON(t *testing.T) { finish := "stop" - chunk := buildStreamChunk("chatcmpl-1", "fake-model", chatCompletionDelta{Content: "Hello"}, &finish, nil) + chunk := buildStreamChunk("chatcmpl-1", "fake-model", chatCompletionDelta{Content: "Hello"}, &finish) // The chat-completions SSE writer adds the framing, so the chunk itself must stay bare. if strings.HasPrefix(string(chunk), "data:") { t.Fatalf("chunk carries SSE framing: %q", chunk) diff --git a/auth-cursor/go/tools_test.go b/auth-cursor/go/tools_test.go index 65f2867..427c3da 100644 --- a/auth-cursor/go/tools_test.go +++ b/auth-cursor/go/tools_test.go @@ -44,6 +44,10 @@ func decodeCompletion(t *testing.T, raw []byte) chatCompletion { if errUnmarshal := json.Unmarshal(env.Result, &response); errUnmarshal != nil { t.Fatalf("decode executor response: %v", errUnmarshal) } + // Cursor's usage is per run, and a run spans every tool round, so no reply carries it. + if usage := gjson.GetBytes(response.Payload, "usage"); usage.Exists() { + t.Fatalf("usage = %s, want none", usage.Raw) + } var completion chatCompletion if errUnmarshal := json.Unmarshal(response.Payload, &completion); errUnmarshal != nil { t.Fatalf("decode completion: %v", errUnmarshal) @@ -259,9 +263,6 @@ func TestExecuteToolCallRoundTrip(t *testing.T) { if finishReason(t, first) != "tool_calls" { t.Fatalf("finish_reason = %q, want tool_calls", finishReason(t, first)) } - if first.Usage != nil { - t.Fatalf("usage = %+v, want none on a tool turn", first.Usage) - } if first.Choices[0].Message == nil || len(first.Choices[0].Message.ToolCalls) != 1 { t.Fatalf("tool_calls = %+v", first.Choices[0].Message) } @@ -298,9 +299,6 @@ func TestExecuteToolCallRoundTrip(t *testing.T) { if second.Choices[0].Message.Content != "Hello world" { t.Errorf("content = %q, want Hello world", second.Choices[0].Message.Content) } - if second.Usage == nil || second.Usage.PromptTokens != 10 || second.Usage.CompletionTokens != 2 { - t.Errorf("usage = %+v, want prompt=10 completion=2", second.Usage) - } if got := bridge.closedAgents(); len(got) != 1 || got[0] != "agent-1" { t.Errorf("closed agents = %v, want [agent-1]", got) } @@ -632,7 +630,7 @@ func TestBuildStreamChunkEmitsIndexedToolCalls(t *testing.T) { Arguments: `{"city":"Paris"}`, }, }}, - }, &finish, nil) + }, &finish) if gjson.GetBytes(chunk, "choices.0.delta.tool_calls.0.index").Int() != 0 { t.Errorf("index omitted: %s", chunk) }