capcompute

package module
v0.0.0-...-2a8be54 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: Apache-2.0 Imports: 9 Imported by: 2 modules, 1 package

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.

README

capcompute

capcompute is a Go library that runs WebAssembly (Wasm) programs as processes. A process can affect the outside world only through one host function, the syscall. The host answers each syscall through a dispatcher that the application supplies.

It is the processor of a small operating system:

  • A Wasm module is a program.
  • A running instance is a process.
  • Host features (network, LLM calls, storage) are syscalls.

docs/ARCHITECTURE.md describes the model and its laws.

Where it fits

capcompute is the bottom layer of the Aurora repos. The aurora-dist README shows how they fit together. Above this repo:

  • aurora-capcompute: the runtime. It owns the reference monitor (grants, flow policy), replay, the journal, sessions, retries and approvals.
  • aurora-dispatchers: the drivers that make HTTP calls, run allowlisted commands and call an LLM.
  • aurora-brains: the agent programs.

What it gives you

Property Effect
No ambient authority. The syscall is the only way out: no filesystem, no network, no environment. Code that an LLM wrote reaches only what the dispatcher serves.
Deterministic execution. The processor owns the WASI clock and random source. A fresh instance sees the same sequence. The layer above can rebuild a process by replaying its journal.
Resource caps. A memory cap per program (Config.MaxMemoryPages), and the deadline of the context a quantum runs under. A guest cannot exhaust the host or loop forever.
Yield and resume. A syscall can yield for outside work, such as an approval or a timer. A process can wait without holding a thread.

The journal, replay, savepoints, grants and flow control are built on this gate in aurora-capcompute (monitor/, replay/, journaled/).

Quick start

Requirement: Go 1.26 or later. The guest tests build their Wasm fixtures with the same go toolchain.

go build ./...
go vet ./...
go test ./...          # -short skips the Wasm guest tests

If the default Go cache is not writable, set GOCACHE:

GOCACHE=/tmp/capcompute-go-build go test ./...

This is a library. The only runnable Wasm is the test fixtures in testdata/.

Lifecycle

A host uses three calls: NewProgram, NewProcess and Resume.

// 1. Compile a program from an Extism manifest.
program, err := capcompute.NewProgram(ctx, capcompute.Config{
    Image:        extism.Manifest{Wasm: []extism.Wasm{extism.WasmFile{Path: "plugin.wasm"}}},
    PluginConfig: extism.PluginConfig{EnableWasi: true},
})
defer program.Close(ctx)

// 2. Create a process. Input, credential and dispatcher are explicit.
//    The process inherits nothing.
process, err := capcompute.NewProcess(ctx, program, capcompute.ProcessSpec[Run]{
    Input:      json.RawMessage(`{"task":"example"}`),
    Entrypoint: "run",
    Cred:       Run{ID: "proc-1"}, // host-side identity; the guest never sees it
    Dispatcher: myDispatcher{},
})
defer process.Close(ctx)

// 3. Run one quantum and read its single result. Ending ctx stops it.
results, _ := capcompute.Resume(ctx, process)
result := <-results
switch result.Status {
case capcompute.ResumeCompleted: // the guest finished
case capcompute.ResumeYielded:   // the guest waits for outside work
case capcompute.ResumeStopped:   // cancelled; create a new process to continue
case capcompute.ResumeFailed:    // apply your error policy
}

Resume binds a dispatch closure to the process's credential and dispatcher. The host function calls that closure, so it needs no process lookup.

The dispatcher

The application answers a process's syscalls:

func (myDispatcher) Dispatch(ctx context.Context, cred Run, call sys.Syscall, auth sys.Authorization) (sys.SyscallResult, error) {
    switch call.Name {
    case "echo":
        return sys.Result(call.Args), nil
    case "wait":
        return sys.Yield("waiting for outside work"), nil
    default:
        return sys.Fail("unknown syscall"), nil
    }
}

The dispatcher decides what a syscall does. Policy (which operations exist, labels, flow rules, approval) belongs to the runtime above. The processor has no vocabulary for it.

The syscall contract

A guest imports one host function:

//go:wasmimport extism:host/compute syscall
func hostSyscall(uint64) uint64

The request is a sys.Syscall and the response is a sys.SyscallResult, both as JSON (ABI version 4, sys.ABIVersion). The journal and the dispatcher chain use the same types. A response has one of three statuses:

  • result: the syscall completed and returned a value.
  • yield: the host needs outside work before the guest can continue.
  • failed: an errno (denied, expired, not_found, invalid_args, transient, internal, bad_abi) and a message.

The guest returns {"status":"completed"} or {"status":"yielded"} from its exported function. The processor reserves these names: sys.begin, sys.commit, sys.input, sys.output, sys.log, sys.abort, sys.timer, sys.now, sys.random.

Scope

capcompute does not own queues, schedulers, stores, replay, async completion or agent policy. The systems on top own them. Every .go file here is processor API that something uses, or a test. Code with no consumer is removed.

Layout

processor.go   Program, Process, NewProcess, Resume
host.go        the host function and syscall dispatch
ambient.go     the deterministic clock and random source
sys/           Syscall, SyscallResult, Dispatcher, errno: the ABI v4 envelope
docs/          ARCHITECTURE.md (the model), ROADMAP.md
testdata/      Wasm guest fixtures for the integration tests

Documentation

Overview

Package capcompute is the processor: it runs Extism wasm programs as processes whose only access to the outside world is host-dispatched syscalls.

The design docs call the processor's role "kernel", in the library operating system model (docs/ARCHITECTURE.md). In code the component is the processor.

Three calls:

  • NewProgram compiles a program image. It refuses any ambient authority: no allowed hosts or paths. The processor pins the clock, the RNG and the environment, and the caller cannot supply them.
  • NewProcess makes a process from a program, with posix_spawn semantics: explicit input, an explicit credential and an explicit syscall dispatcher. Nothing is inherited.
  • Resume gives a process the CPU for one cooperative quantum, until it completes, yields, fails, or is stopped.

The guest reports how a quantum ended in its output: {"status":"completed"} gives ResumeCompleted and {"status":"yielded"} gives ResumeYielded. Any other output, and any guest or runtime error, gives ResumeFailed. A quantum whose ctx ends gives ResumeStopped and terminates the process.

The wrapping system owns process lookup, replay scheduling, async completion, journal policy and cleanup. This package owns none of them.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAmbientAuthority    = errors.New("image grants ambient authority")
	ErrImageMemoryOverride = errors.New("image overrides the program-owned memory cap")
	ErrInvalidGuestOutput  = errors.New("invalid guest output")
	ErrProcessActive       = errors.New("process is already active")
	ErrProcessTerminated   = errors.New("process is terminated")
)

Functions

func Resume

func Resume[K any](ctx context.Context, process *Process[K]) (<-chan ResumeResult, error)

Resume gives a process the CPU in its own goroutine and returns the channel its outcome arrives on. The process runs until it completes, yields, fails, or is stopped — cooperative scheduling, no preemption. ctx is the only way to stop it: a guest still running when ctx ends is stopped, and a stopped process is terminated.

Types

type Config

type Config struct {
	Image        extism.Manifest
	PluginConfig extism.PluginConfig

	// MaxMemoryPages caps each process's linear memory in 64KiB wasm pages
	// (0 = runtime default). A guest allocating past the cap traps and the
	// resume reports ResumeFailed.
	MaxMemoryPages uint32
}

Config contains everything needed to compile a program image.

Image is the program image (an Extism manifest naming the wasm and static config). It must not grant ambient authority: AllowedHosts and AllowedPaths must be empty — network and filesystem access are capabilities served by the dispatcher, never ambient rights. Guest module configuration (clock, RNG, env) is owned by the processor and cannot be supplied by the caller.

type Process

type Process[K any] struct {
	// contains filtered or unexported fields
}

Process owns the reusable Extism plugin instance for one spawned program. Its quanta are serialized: a second Resume while one runs is refused.

func NewProcess

func NewProcess[K any](ctx context.Context, program *Program, spec ProcessSpec[K]) (*Process[K], error)

NewProcess instantiates a fresh process from the program image — posix_spawn semantics: a named program, explicit input, explicitly handed authority (the dispatcher), nothing inherited.

func (*Process[K]) Close

func (process *Process[K]) Close(ctx context.Context) error

Close releases the Extism plugin instance owned by this process.

type ProcessSpec

type ProcessSpec[K any] struct {
	Input      json.RawMessage
	Entrypoint string
	Cred       K
	Dispatcher sys.Dispatcher[K]
}

ProcessSpec describes one process: its program input, entrypoint, credential, and the dispatcher that will serve its syscalls.

Cred is the host-side credential for the process: it identifies the process and carries whatever authority context the app attaches. It is never visible to or supplied by the guest.

type Program

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

Program is one compiled program image: a factory of processes. Many processes may run one program, and they need not share a credential type — the image is just code, so the credential lives on the Process.

func NewProgram

func NewProgram(ctx context.Context, config Config) (*Program, error)

NewProgram compiles a program image and registers the syscall host function. It rejects images that grant ambient authority (non-empty AllowedHosts or AllowedPaths) with ErrAmbientAuthority.

func (*Program) Close

func (p *Program) Close(ctx context.Context) error

Close releases the compiled program image without touching processes.

type ResumeResult

type ResumeResult struct {
	Status ResumeStatus
	Output json.RawMessage
	Err    error
}

ResumeResult is how one quantum ended. Output is the guest's own output. Err says why a quantum failed or was stopped.

type ResumeStatus

type ResumeStatus string

ResumeStatus is how one quantum ended.

const (
	ResumeCompleted ResumeStatus = "completed"
	ResumeYielded   ResumeStatus = "yielded"
	ResumeStopped   ResumeStatus = "stopped"
	ResumeFailed    ResumeStatus = "failed"
)

Directories

Path Synopsis
Package sys defines the vocabulary of the syscall boundary: a Syscall from the guest, a SyscallResult (result, yield, or failure) back, and the Dispatcher interface that turns one into the other.
Package sys defines the vocabulary of the syscall boundary: a Syscall from the guest, a SyscallResult (result, yield, or failure) back, and the Dispatcher interface that turns one into the other.

Jump to

Keyboard shortcuts

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