grpcproc
Documentation

An agent runtime

A tutorial that builds one application: a runtime for AI agents, with a process per conversation, the model on GPU nodes, tools on a sandbox node, and tokens streamed to the browser.

The application

The runtime holds conversations with an assistant. A user says something over HTTP; a model answers a token at a time, and the browser shows each as it comes; when the model asks for a tool, the tool is run and the model goes on with what it returned.

one turn, across the four nodes
node gatewaynode gpu-1node gpu-2node sandboxbrowserweb frontPOST · SSEtopicpub/subconversation/1modelsschedulermodelstoolsrunnergenerationa processcalc 6 * 7a processwhat is 6 * 7?Generatetokensrun calcRun calc42Generatetokensdone
node gatewaynode gpu-1node gpu-2node sandboxbrowserweb frontPOST · SSEtopicpub/subconversation/1modelsschedulermodelstoolsrunnergenerationa processcalc 6 * 7a processwhat is 6 * 7?Generatetokensrun calcRun calc42Generatetokensdone
  • Every conversation is a process, started when it is first spoken to and ended when it has been idle.
  • The model runs on GPU nodes. Each takes as many generations as it has slots, and a generation is a process that publishes its tokens.
  • Tools run on a sandbox node, a process per call, under the caller's deadline. The sandbox answers and may ask nothing.
  • A GPU node lost in the middle of an answer costs that answer's first try: another node starts it over.

There is no model here. A script stands in for it, behind the interface a client of an inference server would implement, so that the tests know what it says. This is a conversation with it, as whoever follows the conversation is sent it; one of the tools crashes:

testdata/chat.txt
> hello there
You said: hello there
  [turn 1 done]
> what is 6 * 7?
Let me calculate that.
  [tool] calc 6 * 7
  [ran] 42
The answer is 42.
  [turn 2 done]
> and 7 / 0?
Let me calculate that.
  [tool] calc 7 / 0
  [ran] failed: panic: runtime error: integer divide by zero
The tool failed: panic: runtime error: integer divide by zero.
  [turn 3 done]

The services are built and wired by golang.yandex/di, a dependency-injection container. The same code runs as one program, for development, or as a program per node, as deployed. Every file shown is from examples/guide in the repository, and every output is pinned by its tests.

Package layout

The code comes in four kinds of package. A service exports one function, Module, which registers with the container what the service needs and, if it runs processes, their supervision tree. A contract is the messages a service's process accepts and the name it is registered under; it is all another service may import. The platform is what every program runs. An entry point is a main that picks the services.

examples/guide
cmd/local/               one program: every service on one node
cmd/gateway/             or several: the web front and the conversations,
cmd/gpu/                 the model, on as many nodes as there are GPUs,
cmd/sandbox/             and the tools
internal/platform/       what every program runs: config, node, root supervisor
internal/conversations/  a process per conversation
internal/models/         the scheduler, and a process per generation
internal/tools/          the runner, and a process per tool call
internal/web/            the HTTP front and the chat page
proto/<service>/v1/      a service's contract: messages, names, an address
*_test.go, testdata/     the tests, and the output this tutorial shows

No service knows where the others run. A conversation reaches the tools through their contract and the node the placement gives, the part of the configuration that says which node runs each service, and the model through the models' contract and the nodes it finds in their process group. The runtime as one program and as four differ in two things only: which services each entry point composes, and what each program is told.

the code, not the nodes: four kinds of package, and what imports the contracts
cmd/localcmd/gatewaycmd/gpucmd/sandboxwhich servicesplatformconfig · node · gRPC server · Inspector · root supervisorevery programconversationsmodelstoolsweba Module eachconversations/v1models/v1tools/v1messages, names

Contracts

A contract is a protobuf package: the messages, and a service marked grpcproc.v1.process, whose rpcs are what the process answers, each a call with its reply, or takes, an rpc marked grpcproc.v1.send. What it publishes is part of its contract too: an Event is what the user said, a token, a tool being called, what the tool returned, or the end of a turn.

proto/conversations/v1/conversations.proto
// The conversations service's contract: what a conversation's process
// accepts, what it publishes, and how one is opened. Another service depends
// on this package, never on the service.
syntax = "proto3";

package conversations.v1;

import "google/protobuf/empty.proto";
import "grpcproc/v1/options.proto";
import "guide/proto/tools/v1/tools.proto";

option go_package = "github.com/floatdrop/grpcproc/examples/guide/proto/conversations/v1;conversationsv1";

// Conversation is one conversation's process, registered as "conversation/"
// and its id: what it accepts, and the topic it publishes on.
// protoc-gen-grpcproc writes its address from this; address.go adds what
// every caller does first, opening it.
service Conversation {
  option (grpcproc.v1.process) = {prefix: "conversation/"};
  // Say tells the conversation what the user said, and returns once its
  // turn has begun: after the turns before it. The answer is published to
  // whoever follows the conversation.
  rpc Say(conversations.v1.Say) returns (Said);
  // Events is the conversation's topic: what the user says, every token of
  // its answers, the tools it calls, and the end of each turn.
  rpc Events(google.protobuf.Empty) returns (Event) {
    option (grpcproc.v1.topic) = {name: "events"};
  }
}

// Open is what the conversations supervisor starts a conversation from.
message Open {
  string id = 1;
}

// Say is what the user said. It is answered as its turn begins; the answer
// itself is published, as it comes, on the conversation's topic.
message Say {
  string text = 1;
}

message Said {
  // Which turn of the conversation this is, from 1.
  uint64 turn = 1;
}

// Event is what a conversation publishes on its topic.
message Event {
  uint64 turn = 1;
  // Which try at an answer a token belongs to, or a retrying begins: see
  // models.v1.Generate.
  uint64 attempt = 9;
  oneof kind {
    // What the user said: the turn begins.
    string said = 8;
    // The next piece of what the assistant says.
    string token = 2;
    // The model asked for a tool, which is now called.
    tools.v1.Run tool = 3;
    // What the tool answered.
    tools.v1.Ran ran = 4;
    // The answer starts over, and why: the node generating it was lost.
    string retrying = 5;
    // The turn is over.
    bool done = 6;
    // The turn failed, and why.
    string failed = 7;
  }
}

buf generate runs protoc-gen-grpcproc beside protoc-gen-go, and the service becomes Go: ConversationAddr, an address with a method per rpc that names the reply once, and ConversationHandler, an interface with a method per rpc, with HandleConversationCall to dispatch a call to it from an actor's HandleCall. The tools runner and the models scheduler are actors and answer through theirs; the conversation is a receive loop that takes its one message itself. Each address method takes its sender, a node or a process, as a grpcproc.Caller.

proto/conversations/v1/conversations_grpcproc.pb.go
// ConversationAddr addresses a process of the Conversation service; its methods are its rpcs.
//
// Conversation is one conversation's process, registered as "conversation/"
// and its id: what it accepts, and the topic it publishes on.
// protoc-gen-grpcproc writes its address from this; address.go adds what
// every caller does first, opening it.
type ConversationAddr struct{ grpcproc.Addr[proto.Message] }

// ConversationAddrOf addresses t, a PID, a Name or a global name, as a process of the Conversation service.
func ConversationAddrOf(t grpcproc.Target) ConversationAddr {
	return ConversationAddr{grpcproc.AddrOf[proto.Message](t)}
}

// Say tells the conversation what the user said, and returns once its
// turn has begun: after the turns before it. The answer is published to
// whoever follows the conversation.
func (c ConversationAddr) Say(ctx context.Context, from grpcproc.Caller, req *Say) (*Said, error) {
	return c.Call[*Said](ctx, from, req)
}

// ConversationHandler is what a process of the Conversation service does with what it is
// sent: a method per call, whose result is the reply, as actor.CallHandler's
// is, and one per send. HandleConversationCall and HandleConversationMessage dispatch to it;
// UnimplementedConversationHandler answers for the methods an implementation leaves out.
type ConversationHandler interface {
	Say(p *grpcproc.Process[proto.Message], m grpcproc.Msg[proto.Message], req *Say) (*Said, error)
}

The service names its processes by a prefix, so ConversationProcess(id), ConversationAt(node, id) and ConversationEventsAt(node, id), the topic, are generated too (Contracts from proto has every form). The package writes by hand only what the service cannot say: the type a caller holds, which wraps the generated address with the conversation's node and id, and what every caller does first. The next page has what open and Follow do.

proto/conversations/v1/address.go
// Conversation is one conversation as a caller reaches it: its process, by
// the name it is registered under, and the supervisor that starts it. The
// generated ConversationAddr is its protocol; Say and Follow add what every
// caller does first, starting the conversation's process if none runs.
type Conversation struct {
	ConversationAddr
	node, id string
}

// NewConversation is the conversation id on node, the node that runs the
// conversations service: the generated address, which is registered under
// a name made from the id for as long as the process runs, with what opens
// it.
func NewConversation(node, id string) Conversation {
	return Conversation{ConversationAddr: ConversationAt(node, id), node: node, id: id}
}

// Say tells the conversation what the user said, and returns once its turn
// has begun: after the turns before it. The answer is published to whoever
// follows the conversation. A process that was idle may end between its
// opening and the call: it is then opened again, once.
func (c Conversation) Say(ctx context.Context, from grpcproc.Caller, text string) (said *Said, err error) {
	for range 2 {
		if err = c.open(ctx, from); err != nil {
			return nil, err
		}
		if said, err = c.ConversationAddr.Say(ctx, from, &Say{Text: text}); !errors.Is(err, grpcproc.ErrNoProc) {
			break
		}
	}
	return said, err
}

A refusal is part of an answer. A GPU node with no slot free answers busy, and a tool that fails answers with why, so an error means one thing: no answer came. The request to generate names the topic its tokens go to, which is how a process on a GPU node streams to a conversation it knows nothing else about.

proto/models/v1/models.proto
// Generate asks for the next thing the assistant says. It is a call,
// answered with Generated once the model is done; the tokens are published
// meanwhile, as they come, on the topic it names.
message Generate {
  repeated Turn history = 1;
  // The topic the tokens go to: its node and its name. Its events are
  // conversations.v1.Event.
  string topic_node = 2;
  string topic = 3;
  // The turn of the conversation this answers, set on every token.
  uint64 turn = 4;
  // Which try at the answer this is, larger than any before it, and set on
  // every token: a node that lost its caller may go on publishing after
  // another was asked, and a subscriber drops the tokens of an earlier try.
  uint64 attempt = 5;
}

// Generated is the scheduler's answer. A node with no slot free says so:
// busy is part of the answer, and an error means the node did not answer.
message Generated {
  // Everything the model said.
  string text = 1;
  // The tool it wants called before it goes on, if any.
  tools.v1.Run tool = 2;
  // No slot was free: nothing was generated.
  bool busy = 3;
}