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 httpapi
import (
"agentflow/internal/domain"
"agentflow/internal/engine"
"agentflow/internal/repo"
"agentflow/internal/runtime"
"bytes"
"context"
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"github.com/gin-gonic/gin"
)
func sharedWorkbenchFixture(t *testing.T) (*assistantHandlers, domain.User, domain.User, domain.User, domain.AssistantSession) {
t.Helper()
h, a := workbenchFixture(t)
for _, name := range []string{"peer", "outsider"} {
if err := h.repo.PutUser(domain.User{LoginName: name, CodeBuddyAPIKey: name + "-test-key"}); err != nil {
t.Fatal(err)
}
}
b, _ := h.repo.UserByLoginName("peer")
out, _ := h.repo.UserByLoginName("outsider")
p, _ := h.repo.Project("workbench")
p.Members = append(p.Members, domain.ProjectMember{UserID: b.ID, Role: "member"})
if err := h.repo.PutProject(p); err != nil {
t.Fatal(err)
}
r := domain.Run{ID: "shared-run", WorkflowID: "wf", WorkflowSnapshot: domain.WorkflowDefinition{ID: "wf", Name: "开发流程", Nodes: []domain.WorkflowNode{{ID: "develop", Type: "agent", AgentID: "dev"}}}, ExecutionContext: domain.TelemetryContext{ProjectID: p.ID}, Status: "waiting", Nodes: map[string]*domain.NodeRun{}}
if err := h.repo.PutRun(r); err != nil {
t.Fatal(err)
}
if err := h.repo.PutAssistantSession(a.ID, domain.AssistantSession{ID: "legacy-discussion", RunID: r.ID, ProjectID: p.ID, Messages: []domain.AssistantMessage{{Role: "user", Content: "历史咨询"}}}); err != nil {
t.Fatal(err)
}
c, w := workbenchContext(a, "GET", "/", bytes.NewBuffer(nil))
c.Params = gin.Params{{Key: "id", Value: r.ID}}
h.runConversation(c)
if w.Code != 200 {
t.Fatal(w.Code, w.Body.String())
}
var session domain.AssistantSession
json.Unmarshal(w.Body.Bytes(), &session)
return h, a, b, out, session
}
func TestSharedRunConversationVisibilityAndAttribution(t *testing.T) {
h, a, b, out, s := sharedWorkbenchFixture(t)
if !s.Shared || s.ID != repo.RunAssistantSessionID("shared-run") || len(s.Messages) != 1 || s.Messages[0].AuthorID != a.ID {
t.Fatal("shared history/author missing")
}
for _, tc := range []struct {
u domain.User
status int
}{{b, 200}, {out, 403}} {
c, w := workbenchContext(tc.u, "GET", "/", bytes.NewBuffer(nil))
c.Params = gin.Params{{Key: "id", Value: s.ID}}
h.session(c)
if w.Code != tc.status {
t.Fatalf("user %d status %d", tc.u.ID, w.Code)
}
}
c, w := workbenchContext(b, "GET", "/", bytes.NewBuffer(nil))
h.listSessions(c)
var list []domain.AssistantSession
json.Unmarshal(w.Body.Bytes(), &list)
found := false
for _, item := range list {
if item.ID == s.ID {
found = true
}
}
if !found {
t.Fatal("peer cannot discover shared discussion")
}
actor := int64(0)
h.generate = func(_ context.Context, prompt string, u domain.User) (string, error) {
actor = u.ID
if u.CodeBuddyAPIKey != b.CodeBuddyAPIKey {
t.Error("creator credentials reused")
}
return "角色回复", nil
}
h.repo.PutAgent(domain.AgentDefinition{ID: "dev", ProjectID: "workbench", Name: "实现工程师", Prompt: "分析开发方案"})
messages := append(s.Messages, assistantMessage{Role: "user", Content: "你做了什么", AuthorID: a.ID, AuthorName: "forged", References: []domain.AssistantReference{{Type: "agent", ID: "dev"}}})
body, _ := json.Marshal(assistantRequest{ProjectID: "workbench", RunID: "shared-run", SessionID: s.ID, Messages: messages})
c, w = workbenchContext(b, "POST", "/api/assistant/chat", bytes.NewBuffer(body))
h.chat(c)
if w.Code != 200 {
t.Fatal(w.Code, w.Body.String())
}
saved, _ := h.repo.AssistantSession(a.ID, s.ID)
last := saved.Messages[len(saved.Messages)-2]
reply := saved.Messages[len(saved.Messages)-1]
if actor != b.ID || last.AuthorID != b.ID || last.AuthorName != b.LoginName || reply.SpeakerName != "" || reply.SpeakerAgentID != "" {
t.Fatalf("wrong actor/reply: %+v %+v", last, reply)
}
for _, method := range []string{"DELETE", "PUT"} {
payload := bytes.NewBufferString(`{"messages":[]}`)
c, w = workbenchContext(b, method, "/", payload)
c.Params = gin.Params{{Key: "id", Value: s.ID}}
h.session(c)
if w.Code < 400 {
t.Fatal("shared transcript mutation allowed")
}
}
c, w = workbenchContext(b, "POST", "/", bytes.NewBufferString(`{"archived":true}`))
c.Params = gin.Params{{Key: "id", Value: s.ID}}
h.archiveSession(c)
if w.Code != 403 {
t.Fatal("shared archive allowed")
}
}
func TestSharedGenerationUsesOneSlotAndMembersCanCancel(t *testing.T) {
h, a, b, out, s := sharedWorkbenchFixture(t)
g := domain.AssistantGeneration{ID: "job", SessionID: s.ID, UserID: a.ID, ActorID: b.ID, ProjectID: s.ProjectID, Status: "running"}
if err := h.repo.BeginAssistantGeneration(g); err != nil {
t.Fatal(err)
}
if err := h.repo.BeginAssistantGeneration(domain.AssistantGeneration{ID: "second", SessionID: s.ID, UserID: a.ID, ActorID: a.ID}); err == nil {
t.Fatal("two shared generations allowed")
}
for _, tc := range []struct {
u domain.User
method string
code int
}{{a, "GET", 200}, {out, "GET", 403}, {out, "POST", 403}, {a, "POST", 200}, {b, "POST", 200}} {
c, w := workbenchContext(tc.u, tc.method, "/?generationId=job", bytes.NewBuffer(nil))
c.Params = gin.Params{{Key: "id", Value: s.ID}}
h.generation(c)
if w.Code != tc.code {
t.Fatalf("%s user %d: %d %s", tc.method, tc.u.ID, w.Code, w.Body.String())
}
}
if _, shared, err := h.conversationScope(func() *gin.Context { c, _ := workbenchContext(b, "GET", "/", bytes.NewBuffer(nil)); return c }(), "private", "shared-run", s.ProjectID, b.ID); err == nil || shared {
t.Fatal("private SOP slot accepted")
}
}
func TestRoleAgentFile(t *testing.T) {
file := roleAgentFile(roleSubagent{
Role: domain.AgentDefinition{ID: "Unit_Test", Name: "单元测试工程师", Description: "生成增量单测: 只改测试", Prompt: "覆盖 {{input}}"},
Rules: []domain.RuleDefinition{{Name: "低优先", Content: "LOW", Priority: 1}, {Name: "高优先", Content: "HIGH", Priority: 9}},
Skills: []domain.SkillDefinition{{ID: "Unit_Test_Gen"}},
})
for _, want := range []string{"---\nname: role-unit-test\n", `description: "角色「单元测试工程师」:生成增量单测: 只改测试"`, "skills:\n - unittestgen\n---\n", "覆盖 (运行时提供)", "## Rules"} {
if !strings.Contains(file, want) {
t.Fatalf("missing %q in\n%s", want, file)
}
}
if high, low := strings.Index(file, "HIGH"), strings.Index(file, "LOW"); high < 0 || low < high {
t.Fatalf("rules not ordered by priority:\n%s", file)
}
}
func TestRoleMentionKeepsMainBotSession(t *testing.T) {
h, _, b, _, session := sharedWorkbenchFixture(t)
h.engine = engine.New(h.repo, runtime.NewManager(filepath.Join(t.TempDir(), "workspaces")))
for _, skill := range []domain.SkillDefinition{
{ID: "project-skill", Name: "project-skill", ProjectID: "workbench", Content: "项目技能", Files: map[string]string{"scripts/run.sh": "echo project"}},
{ID: "role-skill", Name: "role-skill", ProjectID: "workbench", Content: "角色技能"},
{ID: "foreign-skill", Name: "foreign-skill", ProjectID: "elsewhere", Content: "别的项目"},
} {
if err := h.repo.PutSkill(skill); err != nil {
t.Fatal(err)
}
}
if err := h.repo.PutRule(domain.RuleDefinition{ID: "dev-rule", Name: "提交规则", ProjectID: "workbench", Content: "只改测试文件"}); err != nil {
t.Fatal(err)
}
role := domain.AgentDefinition{ID: "real-dev", ProjectID: "workbench", Name: "实现工程师", Prompt: "先分析开发方案,再给出测试依据", Rules: []string{"dev-rule"}, Skills: []string{"role-skill"}}
if err := h.repo.PutAgent(role); err != nil {
t.Fatal(err)
}
r, _ := h.repo.RunState("shared-run")
r.WorkflowSnapshot.Nodes[0].AgentID = role.ID
if err := h.repo.PutRun(r); err != nil {
t.Fatal(err)
}
root := t.TempDir()
bin := filepath.Join(root, "codebuddy")
argsFile := filepath.Join(root, "args")
script := `#!/bin/sh
for arg in "$@"; do printf '%s\n' "$arg"; done > '` + argsFile + `'
printf '%s\n' '{"type":"result","result":"已派发实现工程师"}'
`
if err := os.WriteFile(bin, []byte(script), 0700); err != nil {
t.Fatal(err)
}
t.Setenv("AGENTFLOW_CODEBUDDY_BIN", bin)
messages := append(session.Messages, assistantMessage{Role: "user", Content: "你如何实现", References: []domain.AssistantReference{{Type: "agent", ID: role.ID}}})
body, _ := json.Marshal(assistantRequest{ProjectID: "workbench", RunID: "shared-run", SessionID: session.ID, Messages: messages})
c, w := workbenchContext(b, "POST", "/api/assistant/chat", bytes.NewBuffer(body))
h.chat(c)
if w.Code != 200 {
t.Fatal(w.Code, w.Body.String())
}
args, err := os.ReadFile(argsFile)
if err != nil {
t.Fatal(err)
}
lines := strings.Split(string(args), "\n")
values := map[string]string{}
for i, v := range lines {
if strings.HasPrefix(v, "--") && i+1 < len(lines) {
values[v] = lines[i+1]
}
}
home := filepath.Join(assistantSharedSessionRoot(session.ID), "home", ".codebuddy")
agent, err := os.ReadFile(filepath.Join(home, "agents", "role-real-dev.md"))
if err != nil || strings.Contains(values["--system-prompt"], "你是 实现工程师") || !strings.Contains(string(agent), role.Prompt) {
t.Fatal("role must be a subagent, not replace Main Bot", err)
}
if !strings.Contains(string(agent), "只改测试文件") || !strings.Contains(string(agent), "skills:\n - role-skill\n") {
t.Fatalf("role rules or skills missing:\n%s", agent)
}
if !strings.Contains(values["--disallowedTools"], "Bash,PowerShell,Monitor,Write,Edit") {
t.Fatalf("disallowed=%q", values["--disallowedTools"])
}
skills := filepath.Join(home, "skills")
for _, path := range []string{"project-skill/SKILL.md", "project-skill/scripts/run.sh", "role-skill/SKILL.md"} {
if _, err := os.Stat(filepath.Join(skills, path)); err != nil {
t.Fatalf("skill file %s missing: %v", path, err)
}
}
if _, err := os.Stat(filepath.Join(skills, "foreign-skill")); !os.IsNotExist(err) {
t.Fatal("another project's skill leaked into the conversation")
}
}
func TestSharedAttachmentAccessRequiresRecordedReference(t *testing.T) {
h, a, b, out, s := sharedWorkbenchFixture(t)
id := strings.Repeat("a", 32)
other := strings.Repeat("b", 32)
dir := workbenchAttachmentDir(a.ID)
if err := os.MkdirAll(dir, 0700); err != nil {
t.Fatal(err)
}
for _, key := range []string{id, other} {
record := workbenchAttachmentRecord{ProjectID: s.ProjectID, AssistantAttachment: domain.AssistantAttachment{ID: key, Name: "note.txt", MimeType: "text/plain", Size: 5}}
raw, _ := json.Marshal(record)
if err := os.WriteFile(filepath.Join(dir, key+".json"), raw, 0600); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, key+".bin"), []byte("hello"), 0600); err != nil {
t.Fatal(err)
}
}
s.Messages[0].Attachments = []domain.AssistantAttachment{{ID: id, Name: "note.txt", MimeType: "text/plain", Size: 5}}
if err := h.repo.PutAssistantSession(a.ID, s); err != nil {
t.Fatal(err)
}
for _, tc := range []struct {
u domain.User
id string
code int
}{{b, id, 200}, {b, other, 404}, {out, id, 403}} {
c, w := workbenchContext(tc.u, "GET", "/?sessionId="+s.ID, bytes.NewBuffer(nil))
c.Params = gin.Params{{Key: "id", Value: tc.id}}
h.downloadAttachment(c)
if w.Code != tc.code {
t.Fatalf("attachment user %d status %d", tc.u.ID, w.Code)
}
}
saved, _ := h.repo.AssistantSession(a.ID, s.ID)
req := assistantRequest{ProjectID: s.ProjectID, SessionID: s.ID, RunID: s.RunID, Messages: append(saved.Messages, assistantMessage{Role: "user", Content: "看看附件"})}
c, _ := workbenchContext(b, "POST", "/", bytes.NewBuffer(nil))
if err := h.prepareWorkbenchContext(c, &req, b); err != nil {
t.Fatal(err)
}
if !req.attachmentIDs[id] || req.attachmentOwners[id] != a.ID {
t.Fatalf("peer turn does not carry the referenced attachment: ids=%v owners=%v", req.attachmentIDs, req.attachmentOwners)
}
}
func TestTaskReferenceSearchAndSubmissionScope(t *testing.T) {
h, a, _, _, s := sharedWorkbenchFixture(t)
h.repo.PutAgent(domain.AgentDefinition{ID: "dev", ProjectID: s.ProjectID, Name: "当前开发"})
h.repo.PutAgent(domain.AgentDefinition{ID: "other", ProjectID: s.ProjectID, Name: "其他角色"})
c, w := workbenchContext(a, "GET", "/api/assistant/references?projectId=workbench&scope=all&runId=shared-run&type=agent", bytes.NewBuffer(nil))
h.workbenchReferences(c)
var rows struct {
Items []domain.AssistantReference `json:"items"`
}
if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &rows) != nil || len(rows.Items) != 1 || rows.Items[0].ID != "dev" {
t.Fatalf("task role search: %d %s", w.Code, w.Body.String())
}
c, _ = workbenchContext(a, "POST", "/", bytes.NewBuffer(nil))
req := assistantRequest{ProjectID: s.ProjectID, RunID: s.RunID, SessionID: s.ID, Messages: append(s.Messages, assistantMessage{Role: "user", Content: "咨询", References: []domain.AssistantReference{{Type: "agent", ID: "other"}}})}
if err := h.prepareWorkbenchContext(c, &req, a); err == nil {
t.Fatal("unrelated role bypassed task scope")
}
req.RunID = ""
req.SessionID = ""
if err := h.prepareWorkbenchContext(c, &req, a); err != nil {
t.Fatal("ordinary conversation lost unrestricted reference search", err)
}
}
// A just-sent message renders its attachments while the generation request that
// stores the transcript is still in flight, so the uploader must be able to
// fetch the file before the transcript lists it. Everyone else still needs the
// recorded reference.
func TestSharedAttachmentUploaderReadsBeforeTranscriptIsStored(t *testing.T) {
h, a, b, out, s := sharedWorkbenchFixture(t)
id := strings.Repeat("c", 32)
dir := workbenchAttachmentDir(a.ID)
if err := os.MkdirAll(dir, 0700); err != nil {
t.Fatal(err)
}
record := workbenchAttachmentRecord{ProjectID: s.ProjectID, AssistantAttachment: domain.AssistantAttachment{ID: id, Name: "shot.png", MimeType: "image/png", Size: 5}}
raw, _ := json.Marshal(record)
if err := os.WriteFile(filepath.Join(dir, id+".json"), raw, 0600); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, id+".bin"), []byte("hello"), 0600); err != nil {
t.Fatal(err)
}
for _, tc := range []struct {
u domain.User
code int
}{{a, 200}, {b, 404}, {out, 403}} {
c, w := workbenchContext(tc.u, "GET", "/?sessionId="+s.ID, bytes.NewBuffer(nil))
c.Params = gin.Params{{Key: "id", Value: id}}
h.downloadAttachment(c)
if w.Code != tc.code {
t.Fatalf("user %d status %d body %q; want %d", tc.u.ID, w.Code, w.Body.String(), tc.code)
}
}
}