* Spawn a new process with background-style infrastructure. * * All processes are spawned with nohup/setsid and file-based output, * enabling seamless fg→bg transition via sendToBackground(). * * @param runtime Runtime to spawn the process on * @param workspaceId Workspace ID for t
(
runtime: Runtime,
workspaceId: string,
script: string,
config: {
cwd: string;
env?: Record<string, string>;
/** Human-readable name for the process - used to generate the process ID */
displayName: string;
/** If true, process is foreground (being waited on). Default: false (background) */
isForeground?: boolean;
/** Optional write-time monitor for background output. */
monitor?: BackgroundProcessMonitorConfig;
/** Auto-terminate after this many seconds (background processes only) */
timeoutSecs?: number;
}
)
| 596 | * @param config Execution configuration |
| 597 | */ |
| 598 | async spawn( |
| 599 | runtime: Runtime, |
| 600 | workspaceId: string, |
| 601 | script: string, |
| 602 | config: { |
| 603 | cwd: string; |
| 604 | env?: Record<string, string>; |
| 605 | /** Human-readable name for the process - used to generate the process ID */ |
| 606 | displayName: string; |
| 607 | /** If true, process is foreground (being waited on). Default: false (background) */ |
| 608 | isForeground?: boolean; |
| 609 | /** Optional write-time monitor for background output. */ |
| 610 | monitor?: BackgroundProcessMonitorConfig; |
| 611 | /** Auto-terminate after this many seconds (background processes only) */ |
| 612 | timeoutSecs?: number; |
| 613 | } |
| 614 | ): Promise< |
| 615 | | { success: true; processId: string; outputDir: string; pid: number } |
| 616 | | { success: false; error: string } |
| 617 | > { |
| 618 | log.debug(`BackgroundProcessManager.spawn() called for workspace ${workspaceId}`); |
| 619 | |
| 620 | const processId = this.generateUniqueProcessId(config.displayName); |
| 621 | |
| 622 | // Spawn via executor with background infrastructure |
| 623 | // spawnProcess uses runtime.tempDir() internally for output directory |
| 624 | const result = await spawnProcess(runtime, script, { |
| 625 | cwd: config.cwd, |
| 626 | workspaceId, |
| 627 | processId, |
| 628 | env: config.env, |
| 629 | }); |
| 630 | |
| 631 | if (!result.success) { |
| 632 | log.debug(`BackgroundProcessManager: Failed to spawn: ${result.error}`); |
| 633 | return { success: false, error: result.error }; |
| 634 | } |
| 635 | |
| 636 | const { handle, pid, outputDir } = result; |
| 637 | const startTime = Date.now(); |
| 638 | |
| 639 | // Write meta.json with process info |
| 640 | const meta: BackgroundProcessMeta = { |
| 641 | id: processId, |
| 642 | pid, |
| 643 | script, |
| 644 | startTime, |
| 645 | status: "running", |
| 646 | displayName: config.displayName, |
| 647 | }; |
| 648 | await handle.writeMeta(JSON.stringify(meta, null, 2)); |
| 649 | |
| 650 | const proc: BackgroundProcess = { |
| 651 | id: processId, |
| 652 | pid, |
| 653 | workspaceId, |
| 654 | outputDir, |
| 655 | script, |
no test coverage detected