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
feat(runtime): custom run credentials for self-hosted deployments (#1032)
jason zhang48cdf0c2e214
diff --git a/internal/engine/run_environment.go b/internal/engine/run_environment.goindex af4325d3..cca1afa3 100644--- a/internal/engine/run_environment.go+++ b/internal/engine/run_environment.go@@ -229,6 +229,7 @@ func (e *Engine) resolveRunCredentials(run domain.Run) runtime.RunCredentials {CodeBuddy: resolveCodeBuddyCredential(e.repo, userID),GitToken: resolveGitToken(e.repo, userID),}+ credentials.Extra = runtime.ExtraRunCredentials(userID)credentials.GitSSHKeys = e.repo.GitSSHKeysForRemotes(userID, sshRepositoryURLs(run.RepositoryContexts()))identity := resolveGitIdentity(e.repo, userID)credentials.GitUserName = identity.namediff --git a/internal/httpapi/assistant.go b/internal/httpapi/assistant.goindex 7ac91305..92aafdd6 100644--- a/internal/httpapi/assistant.go+++ b/internal/httpapi/assistant.go@@ -1510,6 +1510,11 @@ func runAssistantCLI(ctx context.Context, req assistantCLIRequest, user domain.Uif err != nil {return "", "", err}+ // Every engine path (CodeBuddy branch and the per-provider invocation+ // constructors) builds inv.credentials independently; load the+ // deployment-registered extra credentials here once so a BindExtra+ // binding on an assistant command can reach them.+ inv.credentials.Extra = runtime.ExtraRunCredentials(user.ID)if inv.resumeMissing {return "", sessionID, errAssistantSessionMissing}diff --git a/internal/httpapi/assistant_provider.go b/internal/httpapi/assistant_provider.goindex 62dfa2ea..801bde44 100644--- a/internal/httpapi/assistant_provider.go+++ b/internal/httpapi/assistant_provider.go@@ -60,6 +60,7 @@ func assistantProviderCredentials(user domain.User) runtime.RunCredentials {CodeBuddy: runtime.CodeBuddyCredential{APIKey: user.CodeBuddyAPIKey, InternetEnvironment: user.CodeBuddyInternetEnvironment},Codex: runtime.ProviderCredential{APIKey: user.CodexAPIKey, BaseURL: user.CodexBaseURL},ClaudeCode: runtime.ProviderCredential{APIKey: user.ClaudeCodeAPIKey, BaseURL: user.ClaudeCodeBaseURL},+ Extra: runtime.ExtraRunCredentials(user.ID),AgentCLI: runtime.AgentCLIUserCredentials(user),}}diff --git a/internal/runtime/command_binding.go b/internal/runtime/command_binding.goindex 7fdb247b..3ae06d29 100644--- a/internal/runtime/command_binding.go+++ b/internal/runtime/command_binding.go@@ -102,6 +102,14 @@ func bindCommand(sandbox Sandbox, workspace string, command CommandSpec, credentif err := bindWorkspaceGitEnvironment(sandbox, workspace, bound.environment, credentials); err != nil {return CommandSpec{}, err}+ case BindExtra:+ // Missing credentials export as empty strings on purpose: a child+ // must not inherit the AgentFlow host process's own value for a+ // variable the deployment routes through Extra credentials.+ for envName, credName := range command.ExtraCredentialEnv {+ bound.environment[envName] = credentials.Extra[credName]+ bound.credentialEnvKeys = append(bound.credentialEnvKeys, envName)+ }case BindMCP:if err := bindMCPCallEnvironment(sandbox, workspace, bound.environment, credentials.MCPServers); err != nil {return CommandSpec{}, errdiff --git a/internal/runtime/conversation_process.go b/internal/runtime/conversation_process.goindex 813bcdb2..5649eb84 100644--- a/internal/runtime/conversation_process.go+++ b/internal/runtime/conversation_process.go@@ -52,18 +52,19 @@ type ConversationFile struct {}type ConversationCommand struct {- RunnerLogin bool `json:",omitempty"`- Argv []string- Provider string- Bindings []CredentialBinding- Environment map[string]string- AgentCLI AgentCLIOptions- Credentials RunCredentials- Interactive bool+ RunnerLogin bool `json:",omitempty"`+ Argv []string+ Provider string+ Bindings []CredentialBinding+ Environment map[string]string+ ExtraCredentialEnv map[string]string+ AgentCLI AgentCLIOptions+ Credentials RunCredentials+ Interactive bool}func ConversationCommandFor(command CommandSpec, credentials RunCredentials) *ConversationCommand {- return &ConversationCommand{Argv: command.Argv, Provider: command.Provider, Bindings: command.CredentialBindings, Environment: CommandEnvironment(command), AgentCLI: command.AgentCLI, Credentials: credentials, Interactive: command.Stdin != nil}+ return &ConversationCommand{Argv: command.Argv, Provider: command.Provider, Bindings: command.CredentialBindings, Environment: CommandEnvironment(command), ExtraCredentialEnv: command.ExtraCredentialEnv, AgentCLI: command.AgentCLI, Credentials: credentials, Interactive: command.Stdin != nil}}type ConversationProcessResult struct {@@ -528,7 +529,7 @@ func (p *ConversationProcessPool) start(ctx context.Context, root, workspace strreturn ConversationProcessResult{}, err}}- command := CommandSpec{Argv: append([]string(nil), req.Command.Argv...), Provider: req.Command.Provider, CredentialBindings: req.Command.Bindings, AgentCLI: req.Command.AgentCLI}+ command := CommandSpec{Argv: append([]string(nil), req.Command.Argv...), Provider: req.Command.Provider, CredentialBindings: req.Command.Bindings, ExtraCredentialEnv: req.Command.ExtraCredentialEnv, AgentCLI: req.Command.AgentCLI}for i, arg := range command.Argv {command.Argv[i] = conversationRemap(arg, req.SourceRoot, root)}diff --git a/internal/runtime/process.go b/internal/runtime/process.goindex f4e14983..54be43a7 100644--- a/internal/runtime/process.go+++ b/internal/runtime/process.go@@ -16,7 +16,11 @@ const processWaitDelay = time.Secondvar errProcessNotStarted = errors.New("process was not started")-func runCommand(cmd *exec.Cmd, sink EventSink) (string, error) {+// runCommand waits for cmd and streams process telemetry. credentialEnvKeys+// names environment variables populated from Extra credentials; their values+// are redacted from the process.started command line by origin, so arbitrary+// names (for example AWS_SECRET_ACCESS_KEY) never rely on naming conventions.+func runCommand(cmd *exec.Cmd, sink EventSink, credentialEnvKeys ...string) (string, error) {var buf bytes.Buffervar mu sync.Mutexstdout := &processWriter{output: &buf, mu: &mu, sink: sink, name: "process.stdout", level: "info"}@@ -34,7 +38,7 @@ func runCommand(cmd *exec.Cmd, sink EventSink) (string, error) {mu.Lock()err := cmd.Start()if err == nil && sink != nil {- sink(domain.TelemetryEvent{Category: "process", Name: "process.started", Timestamp: time.Now().UTC(), Phase: "start", Message: compactCommand(cmd.Args), Attributes: map[string]any{"pid": cmd.Process.Pid}})+ sink(domain.TelemetryEvent{Category: "process", Name: "process.started", Timestamp: time.Now().UTC(), Phase: "start", Message: compactCommand(cmd.Args, credentialEnvKeys...), Attributes: map[string]any{"pid": cmd.Process.Pid}})}mu.Unlock()if err == nil {@@ -129,18 +133,25 @@ func (w *processWriter) emit(line string) {}}-func compactCommand(args []string) string {+func compactCommand(args []string, credentialEnvKeys ...string) string {+ credentialKeys := make(map[string]bool, len(credentialEnvKeys))+ for _, key := range credentialEnvKeys {+ credentialKeys[strings.ToUpper(key)] = true+ }+ redact := func(key string) bool {+ return sensitiveProcessEnvironmentKey(key) || credentialKeys[strings.ToUpper(key)]+ }safe := append([]string(nil), args...)for index := 0; index < len(safe); index++ {value := safe[index]if value == "-e" && index+1 < len(safe) {- if key, _, found := strings.Cut(safe[index+1], "="); found && sensitiveProcessEnvironmentKey(key) {+ if key, _, found := strings.Cut(safe[index+1], "="); found && redact(key) {safe[index+1] = key + "=<redacted>"}index++continue}- if key, _, found := strings.Cut(value, "="); found && sensitiveProcessEnvironmentKey(key) {+ if key, _, found := strings.Cut(value, "="); found && redact(key) {safe[index] = key + "=<redacted>"}}diff --git a/internal/runtime/run_credentials_extra.go b/internal/runtime/run_credentials_extra.gonew file mode 100644index 00000000..c183af19--- /dev/null+++ b/internal/runtime/run_credentials_extra.go@@ -0,0 +1,51 @@+package runtime++import "sync"++// ExtraCredentialSource returns deployment-specific run credentials for a+// user (credential name → secret). The open-source build registers none; a+// self-hosted deployment that manages additional credentials — for example a+// token for an internal gateway its tooling calls — registers a source at+// process start. A source answers from local storage only and must not+// perform network calls.+type ExtraCredentialSource func(userID int64) map[string]string++var (+ extraCredentialMu sync.RWMutex+ extraCredentialSources []ExtraCredentialSource+)++// RegisterExtraCredentialSource adds a source consulted whenever run+// credentials are assembled. It is intended for process-start registration;+// the set stays fixed for the lifetime of the process.+func RegisterExtraCredentialSource(src ExtraCredentialSource) {+ if src == nil {+ return+ }+ extraCredentialMu.Lock()+ defer extraCredentialMu.Unlock()+ extraCredentialSources = append(extraCredentialSources, src)+}++// ExtraRunCredentials merges every registered source for the user. On key+// collisions the later-registered source wins. With no registered sources it+// returns nil so the common open-source path stays allocation-free.+func ExtraRunCredentials(userID int64) map[string]string {+ extraCredentialMu.RLock()+ sources := make([]ExtraCredentialSource, len(extraCredentialSources))+ copy(sources, extraCredentialSources)+ extraCredentialMu.RUnlock()+ if len(sources) == 0 {+ return nil+ }+ merged := make(map[string]string)+ for _, src := range sources {+ if src == nil {+ continue+ }+ for name, secret := range src(userID) {+ merged[name] = secret+ }+ }+ return merged+}diff --git a/internal/runtime/run_credentials_extra_test.go b/internal/runtime/run_credentials_extra_test.gonew file mode 100644index 00000000..0a52a574--- /dev/null+++ b/internal/runtime/run_credentials_extra_test.go@@ -0,0 +1,165 @@+package runtime++import (+ "strings"+ "testing"+)++func TestExtraRunCredentialsNoSources(t *testing.T) {+ if got := ExtraRunCredentials(42); got != nil {+ t.Fatalf("no sources: want nil, got %v", got)+ }+}++func TestExtraRunCredentialsMergesSources(t *testing.T) {+ restore := swapExtraCredentialSources(+ func(userID int64) map[string]string {+ if userID != 7 {+ t.Errorf("source got userID %d, want 7", userID)+ }+ return map[string]string{"gateway": "g1", "shared": "first"}+ },+ func(userID int64) map[string]string {+ return map[string]string{"registry": "r1", "shared": "second"}+ },+ )+ defer restore()++ got := ExtraRunCredentials(7)+ want := map[string]string{"gateway": "g1", "registry": "r1", "shared": "second"}+ if len(got) != len(want) {+ t.Fatalf("merged credentials: got %v, want %v", got, want)+ }+ for k, v := range want {+ if got[k] != v {+ t.Errorf("credential %q: got %q, want %q", k, got[k], v)+ }+ }+}++func TestBindExtraCredentialEnv(t *testing.T) {+ sb := &localSandbox{id: "extra-cred-test", workspace: t.TempDir()}++ command := CommandSpec{+ Argv: []string{"sh", "-c", "exit 0"},+ CredentialBindings: []CredentialBinding{BindExtra},+ ExtraCredentialEnv: map[string]string{+ "TOOL_GATEWAY_TOKEN": "gateway",+ "TOOL_MISSING_TOKEN": "absent",+ },+ }+ credentials := RunCredentials{Extra: map[string]string{"gateway": "tok-1"}}++ bound, err := bindCommand(sb, sb.workspace, command, credentials)+ if err != nil {+ t.Fatalf("bindCommand: %v", err)+ }+ if got := bound.environment["TOOL_GATEWAY_TOKEN"]; got != "tok-1" {+ t.Errorf("TOOL_GATEWAY_TOKEN: got %q, want %q", got, "tok-1")+ }+ // A missing credential must clear the variable instead of letting a child+ // inherit the host process's own value.+ if got := bound.environment["TOOL_MISSING_TOKEN"]; got != "" {+ t.Errorf("TOOL_MISSING_TOKEN: got %q, want empty", got)+ }++ // Without the binding the mapping is inert.+ plain := command+ plain.CredentialBindings = nil+ bound, err = bindCommand(sb, sb.workspace, plain, credentials)+ if err != nil {+ t.Fatalf("bindCommand without binding: %v", err)+ }+ if _, ok := bound.environment["TOOL_GATEWAY_TOKEN"]; ok {+ t.Errorf("TOOL_GATEWAY_TOKEN exported without BindExtra binding")+ }+}++// swapExtraCredentialSources replaces the registered sources for a test and+// returns a func restoring the previous set.+func swapExtraCredentialSources(srcs ...ExtraCredentialSource) func() {+ extraCredentialMu.Lock()+ prev := extraCredentialSources+ extraCredentialSources = srcs+ extraCredentialMu.Unlock()+ return func() {+ extraCredentialMu.Lock()+ extraCredentialSources = prev+ extraCredentialMu.Unlock()+ }+}++func TestCompactCommandRedactsCredentialEnvKeysByOrigin(t *testing.T) {+ args := []string{"docker", "exec", "-e", "AWS_SECRET_ACCESS_KEY=super-secret", "-e", "PATH=/usr/bin", "container", "sh"}+ got := compactCommand(args, "aws_secret_access_key")+ if strings.Contains(got, "super-secret") {+ t.Fatalf("credential value leaked into command line: %s", got)+ }+ if !strings.Contains(got, "AWS_SECRET_ACCESS_KEY=<redacted>") {+ t.Fatalf("expected origin-based redaction, got: %s", got)+ }+ if !strings.Contains(got, "PATH=/usr/bin") {+ t.Fatal("unrelated environment must not be redacted")+ }+}++func TestBindExtraMarksCredentialEnvKeysForRedaction(t *testing.T) {+ sb := &localSandbox{id: "extra-cred-redact", workspace: t.TempDir()}+ command := CommandSpec{+ Argv: []string{"sh", "-c", "exit 0"},+ CredentialBindings: []CredentialBinding{BindExtra},+ ExtraCredentialEnv: map[string]string{"AWS_SECRET_ACCESS_KEY": "gateway"},+ }+ bound, err := bindCommand(sb, sb.workspace, command, RunCredentials{Extra: map[string]string{"gateway": "tok-1"}})+ if err != nil {+ t.Fatalf("bindCommand: %v", err)+ }+ found := false+ for _, key := range bound.credentialEnvKeys {+ if key == "AWS_SECRET_ACCESS_KEY" {+ found = true+ }+ }+ if !found {+ t.Fatalf("credentialEnvKeys not populated: %v", bound.credentialEnvKeys)+ }+}++// TestConversationPathBindsExtraCredentialEnv mirrors the ConversationProcessPool+// rebuild in conversation_process.go: after ConversationCommandFor and the+// CommandSpec reconstruction, BindExtra must still inject (and clear) the+// mapped environment — the conversation child must never inherit the host+// process's own value.+func TestConversationPathBindsExtraCredentialEnv(t *testing.T) {+ sb := &localSandbox{id: "extra-cred-conversation", workspace: t.TempDir()}+ command := CommandSpec{+ Argv: []string{"cli", "--model", "m"},+ Provider: "codebuddy",+ CredentialBindings: []CredentialBinding{BindExtra},+ ExtraCredentialEnv: map[string]string{"MY_TOOL_TOKEN": "gateway", "MY_ABSENT_TOKEN": "absent"},+ }++ // Wire format (what travels to the execution machine).+ wire := ConversationCommandFor(command, RunCredentials{Extra: map[string]string{"gateway": "host-owned-value"}})+ if wire.ExtraCredentialEnv["MY_TOOL_TOKEN"] != "gateway" {+ t.Fatalf("ConversationCommandFor dropped ExtraCredentialEnv: %+v", wire.ExtraCredentialEnv)+ }++ // Pool-side reconstruction (conversation_process.go) followed by binding.+ rebuilt := CommandSpec{+ Argv: append([]string(nil), wire.Argv...),+ Provider: wire.Provider,+ CredentialBindings: wire.Bindings,+ ExtraCredentialEnv: wire.ExtraCredentialEnv,+ }+ bound, err := bindCommand(sb, sb.workspace, rebuilt, wire.Credentials)+ if err != nil {+ t.Fatalf("bindCommand: %v", err)+ }+ if got := bound.environment["MY_TOOL_TOKEN"]; got != "host-owned-value" {+ t.Errorf("MY_TOOL_TOKEN: got %q, want the user credential", got)+ }+ if got := bound.environment["MY_ABSENT_TOKEN"]; got != "" {+ t.Errorf("MY_ABSENT_TOKEN: got %q, want empty (host value must be cleared)", got)+ }+}diff --git a/internal/runtime/runtime.go b/internal/runtime/runtime.goindex aa1fafbf..44d236d9 100644--- a/internal/runtime/runtime.go+++ b/internal/runtime/runtime.go@@ -30,6 +30,11 @@ const (// answers only github.com, GH_TOKEN serves gh and API calls, and// SSH uses the requester's keys per configured host without exposing them.BindGitWorkspace CredentialBinding = "git-workspace"+ // BindExtra exports credentials from RunCredentials.Extra into the+ // environment names listed in CommandSpec.ExtraCredentialEnv. Empty values+ // are exported as empty strings so a child never inherits the host+ // process's own variables.+ BindExtra CredentialBinding = "extra")type CodeBuddyCredential struct {@@ -58,6 +63,11 @@ type RunCredentials struct {GitUserName stringGitUserEmail stringMCPServers []domain.MCPServerDefinition+ // Extra carries deployment-registered credentials (see+ // RegisterExtraCredentialSource). Like GitSSHKeys it lives only on the+ // in-memory sandbox handle and is never serialized into domain objects,+ // run-context.json, or workspace files.+ Extra map[string]string `json:"-"`}type ProviderCredential struct {@@ -99,6 +109,15 @@ type CommandSpec struct {AgentCLI AgentCLIOptionsHookOutput stringTelemetry *InvocationOptions+ // ExtraCredentialEnv maps environment variable names to keys in+ // RunCredentials.Extra and takes effect only together with the BindExtra+ // credential binding. Empty or missing credentials export as empty+ // strings, clearing any value inherited from the host process.+ ExtraCredentialEnv map[string]string+ // credentialEnvKeys records the environment variable names populated from+ // Extra credentials (set by bindCommand) so process telemetry can redact+ // them by origin instead of relying on naming conventions.+ credentialEnvKeys []string// Stdin is forwarded by local, Docker and remote conversation execution.Stdin io.Readerenvironment map[string]stringdiff --git a/internal/runtime/sandbox_docker.go b/internal/runtime/sandbox_docker.goindex 0540f445..def1f23a 100644--- a/internal/runtime/sandbox_docker.go+++ b/internal/runtime/sandbox_docker.go@@ -202,7 +202,7 @@ cd "$1" && shift && exec "$@"`, "conversation", command.WorkingDir}, dropShellLoargs = append(args, argv...)cmd := exec.CommandContext(ctx, "docker", args...)cmd.Stdin = command.Stdin- return runCommand(cmd, sink)+ return runCommand(cmd, sink, command.credentialEnvKeys...)}// dockerWorkspaceExecArgv runs argv inside /workspace without docker exec -w.diff --git a/internal/runtime/workspace_preparation.go b/internal/runtime/workspace_preparation.goindex 1f2b2501..0455b87a 100644--- a/internal/runtime/workspace_preparation.go+++ b/internal/runtime/workspace_preparation.go@@ -179,6 +179,9 @@ func redactPreparationOutput(sb Sandbox, output string) string {credentials := managed.credentialsmanaged.credentialsMu.RUnlock()secrets = append(secrets, credentials.GitToken, credentials.CodeBuddy.APIKey, credentials.Codex.APIKey, credentials.ClaudeCode.APIKey)+ for _, secret := range credentials.Extra {+ secrets = append(secrets, secret)+ }for _, server := range credentials.MCPServers {for _, value := range server.Headers {secrets = append(secrets, value)