Repository navigation
fix(semdiff): give the embedder a context and a bounded timeout - #22
Conversation
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
| // outbound connection. | ||
| type Embedder interface { | ||
| Embed(texts []string) ([][]float32, error) | ||
| Embed(ctx context.Context, texts []string) ([][]float32, error) |
| func Analyze(ctx context.Context, emb Embedder, oldLines, newLines []string) ([]Result, error) { | ||
| if tooLarge(oldLines) || tooLarge(newLines) { | ||
| return nil, ErrTooLarge | ||
| } |
`HTTPEmbedder.Embed` sent its request with `http.NewRequest` and `http.DefaultClient`, which has no timeout. An embeddings endpoint that accepted the connection and never answered blocked the call forever, so a server goroutine and its outbound connection stayed behind after the client gave up, and every retry added another. The change is additive, following the approach agreed on priompt#14: `Embed`, `Analyze` and the `Embedder` interface keep their signatures, so existing implementations and callers compile and behave as before. - `ContextEmbedder.EmbedContext` is optional, in the style of `Calibrated`. `LexicalEmbedder` and `HTTPEmbedder` implement it; their `Embed` delegates with `context.Background()`. - `AnalyzeContext(ctx, ...)` runs the diff under the caller's context and uses `EmbedContext` when the embedder has it, falling back to `Embed` otherwise. `Analyze` is a wrapper around it with `context.Background()`. - `AnalyzeContext` checks `ctx.Err()` before the work starts, so a cancelled call is refused even when no embedding is needed (identical versions, or a whole-prompt deletion that takes the empty-side shortcut). - `HTTPEmbedder.Timeout` bounds a round-trip that arrives without a deadline, with `DefaultEmbedTimeout` (30s) when unset; the request is built with `http.NewRequestWithContext` so cancellation reaches it. Closes priompt#14
59e331c to
cdae377
Compare
|
Thanks, this pull request now follows the contribution process. |
|
Reworked per your comment on #14 — thanks for catching both points.
New tests cover: the never-answering endpoint with a 200ms timeout, in-flight cancellation, an already-cancelled context on both no-embedding paths, and a legacy |
sujal-maheshwari2004
left a comment
There was a problem hiding this comment.
Tested locally; tests pass. Thanks!

Closes #14.
Reworked as you asked: the change is additive now, and nothing on the public surface moved.
API
Embedder.Embed,Analyzeand theEmbedderinterface keep their signatures. Existing implementations and callers compile and behave as before.ContextEmbedder.EmbedContext(ctx, texts)is optional, in the same style asCalibrated.LexicalEmbedderandHTTPEmbedderimplement it; theirEmbedcalls it withcontext.Background(). Any otherEmbedderkeeps working —AnalyzeContextfalls back toEmbedwhen the type does not implement the optional interface.AnalyzeContext(ctx, emb, oldLines, newLines)is the new entry point;Analyzeis a wrapper around it withcontext.Background().AnalyzeContextchecksctx.Err()before anything else, so an already-cancelled call is refused even on the paths that embed nothing: identical versions, and a whole-prompt deletion that takesdeltaText's empty-side shortcut. That was Copilot's second finding, and it was right.HTTPEmbedder.Timeoutbounds a round-trip whose context has no deadline, withDefaultEmbedTimeout = 30swhen unset; the request is built withhttp.NewRequestWithContext. TheErrTooLargebound and the "one non-empty vector per input" checks are untouched.Tests
TestHTTPEmbedderTimesOut: the issue's reproduction — a server that accepts and never answers; withTimeout: 200msthe call returns an error inside 5s. Onmainthe equivalent call never returns (a probe of the oldEmbedpanics withtest timed outat 20s).TestAnalyzeContextHonoursCancellation: cancel in flight, getcontext.Canceled.TestAnalyzeContextRefusesAnAlreadyCancelledContext: identical versions and a whole-prompt deletion, both refused before the work.TestHTTPEmbedderRefusesAnAlreadyCancelledContext: nothing reaches the server.TestHTTPEmbedderDefaultTimeoutIsBounded, andTestLegacyEmbedderStillWorks:Analyzeand anEmbedderthat only implementsEmbedboth still run, including throughAnalyzeContext.Measured:
go test ./...andgo test -race ./...pass;go vet ./...andgofmt -lclean. README'ssemdiff/row says what the context and the timeout are for. Scope is this repo only, as you said.