Skip to content

Commit f28cfac

Browse files
feat: replace background jobs with tmux for persistence
- Update background process management section to use tmux - Add tmux session naming conventions (dev-*, agent-*, etc.) - Replace all & background job examples with tmux patterns - Add session persistence guidance and metadata tracking - Include fallback to container-use when tmux unavailable - Document random port assignment in tmux sessions - Add Playwright testing in tmux sessions
1 parent d3f6039 commit f28cfac

File tree

2 files changed

+240
-102
lines changed

2 files changed

+240
-102
lines changed

claude-code-4.5/CLAUDE.md

Lines changed: 120 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,12 @@ async def process_payment(amount: int, customer_id: str):
177177
# Background Process Management
178178

179179
<background_server_execution>
180-
CRITICAL: When starting any long-running server process (web servers, development servers, APIs, etc.), you MUST:
180+
CRITICAL: When starting any long-running server process (web servers, development servers, APIs, etc.), you MUST use tmux for persistence and management:
181181

182-
1. **Always Run in Background**
182+
1. **Always Run in tmux Sessions**
183183
- NEVER run servers in foreground as this will block the agent process indefinitely
184-
- Use background execution (`&` or `nohup`) or container-use background mode
184+
- ALWAYS use tmux for background execution (provides persistence across disconnects)
185+
- Fallback to container-use background mode if tmux unavailable
185186
- Examples of foreground-blocking commands:
186187
- `npm run dev` or `npm start`
187188
- `python app.py` or `flask run`
@@ -193,96 +194,164 @@ CRITICAL: When starting any long-running server process (web servers, developmen
193194
- ALWAYS use random/dynamic ports to avoid conflicts between parallel sessions
194195
- Generate random port: `PORT=$(shuf -i 3000-9999 -n 1)`
195196
- Pass port via environment variable or command line argument
196-
- Document the assigned port in logs for reference
197+
- Document the assigned port in session metadata
197198

198-
3. **Mandatory Log Redirection**
199-
- Redirect all output to log files: `command > app.log 2>&1 &`
200-
- Use descriptive log names: `server.log`, `api.log`, `dev-server.log`
201-
- Include port in log name when possible: `server-${PORT}.log`
202-
- Capture both stdout and stderr for complete debugging information
199+
3. **tmux Session Naming Convention**
200+
- Dev environments: `dev-{project}-{timestamp}`
201+
- Spawned agents: `agent-{timestamp}`
202+
- Monitoring: `monitor-{purpose}`
203+
- Examples: `dev-myapp-1705161234`, `agent-1705161234`
203204

204-
4. **Container-use Background Mode**
205-
- When using container-use, ALWAYS set `background: true` for server commands
206-
- Use `ports` parameter to expose the randomly assigned port
207-
- Example: `mcp__container-use__environment_run_cmd` with `background: true, ports: [PORT]`
205+
4. **Session Metadata**
206+
- Save session info to `.tmux-dev-session.json` (per project)
207+
- Include: session name, ports, services, created timestamp
208+
- Use metadata for session discovery and conflict detection
208209

209-
5. **Log Monitoring**
210-
- After starting background process, immediately check logs with `tail -f logfile.log`
211-
- Use `cat logfile.log` to view full log contents
212-
- Monitor startup messages to ensure server started successfully
213-
- Look for port assignment confirmation in logs
210+
5. **Log Capture**
211+
- Use `| tee logfile.log` to capture output to both tmux and file
212+
- Use descriptive log names: `server.log`, `api.log`, `dev-server.log`
213+
- Include port in log name when possible: `server-${PORT}.log`
214+
- Logs visible in tmux pane AND saved to disk
214215

215216
6. **Safe Process Management**
216-
- NEVER kill by process name (`pkill node`, `pkill vite`, `pkill uv`) - this affects other parallel sessions
217+
- NEVER kill by process name (`pkill node`, `pkill vite`, `pkill uv`) - affects other sessions
217218
- ALWAYS kill by port to target specific server: `lsof -ti:${PORT} | xargs kill -9`
218-
- Alternative port-based killing: `fuser -k ${PORT}/tcp`
219-
- Check what's running on port before killing: `lsof -i :${PORT}`
220-
- Clean up port-specific processes before starting new servers on same port
219+
- Alternative: Kill entire tmux session: `tmux kill-session -t {session-name}`
220+
- Check what's running on port: `lsof -i :${PORT}`
221221

222222
**Examples:**
223223
```bash
224-
# ❌ WRONG - Will block forever and use default port
224+
# ❌ WRONG - Will block forever
225225
npm run dev
226226

227227
# ❌ WRONG - Killing by process name affects other sessions
228228
pkill node
229229

230-
# ✅ CORRECT - Complete workflow with random port
230+
# ❌ DEPRECATED - Using & background jobs (no persistence)
231231
PORT=$(shuf -i 3000-9999 -n 1)
232-
echo "Starting server on port $PORT"
233232
PORT=$PORT npm run dev > dev-server-${PORT}.log 2>&1 &
234-
tail -f dev-server-${PORT}.log
233+
234+
# ✅ CORRECT - Complete tmux workflow with random port
235+
PORT=$(shuf -i 3000-9999 -n 1)
236+
SESSION="dev-$(basename $(pwd))-$(date +%s)"
237+
238+
# Create tmux session
239+
tmux new-session -d -s "$SESSION" -n dev-server
240+
241+
# Start server in tmux with log capture
242+
tmux send-keys -t "$SESSION:dev-server" "PORT=$PORT npm run dev | tee dev-server-${PORT}.log" C-m
243+
244+
# Save metadata
245+
cat > .tmux-dev-session.json <<EOF
246+
{
247+
"session": "$SESSION",
248+
"port": $PORT,
249+
"created": "$(date -Iseconds)"
250+
}
251+
EOF
252+
253+
echo "✓ Dev server started in tmux session: $SESSION"
254+
echo " Port: $PORT"
255+
echo " Attach: tmux attach -t $SESSION"
256+
echo " Logs: dev-server-${PORT}.log or view in tmux"
235257

236258
# ✅ CORRECT - Safe killing by port
237259
lsof -ti:${PORT} | xargs kill -9
238260

239-
# ✅ CORRECT - Check what's running on port first
240-
lsof -i :${PORT}
261+
# ✅ CORRECT - Or kill entire session
262+
tmux kill-session -t "$SESSION"
241263

242-
# ✅ CORRECT - Alternative killing method
243-
fuser -k ${PORT}/tcp
264+
# ✅ CORRECT - Check session status
265+
tmux has-session -t "$SESSION" 2>/dev/null && echo "Session running"
244266

245-
# ✅ CORRECT - Container-use with random port
267+
# ✅ CORRECT - Attach to monitor logs
268+
tmux attach -t "$SESSION"
269+
270+
# ✅ CORRECT - Flask/Python in tmux
271+
PORT=$(shuf -i 5000-5999 -n 1)
272+
SESSION="dev-flask-$(date +%s)"
273+
tmux new-session -d -s "$SESSION" -n server
274+
tmux send-keys -t "$SESSION:server" "FLASK_RUN_PORT=$PORT flask run | tee flask-${PORT}.log" C-m
275+
276+
# ✅ CORRECT - Next.js in tmux
277+
PORT=$(shuf -i 3000-3999 -n 1)
278+
SESSION="dev-nextjs-$(date +%s)"
279+
tmux new-session -d -s "$SESSION" -n server
280+
tmux send-keys -t "$SESSION:server" "PORT=$PORT npm run dev | tee nextjs-${PORT}.log" C-m
281+
```
282+
283+
**Fallback: Container-use Background Mode** (when tmux unavailable):
284+
```bash
285+
# Only use if tmux is not available
246286
mcp__container-use__environment_run_cmd with:
247287
command: "PORT=${PORT} npm run dev"
248288
background: true
249289
ports: [PORT]
250-
251-
# ✅ CORRECT - Flask/Python example
252-
PORT=$(shuf -i 3000-9999 -n 1)
253-
FLASK_RUN_PORT=$PORT python app.py > flask-${PORT}.log 2>&1 &
254-
255-
# ✅ CORRECT - Next.js example
256-
PORT=$(shuf -i 3000-9999 -n 1)
257-
PORT=$PORT npm run dev > nextjs-${PORT}.log 2>&1 &
258290
```
259291

260-
**Playwright Testing Background Execution:**
292+
**Playwright Testing in tmux:**
261293

262-
- **ALWAYS run Playwright tests in background** to prevent agent blocking
263-
- **NEVER open test report servers** - they will block agent execution indefinitely
264-
- Use `--reporter=json` and `--reporter=line` for programmatic result parsing
265-
- Redirect all output to log files for later analysis
294+
- **Run Playwright tests in tmux** for persistence and log monitoring
295+
- **NEVER open test report servers** - they block agent execution
296+
- Use `--reporter=json` and `--reporter=line` for programmatic parsing
266297
- Examples:
267298

268299
```bash
269-
# ✅ CORRECT - Background Playwright execution
270-
npx playwright test --reporter=json > playwright-results.log 2>&1 &
300+
# ✅ CORRECT - Playwright in tmux session
301+
SESSION="test-playwright-$(date +%s)"
302+
tmux new-session -d -s "$SESSION" -n tests
303+
tmux send-keys -t "$SESSION:tests" "npx playwright test --reporter=json | tee playwright-results.log" C-m
271304

272-
# ✅ CORRECT - Custom config with background execution
273-
npx playwright test --config=custom.config.js --reporter=line > test-output.log 2>&1 &
305+
# Monitor progress
306+
tmux attach -t "$SESSION"
307+
308+
# ❌ DEPRECATED - Background job (no persistence)
309+
npx playwright test --reporter=json > playwright-results.log 2>&1 &
274310

275311
# ❌ WRONG - Will block agent indefinitely
276312
npx playwright test --reporter=html
277313
npx playwright show-report
278314

279315
# ✅ CORRECT - Parse results programmatically
280-
cat playwright-results.json | jq '.stats'
281-
tail -20 test-output.log
316+
cat playwright-results.log | jq '.stats'
282317
```
283318

319+
**Using Generic /start-* Commands:**
320+
321+
For common development scenarios, use the generic commands:
322+
323+
```bash
324+
# Start local web development (auto-detects framework)
325+
/start-local development # Uses .env.development
326+
/start-local staging # Uses .env.staging
327+
/start-local production # Uses .env.production
328+
329+
# Start iOS development (auto-detects project type)
330+
/start-ios Debug # Uses .env.development
331+
/start-ios Staging # Uses .env.staging
332+
/start-ios Release # Uses .env.production
333+
334+
# Start Android development (auto-detects project type)
335+
/start-android debug # Uses .env.development
336+
/start-android staging # Uses .env.staging
337+
/start-android release # Uses .env.production
338+
```
284339

285-
RATIONALE: Background execution with random ports prevents agent process deadlock while enabling parallel sessions to coexist without interference. Port-based process management ensures safe cleanup without affecting other concurrent development sessions. This maintains full visibility into server status through logs while ensuring continuous agent operation.
340+
These commands automatically:
341+
- Create organized tmux sessions
342+
- Assign random ports
343+
- Start all required services
344+
- Save session metadata
345+
- Setup log monitoring
346+
347+
**Session Persistence Benefits:**
348+
- Survives SSH disconnects
349+
- Survives terminal restarts
350+
- Easy reattachment: `tmux attach -t {session-name}`
351+
- Live log monitoring in split panes
352+
- Organized multi-window layouts
353+
354+
RATIONALE: tmux provides persistence across disconnects, better visibility through split panes, and session organization. Random ports prevent conflicts between parallel sessions. Port-based or session-based process management ensures safe cleanup. Generic /start-* commands provide consistent, framework-agnostic development environments.
286355
</background_server_execution>
287356

288357
# Session Management System

0 commit comments

Comments
 (0)