Got it! Thanks to Stephane Chazelas for the help. The trick was to look for the child processes... D'oh!
My script is now:
#!/usr/bin/env bash
ppid=$(xdotool getactivewindow getwindowpid) # PID of process in the window
pid=$(pgrep -P $ppid | tail -n1) # PID of the last child
cwd="$(readlink /proc/${pid:-$ppid}/cwd)" # current CWD of pid, or ppid if no pid
cd "$cwd"
"$@"
You can use it by simply prefixing any command with the name of the script, eg. `incwd urxvt`.
The only caveat is that some programs, like `evince`, reset their `cwd`. I doubt there's anything I can do in these cases.