Session

works.iterative.claude.direct.Session
trait Session

An active Claude Code session backed by a long-lived CLI process.

Each session maintains a single CLI process whose stdin is kept open for multi-turn conversation. send writes a prompt to stdin; stream reads stdout until the next ResultMessage — see the hazard note on stream before correlating the two. Call close to shut down the process cleanly when the session is no longer needed.

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Abstract methods

def close(): Unit

Shuts down the underlying CLI process.

Shuts down the underlying CLI process.

Closes stdin, waits briefly for the process to exit, and forcibly destroys it if it does not exit in time.

Attributes

def send(prompt: String): Unit

Writes a prompt to the session's stdin as an SDKUserMessage JSON line.

Writes a prompt to the session's stdin as an SDKUserMessage JSON line.

Attributes

def sessionId: String

The session ID assigned by the CLI.

The session ID assigned by the CLI.

Returns "pending" until the CLI emits an init SystemMessage or until the first turn completes (at which point the ResultMessage session ID is used).

Attributes

def stream(): Flow[Message]

Reads stdout messages until the next ResultMessage.

Reads stdout messages until the next ResultMessage.

HAZARD — the messages read are NOT "the reply to your last send". The underlying message queue is shared and long-lived across the whole session, and the CLI owns turn boundaries:

  • it emits output with no send in flight (a background task finishing resumes the main loop and produces a full extra ResultMessage), and
  • it merges a send issued while a turn is running into that turn, so the merged send never gets a ResultMessage of its own. Correlating send and stream positionally therefore attributes replies to the wrong message. At most one consumer may read at a time; concurrent readers silently split the messages between them. See docs/adr/0001-session-is-a-stream-not-turns.md.

Attributes