Skip to content

How weft is put together

View as Markdown

weft is two Go modules. github.com/weftgo/weft is the framework: one go get, one version, every package from the provider adapters to Studio. github.com/weftgo/weft/core is the agent loop alone, and its only dependency is the OpenTelemetry API. The framework’s root package is a generated facade over core, so weft.Agent and core.Agent are the same type.

The layout is two modules; ADR 0027 split the repository this way in 0.9.0. Before it, github.com/weftgo/weft was the loop and every layer was its own module with its own tag, so a release note ended in a compatibility sentence such as “requires weft v0.8.0 and thread v0.9.1”. The split keeps the property the old layout had, a loop whose dependency list is the OpenTelemetry API, and removes the version matrix. The directory-by-directory layout is in How the weft repository is organised and checked.

ModuleHoldsDepends on
github.com/weftgo/weftthe root facade, openai, anthropic, google, mcp, mw, wefttest, thread (+ sqlite, jsonl, pool), otel, obsdb (+ sqlite, clickhouse), studio, runtime, scope, cmd/weftcore at the same version, the vendor SDKs, the MCP Go SDK, the OpenTelemetry SDK and exporters, SQLite and the ClickHouse driver
github.com/weftgo/weft/corethe loop: agents, tools, messages, events, errors, the model contract, core/mw, core/wefttestthe OpenTelemetry API: go.opentelemetry.io/otel, go.opentelemetry.io/otel/log, go.opentelemetry.io/otel/trace

A release is two tags in order: the core module’s tag (core/ plus the version), then the root, tidied against it, tagged with the bare version. The root’s go.mod requires core at that exact version with no replace; go.work joins the two modules (and two example modules) for development.

The cost is stated in the ADR: a program that requires the framework carries every layer’s dependencies in its module graph. Only the packages it imports are compiled and linked, so the cost is go.sum lines and downloads. A program that wants none of it imports core. Install weft and choose a module shows both.

The root package weft declares nothing of its own. Every exported name of core exists in it as a type alias (type Agent = core.Agent), a constant or variable bound to the original, or a one-line wrapper function, so signatures, generics and doc comments survive on pkg.go.dev. mw, wefttest and wefttest/conformance are the same kind of facade over core/mw, core/wefttest and core/wefttest/conformance.

The facades are generated, not written. internal/facadegen produces them from core’s source (facade.go starts Code generated by internal/cmd/genfacade; DO NOT EDIT.), make generate regenerates them, and TestFacadesAreComplete fails make test when a facade lacks a name its source exports or differs from what the generator writes. How the weft repository is organised and checked covers the gate.

Three rules follow from the facade:

  • User code imports weft; the layers import core. thread, otel, studio and the adapters name core.Model and core.Agent in their signatures. An application holding a weft.Model passes the same type, so nothing converts.
  • Definition sites skip the facade. weft.Tool, weft.Output and weft.Subagent record the caller’s file and line for the manifest. The recorder walks past frames in the facade package, so weft.json names your file, not facade.go.
  • Methods live on core’s page. pkg.go.dev lists no methods under an alias, so Agent.Generate is documented at core.Agent.Generate.

Imports point down. The core imports nothing above it: no HTTP, no database, no tracing backend (rule 6 of Design principles). Because core is its own module, any import from outside it needs a require line in core/go.mod, and the release gate checks both (clause k1 of scripts/ux-gate/gate8.sh: no package outside the core module among core’s dependencies, core/go.mod and go.sum unchanged).

weft's four layers, each importing only the layers below it; the core is its own module, its only dependency the OpenTelemetry API.The module github.com/weftgo/weft holds four layers, top to bottom: the UI (studio, runtime), recording (otel, obsdb), sessions (thread) and the loop, whose root package weft is a generated facade over core. Imports point down: each layer imports only the layers below it, and nothing imports upward. The loop itself, core, is its own module, github.com/weftgo/weft/core, and its only dependency is the OpenTelemetry API.
The four layers, imports pointing down. The loop's core is its own module; its only dependency is the OpenTelemetry API.
LayerPackagesImports from weft
The loopcore, core/mw, core/wefttestnothing outside the core module (core/mw and core/wefttest import core)
The facaderoot weft, mw, wefttestcore (and, for mw and wefttest, the root)
Providers and interopopenai, anthropic, google, mcp, scopecore
Sessionsthread; thread/jsonl, thread/sqlite (storage); thread/poolcore; the storage packages import thread and thread/backend
The recordobsdb, obsdb/sqlite, obsdb/clickhousecore, obsdb
The pipelineotelcore, obsdb, obsdb/sqlite
The Inspectorstudiocore, obsdb, obsdb/sqlite
The playgroundruntimecore, obsdb, otel, studio, thread
The binarycmd/weftobsdb, its two backends, studio

Each row may import the rows above it and none below. The record (obsdb) knows nothing of the pipeline that writes it or the Inspector that reads it. thread knows the loop and nothing about recording: a session’s runs are recorded because the loop reports through the OpenTelemetry providers otel.Install registered, not because thread imports otel.

Where behaviour attaches: two seams and one tap

Section titled “Where behaviour attaches: two seams and one tap”

Inside the loop there are exactly two places code can change behaviour and one place it can watch (rule 10, ADR 0006):

  • weft.WrapModel wraps the model call: func(next weft.Model) weft.Model. mw.Retry and mw.Fallback sit here.
  • weft.WrapTools wraps each tool call: func(next weft.ToolCaller) weft.ToolCaller, per agent or per tool. mw.Allow, mw.Audit and mw.MapErrors sit here.
  • weft.Tap observes every event and changes nothing.

Everything else is configuration the loop reads, not a hook. weft.PrepareStep rewrites the request before the model seam (ADR 0006’s amendment calls it “the one loop knob”). weft.OnRunEnd and weft.OnMessages are observers like Tap. The loop’s OpenTelemetry spans and log records are its own reporting at fixed phases (ADR 0016), emitted through the API that weft.TracerProvider and weft.LoggerProvider point at, and a no-op until an SDK registers. A third seam needs an ADR. The agent loop, from first principles explains why the field’s lifecycle hooks were refused.

From a run to Studio: the pipeline, obsdb and the Inspector

Section titled “From a run to Studio: the pipeline, obsdb and the Inspector”

The loop reports; something else records. Three packages share the work, joined by the OpenTelemetry data model rather than by Go calls.

Recording: the app's runs go through otel.Install to the local sink or a Studio, and the Inspector reads them back.Your app runs agents with agt.Generate; every run leaves as OpenTelemetry spans and log records. otel.Install starts the pipeline and sends them to every destination at once; with no options and no destination named by the environment it writes the local sink only. otel.Local writes .weft/weft.db synchronously; otel.Studio exports OTLP over HTTP to a running weft studio. The Inspector reads them back: embedded with studio.Handler over otel.LocalDB(), or served by weft studio.
otel.Install's pipeline sends each run's spans and log records to every destination at once; obsdb stores them; Studio reads them back.
  1. The pipeline, otel. otel.Install builds OpenTelemetry tracer and logger providers with one or more destinations, each with its own content policy, and installs them as the global providers the loop reports through. With no options it writes the local sink, ./.weft/weft.db, unless the environment names a destination: WEFT_STUDIO_URL, OTEL_EXPORTER_OTLP_ENDPOINT, or a running Studio’s discovery file each become a destination, and then no local sink is written unless you add otel.Local.
  2. The record, obsdb. obsdb.DB is the observability database: runs, records, spans and app logs, with a SQLite backend (the local sink) and a ClickHouse backend. It is the only thing the Inspector reads; otel.LocalDB returns the local sink’s handle.
  3. The Inspector, studio. studio.Handler serves the UI, the JSON API, the live stream and OTLP ingest over one obsdb.DB. Embedded beside the app with studio.DB(otel.LocalDB()) (setup A), it shares the sink’s in-process live hub. Run as weft studio (setup B), it ingests OTLP from any process, in any language.

The UI is a single-page app (TanStack Start and React, built with Bun) prebuilt into studio/dist, committed, and embedded with go:embed. A user of studio needs Go only: there is no build step, and the page loads nothing from another origin. The devtools panel, /panel.js, is served by the same handler and reads the same API. Run Studio has the setups.

Two packages connect the app to Studio in the other direction. runtime.Install dials Studio out so the playground can run experiments as runs of the agents you register. scope.Header tells the devtools panel which conversation an HTTP response belongs to. Neither is imported by the core or by thread.

  • No layer is required. A program that imports core alone has a working agent loop, offline tests through core/wefttest, and spans through whatever OpenTelemetry SDK it registers.
  • No reverse imports. The core cannot call otel, and obsdb cannot call studio. A feature “for the UI” that would need the core to know about Studio is a design error, not a missing import.
  • No single version per layer. The layers ship at the framework’s version. Pre-1.0, thread, obsdb, otel, runtime and studio may change incompatibly in a minor release; core’s API is gated (see Upgrade between releases).

Symbols used: weft.WrapModel · weft.WrapTools · weft.Tap · weft.PrepareStep · weft.OnRunEnd · weft.Tool · weft.Subagent · core.Agent · otel.Install · otel.LocalDB · obsdb.DB · studio.Handler · studio.DB · runtime.Install · scope.Header