RFC Background Bash Tasks Implementation Plan #351
Closed
xierenyuan
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Background Bash Tasks Implementation Plan
Goal: Enable bash tool to run commands in background and retrieve their output asynchronously through a new bash_output tool.
Architecture: Create a BackgroundTaskManager in src/utils to manage background process lifecycle, output buffering, and cleanup. Extend bash tool with background parameter, add bash_output tool for retrieval. Manager is session-scoped and auto-cleans on session end.
Tech Stack: Node.js child_process, existing shellExecute utility from src/utils/shell-execution.ts, reuse executeCommand and validateCommand from src/tools/bash.ts, zod for validation
Key Design Decision: The BackgroundTaskManager reuses the command execution logic from src/tools/bash.ts to ensure consistency:
validateCommand()- For command validation before executionshellExecute()- For actual command execution with proper shell handlingTask 0: Export bash.ts Functions for Reuse
Files:
src/tools/bash.tsStep 1: Export executeCommand function
Change
async function executeCommandtoexport async function executeCommandinsrc/tools/bash.tsStep 2: Export validateCommand function
Change
function validateCommandtoexport function validateCommandinsrc/tools/bash.tsStep 3: Verify exports
Run:
npm run typecheckExpected: No errors
Status: ✅ COMPLETED - Functions have been exported from src/tools/bash.ts
Task 1: Create BackgroundTaskManager Core
Files:
src/utils/backgroundTasks.tssrc/utils/backgroundTasks.test.tsStep 1: Write failing test for manager initialization
Step 2: Run test to verify it fails
Run:
npm test src/utils/backgroundTasks.test.tsExpected: FAIL with "Cannot find module './backgroundTasks'"
Step 3: Create minimal BackgroundTaskManager class
In
src/utils/backgroundTasks.ts:Step 4: Run test to verify it passes
Run:
npm test src/utils/backgroundTasks.test.tsExpected: PASS
Task 2: Implement Task Starting
Files:
src/utils/backgroundTasks.tssrc/utils/backgroundTasks.test.tsStep 1: Write failing test for startTask
Add to
src/utils/backgroundTasks.test.ts:Step 2: Run test to verify it fails
Run:
npm test src/utils/backgroundTasks.test.tsExpected: FAIL with "manager.startTask is not a function"
Step 3: Implement startTask method
Add to
src/utils/backgroundTasks.ts:Note: The implementation now reuses
validateCommandandshellExecutefromsrc/tools/bash.tsto ensure consistent command validation and execution behavior with the bash tool.Step 4: Run test to verify it passes
Run:
npm test src/utils/backgroundTasks.test.tsExpected: PASS
Step 5: Add integration test with real process
Add to
src/utils/backgroundTasks.test.ts:Step 6: Add getTask helper method
Add to
src/utils/backgroundTasks.ts:Step 7: Run integration test
Run:
npm test src/utils/backgroundTasks.test.tsExpected: PASS
Task 3: Implement Output Retrieval
Files:
src/utils/backgroundTasks.tssrc/utils/backgroundTasks.test.tsStep 1: Write failing test for getTaskOutput (full mode)
Add to
src/utils/backgroundTasks.test.ts:Step 2: Run test to verify it fails
Run:
npm test src/utils/backgroundTasks.test.tsExpected: FAIL with "manager.getTaskOutput is not a function"
Step 3: Implement getTaskOutput method
Add to
src/utils/backgroundTasks.ts:Step 4: Run test to verify it passes
Run:
npm test src/utils/backgroundTasks.test.tsExpected: PASS
Step 5: Write test for incremental output
Add to
src/utils/backgroundTasks.test.ts:Step 6: Run test to verify incremental works
Run:
npm test src/utils/backgroundTasks.test.tsExpected: PASS
Task 4: Implement Task Status and Cleanup
Files:
src/utils/backgroundTasks.tssrc/utils/backgroundTasks.test.tsStep 1: Write failing test for getTaskStatus
Add to
src/utils/backgroundTasks.test.ts:Step 2: Run test to verify it fails
Run:
npm test src/utils/backgroundTasks.test.tsExpected: FAIL with "manager.getTaskStatus is not a function"
Step 3: Implement getTaskStatus
Add to
src/utils/backgroundTasks.ts:Step 4: Run test to verify it passes
Run:
npm test src/utils/backgroundTasks.test.tsExpected: PASS
Step 5: Write failing test for cleanup
Add to
src/utils/backgroundTasks.test.ts:Step 6: Run test to verify it fails
Run:
npm test src/utils/backgroundTasks.test.tsExpected: FAIL with "manager.cleanup is not a function"
Step 7: Implement cleanup method
Add to
src/utils/backgroundTasks.ts:Step 8: Run test to verify it passes
Run:
npm test src/utils/backgroundTasks.test.tsExpected: PASS
Step 9: Commit
Run:
git add src/utils/backgroundTasks.ts src/utils/backgroundTasks.test.tsRun:
git commit -m "feat: implement task status and cleanup"Task 5: Integrate Manager into Context
Files:
src/context.tssrc/session.tsStep 1: Add BackgroundTaskManager to Context interface
In
src/context.ts, find theContexttype and add:Step 2: Initialize manager in session creation
In
src/session.ts, find where Context is created and add:Step 3: Add cleanup hook in session end
In
src/session.ts, find where session cleanup happens and add:Step 4: Run typecheck
Run:
npm run typecheckExpected: No errors
Task 6: Extend bash Tool with Background Support
Files:
src/tools/bash.tsStep 1: Add background parameter to schema
In
src/tools/bash.ts, modify the parameters object:Step 2: Update createBashTool to accept context
Modify function signature:
Step 3: Add background execution logic
In the
executefunction, before the existing logic:Step 4: Update tool description
Add to the description string:
Step 5: Run typecheck
Run:
npm run typecheckExpected: No errors
Task 7: Create bash_output Tool
Files:
src/tools/bash.tssrc/constants.tsStep 1: Add BASH_OUTPUT to tool names
In
src/constants.ts, findTOOL_NAMESand add:Step 2: Create bash_output tool function
Add to end of
src/tools/bash.ts:Step 3: Run typecheck
Run:
npm run typecheckExpected: No errors
Task 8: Register Tools in Context
Files:
src/tool.ts(to understand tool registration)src/index.tsorsrc/context.ts)Step 1: Find tool registration location
Run:
rg "createBashTool" src --type tsExpected: Find where bash tool is created and registered
Step 2: Update bash tool creation to pass context
In the file found above, modify:
Step 3: Add bash_output tool registration
In the same location:
Step 4: Add to tools array/map
Find where tools are collected and add:
Step 5: Run typecheck
Run:
npm run typecheckExpected: No errors
Task 9: Add Memory Management and Limits
Files:
src/utils/backgroundTasks.tssrc/utils/backgroundTasks.test.tsStep 1: Add constants for limits
At top of
src/utils/backgroundTasks.ts:Step 2: Write test for task limit
Add to
src/utils/backgroundTasks.test.ts:Step 3: Implement task limit check
In
src/utils/backgroundTasks.ts, modifystartTask:Step 4: Add output line truncation
Modify
setupProcessHandlersstdout/stderr handlers:Step 5: Run tests
Run:
npm test src/utils/backgroundTasks.test.tsExpected: PASS
Task 11: Manual Testing
Step 1: Build the project
Run:
npm run buildExpected: Build succeeds without errors
Step 2: Start interactive session
Run:
./dist/cli.mjsornpm startExpected: CLI starts successfully
Step 3: Test background task
In CLI, test with LLM:
Expected: LLM uses bash with background=true, returns task ID
Step 4: Test output retrieval
Expected: LLM uses bash_output, shows current output
Step 5: Test incremental mode
Wait a few seconds, then:
Expected: LLM uses bash_output with incremental=true, shows only new lines
Step 6: Test task completion
Wait for task to complete:
Expected: Shows status=completed, exit code 0
Step 7: Test error handling
Expected: Task starts, can retrieve error output
Step 8: Test cleanup
Exit CLI and restart, verify old tasks are gone.
Task 12: Run Full Test Suite and CI
Step 1: Run all tests
Run:
npm testExpected: All tests pass
Step 2: Run typecheck
Run:
npm run typecheckExpected: No type errors
Step 3: Run formatting check
Run:
npm run biome:formatExpected: All files properly formatted
Step 4: Fix any formatting issues
Run:
npm run biome:format -- --writeStep 5: Run full CI pipeline
Run:
npm run ciExpected: All checks pass
Completion Checklist
Beta Was this translation helpful? Give feedback.
All reactions