Skip to content

Sessions

A session is one agent process working in one directory. It stays open until the scope it was opened in closes, so you can keep giving it work.

turn sends a message and waits for the turn it starts to end.

const agent = yield* Agent
const session = yield* agent.open({ cwd: "/path/to/repo" })
const result = yield* session.turn("Fix the failing test")
yield* Effect.log(result.finalText)

If the turn fails, turn fails with a typed error. See Events and endings.

steer delivers a message into the running turn, at its next tool boundary. If that turn ends first, the message starts a new one.

yield* session.send("Upgrade the project to Effect 4")
// Later, when the user adds something
const steered = yield* session.steer("Leave the docs folder alone")
// { _tag: "Landed", turnId, messageId } or { _tag: "StartedNewTurn", ... }

send returns once the message is accepted. If a turn is running, the message waits for it to end.

yield* session.send("Fix the lint errors")
yield* session.send("Then update the changelog") // runs after the first turn

interrupt ends the running turn with Interrupted. The session stays open.

yield* session.interrupt
yield* session.send("Try a smaller change instead")

Closing the scope stops the agent right away, along with every process it started. end lets queued messages run first.

yield* session.end // finish the queue, then exit

appendSystemPrompt is added to the provider’s default system prompt. It doesn’t replace it.

agent.open({
cwd,
model: "claude-sonnet-5-5",
effort: "high",
appendSystemPrompt: "Commit when the tests pass. Never push."
})

The turn that crosses the cap fails with SpendCapReached. Codex enforces it from estimated cost, by interrupting the turn.

agent.open({ cwd, spendCapUsd: 5 })

Nobody is there to answer questions, so turn off the ask-the-user tools. You can turn off subagents and named tools too.

agent.open({
cwd,
disable: { askUser: true, subagents: true, tools: ["WebSearch"] }
})

Sessions always run with permission checks off (Claude’s bypassPermissions). Without mcp (below) they load no MCP servers: none of the user’s, and no claude.ai connectors or ChatGPT apps. One exception: Codex still loads the servers in its config.toml, because no per-session setting turns those off (see How Codex differs).

mcp lets a session use the MCP servers and claude.ai connectors already set up on the machine, limited by globs. Each call to an MCP tool is checked in the session’s own process before it reaches the server, permissions bypassed or not, and refused unless its name matches an allow glob and no deny glob.

agent.open({
cwd,
mcp: {
allow: ["claude_ai_Slack/slack_{read,search}_*", "claude_ai_Notion/notion-{search,fetch}"],
deny: ["*/*{send,reply,delete,trash}*"]
}
})

Globs are matched against server/tool: the provider’s mcp__<server>__<tool>, split at the first __. A claude.ai connector’s server is claude_ai_<Name>, as claude_ai_Slack or claude_ai_Google_Calendar, and characters outside [A-Za-z0-9_-] are already _. The globs are effect/Glob’s, so * stays within the server or the tool, and {a,b} is either.

  • A refused call reaches the model as the tool’s result, with the reason (“is not in mcp.allow”, or “matches mcp.deny pattern …”). The model may still see the tool.
  • allow: [] loads the servers, but nothing can be called.
  • A name with a second __, which could belong to another server, must pass under every reading: mcp__a__b__c is checked as both a/b__c and a__b/c.
  • Reading MCP resources is always refused, because those tools name the server in their input.
  • Built-in tools are unaffected. disable.tools governs those.
  • On Codex the check is a hook, so it costs what disable.askUser does, and open fails where hooks can’t run. claude.ai connectors are Claude’s; ChatGPT apps stay off.

env is applied over your process’s environment. undefined removes a variable.

agent.open({
cwd,
env: { GIT_PAGER: "cat", NODE_OPTIONS: undefined }
})

Variables that tell an agent it’s nested, such as CLAUDECODE, are always removed. Codex sessions also drop Claude Code’s login token, CLAUDE_CODE_OAUTH_TOKEN: Codex passes its environment to every command it runs. Set it in env to keep it.

RunSettings is RunOptions without hooks, as a Schema. Durations encode as milliseconds and an undefined env value as null, so the result is plain JSON.

const json = yield* Schema.encodeEffect(Schema.fromJsonString(RunSettings))(settingsOf(options))

A beforeStop hook runs when the agent is about to end its turn. Returning text keeps the turn going, with that text added to its context.

agent.open({
cwd,
hooks: {
beforeStop: () =>
testsPass.pipe(
Effect.map((ok) => (ok ? Option.none() : Option.some("The tests still fail.")))
)
}
})

Hooks run inside the agent’s tool loop, so keep them fast. A hook that fails adds nothing. They run even if the user has turned their own hooks off.

beforeTool runs before each tool call, and can add a note the model sees with it.

const beforeTool = (call: ToolHookInput) =>
Effect.succeed(
call.name === "Bash" ? Option.some("Use pnpm, not npm.") : Option.none()
)

beforeTool can return refuseTool(reason) instead: the tool doesn’t run, and the model gets the reason as its result. It holds with permissions bypassed, for built-in tools (Claude’s Bash and Write, Codex’s commands and apply_patch) and MCP tools alike.

const beforeTool = (_call: ToolHookInput) =>
Ref.get(paused).pipe(
Effect.map((on) => (on ? refuseTool("The board is paused; stop here.") : Option.none()))
)

A refusal fails open: a hook that fails or times out, or on a detached session has no client to answer it, refuses nothing. It stops a call before it starts; a command that’s already running, in the background too, runs on. capabilities.refuseTool says whether a session can refuse. A host from a release before 0.7.0 can’t, and adds the reason as a note instead, while the tool runs.

Pass the session id to resume. It fails with SessionNotFound if the provider has no record of it.

yield* store.save(session.sessionId)
// Later, in another process
const session = yield* agent.open({ cwd, resume: yield* store.load })

history returns a stored session’s messages, tool calls and turn endings as events. It doesn’t start an agent.

const past = yield* agent.history(sessionId, {
cwd
})
const prompts = past.filter((event) => event._tag === "UserMessage")