Condukt.Sandbox.Services (Condukt v3.0.0)

Copy Markdown View Source

Long-lived processes in a sandbox, such as a dev server or a database, that belong to a session rather than a turn: aborting a turn or a tool call leaves them alone, and they run until the services process stops.

Each service is a sandbox process (Condukt.Sandbox.start_process/4) named condukt-service:<owner>:<name>, outside the ids Condukt.Tools.Bash uses, so nothing a turn does reaches it. The services process starts them, checks their readiness, restarts one that exits, and exposes its port when the sandbox implements Condukt.Sandbox.expose_port/3.

{:ok, services} =
  Condukt.Sandbox.Services.start_link(sandbox,
    owner: session_id,
    services: [
      %{name: "web", command: "bin/dev", port: 4000, ready: {:port, 4000}},
      %{name: "db", command: "postgres -D tmp/db", ready: {:command, "pg_isready"}}
    ]
  )

Condukt.Sandbox.Services.list(services)
#=> [%{name: "web", status: :ready, url: "https://…", restarts: 0}, …]

A service map takes :name, :command, and optionally :cwd, :env, :port (exposed once ready), :ready ({:port, port}, checked with bash's /dev/tcp, or {:command, shell}, which must exit 0; without it a running service is ready), and :restart (:always, the default, :on_failure, or :never). Restarts back off from one second to a minute, and the backoff starts over once a service has stayed ready for 30 seconds, so one that crashes soon after it is ready keeps backing off. A failed status read leaves a service alone until the next check; only a process the sandbox no longer has counts as exited.

Subscribers get {Condukt.Sandbox.Services, name, status} whenever a service's status changes. Pass subscribers: [pid] to start_link/2 to hear the first changes, which may come before a later subscribe/1. A host shows list/1 beside the session's view; the session itself does not hold services.

Environment and secrets are given once, at start. restart/2 with :env starts a service again with new ones, as refreshing credentials needs.

Summary

Functions

Returns a specification to start this module under a supervisor.

Each service's name, status (:starting | :ready | :exited | :failed), url, and restarts.

The sandbox process id of a service.

Starts a service again, with :env replacing its environment when given.

Subscribes the caller to status changes.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

list(services)

Each service's name, status (:starting | :ready | :exited | :failed), url, and restarts.

process_id(owner, name)

The sandbox process id of a service.

restart(services, name, opts \\ [])

Starts a service again, with :env replacing its environment when given.

start_link(sandbox, opts)

subscribe(services)

Subscribes the caller to status changes.