Repository navigation
feat(embedding): release the local ONNX model after an idle timeout #459
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
EyJunge1
merged 1 commit into
tickernelz:main
from
hu-ja-ja:feat/embedding-idle-release
Oct 30, 2026
+259
−32
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
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
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,103 @@ | ||
| import { afterAll, describe, expect, it } from "bun:test"; | ||
| import { mkdtempSync } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
|
|
||
| const home = mkdtempSync(join(tmpdir(), "opencode-mem-idle-")); | ||
| const originalHome = process.env.HOME; | ||
| const originalUserProfile = process.env.USERPROFILE; | ||
| process.env.HOME = home; | ||
| process.env.USERPROFILE = home; | ||
|
|
||
| const { CONFIG } = await import("../src/config.js"); | ||
| const { EmbeddingService } = await import("../src/services/embedding.js"); | ||
|
|
||
| afterAll(() => { | ||
| process.env.HOME = originalHome; | ||
| process.env.USERPROFILE = originalUserProfile; | ||
| }); | ||
|
|
||
| function makeService(): { service: any; disposed: string[] } { | ||
| const disposed: string[] = []; | ||
| const service = new EmbeddingService(); | ||
| service.pipe = Object.assign(async () => ({ data: new Float32Array([1, 2, 3]) }), { | ||
| dispose: async () => { | ||
| disposed.push("dispose"); | ||
| }, | ||
| }); | ||
| service.isWarmedUp = true; | ||
| return { service, disposed }; | ||
| } | ||
|
|
||
| async function withIdleTimeout(ms: number, run: () => Promise<void>): Promise<void> { | ||
| const previous = CONFIG.embeddingIdleTimeoutMs; | ||
| CONFIG.embeddingIdleTimeoutMs = ms; | ||
| CONFIG.embeddingApiUrl = undefined; | ||
| CONFIG.embeddingApiKey = undefined; | ||
| try { | ||
| await run(); | ||
| } finally { | ||
| CONFIG.embeddingIdleTimeoutMs = previous; | ||
| } | ||
| } | ||
|
|
||
| describe("EmbeddingService idle release", () => { | ||
| it("disposes the local pipeline after the idle timeout", async () => { | ||
| await withIdleTimeout(20, async () => { | ||
| const { service, disposed } = makeService(); | ||
| await service.embed("hello"); | ||
| expect(disposed).toEqual([]); | ||
|
|
||
| await new Promise((resolve) => setTimeout(resolve, 80)); | ||
|
|
||
| expect(disposed).toEqual(["dispose"]); | ||
| expect(service.isWarmedUp).toBe(false); | ||
| expect(service.pipe).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| it("keeps the model loaded when the timeout is 0", async () => { | ||
| await withIdleTimeout(0, async () => { | ||
| const { service, disposed } = makeService(); | ||
| await service.embed("hello"); | ||
|
|
||
| await new Promise((resolve) => setTimeout(resolve, 80)); | ||
|
|
||
| expect(disposed).toEqual([]); | ||
| expect(service.isWarmedUp).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| it("defers disposal while an embed is in flight and releases it afterwards", async () => { | ||
| await withIdleTimeout(0, async () => { | ||
| const { service, disposed } = makeService(); | ||
| let release!: () => void; | ||
| const gate = new Promise<void>((resolve) => { | ||
| release = resolve; | ||
| }); | ||
| service.pipe = Object.assign( | ||
| async () => { | ||
| await gate; | ||
| return { data: new Float32Array([1]) }; | ||
| }, | ||
| { dispose: async () => disposed.push("dispose") } | ||
| ); | ||
|
|
||
| const running = service.embed("in-flight"); | ||
| await new Promise((resolve) => setTimeout(resolve, 10)); | ||
| await service.dispose("client-close"); | ||
|
|
||
| // The running embed still holds the pipeline, so nothing is released yet. | ||
| expect(disposed).toEqual([]); | ||
|
|
||
| release(); | ||
| await running; | ||
| await new Promise((resolve) => setTimeout(resolve, 10)); | ||
|
|
||
| // Release happens without a second dispose() call, even with the timer off. | ||
| expect(disposed).toEqual(["dispose"]); | ||
| expect(service.isWarmedUp).toBe(false); | ||
| expect(service.pipe).toBeNull(); | ||
| }); | ||
| }); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the existing cold-buffer path (buffered while embeddings are unavailable, drained on the next warm merge, cap 50); warming on demand here would reload the model at session idle, which is when profile learning runs, so it is out of scope for this PR.