File Watching
Monitor filesystem changes in real-time using Linux's native inotify system. The file watching API provides efficient monitoring of files and directories with support for filtering, exclusions, and callback-based event handling.
Watch a directory for filesystem changes in real-time.
await sandbox.watch(path: string, options?: WatchOptions): Promise<WatchHandle>Parameters:
path- Absolute path or relative to/workspace(e.g.,/app/srcorsrc)options(optional):recursive- Watch subdirectories recursively (default:true)include- Glob patterns to include (e.g.,['*.ts', '*.js'])exclude- Glob patterns to exclude (default:['.git', 'node_modules', '.DS_Store'])signal- AbortSignal to cancel the watchonEvent- Callback for file change eventsonError- Callback for watch errors
Returns: WatchHandle with stop() method and metadata properties
// Watch entire project directoryconst watcher = await sandbox.watch("/workspace", { onEvent: (event) => { console.log(`${event.type}: ${event.path}`); console.log(`Is directory: ${event.isDirectory}`); }, onError: (error) => { console.error("Watch error:", error.message); },});
// Stop watching when doneawait watcher.stop();// Watch entire project directoryconst watcher = await sandbox.watch('/workspace', { onEvent: (event) => { console.log(`${event.type}: ${event.path}`); console.log(`Is directory: ${event.isDirectory}`); }, onError: (error) => { console.error('Watch error:', error.message); }});
// Stop watching when doneawait watcher.stop();// Watch specific file types in a directoryconst watcher = await sandbox.watch("/workspace/src", { include: ["*.ts", "*.tsx"], exclude: ["*.test.ts", "dist"], onEvent: (event) => { if (event.type === "modify") { console.log(`TypeScript file modified: ${event.path}`); } },});// Watch specific file types in a directoryconst watcher = await sandbox.watch('/workspace/src', { include: ['*.ts', '*.tsx'], exclude: ['*.test.ts', 'dist'], onEvent: (event) => { if (event.type === 'modify') { console.log(`TypeScript file modified: ${event.path}`); } }});Get raw SSE stream for file watching (advanced usage).
const stream = await sandbox.watchStream(path: string, options?: WatchRequest): Promise<ReadableStream<Uint8Array>>Most users should use watch() instead, which provides a higher-level API with proper lifecycle management.
Stop a specific watch by ID.
await sandbox.stopWatch(watchId: string): Promise<{ success: boolean }>Parameters:
watchId- Watch ID from the WatchHandle
List all active watches.
const result = await sandbox.listWatches(): Promise<WatchListResult>Returns:
interface WatchListResult { success: boolean; watches: Array<{ id: string; path: string; startedAt: string; }>; count: number; timestamp: string;}Handle returned from watch() to control and inspect the watch.
interface WatchHandle { /** Stop watching and clean up resources */ stop(): Promise<void>; /** The watch ID (for debugging) */ readonly id: string; /** The path being watched */ readonly path: string;}File system change event passed to the onEvent callback.
interface WatchEvent { /** The type of change that occurred */ type: WatchEventType; /** Absolute path to the file or directory that changed */ path: string; /** Whether the changed path is a directory */ isDirectory: boolean;}Types of filesystem changes that can be detected.
type WatchEventType = "create" | "modify" | "delete" | "rename";create- File or directory was createdmodify- File content or directory attributes changeddelete- File or directory was deletedrename- File or directory was moved/renamed
Configuration options for watching directories.
interface WatchOptions { /** Watch subdirectories recursively (default: true) */ recursive?: boolean; /** Glob patterns to include (e.g., ['*.ts', '*.js']) */ include?: string[]; /** Glob patterns to exclude (default: ['.git', 'node_modules', '.DS_Store']) */ exclude?: string[]; /** AbortSignal to cancel the watch */ signal?: AbortSignal; /** Callback for file change events */ onEvent?: (event: WatchEvent) => void; /** Callback for errors (e.g., watch process died) */ onError?: (error: Error) => void;}- Watch filesystem changes guide - Patterns, best practices, and real-world examples