Documentation
¶
Overview ¶
Package dev runs the dev reasoning core (HIP-1330) inside the calling process.
The core is this repository's loop — crates/core behind the C ABI of crates/ffi — built as a wasm module, which this package embeds and runs under wazero: no cgo, and no Rust toolchain in a build that imports it. A Core compiles the module once. A Session is one instance of it, with memory of its own, holding one conversation: step it with an Event, perform the Actions it answers with, and step it again with what they observed.
The module imports environ_get, environ_sizes_get, fd_write and proc_exit, and nothing else. It reaches no file, socket, clock or source of randomness, so every effect an Action names is the host's to perform or refuse.
Example ¶
A host drives the loop: it steps the session with an event, performs the actions that come back, and steps it again with what they observed. Here the model is a stand-in that reads one file and then answers.
package main
import (
"context"
"fmt"
"log"
dev "github.com/hanzoai/dev/go"
)
// A host drives the loop: it steps the session with an event, performs the
// actions that come back, and steps it again with what they observed. Here
// the model is a stand-in that reads one file and then answers.
func main() {
ctx := context.Background()
core, err := dev.Open(ctx)
if err != nil {
log.Fatal(err)
}
defer core.Close(ctx)
s, err := core.New(ctx, dev.Config{Model: new("zen5")})
if err != nil {
log.Fatal(err)
}
defer s.Close(ctx)
var state []byte
events := []dev.Event{{Turn: &dev.Turn{ID: 1, Prompt: "What does main.go do?"}}}
for len(events) > 0 {
actions, err := s.Step(ctx, events[0])
if err != nil {
log.Fatal(err)
}
events = events[1:]
for _, a := range actions {
switch op := a.Op; {
case op.Model != nil:
events = append(events, dev.Event{Model: &dev.Answer{ID: a.ID, Reply: model(op.Model)}})
case op.Read != nil:
fmt.Println("read", op.Read.Path)
events = append(events, dev.Event{File: &dev.Output{ID: a.ID, Text: "package main\n\nfunc main() {}\n"}})
case op.Emit != nil:
fmt.Println(*op.Emit)
case op.Save:
// A real host writes this beside its journal, at this action's id.
if state, err = s.Snapshot(ctx); err != nil {
log.Fatal(err)
}
case op.Done != nil:
fmt.Println("done", op.Done.Turn)
}
}
}
fmt.Println(len(state) > 0)
}
// model stands in for a provider: it asks for main.go, and answers once the
// file is in the conversation.
func model(ask *dev.Ask) dev.Reply {
if last := ask.Messages[len(ask.Messages)-1]; last.Tool == nil {
return dev.Reply{Calls: []dev.Call{{Read: &dev.Read{Path: "main.go"}}}}
}
return dev.Reply{Text: new("It declares package main and does nothing.")}
}
Output: read main.go It declares package main and does nothing. done 1 true
Index ¶
Examples ¶
Constants ¶
const ABI = 1
ABI is the version of the C ABI, and of the payload encoding behind it, that this package speaks. Open refuses a module that answers dev_abi with any other.
Variables ¶
var ( ErrHandle = errors.New("dev: the handle names no live session") ErrMalformed = errors.New("dev: the payload is not a message of this protocol") ErrPoison = errors.New("dev: a previous call on this session panicked") ErrPanic = errors.New("dev: this call panicked; the session is now poisoned") ErrNull = errors.New("dev: a pointer the caller must supply was null") ErrBusy = errors.New("dev: the session is inside another call") ErrFull = errors.New("dev: no slot is left for another session") )
The status codes of crates/ffi/include/dev.h. Match them with errors.Is.
Functions ¶
This section is empty.
Types ¶
type Action ¶
Action is what the core asks the host to do, under the id the host's ledger records it by and its result names.
type Agent ¶
Agent is what the model said and the calls it made, each under the id the core minted for it, a call it refused included.
type Ask ¶
type Ask struct {
Model *string `json:"model"`
// Prelude is the session's standing instructions, ahead of the
// conversation.
Prelude *string `json:"prelude"`
Messages []Message `json:"messages"`
}
Ask is a request for the model: the whole conversation, every time.
type Call ¶
Call is a tool call, already typed: the host maps a provider's function call onto one, and the core never dispatches on a string.
type Cancel ¶
type Cancel struct {
Turn uint64 `json:"turn"`
}
Cancel names the turn to stop, by the id its Turn carried.
type Config ¶
type Config struct {
// Model is named in every Model ask; the host routes it.
Model *string `json:"model"`
// Prelude is the standing instructions every ask carries.
Prelude *string `json:"prelude"`
}
Config is what a session is born with.
type Core ¶
type Core struct {
// contains filtered or unexported fields
}
Core is the compiled module. Open one per process, and New or Restore a Session from it per conversation. A Core is safe for concurrent use.
func (*Core) Close ¶
Close releases the runtime. A Session started from it has nothing left to run in, and answers ErrHandle from then on, or ErrPoison if it had already trapped.
func (*Core) Restore ¶
Restore rebuilds a session from its Snapshot, in an instance of its own.
A snapshot damaged in storage — truncated, corrupted, or written under another ABI — answers ErrMalformed. The checksum is not a signature: a host that stores snapshots where others can write must authenticate them itself. Intact bytes are not proof that the sequence agrees with the host's journal either: a snapshot older than the journal mints again ids the host has already dispatched under, so reconcile the two before dispatching.
type Event ¶
type Event struct {
// Turn is a person asking for work.
Turn *Turn
// Model answers the ask dispatched under its id.
Model *Answer
// Exec is a command dispatched under its id exiting.
Exec *Output
// File is a read, write or patch dispatched under its id finishing.
File *Output
// Git is a git command dispatched under its id exiting.
Git *Output
// Browse is a browser request dispatched under its id returning.
Browse *Output
// Timer is time passing; a long turn should checkpoint.
Timer bool
// Cancel stops the turn it names.
Cancel *Cancel
}
Event is what the host tells the core.
type Message ¶
type Message struct {
// User is a person asking for work.
User *string
// Agent is the model speaking, asking for tools, or both.
Agent *Agent
// Tool is a tool answering the call dispatched under its id.
Tool *Tool
}
Message is one entry of the conversation the core carries. An Agent entry carries its calls under the ids they were dispatched with, and a Tool entry names the id it answers, so a provider that wants the call ahead of its result can be driven straight from the transcript.
type Op ¶
type Op struct {
Model *Ask
Read *Read
Write *Write
Patch *Patch
Exec *Exec
Git *Git
Browse *Browse
// Emit is text to show whoever is watching.
Emit *string
// Save asks the host to persist the core's snapshot at this point in the
// sequence.
Save bool
// Done ends a turn.
Done *Done
}
Op is the ten things a core can ask for.
type Patch ¶
Patch is a diff and the one file it edits: the host applies Diff to Path and to nothing else.
type Read ¶
type Read struct {
Path string `json:"path"`
}
Read asks for a file's text.
A path in this protocol is relative to the workspace the host mounted for the session. The core refuses a call whose path would leave it, so no Action names one; resolving a path against the real tree, symlinks and all, is still the host's.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session is one conversation, in a wasm instance of its own: the unit of isolation. What a session does to its memory, a panic included, reaches no other.
That memory is capped at 64 MiB, and every ask and every Snapshot copies the whole conversation several times over while it runs. So a session holds about 14 MiB of conversation, and about 8 MiB in any one event; a call past that traps, and answers ErrPanic. A session restored from its snapshot holds the same conversation and meets the same wall, so start a new session before one reaches it.
A Session is not safe for concurrent use. It runs one call at a time and does not queue: a call that arrives while another is running answers ErrBusy, as DEV_BUSY does in C, and never reaches the instance.
A call runs under its ctx, and each call into the instance under the 5s hanzoai/wasm allows one. A call either one ends is stopped where it stands, and the session with it: it answers ErrHandle, which matches context.Canceled or context.DeadlineExceeded too, and every call after answers ErrHandle. Restore the last Snapshot to carry on.
func (*Session) Close ¶
Close closes the session's instance, and the session and every byte it held go with it. A call after Close answers ErrHandle, and a second Close answers nil.
func (*Session) Snapshot ¶
Snapshot is the core's own state, one of the three facts a session is (HIP-1330): packed under the ABI with a checksum, for Restore.
func (*Session) Step ¶
Step feeds the core one event and returns the actions it answers with, in the order it asks for them. An event it cannot read answers ErrMalformed and changes nothing.
An answer this package cannot read is another matter: the core has taken the event by then, and what it asked for is lost to the host. That ends the session, as an ended context does: ErrHandle, and ErrHandle for every call after. Restore the last Snapshot and step the event again. A Step that answers an error answers no actions.