Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.
JavaScript agent-orchestrationagentic-workflowai-agentsai-codingclaude-codecode-reviewcodebuddycodexcoding-agentscursordeveloper-toolshuman-on-the-loopllmmcpmulti-agentsdlcself-hostedsopworkflow-automationworkflow-engine
package runner
import (
"context"
"encoding/json"
"testing"
"time"
"agentflow/internal/domain"
)
func TestQueuedTaskWithLegacyRequirementsKeepsCodexLoginRelay(t *testing.T) {
const queued = `{
"id": "rt-queued", "runId": "run-queued", "workspace": "runs/run-queued/workspace",
"provider": "codex", "invocation": {"credentialSource": "local"},
"sandbox": {"profile": {"driver": "docker"}, "requirements": {"privateHome": true}}
}`
for _, tc := range []struct {
name, provider, driver, authMode string
wantRelay bool
}{
{name: "Docker Codex account login", provider: "codex", driver: "docker", authMode: domain.RuntimeAuthLocal, wantRelay: true},
{name: "Docker Codex API key", provider: "codex", driver: "docker", authMode: domain.RuntimeAuthPersonal},
{name: "native Codex account login", provider: "codex", driver: "native", authMode: domain.RuntimeAuthLocal},
{name: "Docker Claude account login", provider: "claude-code", driver: "docker", authMode: domain.RuntimeAuthLocal},
} {
t.Run(tc.name, func(t *testing.T) {
var task domain.RunnerTask
if err := json.Unmarshal([]byte(queued), &task); err != nil {
t.Fatal(err)
}
task.Provider = tc.provider
task.Sandbox.Profile.Driver = tc.driver
task.Invocation.CredentialSource = tc.authMode
requirements, resources := taskSandboxSpec(task)
if !requirements.PrivateHome || requirements.RunnerLoginRelay != tc.wantRelay {
t.Fatalf("sandbox requirements = %+v, want private HOME and login relay %v", requirements, tc.wantRelay)
}
if task.Sandbox.Requirements.RunnerLoginRelay {
t.Fatal("loading a task changed its saved requirements")
}
if tc.wantRelay {
_, withRelay, _ := runSandboxKey(task, requirements, resources)
requirements.RunnerLoginRelay = false
_, withoutRelay, _ := runSandboxKey(task, requirements, resources)
if withRelay == withoutRelay {
t.Fatal("Codex login would reuse the container without the relay network")
}
}
})
}
}
func TestSharedRunTasksReuseOneSandboxUntilCleanup(t *testing.T) {
d := &Daemon{cfg: Config{DefaultWorkspaceRoot: t.TempDir()}}
task := func(id, runID, workspace, provider string) domain.RunnerTask {
return domain.RunnerTask{ID: id, RunID: runID, Workspace: workspace, Provider: provider, Sandbox: &domain.RunnerSandboxSpec{Profile: domain.SandboxProfile{ID: "p", Driver: "local"}}}
}
acquire := func(tk domain.RunnerTask) any {
t.Helper()
sandbox, release, err := d.acquireTaskSandbox(context.Background(), tk)
if err != nil {
t.Fatal(err)
}
release()
return sandbox
}
first := acquire(task("t1", "run-1", "runs/run-1/workspace", "terminal"))
if second := acquire(task("t2", "run-1", "runs/run-1/workspace", "terminal")); second != first {
t.Fatal("second node of the run got a new sandbox")
}
if other := acquire(task("t3", "run-1", "runs/run-1/workspace", "codex")); other == first {
t.Fatal("a different sandbox definition shared the container")
}
if legacy := acquire(task("t4", "run-1", "runs/run-1/node", "terminal")); legacy == first {
t.Fatal("a per-node workspace shared the run sandbox")
}
if len(d.runSandboxes) != 2 {
t.Fatalf("shared sandboxes = %d, want 2", len(d.runSandboxes))
}
d.closeRunSandboxes(context.Background(), "run-1")
if len(d.runSandboxes) != 0 {
t.Fatalf("cleanup left %d shared sandboxes", len(d.runSandboxes))
}
if again := acquire(task("t5", "run-1", "runs/run-1/workspace", "terminal")); again == first {
t.Fatal("sandbox survived cleanup")
}
}
// 子 SOP 在父流程的检出目录里跑,但沙箱是自己的,按子流程回收。
func TestChildRunSandboxInParentCheckout(t *testing.T) {
d := &Daemon{cfg: Config{DefaultWorkspaceRoot: t.TempDir()}}
spec := &domain.RunnerSandboxSpec{Profile: domain.SandboxProfile{ID: "p", Driver: "local"}}
parent, releaseParent, err := d.acquireTaskSandbox(context.Background(), domain.RunnerTask{ID: "t1", RunID: "run-parent", Workspace: "runs/run-parent/workspace", Provider: "terminal", Sandbox: spec})
if err != nil {
t.Fatal(err)
}
releaseParent()
child, releaseChild, err := d.acquireTaskSandbox(context.Background(), domain.RunnerTask{ID: "t2", RunID: "run-child", Workspace: "runs/run-parent/workspace", Provider: "terminal", Sandbox: spec})
if err != nil {
t.Fatal(err)
}
releaseChild()
if child == parent || len(d.runSandboxes) != 2 {
t.Fatalf("child must keep its own shared sandbox: %d", len(d.runSandboxes))
}
d.closeRunSandboxes(context.Background(), "run-child")
if len(d.runSandboxes) != 1 {
t.Fatalf("child cleanup left %d sandboxes, want the parent's", len(d.runSandboxes))
}
if sharedRunWorkspace("runs/../workspace") || sharedRunWorkspace("runs/a/b/workspace") {
t.Fatal("unsafe workspace treated as shared")
}
}
// 失败后没人删除的运行不会收到 cleanup_run,空闲太久的共享沙箱要自动回收,正在用的不能回收。
func TestIdleSharedRunSandboxIsReaped(t *testing.T) {
d := &Daemon{cfg: Config{DefaultWorkspaceRoot: t.TempDir()}}
spec := &domain.RunnerSandboxSpec{Profile: domain.SandboxProfile{ID: "p", Driver: "local"}}
_, releaseIdle, err := d.acquireTaskSandbox(context.Background(), domain.RunnerTask{ID: "t1", RunID: "run-idle", Workspace: "runs/run-idle/workspace", Provider: "terminal", Sandbox: spec})
if err != nil {
t.Fatal(err)
}
releaseIdle()
_, releaseBusy, err := d.acquireTaskSandbox(context.Background(), domain.RunnerTask{ID: "t2", RunID: "run-busy", Workspace: "runs/run-busy/workspace", Provider: "terminal", Sandbox: spec})
if err != nil {
t.Fatal(err)
}
defer releaseBusy()
time.Sleep(5 * time.Millisecond)
d.reapIdleRunSandboxes(context.Background(), time.Millisecond)
if len(d.runSandboxes) != 1 {
t.Fatalf("shared sandboxes after reap = %d, want 1", len(d.runSandboxes))
}
for _, entry := range d.runSandboxes {
if entry.runID != "run-busy" {
t.Fatalf("reaped the busy sandbox, kept %s", entry.runID)
}
}
d.closeAllRunSandboxes(context.Background())
if len(d.runSandboxes) != 0 {
t.Fatalf("shutdown left %d shared sandboxes", len(d.runSandboxes))
}
}