daemon

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 25, 2026 License: Apache-2.0 Imports: 22 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package daemon serves MCP to many clients from one process, over a unix socket and over Streamable HTTP at once.

The saving is measured, and it is the slope rather than any one total. The load was counted, not chosen: over two days of real use, 48 of 51 servers were asked nothing at all, so the median session makes no call whatsoever.

load      daemon slope       N processes    8 clients            1 client
none      indistinguishable  10 MB/client   10-13 against 77-81  daemon +2-3 MB
8 calls   0,6-0,9 MB/client  39 MB/client   60-62 against 323-330  ties

Starting up is no longer the cost: since ADR 0067 the graph is read by the first query that needs it, so an idle server holds 10 MB where it used to hold 33. What a daemon still saves is the rest -- and at one client it now loses by a couple of megabytes, which is the extra process.

The two doors are indistinguishable at both loads -- 9,8-10,6 MB per client over HTTP against 10,0-10,7 over the socket -- and the door that matters is HTTP because no MCP client configuration dials a unix socket: it takes an executable or a url. Over 108.737 symbols of kena, on Linux, in benchmarks/daemon-cost.

The widest gap is the peak, and it does not depend on anyone asking anything: 179-186 MB for eight processes against 26-29 for one daemon, because eight editors starting at once pay eight processes at once.

Under sustained traffic HTTP costs more -- 11-13 MB per client at 2000 calls a session -- because the SDK gives every session a 10 MiB resumption buffer that response bytes fill. That is a ceiling no real session reaches, it depends on the corpus, and it cannot be capped from here: the handler builds its own transport and takes no event store.

What is *not* the saving is the snapshot. It is the same mapped file in every server and those pages are clean, so a reader expecting its `87 MB` to disappear is looking at the wrong half: the bytes at stake are the private ones. Nor did allocating less buy them -- `LUQUE-2216` to `LUQUE-2220` took `60,5 MB` off what a load allocates and the resident figure moved `0,75 %`, because the allocator recycles those pages rather than keeping them.

At one client a daemon loses by a couple of megabytes on both doors, because a server that answers nothing no longer reads the graph. It wins from the second.

Index

Constants

View Source
const DefaultAddress = "127.0.0.1:7788"

DefaultAddress is the daemon's HTTP bind, loopback and on a port of its own.

It is not the viewer's 7777: those are two contracts versioned independently, and sharing a port would mix them.

View Source
const EndpointName = "daemon.json"

EndpointName is the file a daemon publishes its HTTP endpoint in, inside the state directory.

It exists because a port has no path. The socket carried its own key -- who can traverse the state directory can reach it -- and a loopback port carries none: any local process can connect. So the key moves into this file, which holds the token, and who can read it is exactly who could have opened the socket.

View Source
const MCPPath = "/mcp"

MCPPath is where the streamable HTTP transport is served.

View Source
const SocketName = "daemon.sock"

SocketName is the socket a daemon listens on inside the state directory.

The state directory is the key: two configurations pointing at different directories get different daemons, so a client never reaches a graph built from someone else's repositories. Nothing here shares by machine or by user.

View Source
const TokenName = "daemon.token"

TokenName is the file the token itself lives in, separately from the endpoint.

The split is the difference between a transport a client can be configured for and one it cannot. The endpoint carries liveness -- an address and a pid -- and is removed when the daemon stops. The token is identity, and it has to outlive the process: a client configuration holds it, and a token minted afresh on every restart would invalidate that configuration every time the daemon came back.

Variables

View Source
var ErrAddressNotLoopback = errors.New("daemon: the HTTP address is not loopback")

ErrAddressNotLoopback reports a bind that would expose the graph beyond this host.

View Source
var ErrEndpointUnreachable = errors.New("daemon: the published endpoint answers nothing")

ErrEndpointUnreachable reports a published endpoint nothing answers on.

It is a distinct error from a missing file because the two need different remedies: no file means no daemon ever ran, an unreachable one means a daemon died without withdrawing its endpoint. A caller about to write that url into a client configuration has to tell them apart.

View Source
var ErrSocketPathTooLong = errors.New("daemon: socket path is too long")

ErrSocketPathTooLong reports a state directory whose socket path does not fit the kernel's address field.

Functions

func BearerHeader

func BearerHeader(token string) string

BearerHeader is the header value a client sends. It exists so a caller does not assemble the scheme by hand and get the spacing wrong.

func EndpointPath

func EndpointPath(stateDirectory string) string

EndpointPath returns the file a daemon for stateDirectory publishes.

func Listen

func Listen(options Options) (net.Listener, error)

Listen binds the daemon's socket.

A stale socket file left by a killed process is removed first: a unix socket is a file, and bind refuses an existing path. Removing it is safe because the path is inside the state directory and because a live daemon holding it would answer -- which is what Dial checks before this is called.

The socket is created with its mode already restricted rather than chmod'd afterwards, and that is not a style choice: chmod on a socket returns EINVAL on some filesystems -- a virtiofs bind mount is one -- so a daemon that set the mode after binding could not start there at all. Measured while running `benchmarks/daemon-cost` under Docker.

What the mode buys is platform-dependent, so neither half is left implicit. Linux checks write permission on the socket at connect, so 0600 is a real gate. Darwin ignores socket permissions for connect entirely; there the gate is the state directory, which has to be traversed to reach the path at all.

func LoopbackAddress

func LoopbackAddress(address string) bool

LoopbackAddress reports whether address binds only to this host.

An empty host means every interface, which is the case a reader most often gets wrong: `:7788` is not loopback. There is no branch for it because none is needed -- ParseIP("") is nil, so the last line already answers no -- and a branch that cannot change an answer reads as a check that does something.

func Reachable added in v0.7.0

func Reachable(ctx context.Context, endpoint Endpoint, timeout time.Duration) error

Reachable reports whether something is listening on a published endpoint.

It dials rather than checking the PID, and that is the point: a PID answers whether a process exists, and a client needs to know whether a connection succeeds. On a machine that recycles PIDs the two disagree, and the connection is the one that decides whether the registration works.

It does not send a request. Establishing the connection is what a client's first call does anyway, and an MCP handshake here would cost a session on the daemon to learn something the dial already answered.

func Serve

func Serve(ctx context.Context, listener net.Listener, options Options) error

Serve accepts connections until ctx is cancelled or the listener fails.

Every accepted connection gets its own MCP server rather than sharing one. That is not about isolation: the tool surface is decided when a server is built, because a process with no published generation publishes only index_project and says so in its instructions. A daemon outlives generations, so a server built once at startup would keep telling every future client that there is no graph. Building one per session costs eleven tool registrations and answers with the generation that exists now.

func SocketPath

func SocketPath(stateDirectory string) (string, error)

SocketPath returns the socket a daemon for stateDirectory listens on.

func TokenPath

func TokenPath(stateDirectory string) string

TokenPath returns the token file for a state directory.

Types

type Endpoint

type Endpoint struct {
	// URL is the streamable HTTP endpoint, ready to paste into a client.
	URL string `json:"url"`
	// Token authorises a request. A client sends it as `Authorization: Bearer`.
	Token string `json:"token"`
	// Socket is the unix socket the same daemon also serves, for a client that
	// cannot speak HTTP.
	Socket string `json:"socket"`
	// PID is the daemon holding both, so a reader can tell a live endpoint from
	// a file left behind.
	PID int `json:"pid"`
}

Endpoint is what a client needs to reach a running daemon.

func ReadEndpoint

func ReadEndpoint(stateDirectory string) (Endpoint, error)

ReadEndpoint reads a published endpoint.

It does not check that the daemon is alive: a caller that needs to know dials it. Reporting a stale file as an error here would make every reader implement the same recovery.

func WaitReachable added in v0.7.0

func WaitReachable(ctx context.Context, stateDirectory string, deadline time.Duration) (Endpoint, error)

WaitReachable waits for a published endpoint to answer.

A supervisor starts the daemon asynchronously, so a caller that installs one and reads the endpoint immediately would find no file at all. Polling is what closes that gap; the deadline is what keeps a daemon that cannot bind from hanging the command.

It re-reads the endpoint on every attempt rather than once, because the file a dead daemon left behind carries the previous port: waiting on a stale address would time out while the new daemon was already answering elsewhere.

type HTTPOptions

type HTTPOptions struct {
	// Address is the bind. Empty means DefaultAddress.
	Address string
	// AllowRemote permits a bind outside loopback. Without it a non-loopback
	// address is refused rather than warned about: the socket's whole guarantee
	// is that the graph does not leave the machine, and a warning on a server
	// nobody watches is not a decision.
	AllowRemote bool
	// OnWarning, when set, receives operational notices.
	OnWarning func(message string)
}

HTTPOptions are what the daemon's HTTP listener needs.

type HTTPServer

type HTTPServer struct {
	// contains filtered or unexported fields
}

HTTPServer is a daemon's HTTP half.

func ListenHTTP

func ListenHTTP(options Options, httpOptions HTTPOptions) (*HTTPServer, error)

ListenHTTP binds the daemon's HTTP transport and publishes its endpoint.

The endpoint file is written with the same private umask as the socket, because it holds the token that replaces the socket's mode as the key.

func (*HTTPServer) Addr

func (served *HTTPServer) Addr() net.Addr

Addr returns the address actually bound, which is what a caller logs: a port of zero resolves to a real one only after the bind.

func (*HTTPServer) Close

func (served *HTTPServer) Close() error

Close releases the bind and withdraws the published endpoint without ever having served.

It exists for the caller that binds and then fails at something else: the endpoint file claims a daemon is answering, and leaving it behind sends the next client to a closed port with no way to tell that from a bug. Serve does the same withdrawal on its own way out, so calling both is safe.

func (*HTTPServer) Endpoint

func (served *HTTPServer) Endpoint() Endpoint

Endpoint returns what a client needs to reach this server.

func (*HTTPServer) Serve

func (served *HTTPServer) Serve(ctx context.Context) error

Serve answers until ctx is cancelled, then removes the published endpoint.

type Options

type Options struct {
	StateDirectory string
	SnapshotStore  *hotsnapshot.SnapshotStore
	Registry       *metrics.Registry
	Indexer        indexing.ProjectIndexer

	// OnSession, when set, is called when a session starts and when it ends.
	// The error is nil on a clean end. It is a typed hook rather than a
	// formatter because the caller logs structurally, and a format string would
	// arrive there as one opaque message.
	OnSession func(event string, err error)
}

Options are what a daemon serves.

The store, the registry and the indexer are shared by every session, and that sharing is the point: the snapshot is mapped once and the metrics graph_status reports are the process's, not one client's.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL