Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
402 changes: 369 additions & 33 deletions deploy/bind-cli.sh

Large diffs are not rendered by default.

501 changes: 501 additions & 0 deletions deploy/bind_cli_docker_runner_test.go

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions docs/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ The upstream process roles remain `-role api`, `-role worker` and `-role all`. A

Personal runners are connected from the execution-machine page with a short-lived binding code. The installer is served at `/runners/install.sh`. `AGENTFLOW_RUNNER_BINARY` can point to binaries with `{os}` and `{arch}` substitutions. Container images bundle Linux/macOS amd64/arm64 runner and gate binaries. Native source installations can build on demand with Go.

Docker workspaces on macOS use a Linux Runner executable for Git credentials and MCP calls. The installer chooses the architecture of the installed macOS Runner, including Intel binaries running under Rosetta. For a universal Runner on Apple Silicon it installs the arm64 helpers and adds the amd64 helpers when available. Foreground starts and the command printed by `--no-start` select the native arm64 slice, including from a Rosetta shell. On Intel Macs the installer installs the amd64 helpers. By default, it places the helpers next to the host binary. A Runner-side `AGENTFLOW_RUNNER_BINARY` template with `{os}` selects the installation and runtime path. To install both helpers, use `{arch}` and give each architecture a distinct destination. The installer saves the template for upgrades and includes it in launchd and in the startup command printed by `--no-start`. Helper paths must resolve to files separate from the macOS Runner.

The installer checks the helper destinations against the staged macOS binary and installs the required Gate MCP helpers before replacing an installed Runner. When the existing Runner has a working Docker helper, an upgrade requires a compatible helper at the new Runner's configured path before replacing the binary and restarting the service. Relative templates in the installed launchd configuration use the job's working directory. The installer and updater validate the Linux executable's format, architecture, and load segments before replacing it. Custom installs keep a compatible existing helper. A Runner built from a local checkout can build its companion from that checkout. A download URL with an `{os}` placeholder can supply both binaries when used as the Runner's download source.

When platform updates are enabled, macOS runners synchronize the helper in the background at startup. A failed version check is retried during rescans, including when an older helper remains usable. A readable, compatible local ELF or a locally configured script wrapper can be copied into a Docker workspace with executable permissions while the sync runs. Operations that need a missing helper for Git or MCP can wait up to five seconds for the initial sync. The background download continues within its own transfer timeout.

A platform administrator can share one CodeBuddy API Key under **Platform administration → Shared CodeBuddy Key** (平台共享 CodeBuddy Key): pick the network environment that issued it and the models members may use (the first is the default). Members without a key of their own then chat and run SOPs on it, limited to those models; a role configured with another model runs on the default and the run says so. Members with their own key keep using it with any model. Scheduled tasks, clarification, reports, bots and run evaluation still need the runner's own key. A scheduled task uses the key for its selected CLI and does not borrow the shared CodeBuddy key. The key is stored in the database like personal keys and is never shown again after saving. `AGENTFLOW_ASSISTANT_SPONSOR_LOGIN`, which lent one account's key limited to `deepseek-v4.1-flash`, still applies while no shared key is configured.

Configure provider credentials in the platform UI, not the host's global CLI configuration. Credentials are bound to the responsible actor by the upstream runtime. The home composer and SOP roles each choose a built-in CLI independently. Select the executor beside the home model selector; its API key (and optional Codex/Claude base URL) comes from your personal settings. Changing the executor preserves the draft; saved conversations record the executor used. Main Bot writes still pass through the platform tool gateway and its approval checks. Requirement clarification and notifications continue to use CodeBuddy. A scheduled assistant task can select any supported CLI and runs as an ordinary chat turn; Codex and Claude Code still need that account's own credential. Live model tests are opt-in; set `AGENTFLOW_LIVE_TESTS=1` only for the acceptance suites that need to load local credentials.
Expand Down
26 changes: 21 additions & 5 deletions internal/runner/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type Daemon struct {
updateMu sync.RWMutex
updateReady atomic.Bool
updateDownloadMu sync.Mutex
dockerHelperSyncDone chan struct{} // closed after the initial macOS helper sync
cancelClaimLoop context.CancelFunc
heartbeatWake chan struct{}
maintenance runnerMaintenanceState
Expand Down Expand Up @@ -157,7 +158,10 @@ func (d *Daemon) Run(ctx context.Context) error {
claimCtx, cancelClaim := context.WithCancel(ctx)
defer cancelClaim()
d.cancelClaimLoop = cancelClaim
go d.rescanLoop(ctx)
if goruntime.GOOS == "darwin" && !d.cfg.DisableAutoUpdate {
d.dockerHelperSyncDone = make(chan struct{})
}
go d.rescanLoop(ctx, 5*time.Minute)
heartbeatDone := make(chan struct{})
go func() { defer close(heartbeatDone); d.heartbeatLoop(ctx) }()
controlDone := make(chan struct{})
Expand All @@ -172,6 +176,9 @@ func (d *Daemon) Run(ctx context.Context) error {
<-controlDone
d.maintenance.jobs.Wait()
<-heartbeatDone
if d.dockerHelperSyncDone != nil {
<-d.dockerHelperSyncDone
}
closeCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
d.closeAllRunSandboxes(closeCtx)
cancel()
Expand Down Expand Up @@ -371,15 +378,23 @@ func (d *Daemon) availableProviders() []string {
return providers
}

func (d *Daemon) rescanLoop(ctx context.Context) {
ticker := time.NewTicker(5 * time.Minute)
func (d *Daemon) rescanLoop(ctx context.Context, interval time.Duration) {
helperSynced := false
if d.dockerHelperSyncDone != nil {
helperSynced = d.refreshDockerRunnerHelper(ctx, false)
close(d.dockerHelperSyncDone)
}
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
d.refreshRuntimes(ctx)
if d.dockerHelperSyncDone != nil {
helperSynced = d.refreshDockerRunnerHelper(ctx, helperSynced)
}
d.reapIdleRunSandboxes(ctx, runSandboxIdleTTL)
}
}
Expand Down Expand Up @@ -531,6 +546,7 @@ func (d *Daemon) executeTask(ctx context.Context, task domain.RunnerTask) {
// locks, cloning repositories, creating sandboxes and collecting outputs too.
ctx, stopLease := d.keepTaskLease(ctx, task, leaseRenewInterval)
defer stopLease()
ctx = d.withDockerRunnerHelperWait(ctx)
// The read fence ends before the completion report: once the control plane
// sees this task terminal (and lets a paused run continue), a follow-up
// preparation or snapshot must not still find the gate held.
Expand Down Expand Up @@ -761,7 +777,7 @@ func (d *Daemon) executeTerminal(ctx context.Context, task domain.RunnerTask) ex
if err := runtime.ProvisionMCPAssets(ctx, sandbox, workspace, bound, nil); err != nil {
return execResult{status: domain.RunnerTaskFailed, errMsg: fmt.Sprintf("provision MCP: %v", err)}
}
env, err := runtime.ScriptMCPEnvironment(sandbox, workspace, bound.MCPServers)
env, err := runtime.ScriptMCPEnvironment(ctx, sandbox, workspace, bound.MCPServers)
if err != nil {
return execResult{status: domain.RunnerTaskFailed, errMsg: fmt.Sprintf("prepare mcp call: %v", err)}
}
Expand Down Expand Up @@ -964,7 +980,7 @@ func (d *Daemon) executeInvocation(ctx context.Context, task domain.RunnerTask)
gateAssets, _, _ := runtime.PrepareMCPServers(domain.AgentAssetBundle{MCPServers: servers}, d.mcpTarget(false, inv.GitHTTPCredentials))
servers = gateAssets.MCPServers
}
mcpEnv, mcpErr := runtime.ScriptMCPEnvironment(sandbox, workspace, servers)
mcpEnv, mcpErr := runtime.ScriptMCPEnvironment(ctx, sandbox, workspace, servers)
if mcpErr != nil {
return execResult{status: domain.RunnerTaskFailed, errMsg: fmt.Sprintf("prepare mcp call: %v", mcpErr)}, nil
}
Expand Down
163 changes: 150 additions & 13 deletions internal/runner/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package runner
import (
"context"
"crypto/sha256"
"debug/elf"
"encoding/hex"
"encoding/json"
"errors"
Expand All @@ -16,6 +17,8 @@ import (
"strings"
"sync/atomic"
"time"

flowruntime "agentflow/internal/runtime"
)

// ErrUpdateReady tells the entry point to exec the newly installed binary.
Expand Down Expand Up @@ -51,14 +54,136 @@ func (d *Daemon) checkGateMCPUpdates(ctx context.Context) error {
for _, targetOS := range targetOSes {
path := strings.ReplaceAll(strings.ReplaceAll(pattern, "{os}", targetOS), "{arch}", runtime.GOARCH)
endpoint := fmt.Sprintf("%s/runners/agentflow-gate-mcp?os=%s&arch=%s", d.client.base, targetOS, runtime.GOARCH)
if err := d.checkGateMCPUpdate(ctx, endpoint, path); err != nil {
if err := d.checkCompanionBinaryUpdate(ctx, "gate MCP", endpoint, path, nil, true); err != nil {
return fmt.Errorf("%s/%s: %w", targetOS, runtime.GOARCH, err)
}
}
return nil
}

func (d *Daemon) checkGateMCPUpdate(ctx context.Context, endpoint, path string) error {
// Until the first successful sync, a rescan must retry the version check even
// when an older helper is usable. Downloads use the normal updater's timeout.
func (d *Daemon) refreshDockerRunnerHelper(ctx context.Context, repairOnly bool) bool {
if err := d.checkDockerRunnerHelperUpdate(ctx, repairOnly); err != nil {
if ctx.Err() == nil {
log.Printf("Docker Git/MCP helper update: %v", err)
}
return false
}
return true
}

func (d *Daemon) checkDockerRunnerHelperUpdate(ctx context.Context, repairOnly bool) error {
path, err := flowruntime.DockerRunnerHelperPath()
if err != nil {
return err
}
validateInstalled := func(path string) error {
return validateDockerRunnerHelper(path, false)
}
if repairOnly && validateInstalled(path) == nil {
return nil
}
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return fmt.Errorf("create Docker Git/MCP helper directory: %w", err)
}
// Sandboxes copy this executable into the workspace, so an atomic replacement
// can run alongside active tasks and requests waiting for the first download.
return d.checkCompanionBinaryUpdate(ctx, "Docker Git/MCP helper", d.runnerUpdateEndpoint("linux"), path, validateInstalled, false)
}

func (d *Daemon) withDockerRunnerHelperWait(ctx context.Context) context.Context {
if d.dockerHelperSyncDone == nil {
return ctx
}
return flowruntime.WithDockerRunnerHelperWait(ctx, d.waitForDockerRunnerHelper)
}

func (d *Daemon) waitForDockerRunnerHelper(ctx context.Context, path string) error {
select {
case <-d.dockerHelperSyncDone:
return nil
default:
}
// A workspace copy gets its own executable permissions.
if validateDockerRunnerHelper(path, true) == nil {
return nil
}
// A workspace request can give up while the download continues. It never
// controls the downloader's deadline.
waitCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
select {
case <-d.dockerHelperSyncDone:
return nil
case <-waitCtx.Done():
return fmt.Errorf("Docker Git/MCP helper is still syncing: %w", waitCtx.Err())
}
}

func validateDockerRunnerHelper(path string, forWorkspace bool) error {
var machine elf.Machine
switch runtime.GOARCH {
case "arm64":
machine = elf.EM_AARCH64
case "amd64":
machine = elf.EM_X86_64
default:
return fmt.Errorf("unsupported Linux helper architecture %s", runtime.GOARCH)
}
info, err := os.Stat(path)
if err != nil {
return err
}
if !info.Mode().IsRegular() {
return fmt.Errorf("expected regular Linux/%s helper file", runtime.GOARCH)
}
if !forWorkspace && info.Mode().Perm()&0o111 == 0 {
return fmt.Errorf("expected executable Linux/%s helper", runtime.GOARCH)
}
source, err := os.Open(path)
if err != nil {
return err
}
defer source.Close()
if forWorkspace {
// Match the installer for local custom wrappers. Platform artifacts must be ELF.
var header [2]byte
if _, err := source.ReadAt(header[:], 0); err == nil && header == [2]byte{'#', '!'} {
return nil
}
}
file, err := elf.NewFile(source)
if err != nil {
return fmt.Errorf("expected Linux/%s ELF helper: %w", runtime.GOARCH, err)
}
if file.Class != elf.ELFCLASS64 || file.Data != elf.ELFDATA2LSB || file.Machine != machine ||
(file.OSABI != elf.ELFOSABI_NONE && file.OSABI != elf.ELFOSABI_LINUX) ||
(file.Type != elf.ET_EXEC && file.Type != elf.ET_DYN) {
return fmt.Errorf("expected Linux/%s executable, got ELF %s %s %s %s %s", runtime.GOARCH,
file.Class, file.Data, file.Machine, file.OSABI, file.Type)
}
size := uint64(info.Size())
hasExecutableSegment := false
for _, segment := range file.Progs {
if segment.Type != elf.PT_NULL && segment.Filesz > 0 &&
(segment.Off > size || segment.Filesz > size-segment.Off) {
return fmt.Errorf("incomplete Linux/%s ELF helper segment", runtime.GOARCH)
}
if segment.Type == elf.PT_LOAD {
if segment.Filesz > segment.Memsz {
return fmt.Errorf("invalid Linux/%s ELF helper load segment", runtime.GOARCH)
}
hasExecutableSegment = hasExecutableSegment || segment.Filesz > 0 && segment.Flags&elf.PF_X != 0
}
}
if !hasExecutableSegment {
return fmt.Errorf("Linux/%s ELF helper has no executable load segment", runtime.GOARCH)
}
return nil
}

func (d *Daemon) checkCompanionBinaryUpdate(ctx context.Context, name, endpoint, path string, validate func(string) error, requireIdle bool) error {
metadata, err := runnerBinaryMetadata(ctx, endpoint)
if err != nil {
return err
Expand All @@ -67,30 +192,42 @@ func (d *Daemon) checkGateMCPUpdate(ctx context.Context, endpoint, path string)
if err != nil && !os.IsNotExist(err) {
return err
}
if localHash == metadata.SHA256 {
if strings.EqualFold(localHash, metadata.SHA256) && (validate == nil || validate(path) == nil) {
return nil
}
tmp, err := downloadRunnerBinary(ctx, endpoint, path, metadata.SHA256)
if err != nil {
return err
}
defer os.Remove(tmp)
d.updateMu.Lock()
defer d.updateMu.Unlock()
if d.updateReady.Load() || atomic.LoadInt32(&d.running) != 0 || ctx.Err() != nil {
if validate != nil {
if err := validate(tmp); err != nil {
return fmt.Errorf("validate %s: %w", name, err)
}
}
if requireIdle {
if !d.updateMu.TryLock() {
return nil
}
defer d.updateMu.Unlock()
if d.runnerExecutionActive() {
return nil
}
}
if d.updateReady.Load() || ctx.Err() != nil {
return nil
}
current, err := hashFile(path)
if err != nil && !os.IsNotExist(err) {
return err
}
if current != localHash {
return errors.New("gate MCP executable changed during update")
return fmt.Errorf("%s executable changed during update", name)
}
if err := os.Rename(tmp, path); err != nil {
return fmt.Errorf("replace gate MCP executable: %w", err)
return fmt.Errorf("replace %s executable: %w", name, err)
}
log.Printf("gate MCP update installed (%s) at %s", metadata.SHA256[:12], path)
log.Printf("%s update installed (%s) at %s", name, metadata.SHA256[:12], path)
return nil
}

Expand All @@ -107,16 +244,16 @@ func currentRunnerExecutable() (string, error) {

var errRunnerUpdateBusy = errors.New("runner is busy")

func (d *Daemon) runnerUpdateEndpoint() string {
return fmt.Sprintf("%s/runners/agentflow-runner?os=%s&arch=%s", d.client.base, runtime.GOOS, runtime.GOARCH)
func (d *Daemon) runnerUpdateEndpoint(targetOS string) string {
return fmt.Sprintf("%s/runners/agentflow-runner?os=%s&arch=%s", d.client.base, targetOS, runtime.GOARCH)
}

func (d *Daemon) runnerUpdateHashes(ctx context.Context, binary string) (local, expected string, err error) {
local, err = hashFile(binary)
if err != nil {
return "", "", err
}
remote, err := runnerBinaryMetadata(ctx, d.runnerUpdateEndpoint())
remote, err := runnerBinaryMetadata(ctx, d.runnerUpdateEndpoint(runtime.GOOS))
return local, remote.SHA256, err
}

Expand Down Expand Up @@ -157,7 +294,7 @@ func (d *Daemon) installRunnerUpdate(ctx context.Context, binary string) (bool,
if strings.EqualFold(localHash, expectedHash) {
return false, nil
}
tmp, err := downloadRunnerBinary(ctx, d.runnerUpdateEndpoint(), binary, expectedHash)
tmp, err := downloadRunnerBinary(ctx, d.runnerUpdateEndpoint(runtime.GOOS), binary, expectedHash)
if err != nil {
return false, err
}
Expand Down
Loading
Loading