Skip to content

Where sessions run

A session needs somewhere to keep its conversation and to wake up again after an eviction. You do not write that place. A process’s Layer says how the work is done; a host takes that Layer and gives each session an instance of its own.

Declare the process by name, with its prose in a Layer beside it:

src/Engineer.process.ts
import * as AI from "@alchemy.run/ai";
import { Workshop } from "./Workshop/Workshop.model.ts";
export class Engineer extends AI.Process<Engineer>()("Engineer", {}) {}
export const EngineerLive = Engineer.toLayer({
description: "Implement one task, from reading the code to a passing change.",
})`
Implement the task you are given with ${AI.Read} and ${AI.Edit}.
When it passes, send ${() => Workshop.CompleteTask}.
`;

The process belongs to no model. Its prose mentions the Domain models it works with, as many as it needs, and each mention makes EngineerLive require that model.

A host is a Layer. Give it the process’s Layer, with what a session runs on and the models its prose mentions, and it gives back the same process:

const EngineerSessions = AICloudflare.ProcessSessions(
EngineerLive.pipe(Layer.provide(Layer.mergeAll(Harness, WorkshopLive))),
);

Each platform has a page: Cloudflare, Rivet, Effect Cluster, and in memory for tests. The rest of what follows is the same on all of them.

A session’s key is its object’s name. Sessions run at the same time and never wait for each other, and each has its own storage, its own alarm and its own sandbox.

A session started by another session gets an object of its own when its process is hosted too: give the host both Layers, Layer.mergeAll(EngineerLive, ResearchLive). One of the same process, or of a process whose prose is on its class, lives in the object of the session that started it.

Anything the hosted Layer is provided to holds the process’s client. get takes the session’s key, and each call goes to that session’s object.

A policy or a saga of a model yields the process:

src/Workshop/TaskWork.policy.ts
import * as Domain from "@alchemy.run/domain";
import * as Effect from "effect/Effect";
import { Engineer } from "../Engineer.process.ts";
import { TaskOpened } from "./OpenTask.command.ts";
import { Task } from "./Task.aggregate.ts";
export class TaskWork extends Domain.Policy("TaskWork", { from: Task, on: [TaskOpened] }) {}
export const TaskWorkLive = TaskWork.toLayer(
Effect.gen(function* () {
const engineer = yield* Engineer;
return Effect.fn(function* ({ source, event }) {
yield* engineer
.get(source.id)
.send(event.description, { requestId: "start" })
.pipe(Effect.orDie);
});
}),
);

A Worker does the same:

src/Api.ts
import * as Cloudflare from "@alchemy.run/cloudflare";
import * as Effect from "effect/Effect";
import { HttpServerRequest } from "effect/http/HttpServerRequest";
import * as HttpServerResponse from "effect/http/HttpServerResponse";
import { Engineer } from "./Engineer.process.ts";
import { Live } from "./Workshop.ts";
export default class Api extends Cloudflare.Worker<Api>()(
"Api",
{ main: import.meta.url },
Effect.gen(function* () {
const engineer = yield* Engineer;
return {
fetch: Effect.gen(function* () {
const request = yield* HttpServerRequest;
const id = new URL(request.url, "http://api").pathname.slice(1);
const reply = yield* engineer.get(id).prompt(yield* request.text);
return HttpServerResponse.text(reply.text);
}).pipe(Effect.orDie),
};
}).pipe(Effect.provide(Live)),
) {}

send returns once the message is stored. prompt waits for the answer.

The client reads a session too. status says whether it is working and what is queued, and history pages through its transcript:

const session = engineer.get(taskId);
const { status, queued } = yield* session.status;
const page = yield* session.history({ after, take: 100 });

history({ after }) returns the items stored after the one you last saw, so a caller that asks again reads only what is new. A Worker can serve the two on a route of its own for a page to poll: examples/cloudflare-ai-issues does.

A session’s live events stream stays in its object. Read it there, or serve it from an object you write.

A Domain model knows nothing of a session. A session tells it by sending a command its prose mentions, as CompleteTask above: the command is an event of the model, which its views show and its policies react to. What the model should know of the work is a command you declare, and the session sends it when the prose says to.

TaskWork imports Engineer, the model imports TaskWork, and Engineer’s prose imports the model. The prose’s mentions and the model’s members are thunks, each read when first used. Break the cycle with thunks says why.

A harness arms the object’s alarm while a session has work to do. An object that is evicted in the middle of a tool call wakes up, and the session carries on. See Surviving a crash for Pi and Sessions and prompts for OpenCode.

A process’s Layer runs its sessions wherever it is built. Yield the process in a Durable Object you write, and its sessions live in that object’s storage, as many as you give keys:

src/Reviewers.ts
import * as Cloudflare from "@alchemy.run/cloudflare";
import * as Effect from "effect/Effect";
import { Reviewer } from "./Reviewer.process.ts";
import { Live } from "./Layers.ts";
export default class Reviewers extends Cloudflare.DurableObject<Reviewers>()(
"Reviewers",
Effect.gen(function* () {
const reviewer = yield* Reviewer;
return Effect.succeed({
review: Effect.fn(function* (id: string, diff: string) {
const reply = yield* reviewer.get(id).prompt(diff);
return reply.text;
}, Effect.orDie),
});
}).pipe(Effect.provide(Live)),
) {}

Here get is a lookup in this object, not a call to another one, and the session’s live events can be read where it is yielded. Reach for it when a session belongs to something that is already an object of yours.