-
Notifications
You must be signed in to change notification settings - Fork 164
feat(api): abort signal core plumbing (#615) #620
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
easonLiangWorldedtech
wants to merge
4
commits into
Zoo-Code-Org:main
from
easonLiangWorldedtech:feat/abort-signal-core-plumbing
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a61e13d
feat(api): abort signal core plumbing (#615)
easonliang28 99a978a
test(utils): add metadata forwarding test for singleCompletionHandler
easonliang28 d24a90c
fix(task): add { once: true } to abort event listeners to prevent mem…
easonliang28 12fd25e
test(task): add tests for task abort signal core plumbing
easonliang28 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,65 @@ | ||
| import { describe, it, expect } from "vitest" | ||
|
|
||
| import type { ApiHandlerCreateMessageMetadata } from "../index" | ||
|
|
||
| describe("abort signal passing", () => { | ||
| it("should pass the same AbortController signal instance to metadata.abortSignal", () => { | ||
| // Arrange: create an AbortController | ||
| const controller = new AbortController() | ||
|
|
||
| // Act: simulate what Task.ts does - construct metadata with abortSignal | ||
| const metadata: ApiHandlerCreateMessageMetadata = { | ||
| taskId: "test-task-id", | ||
| abortSignal: controller.signal, | ||
| } | ||
|
|
||
| // Assert: signal identity (toBe, not just toBeInstanceOf) | ||
| expect(metadata.abortSignal).toBe(controller.signal) | ||
| }) | ||
|
|
||
| it("should create a fresh AbortController for each request", () => { | ||
| // Arrange: simulate two sequential requests | ||
| const controller1 = new AbortController() | ||
| const metadata1: ApiHandlerCreateMessageMetadata = { | ||
| taskId: "task-1", | ||
| abortSignal: controller1.signal, | ||
| } | ||
|
|
||
| const controller2 = new AbortController() | ||
| const metadata2: ApiHandlerCreateMessageMetadata = { | ||
| taskId: "task-2", | ||
| abortSignal: controller2.signal, | ||
| } | ||
|
|
||
| // Assert: different instances | ||
| expect(metadata1.abortSignal).not.toBe(metadata2.abortSignal) | ||
| expect(controller1.signal).not.toBe(controller2.signal) | ||
| }) | ||
|
|
||
| it("should have abortSignal as undefined when not provided", () => { | ||
| const metadata: ApiHandlerCreateMessageMetadata = { | ||
| taskId: "test-task-id", | ||
| } | ||
|
|
||
| expect(metadata.abortSignal).toBeUndefined() | ||
| }) | ||
|
|
||
| it("should preserve abortSignal state (aborted vs non-aborted)", () => { | ||
| const controller1 = new AbortController() | ||
| const controller2 = new AbortController() | ||
| controller2.abort() | ||
|
|
||
| const metadata1: ApiHandlerCreateMessageMetadata = { | ||
| taskId: "task-1", | ||
| abortSignal: controller1.signal, | ||
| } | ||
|
|
||
| const metadata2: ApiHandlerCreateMessageMetadata = { | ||
| taskId: "task-2", | ||
| abortSignal: controller2.signal, | ||
| } | ||
|
|
||
| expect(metadata1.abortSignal?.aborted).toBe(false) | ||
| expect(metadata2.abortSignal?.aborted).toBe(true) | ||
| }) | ||
| }) |
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,106 @@ | ||
| import { describe, it, expect } from "vitest" | ||
|
|
||
| import type { ApiHandlerCreateMessageMetadata } from "../../api" | ||
|
|
||
| describe("abort signal passing", () => { | ||
| it("should pass the same AbortController signal instance to metadata.abortSignal", async () => { | ||
| // Arrange: create an AbortController | ||
| const controller = new AbortController() | ||
|
|
||
| // Act: simulate what Task.ts does - construct metadata with abortSignal | ||
| const metadata: ApiHandlerCreateMessageMetadata = { | ||
| taskId: "test-task-id", | ||
| abortSignal: controller.signal, | ||
| } | ||
|
|
||
| // Assert: signal identity (toBe, not just toBeInstanceOf) | ||
| expect(metadata.abortSignal).toBe(controller.signal) | ||
| }) | ||
|
|
||
| it("should create a fresh AbortController for each request", async () => { | ||
| // Arrange: simulate two sequential requests | ||
| const controller1 = new AbortController() | ||
| const metadata1: ApiHandlerCreateMessageMetadata = { | ||
| taskId: "task-1", | ||
| abortSignal: controller1.signal, | ||
| } | ||
|
|
||
| const controller2 = new AbortController() | ||
| const metadata2: ApiHandlerCreateMessageMetadata = { | ||
| taskId: "task-2", | ||
| abortSignal: controller2.signal, | ||
| } | ||
|
|
||
| // Assert: different instances | ||
| expect(metadata1.abortSignal).not.toBe(metadata2.abortSignal) | ||
| expect(controller1.signal).not.toBe(controller2.signal) | ||
| }) | ||
|
|
||
| it("should trigger abort listener and clear controller reference", async () => { | ||
| const controller = new AbortController() | ||
| let controllerRef: AbortController | undefined = controller | ||
|
|
||
| // Simulate Task.ts abort listener setup with { once: true } | ||
| controller.signal.addEventListener( | ||
| "abort", | ||
| () => { | ||
| controllerRef = undefined | ||
| }, | ||
| { once: true }, | ||
| ) | ||
|
|
||
| // Verify initial state | ||
| expect(controllerRef).toBe(controller) | ||
| expect(controller.signal.aborted).toBe(false) | ||
|
|
||
| // Trigger abort | ||
| controller.abort() | ||
|
|
||
| // Verify listener was called and cleared the reference | ||
| expect(controllerRef).toBeUndefined() | ||
| expect(controller.signal.aborted).toBe(true) | ||
| }) | ||
|
|
||
| it("should only trigger once even if signal is aborted multiple times", async () => { | ||
| const controller = new AbortController() | ||
| let callCount = 0 | ||
|
|
||
| controller.signal.addEventListener( | ||
| "abort", | ||
| () => { | ||
| callCount++ | ||
| }, | ||
| { once: true }, | ||
| ) | ||
|
|
||
| // First abort | ||
| controller.abort() | ||
| expect(callCount).toBe(1) | ||
|
|
||
| // Second abort (AbortSignal allows this, though unusual) | ||
| controller.abort() | ||
| expect(callCount).toBe(1) // Should still be 1 because of { once: true } | ||
| }) | ||
|
|
||
| it("should reject promise immediately if signal already aborted", async () => { | ||
| const controller = new AbortController() | ||
| controller.abort() | ||
|
|
||
| // Simulate the abortPromise logic from Task.ts | ||
| const abortPromise = new Promise<never>((_, reject) => { | ||
| if (controller.signal.aborted) { | ||
| reject(new Error("Request cancelled by user")) | ||
| } else { | ||
| controller.signal.addEventListener( | ||
| "abort", | ||
| () => { | ||
| reject(new Error("Request cancelled by user")) | ||
| }, | ||
| { once: true }, | ||
| ) | ||
| } | ||
| }) | ||
|
|
||
| await expect(abortPromise).rejects.toThrow("Request cancelled by user") | ||
| }) | ||
| }) | ||
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.
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.
Test coverage gap: tests verify JavaScript API behavior but not the actual implementation.
The tests manually construct
ApiHandlerCreateMessageMetadataobjects and verify thatAbortSignalidentity is preserved, but they don't test that the actual code changes work correctly:Task.attemptApiRequestcreates anAbortControllerand includes its signal in the metadata passed toapi.createMessagesingleCompletionHandlerforwards the metadata (withabortSignal) to the underlying handlerAs per coding guidelines, unit tests should cover "request construction" - these tests should verify that the Task and utility functions correctly build and forward the metadata with the abort signal.
Suggested additions:
buildApiHandlerand verifyattemptApiRequestpasses metadata withabortSignaltoapi.createMessagesingleCompletionHandlercallshandler.completePrompt(prompt, metadata)with the provided metadata🤖 Prompt for AI Agents
Source: Coding guidelines