Skip to content

fix(semdiff): give the embedder a context and a bounded timeout - #22

Merged
sujal-maheshwari2004 merged 1 commit into
priompt:mainfrom
tyza66:fix/14-embedder-context-timeout
Nov 18, 2026
Merged

sujal-maheshwari2004 merged 1 commit into
priompt:mainfrom
tyza66:fix/14-embedder-context-timeout

Conversation

@tyza66

@tyza66 tyza66 commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Closes #14.

Reworked as you asked: the change is additive now, and nothing on the public surface moved.

API

  • Embedder.Embed, Analyze and the Embedder interface keep their signatures. Existing implementations and callers compile and behave as before.
  • ContextEmbedder.EmbedContext(ctx, texts) is optional, in the same style as Calibrated. LexicalEmbedder and HTTPEmbedder implement it; their Embed calls it with context.Background(). Any other Embedder keeps working — AnalyzeContext falls back to Embed when the type does not implement the optional interface.
  • AnalyzeContext(ctx, emb, oldLines, newLines) is the new entry point; Analyze is a wrapper around it with context.Background().
  • AnalyzeContext checks ctx.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 takes deltaText's empty-side shortcut. That was Copilot's second finding, and it was right.
  • HTTPEmbedder.Timeout bounds a round-trip whose context has no deadline, with DefaultEmbedTimeout = 30s when unset; the request is built with http.NewRequestWithContext. The ErrTooLarge bound and the "one non-empty vector per input" checks are untouched.

Tests

  • TestHTTPEmbedderTimesOut: the issue's reproduction — a server that accepts and never answers; with Timeout: 200ms the call returns an error inside 5s. On main the equivalent call never returns (a probe of the old Embed panics with test timed out at 20s).
  • TestAnalyzeContextHonoursCancellation: cancel in flight, get context.Canceled.
  • TestAnalyzeContextRefusesAnAlreadyCancelledContext: identical versions and a whole-prompt deletion, both refused before the work.
  • TestHTTPEmbedderRefusesAnAlreadyCancelledContext: nothing reaches the server.
  • TestHTTPEmbedderDefaultTimeoutIsBounded, and TestLegacyEmbedderStillWorks: Analyze and an Embedder that only implements Embed both still run, including through AnalyzeContext.
  • Existing tests unchanged.

Measured: go test ./... and go test -race ./... pass; go vet ./... and gofmt -l clean. README's semdiff/ row says what the context and the timeout are for. Scope is this repo only, as you said.

Copilot AI balanced review requested due to automatic review settings November 15, 2026 09:25

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The changes break required compatibility for existing embedders and callers, and Analyze can succeed with an already-cancelled context when no embedding is needed.

2 open issues

Review effort: Balanced

Comment thread semdiff/semdiff.go Outdated
// outbound connection.
type Embedder interface {
Embed(texts []string) ([][]float32, error)
Embed(ctx context.Context, texts []string) ([][]float32, error)
Comment thread semdiff/semdiff.go
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
@tyza66
tyza66 force-pushed the fix/14-embedder-context-timeout branch from 59e331c to cdae377 Compare November 15, 2026 10:39
@github-actions

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Thanks, this pull request now follows the contribution process.

@tyza66

tyza66 commented Nov 15, 2026

Copy link
Copy Markdown
Contributor Author

Reworked per your comment on #14 — thanks for catching both points.

  • No signature change. Embed, Analyze and the Embedder interface are untouched. ContextEmbedder.EmbedContext is optional (same style as Calibrated), implemented by LexicalEmbedder and HTTPEmbedder; their Embed delegates with context.Background(). AnalyzeContext uses EmbedContext when present and falls back to Embed, so any existing embedder keeps working.
  • Cancellation before the work. AnalyzeContext returns ctx.Err() first, so identical versions and a whole-prompt deletion (which takes deltaText's empty-side shortcut and embeds nothing) are refused too. That was Copilot's second finding and it was right.
  • ErrTooLarge and the per-input vector check stay where they are on main.

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 Embedder running through AnalyzeContext. go test ./... and -race pass. Scope stays this repo.

@sujal-maheshwari2004 sujal-maheshwari2004 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally; tests pass. Thanks!

@sujal-maheshwari2004
sujal-maheshwari2004 merged commit a4abc9b into priompt:main Nov 18, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HTTPEmbedder has no timeout and ignores request context

3 participants