Interactive sessions¶
An interactive session is a command whose standard input a program writes and
whose output it reads as it comes: a filter fed with data, a server that runs
until the sandbox goes, a shell in a terminal, the agent of the kit. This page
covers sandbox.attach(), the
Interactive session it returns, and terminals.
import fastsandbox
async def main() -> None:
client = await fastsandbox.connect()
async with client.create(kit="shell") as sandbox, sandbox.running():
upper = await sandbox.attach(["tr", "a-z", "A-Z"])
output = await upper.collect("piped through the sandbox\n")
print(output.stdout.decode(), end="")
greeter = await sandbox.attach("read name; echo hello $name")
await greeter.write("sandbox\n")
await greeter.close_stdin()
async for chunk in greeter:
print(chunk.text, end="")
if __name__ == "__main__":
fastsandbox.run(main())
The first command gets its whole input at once and is collected to its exit; the second is written to, then read chunk by chunk.
Starting a session¶
attach() takes a command as exec() does: a
string run with sh -c, a list, or a full
AttachSpec; with user, working_dir and
env, which, like tty and size, raise TypeError along with a full spec,
which holds them itself. The command runs until it exits or the sandbox goes: a server started
with attach() keeps serving while the program does other work.
Input and output¶
| Call | What it does |
|---|---|
write(data) |
writes a string (as UTF-8) or bytes to the standard input |
close_stdin() |
closes it: the command reads the end of the file |
async for chunk in session |
reads the output as it comes, an OutputChunk at a time |
wait(timeout=60.0) |
waits for the exit code |
inspect() |
the state of the execution |
collect(stdin=b"", exit_timeout=10.0) |
writes stdin, closes it, and gathers everything until the exit |
An OutputChunk has the stream it comes
from, stdout or stderr, and its data as bytes;
text decodes it, replacing invalid
sequences. collect() returns an
AttachOutput, whose stdout and stderr
are bytes; it is only possible before the session is read chunk by chunk.
Used as an async context manager, a session closes its standard input on the way out.
Terminals¶
tty=True gives the command a pseudo-terminal, of size=(width, height)
characters; resize(width, height) changes
it, as a window does. A terminal merges the output into one stream, stdout.
shell = await sandbox.attach(["bash", "-l"], tty=True, size=(120, 40))
await shell.write("ls\n")
await shell.resize(160, 50)
Ending a terminal session
Closing the standard input of a terminal session does not end the
program in it, as closing a terminal would not: type what a user would,
exit or Ctrl-D (await shell.write(b"\x04")).
The shell-interactive example relays a terminal session to the local
terminal, keys, output and window size included (see Examples).
The agent of the kit¶
sandbox.agent_session() starts the agent
of the sandbox kit in a session, with its arguments as sbx run AGENT -- ARGS
passes them, or a full AgentSessionSpec,
which takes no other argument (TypeError). Interactive agents expect a terminal (tty=True); without one, an unattended
run gets its standard output and error apart. To run coding agents on tasks,
see Agents.
Testing
Interactive sessions upgrade their connection to the daemon, as the fake
daemon of fastsandbox.testing does too: its
on_exec handler answers an attached command and the agent of a session,
and the command has ended once it has answered.