-
Notifications
You must be signed in to change notification settings - Fork 3
Forward idempotency_key on AI assist so retries are not charged twice #110
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
Merged
feruzm
merged 2 commits into
ecency:main
from
hiveuprss:cursor/ai-assist-idempotency-key-24e9
Sep 30, 2026
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| using System.Text.Json.Nodes; | ||
| using EcencyApi.Handlers; | ||
| using EcencyApi.Infrastructure; | ||
| using Xunit; | ||
|
|
||
| namespace EcencyApi.Tests; | ||
|
|
||
| /// <summary> | ||
| /// AI assist bills whoever <c>us</c> names. ePoints returns the cached result for a | ||
| /// repeated <c>idempotency_key</c> instead of charging again, so the proxy has to | ||
| /// forward the key the client sent. Older clients omit it; those requests still go | ||
| /// through. | ||
| /// </summary> | ||
| [Collection("ai-assist")] | ||
| public class AiAssistHandlerTests : IDisposable | ||
| { | ||
| private readonly CurationDeskTestSupport.Recorder _upstream = new(); | ||
|
|
||
| public AiAssistHandlerTests() | ||
| { | ||
| // `code` "as:alice" validates as alice; anything else is invalid. | ||
| // Same extraction as production ValidateCode: a lone-surrogate escape is a | ||
| // real string, and GetValue<string>() throws on it. | ||
| PrivateApi.AiAssistValidateCode = body => | ||
| { | ||
| string? code = null; | ||
| if (body["code"] is JsonValue codeValue && JsVal.TryGetStringLenient(codeValue, out var codeStr)) | ||
| code = codeStr; | ||
| return Task.FromResult(code != null && code.StartsWith("as:", StringComparison.Ordinal) ? code[3..] : null); | ||
| }; | ||
| PrivateApi.AiAssistUpstream = (endpoint, method, payload, _) => | ||
| _upstream.Handle(endpoint, method, Array.Empty<KeyValuePair<string, string>>(), payload); | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| PrivateApi.AiAssistValidateCode = PrivateApi.ValidateCode; | ||
| PrivateApi.AiAssistUpstream = (endpoint, method, payload, timeoutMs) => | ||
| EcencyApi.Infrastructure.ApiClient.ApiRequest(endpoint, method, null, payload, null, timeoutMs); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ForwardsTheIdempotencyKeyOnTheUpstreamBody() | ||
| { | ||
| var ctx = CurationDeskTestSupport.Post("/private-api/ai-assist", """ | ||
| {"code":"as:alice","action":"summarize","text":"hello world","idempotency_key":"abcd1234efgh"} | ||
| """); | ||
| await PrivateApi.AiAssist(ctx); | ||
|
|
||
| Assert.Equal(200, ctx.Response.StatusCode); | ||
| var call = Assert.Single(_upstream.Calls); | ||
| Assert.Equal("ai-assist", call.Endpoint); | ||
| Assert.Equal(HttpMethod.Post, call.Method); | ||
| var payload = Assert.IsType<JsonObject>(call.Payload); | ||
| Assert.Equal("alice", payload["us"]?.GetValue<string>()); | ||
| Assert.Equal("summarize", payload["action"]?.GetValue<string>()); | ||
| Assert.Equal("hello world", payload["text"]?.GetValue<string>()); | ||
| Assert.Equal("abcd1234efgh", payload["idempotency_key"]?.GetValue<string>()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ForwardsWhenTheClientOmitsTheIdempotencyKey() | ||
| { | ||
| var ctx = CurationDeskTestSupport.Post("/private-api/ai-assist", """ | ||
| {"code":"as:alice","action":"summarize","text":"hello world"} | ||
| """); | ||
| await PrivateApi.AiAssist(ctx); | ||
|
|
||
| Assert.Equal(200, ctx.Response.StatusCode); | ||
| var call = Assert.Single(_upstream.Calls); | ||
| var payload = Assert.IsType<JsonObject>(call.Payload); | ||
| Assert.Equal("alice", payload["us"]?.GetValue<string>()); | ||
| Assert.Equal("summarize", payload["action"]?.GetValue<string>()); | ||
| Assert.Equal("hello world", payload["text"]?.GetValue<string>()); | ||
| Assert.False(payload.ContainsKey("idempotency_key")); | ||
| } | ||
| } | ||
|
|
||
| [CollectionDefinition("ai-assist", DisableParallelization = true)] | ||
| public class AiAssistCollection { } |
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.
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.