-
-
Notifications
You must be signed in to change notification settings - Fork 422
perf(agents): pool shared harness runtimes #191
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
Closed
Waishnav
wants to merge
5
commits into
refactor/subagent-runtime-owner
from
perf/subagent-runtime-pool
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
37178a9
perf(agents): pool shared OpenCode runtime
Waishnav 84ef87f
refactor(server): own harness runtime registry
Waishnav af14576
test(agents): cover runtime reuse and eviction
Waishnav d256e42
fix(opencode): evict failed pooled runtimes
Waishnav e1e8a6f
refactor(agents): require an explicit runtime owner
Waishnav 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import assert from "node:assert/strict"; | ||
| import { HarnessRuntimePool, type HarnessDriver, type HarnessRuntime } from "./local-agent-runtime-pool.js"; | ||
| import { LocalAgentRuntimeRegistry } from "./local-agent-runtime-registry.js"; | ||
| import type { LocalAgentRunInput, LocalAgentRunResult } from "./local-agent-runtime.js"; | ||
|
|
||
| class FakeRuntime implements HarnessRuntime { | ||
| readonly prompts: string[] = []; | ||
| closed = false; | ||
| private sessionCount = 0; | ||
|
|
||
| constructor(private readonly name: string) {} | ||
|
|
||
| async run(input: LocalAgentRunInput): Promise<LocalAgentRunResult> { | ||
| assert.equal(this.closed, false); | ||
| this.prompts.push(input.prompt); | ||
| const providerSessionId = input.providerSessionId ?? `${this.name}-session-${++this.sessionCount}`; | ||
| return { | ||
| provider: "opencode", | ||
| providerSessionId, | ||
| finalResponse: `${this.name}:${input.prompt}`, | ||
| items: [], | ||
| }; | ||
| } | ||
|
|
||
| isUsable(): boolean { | ||
| return !this.closed; | ||
| } | ||
|
|
||
| async close(): Promise<void> { | ||
| this.closed = true; | ||
| } | ||
| } | ||
|
|
||
| class FailingRuntime extends FakeRuntime { | ||
| failed = false; | ||
|
|
||
| override async run(input: LocalAgentRunInput): Promise<LocalAgentRunResult> { | ||
| if (input.prompt === "fail") { | ||
| this.failed = true; | ||
| throw new Error("runtime transport failed"); | ||
| } | ||
| return super.run(input); | ||
| } | ||
|
|
||
| override isUsable(): boolean { | ||
| return !this.closed && !this.failed; | ||
| } | ||
| } | ||
|
|
||
| let now = 0; | ||
| let created = 0; | ||
| const runtimes: FakeRuntime[] = []; | ||
| const driver: HarnessDriver = { | ||
| provider: "opencode", | ||
| runtimeKey: () => "shared", | ||
| async createRuntime() { | ||
| created += 1; | ||
| const runtime = new FakeRuntime(`runtime-${created}`); | ||
| runtimes.push(runtime); | ||
| return runtime; | ||
| }, | ||
| }; | ||
| const pool = new HarnessRuntimePool({ idleMs: 10, reapIntervalMs: 0, now: () => now }); | ||
| const registry = new LocalAgentRuntimeRegistry({ pool, opencodeDriver: driver }); | ||
|
|
||
| try { | ||
| const first = await registry.run("opencode", input("/tmp/a", "first")); | ||
| const second = await registry.run("opencode", input("/tmp/b", "second", first.providerSessionId ?? undefined)); | ||
|
|
||
| assert.equal(created, 1, "compatible OpenCode runs should share one live runtime"); | ||
| assert.equal(first.providerSessionId, "runtime-1-session-1"); | ||
| assert.equal(second.providerSessionId, "runtime-1-session-1"); | ||
| assert.deepEqual(runtimes[0]?.prompts, ["first", "second"]); | ||
|
|
||
| now = 9; | ||
| await pool.reapIdle(); | ||
| assert.equal(runtimes[0]?.closed, false); | ||
|
|
||
| now = 10; | ||
| await pool.reapIdle(); | ||
| assert.equal(runtimes[0]?.closed, true, "idle runtimes should be reclaimable without touching durable session ids"); | ||
|
|
||
| await registry.run("opencode", input("/tmp/a", "third", second.providerSessionId ?? undefined)); | ||
| assert.equal(created, 2, "a later run should recreate an evicted runtime"); | ||
| assert.equal(runtimes[1]?.prompts[0], "third"); | ||
| } finally { | ||
| await registry.shutdown(); | ||
| } | ||
|
|
||
| { | ||
| let failureCreates = 0; | ||
| const failedRuntimes: FailingRuntime[] = []; | ||
| const failureDriver: HarnessDriver = { | ||
| provider: "opencode", | ||
| runtimeKey: () => "failure-recovery", | ||
| async createRuntime() { | ||
| const runtime = new FailingRuntime(`failure-runtime-${++failureCreates}`); | ||
| failedRuntimes.push(runtime); | ||
| return runtime; | ||
| }, | ||
| }; | ||
| const failurePool = new HarnessRuntimePool({ reapIntervalMs: 0 }); | ||
|
|
||
| try { | ||
| await assert.rejects( | ||
| failurePool.run(failureDriver, input("/tmp/a", "fail")), | ||
| /runtime transport failed/, | ||
| ); | ||
| assert.equal(failedRuntimes[0]?.closed, true, "an unusable failed runtime should be evicted immediately"); | ||
|
|
||
| const recovered = await failurePool.run(failureDriver, input("/tmp/a", "recover")); | ||
| assert.equal(failureCreates, 2, "the next turn should create a fresh runtime after a transport failure"); | ||
| assert.equal(recovered.finalResponse, "failure-runtime-2:recover"); | ||
| } finally { | ||
| await failurePool.shutdown(); | ||
| } | ||
| } | ||
|
|
||
| function input(workspace: string, prompt: string, providerSessionId?: string): LocalAgentRunInput { | ||
| return { | ||
| workspace, | ||
| prompt, | ||
| providerSessionId, | ||
| writeMode: "allowed", | ||
| }; | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.