# HTTP Server Working Directory Gotcha

When launching `python3 -m http.server` as a background process from within a session, the server's current working directory (cwd) may differ from the intended document root. This causes 404 errors even when the files exist on disk.

## Problem

```bash
# This seems correct but may fail if the process inherits a different cwd
python3 -m http.server 8765 --directory /home/thesage --bind 127.0.0.1
```

Symptom: `curl http://127.0.0.1:8765/kbc-flow/index.html` returns 404, but `ls /home/thesage/kbc-flow/index.html` confirms the file exists.

## Diagnosis

Check the process's actual cwd:
```bash
# Find the PID
ps aux | grep "http.server"

# Check where it's running from
ls -la /proc/<PID>/cwd
```

If `cwd` points to a subdirectory (e.g., `/home/thesage/tailscale-inbox` or `~/.hermes/scripts`), that's the problem.

## Fix

**Option 1: Kill and restart with explicit cwd**
```bash
# Kill old server
pkill -f "http.server 8765"

# Restart with explicit directory
python3 -m http.server 8765 --directory /home/thesage --bind 127.0.0.1
```

**Option 2: Use absolute path in --directory**
```bash
python3 -m http.server 8765 --directory $(realpath /home/thesage) --bind 127.0.0.1
```

**Option 3: Use `cd` in the command (foreground)**
```bash
cd /home/thesage && python3 -m http.server 8765 --bind 127.0.0.1
```

## Prevention

- Always verify with `curl -s -o /dev/null -w "%{http_code}" <url>` before telling the user the page is ready
- When using `terminal(background=true)`, pass `workdir` parameter if the tool supports it
- Check `/proc/<PID>/cwd` after launching a background server

## See Also

- `references/tailscale-funnel-serving.md` — exposing local server to internet via Tailscale
