coding

package
v1.801.108 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package coding is the keystone that turns @hanzo from a chatbot into an engineer: it orchestrates ONE autonomous coding run — register a live agent session, dispatch the job to the bot-gateway sandbox runtime, mirror the sandbox's progress into the session live, verify the pushed branch landed in native git, and open a native "PR" work item — then returns a Result the trigger surface (Slack today, console/API tomorrow) renders.

It is a LEAF, transport-agnostic library. It touches its collaborators only through interface seams (Sessions, Tracker, Runner) plus two git functions (CloneURL, VerifyRef), so the whole orchestration is unit-testable with fakes and — critically — coding does NOT import clients/git: git imports clients/integrations, integrations calls coding, so coding->git would cycle. The composition root assembles the real Dispatcher (adapters.go) and injects it into the trigger surface.

ISOLATION: org is the ONLY tenant key and is threaded to every seam call (session, tracker, git, and the bot-gateway X-Org-Id). A run for org A can only ever open A's session, read/verify A's repo, and file A's PR. The clone URL is built from (org, repo) so the sandbox is pointed only at this org's namespace, and the credential (write-only) is scoped by IAM to this org at the edge.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeliverRoutedRunActivity added in v1.801.79

func DeliverRoutedRunActivity(ctx context.Context, in agents.RoutedRun) (agents.RoutedResult, error)

DeliverRoutedRunActivity offers the run to the live mailbox and blocks until the machine reports a terminal result or the budget elapses. It derives an internal deadline from the same budget so the goroutine can never outlive the activity even if the engine does not cancel the passed ctx exactly at StartToClose. Exported for worker registration; not called directly.

func RoutedRunWorkflow added in v1.801.79

func RoutedRunWorkflow(ctx workflow.Context, in agents.RoutedRun) (agents.RoutedResult, error)

RoutedRunWorkflow is the durable owner of one routed run. Exported for worker registration; not called directly.

Types

type Dispatcher

type Dispatcher struct {
	Sessions  Sessions
	Tracker   Tracker
	Runner    Runner
	CloneURL  func(org, repo string) string
	VerifyRef func(ctx context.Context, org, repo, branch string) (string, bool)
	// Log is an optional structured log seam for best-effort mirror failures; nil
	// is fine (mirror failures are non-fatal and simply dropped).
	Log func(msg string, kv ...any)
	// Route enqueues a routed run on the durable engine (the tasks-engine binding
	// in routed.go). Nil disables routing — a run with a TargetID then fails
	// closed rather than silently running in the sandbox.
	Route func(ctx context.Context, run RoutedRun) error
	// TargetGate is the fail-closed liveness+existence check for a routed run's
	// target (agents.TargetDispatchable): the target exists in this org, is
	// online, and has a live runner. Nil disables routing.
	TargetGate func(ctx context.Context, org, targetID string) error
}

Dispatcher wires the seams. The two git functions are injected (not an interface) because they are pure reads with no cloud-side state.

func NewDispatcher

func NewDispatcher(
	cloneURL func(org, repo string) string,
	verifyRef func(ctx context.Context, org, repo, branch string) (string, bool),
	log func(msg string, kv ...any),
) Dispatcher

NewDispatcher assembles the production Dispatcher: sessions on the live agent registry, PRs on the tracker, the runner on coding's own runtime stub, plus the two git seams (cloneURL, verifyRef) the composition root passes from clients/git (which coding cannot import directly). log is the structured logger for best-effort mirror failures.

func (Dispatcher) Run

func (d Dispatcher) Run(ctx context.Context, req Req) Result

Run executes one coding job end to end and returns its Result. It never panics on a seam failure (each is turned into a recorded error); the caller runs it under a bounded, recovered goroutine with a deadline ctx.

type PRInput

type PRInput struct {
	Org      string
	Project  string
	Repo     string
	Base     string
	Head     string
	Title    string
	Body     string
	Assignee string
}

PRInput / PRRef mirror tracker's agent-PR shape without leaking its types into the seam (the adapter bridges).

type PRRef

type PRRef struct {
	Identifier string
	ProjectKey string
	Number     int
}

type Req

type Req struct {
	Org            string
	UserID         string // linked Hanzo subject — session attribution + X-User-Id
	AgentRef       string // agent label (e.g. "hanzo")
	Repo           string
	Project        string // IAM project slug (tracker + git scope); "" = org default
	Base           string // base branch; "" = repo default
	Prompt         string
	CredUser       string
	CredToken      string
	TimeoutSeconds int
	TargetID       string // when set, route to this registered machine instead of the sandbox
}

Req is one coding request the trigger surface dispatches. Credential is the per-org agent git secret the caller resolved from KMS fail-closed; it is relayed to the sandbox and NEVER logged or placed in a session event.

TargetID, when set, ROUTES the run to a registered machine (#48): the run is enqueued as a durable task addressed to that target instead of executing in the cloud-side sandbox, and the credential is NOT used (the machine authenticates with its own). When empty, the local sandbox path runs unchanged.

type Result

type Result struct {
	SessionID string
	Repo      string
	Branch    string
	CommitSha string
	Diffstat  string
	Changed   bool
	OK        bool
	Verified  bool // pushed branch confirmed present in native git
	PR        PRRef
	LogTail   string
	Error     string
	// Routed reports that the run was ENQUEUED to a target machine rather than run
	// in the cloud sandbox. When true, OK means "accepted + queued" (not
	// "completed"): the terminal outcome flows through the session stream as the
	// machine executes. TargetID is the machine it was routed to.
	Routed   bool
	TargetID string
}

Result is the terminal outcome the trigger surface renders.

type RoutedRun added in v1.801.79

type RoutedRun struct {
	Org            string
	TargetID       string
	SessionID      string
	Repo           string
	Project        string
	Base           string
	Branch         string
	Prompt         string
	CloneURL       string
	TimeoutSeconds int
}

RoutedRun is the NON-SECRET spec coding hands the Route seam to enqueue on the durable engine. It mirrors agents.RoutedRun so coding.go stays pure (no agents import); the adapter bridges the two, exactly as PRInput/RunRequest mirror their downstream types. It carries no credential by design — the executing machine authenticates git + model routing with its own already-held creds.

type RunRequest

type RunRequest struct {
	CloneURL          string
	BaseBranch        string
	Branch            string
	Prompt            string
	SessionID         string
	RunTimeoutSeconds int
	CredUser          string
	CredToken         string // write-only secret — never logged
}

RunRequest / Step / RunResult mirror the bot coding contract.

type RunResult

type RunResult struct {
	Branch    string
	CommitSha string
	Diffstat  string
	Changed   bool
	OK        bool
	LogTail   string
	Error     string
}

type Runner

type Runner interface {
	Run(ctx context.Context, org, userID string, req RunRequest, onStep func(Step)) (RunResult, error)
}

Runner is the bot-gateway coding-task seam (clients/bot in-process client).

type Sessions

type Sessions interface {
	Open(ctx context.Context, org, actor, agent, title string) (string, error)
	// OpenOn opens a session tagged with the run's dispatch TARGET, so a routed
	// run shows in mission-control on the machine it was sent to. An empty target
	// behaves exactly like Open.
	OpenOn(ctx context.Context, org, actor, agent, title, target string) (string, error)
	Log(ctx context.Context, org, sessionID, kind, actor string, payload []byte) error
	Close(ctx context.Context, org, sessionID, status string) error
}

Sessions is the live agent-session registry seam (clients/agents in-process).

type Step

type Step struct {
	Type    string
	Step    string
	Message string
	Status  string
}

type Tracker

type Tracker interface {
	CreatePR(ctx context.Context, in PRInput) (PRRef, error)
}

Tracker is the work-item seam (clients/tracker in-process).

Jump to

Keyboard shortcuts

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