NOTE: I'm really looking for an SSH-y solution here, more than bash-y one.
I need to execute a series of commands over ssh, programatically. There are many ways to do this, but I can't find any that works with my constraints:
- some of the commands set variables in the shell. Therefore I dont think I can make this work using multiple shells. 
- some of these variables are functions, not normal values, so I dont think I can simply serialize the output of - envto "restore" the old environment.
- I need to access the stdout, stderr and exit code of each command separately. This makes it difficult to chain the commands using - &&or- ;. I believe this also makes it difficult to use- screen, because we cant reliably tell when a command finishes.
- I need real-time access to the stdout, stderr, etc so we can record when the output occurs. (I use "real-time" to mean "nowish", not "hard real-time" or anything like that). 
Update
Why do I need a single bash session? Because commands may store arbitrary variables and functions in the environment. For example, it would be common to run rvm, which is a bash function added by a previously run command.
Why can't I use && or ;? Because I need the stdout and stderr or each command individually, and this would mix them.
Why can't I use screen? Because we want to run commands automatically, not as part of an interactive session. Consider the difficulty of this: I'd have to run the command with input form redirection, output via redirection, and then scp the results back. I'd need to echo $? to get the exit code. I wouldn't be able to timestamp the stderr/stdout. I wouldn't be able to timeout the command. And it would be incredibly buggy.

