Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
fix(js-sdk): treat WatchHandle.stop() as a clean watch end (#1923)
devin-ai-integration[bot]d0ed5c1f170d
diff --git a/.changeset/watch-stop-clean-exit.md b/.changeset/watch-stop-clean-exit.mdnew file mode 100644index 000000000..6472300db--- /dev/null+++ b/.changeset/watch-stop-clean-exit.md@@ -0,0 +1,5 @@+---+'e2b': patch+---++fix(js-sdk): treat `WatchHandle.stop()` as a clean watch end - `onExit` now fires with no error instead of a `TimeoutError` blaming `requestTimeoutMs`, matching the Python SDK behavior.diff --git a/packages/js-sdk/src/sandbox/filesystem/index.ts b/packages/js-sdk/src/sandbox/filesystem/index.tsindex c07a12ae0..f3bae6386 100644--- a/packages/js-sdk/src/sandbox/filesystem/index.ts+++ b/packages/js-sdk/src/sandbox/filesystem/index.ts@@ -339,6 +339,9 @@ export interface WatchOpts extends FilesystemRequestOpts {timeoutMs?: number/*** Callback to call when the watch operation stops.+ *+ * `err` is `undefined` after {@link WatchHandle.stop} or a clean stream end,+ * and is set only when the watch fails.*/onExit?: (err?: Error) => void | Promise<void>/**diff --git a/packages/js-sdk/src/sandbox/filesystem/watchHandle.ts b/packages/js-sdk/src/sandbox/filesystem/watchHandle.tsindex cf16fc6a7..7127419f8 100644--- a/packages/js-sdk/src/sandbox/filesystem/watchHandle.ts+++ b/packages/js-sdk/src/sandbox/filesystem/watchHandle.ts@@ -77,6 +77,8 @@ export interface FilesystemEvent {* Use {@link WatchHandle.stop} to stop watching the directory.*/export class WatchHandle {+ private stopped = false+constructor(private readonly handleStop: () => void,private readonly events: AsyncIterable<WatchDirResponse>,@@ -89,8 +91,11 @@ export class WatchHandle {/*** Stop watching the directory.+ *+ * `onExit` is then called asynchronously, with no error.*/async stop() {+ this.stopped = truethis.handleStop()}@@ -104,6 +109,10 @@ export class WatchHandle {}}} catch (err) {+ // `stop()` aborts the stream, so a stream error after it is a clean end.+ if (this.stopped) {+ return+ }throw await handleRpcErrorWithHealthCheck(err, this.checkHealth)}}diff --git a/packages/js-sdk/tests/sandbox/files/watchHandle.test.ts b/packages/js-sdk/tests/sandbox/files/watchHandle.test.tsindex d4c4dc4f1..0eae32028 100644--- a/packages/js-sdk/tests/sandbox/files/watchHandle.test.ts+++ b/packages/js-sdk/tests/sandbox/files/watchHandle.test.ts@@ -1,3 +1,4 @@+import { Code, ConnectError } from '@connectrpc/connect'import { describe, expect, it, vi } from 'vitest'import { EventType } from '../../../src/envd/filesystem/filesystem_pb'@@ -131,4 +132,83 @@ describe('WatchHandle', () => {expect(stopped).toBe(true)})})++ it('calls onExit with no argument when the watch is stopped by the user', async () => {+ // Simulates the transport: the stream stays open until `handleStop`+ // aborts it, then surfaces the abort as a Connect cancellation (which+ // `handleRpcError` would map to `TimeoutError`).+ let abortStream: (() => void) | undefined+ const stream = (async function* () {+ await new Promise<never>((_, reject) => {+ abortStream = () =>+ reject(new ConnectError('This operation was aborted', Code.Canceled))+ })+ })()++ let exitArgs: unknown[] | undefined++ const handle = new WatchHandle(+ () => abortStream?.(),+ stream,+ undefined,+ (...args: unknown[]) => {+ exitArgs = args+ }+ )++ await handle.stop()++ await vi.waitFor(() => {+ expect(exitArgs).toBeDefined()+ })+ // A user-initiated stop is a clean end - no error is passed to onExit.+ expect(exitArgs).toEqual([])+ })++ it('still reports stream errors when the watch was not stopped by the user', async () => {+ const failure = new Error('stream broke')++ const stream = (async function* () {+ throw failure+ })()++ let exitArgs: unknown[] | undefined++ new WatchHandle(+ () => {},+ stream,+ undefined,+ (...args: unknown[]) => {+ exitArgs = args+ }+ )++ await vi.waitFor(() => {+ expect(exitArgs).toBeDefined()+ })+ expect(exitArgs).toEqual([failure])+ })++ it('still reports onEvent errors raised after the watch is stopped', async () => {+ const failure = new Error('onEvent failed')+ let handle: WatchHandle | undefined+ let exitArgs: unknown[] | undefined++ handle = new WatchHandle(+ () => {},+ events([filesystemEvent('a.txt')]),+ async () => {+ await handle?.stop()+ throw failure+ },+ (...args: unknown[]) => {+ exitArgs = args+ }+ )++ await vi.waitFor(() => {+ expect(exitArgs).toBeDefined()+ })+ expect(exitArgs).toEqual([failure])+ })})