From 9cc146ee21c296d553ae684531219a5c711a4213 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Flor=C3=AAncio?= Date: Sun, 13 Sep 2026 12:19:46 +0100 Subject: [PATCH] fix: bridge executor HTTPS Git access through host gh (#3072) (#3635) --- .../internal/agent/runtime/agentctl/client.go | 14 + .../runtime/agentctl/client_core_test.go | 21 + .../agent/runtime/environment/environment.go | 188 +++- .../runtime/environment/environment_test.go | 45 + .../agent/runtime/lifecycle/manager_launch.go | 28 +- .../runtime/lifecycle/manager_launch_test.go | 135 ++- .../agent/runtime/lifecycle/profile_env.go | 37 + .../runtime/lifecycle/profile_env_test.go | 28 + .../internal/agentctl/server/api/server.go | 13 +- .../agentctl/server/config/config_test.go | 57 ++ .../agentctl/server/process/manager.go | 99 ++- .../server/process/manager_temp_test.go | 164 ++++ .../server/process/workspace_git_env_test.go | 50 ++ apps/backend/internal/backendapp/adapters.go | 7 + .../internal/gitconfigenv/environment.go | 9 + .../internal/gitconfigenv/environment_test.go | 37 + .../internal/githubauth/environment.go | 23 + .../orchestrator/executor/executor.go | 20 +- .../executor/executor_credentials.go | 348 ++++++-- .../executor_credentials_preflight_test.go | 27 +- .../executor/executor_credentials_test.go | 144 ++- .../orchestrator/executor/executor_execute.go | 94 +- .../executor/executor_host_gh_bridge.go | 482 ++++++++++ .../executor_host_gh_bridge_profile_test.go | 273 ++++++ .../executor/executor_host_gh_bridge_test.go | 821 ++++++++++++++++++ .../executor_kubernetes_parity_test.go | 4 +- .../executor/executor_mocks_test.go | 8 + .../orchestrator/executor/executor_resume.go | 25 +- docs/plans/executor-host-gh-bridge/plan.md | 181 ++++ .../task-01-host-cli-route.md | 135 +++ .../task-02-runtime-propagation.md | 147 ++++ .../task-03-document-https-access.md | 99 +++ docs/public/integrations.md | 13 +- .../requirements/github-authentication.md | 21 +- .../system-design/github-authentication-01.md | 2 +- .../system-design/github-authentication-02.md | 105 +++ 36 files changed, 3787 insertions(+), 117 deletions(-) create mode 100644 apps/backend/internal/orchestrator/executor/executor_host_gh_bridge.go create mode 100644 apps/backend/internal/orchestrator/executor/executor_host_gh_bridge_profile_test.go create mode 100644 apps/backend/internal/orchestrator/executor/executor_host_gh_bridge_test.go create mode 100644 docs/plans/executor-host-gh-bridge/plan.md create mode 100644 docs/plans/executor-host-gh-bridge/task-01-host-cli-route.md create mode 100644 docs/plans/executor-host-gh-bridge/task-02-runtime-propagation.md create mode 100644 docs/plans/executor-host-gh-bridge/task-03-document-https-access.md diff --git a/apps/backend/internal/agent/runtime/agentctl/client.go b/apps/backend/internal/agent/runtime/agentctl/client.go index 1a10ee98ee..4aabd77b4f 100644 --- a/apps/backend/internal/agent/runtime/agentctl/client.go +++ b/apps/backend/internal/agent/runtime/agentctl/client.go @@ -307,6 +307,18 @@ func (c *Client) GetStatus(ctx context.Context) (*StatusResponse, error) { // ConfigureAgent configures the agent command and optional approval policy. Must be called before Start(). // continueCommand is optional — when set, the adapter uses it for one-shot follow-up prompts. func (c *Client) ConfigureAgent(ctx context.Context, command string, agentArgs []string, env map[string]string, approvalPolicy, continueCommand string, continueArgs []string) error { + return c.configureAgent(ctx, command, agentArgs, env, approvalPolicy, continueCommand, continueArgs, false) +} + +// ConfigureAgentWithEnvironment configures the agent with a complete effective +// environment. The agentctl instance replaces its indexed Git configuration +// block while preserving ordinary instance variables. Use this when the +// caller has already composed inherited and generated environment sources. +func (c *Client) ConfigureAgentWithEnvironment(ctx context.Context, command string, agentArgs []string, env map[string]string, approvalPolicy, continueCommand string, continueArgs []string) error { + return c.configureAgent(ctx, command, agentArgs, env, approvalPolicy, continueCommand, continueArgs, true) +} + +func (c *Client) configureAgent(ctx context.Context, command string, agentArgs []string, env map[string]string, approvalPolicy, continueCommand string, continueArgs []string, replaceEnv bool) error { ctx, span := tracing.TraceHTTPRequest(ctx, "POST", "/api/v1/agent/configure", c.executionID) defer span.End() @@ -316,11 +328,13 @@ func (c *Client) ConfigureAgent(ctx context.Context, command string, agentArgs [ ContinueCommand string `json:"continue_command,omitempty"` ContinueArgs *[]string `json:"continue_args,omitempty"` Env map[string]string `json:"env,omitempty"` + ReplaceEnv bool `json:"replace_env,omitempty"` ApprovalPolicy string `json:"approval_policy,omitempty"` }{ Command: command, ContinueCommand: continueCommand, Env: env, + ReplaceEnv: replaceEnv, ApprovalPolicy: approvalPolicy, } if agentArgs != nil { diff --git a/apps/backend/internal/agent/runtime/agentctl/client_core_test.go b/apps/backend/internal/agent/runtime/agentctl/client_core_test.go index 72e874bdaf..962a6b08e3 100644 --- a/apps/backend/internal/agent/runtime/agentctl/client_core_test.go +++ b/apps/backend/internal/agent/runtime/agentctl/client_core_test.go @@ -289,6 +289,27 @@ func TestConfigureAgent_SendsCommandEnvAndApprovalPolicy(t *testing.T) { } } +func TestConfigureAgentWithEnvironmentRequestsIndexedReplacement(t *testing.T) { + srv, got := captureServer(t, jsonResponder(http.StatusOK, `{"success":true}`)) + + err := newHTTPOnlyClient(srv.URL).ConfigureAgentWithEnvironment( + context.Background(), "claude-code acp", nil, + map[string]string{"GIT_CONFIG_COUNT": "1"}, "never", "", nil) + if err != nil { + t.Fatalf("ConfigureAgentWithEnvironment: %v", err) + } + + var sent struct { + ReplaceEnv bool `json:"replace_env"` + } + if err := json.Unmarshal(got.Body, &sent); err != nil { + t.Fatalf("decode sent body: %v", err) + } + if !sent.ReplaceEnv { + t.Fatal("replace_env = false, want true") + } +} + func TestConfigureAgent_FailureModes(t *testing.T) { tests := []struct { name string diff --git a/apps/backend/internal/agent/runtime/environment/environment.go b/apps/backend/internal/agent/runtime/environment/environment.go index 01d10e6067..25b6456efa 100644 --- a/apps/backend/internal/agent/runtime/environment/environment.go +++ b/apps/backend/internal/agent/runtime/environment/environment.go @@ -11,6 +11,8 @@ import ( "fmt" "sort" "strings" + + "github.com/kandev/kandev/internal/gitconfigenv" ) // Definition describes one environment value without including its plaintext @@ -59,8 +61,11 @@ type RevealFunc func(context.Context, Definition) (string, error) // override record, no log, and no counter increment — the environment // package holds no logger and stays a pure function of its inputs. func Validate(definitions []Definition) error { - _, _, err := selectDefinitions(definitions) - return err + ordinary, indexed := splitIndexedDefinitions(definitions) + if _, _, err := selectDefinitions(ordinary); err != nil { + return err + } + return validateIndexedDefinitions(indexed) } // Resolve sorts definitions deterministically over five named columns, @@ -70,7 +75,12 @@ func Validate(definitions []Definition) error { // nil []OverrideRecord — all or nothing, even when tier precedence had // already resolved other keys before the failing key was reached. func Resolve(ctx context.Context, definitions []Definition, reveal RevealFunc) (map[string]string, []OverrideRecord, error) { - selected, records, err := selectDefinitions(definitions) + ordinary, indexed := splitIndexedDefinitions(definitions) + indexedSelections, err := prepareIndexedDefinitions(indexed) + if err != nil { + return nil, nil, err + } + selected, records, err := selectDefinitions(ordinary) if err != nil { return nil, nil, err } @@ -95,9 +105,181 @@ func Resolve(ctx context.Context, definitions []Definition, reveal RevealFunc) ( } resolved[key] = value } + + indexedEnv, err := resolveIndexedDefinitions(ctx, indexedSelections, reveal) + if err != nil { + return nil, nil, err + } + for key, value := range indexedEnv { + resolved[key] = value + } return resolved, records, nil } +// splitIndexedDefinitions keeps Git's indexed configuration protocol as one +// composed value. Treating GIT_CONFIG_COUNT, GIT_CONFIG_KEY_* and +// GIT_CONFIG_VALUE_* as independent environment keys makes two authoritative +// sources conflict or lets one source hide the other source's entries. +func splitIndexedDefinitions(definitions []Definition) (ordinary, indexed []Definition) { + ordinary = make([]Definition, 0, len(definitions)) + indexed = make([]Definition, 0) + for _, definition := range definitions { + if gitconfigenv.IsIndexedKey(definition.Key) { + indexed = append(indexed, definition) + continue + } + ordinary = append(ordinary, definition) + } + return ordinary, indexed +} + +type indexedDefinitionBlock struct { + origin string + workspaceID string + definitions []Definition +} + +type indexedDefinitionSelection struct { + block indexedDefinitionBlock + selected map[string]Definition +} + +func indexedDefinitionBlocks(definitions []Definition) []indexedDefinitionBlock { + if len(definitions) == 0 { + return nil + } + bySource := make(map[string]*indexedDefinitionBlock, len(definitions)) + for _, definition := range definitions { + identity := definition.Origin + "\x00" + definition.WorkspaceID + block, ok := bySource[identity] + if !ok { + block = &indexedDefinitionBlock{ + origin: definition.Origin, + workspaceID: definition.WorkspaceID, + } + bySource[identity] = block + } + block.definitions = append(block.definitions, definition) + } + blocks := make([]indexedDefinitionBlock, 0, len(bySource)) + for _, block := range bySource { + blocks = append(blocks, *block) + } + // Weaker sources are composed first. Within one tier the origin and + // workspace order is deterministic; the later block is the effective Git + // configuration source when repeated Git keys are present. + sort.Slice(blocks, func(i, j int) bool { + ti, tj := TierForOrigin(blocks[i].origin), TierForOrigin(blocks[j].origin) + if ti != tj { + return ti > tj + } + if blocks[i].origin != blocks[j].origin { + return blocks[i].origin < blocks[j].origin + } + return blocks[i].workspaceID < blocks[j].workspaceID + }) + return blocks +} + +func validateIndexedDefinitions(definitions []Definition) error { + _, err := prepareIndexedDefinitions(definitions) + return err +} + +func prepareIndexedDefinitions(definitions []Definition) ([]indexedDefinitionSelection, error) { + blocks := indexedDefinitionBlocks(definitions) + if len(blocks) == 0 { + return nil, nil + } + selections := make([]indexedDefinitionSelection, 0, len(blocks)) + composedShape := make(map[string]string) + for _, block := range blocks { + selected, _, err := selectDefinitions(block.definitions) + if err != nil { + return nil, err + } + shape := indexedDefinitionValues(selected, true) + if _, err := gitconfigenv.Merge(nil, shape); err != nil { + return nil, fmt.Errorf("validate indexed Git configuration from %s: %w", indexedSourceLabel(block), err) + } + selections = append(selections, indexedDefinitionSelection{block: block, selected: selected}) + // Secret-backed counts cannot be shape-validated until reveal. The + // known portion still needs a combined-limit check before any secret + // is revealed by Resolve. + mergedShape, err := gitconfigenv.Merge(composedShape, shape) + if err != nil { + return nil, fmt.Errorf("compose indexed Git configuration: %w", err) + } + composedShape = mergedShape + } + return selections, nil +} + +func resolveIndexedDefinitions(ctx context.Context, selections []indexedDefinitionSelection, reveal RevealFunc) (map[string]string, error) { + resolved := make(map[string]string) + for _, selection := range selections { + block := selection.block + selected := selection.selected + source := make(map[string]string, len(selected)) + var err error + keys := make([]string, 0, len(selected)) + for key := range selected { + keys = append(keys, key) + } + sort.Strings(keys) + for _, key := range keys { + definition := selected[key] + value := definition.Literal + if definition.SecretID != "" { + if reveal == nil { + return nil, &SecretError{Key: definition.Key, Origin: definition.Origin, err: fmt.Errorf("secret resolver unavailable")} + } + value, err = reveal(ctx, definition) + if err != nil { + return nil, &SecretError{Key: definition.Key, Origin: definition.Origin, err: err} + } + } + source[key] = value + } + // Re-parse after revealing secrets. Validate cannot inspect secret + // values, so the final boundary must still reject a malformed counted + // block or an invalid combined count. + if _, err := gitconfigenv.Merge(nil, source); err != nil { + return nil, fmt.Errorf("resolve indexed Git configuration from %s: %w", indexedSourceLabel(block), err) + } + resolved, err = gitconfigenv.Merge(resolved, source) + if err != nil { + return nil, fmt.Errorf("compose indexed Git configuration: %w", err) + } + } + return resolved, nil +} + +func indexedDefinitionValues(selected map[string]Definition, shapeOnly bool) map[string]string { + values := make(map[string]string, len(selected)) + for key, definition := range selected { + value := definition.Literal + if shapeOnly && definition.SecretID != "" && key != "GIT_CONFIG_COUNT" { + value = "" + } + // A secret-backed count cannot be shape-validated without revealing it. + // Leave it absent for the non-revealing preflight; Resolve validates the + // actual count after the final secret boundary. + if shapeOnly && definition.SecretID != "" && key == "GIT_CONFIG_COUNT" { + continue + } + values[key] = value + } + return values +} + +func indexedSourceLabel(block indexedDefinitionBlock) string { + if block.workspaceID == "" { + return block.origin + } + return block.origin + " workspace " + block.workspaceID +} + // selectDefinitions implements the resolution procedure: a deterministic // five-column sort, per-key identity merging, the secret veto, and tier // precedence over any remaining ambiguity. diff --git a/apps/backend/internal/agent/runtime/environment/environment_test.go b/apps/backend/internal/agent/runtime/environment/environment_test.go index 263f4b5179..24a39fbf56 100644 --- a/apps/backend/internal/agent/runtime/environment/environment_test.go +++ b/apps/backend/internal/agent/runtime/environment/environment_test.go @@ -53,6 +53,51 @@ func TestResolve_ExecutorProfileBeatsAgentProfile(t *testing.T) { // AC-1 } } +func TestResolve_ComposesIndexedGitConfigAcrossSources(t *testing.T) { + env, records := mustResolve(t, []Definition{ + {Key: "GIT_CONFIG_COUNT", Literal: "1", Origin: OriginAgentProfile}, + {Key: "GIT_CONFIG_KEY_0", Literal: "core.autocrlf", Origin: OriginAgentProfile}, + {Key: "GIT_CONFIG_VALUE_0", Literal: "input", Origin: OriginAgentProfile}, + {Key: "GIT_CONFIG_COUNT", Literal: "2", Origin: OriginExecutorProfile}, + {Key: "GIT_CONFIG_KEY_0", Literal: "notes.augment.mergeStrategy", Origin: OriginExecutorProfile}, + {Key: "GIT_CONFIG_VALUE_0", Literal: "union", Origin: OriginExecutorProfile}, + {Key: "GIT_CONFIG_KEY_1", Literal: "core.hooksPath", Origin: OriginExecutorProfile}, + {Key: "GIT_CONFIG_VALUE_1", Literal: "/profile/hooks", Origin: OriginExecutorProfile}, + {Key: "GIT_CONFIG_COUNT", Literal: "1", Origin: OriginManagedRuntime}, + {Key: "GIT_CONFIG_KEY_0", Literal: "credential.https://github.com.helper", Origin: OriginManagedRuntime}, + {Key: "GIT_CONFIG_VALUE_0", Literal: "!f() { : kandev-host-gh-bridge; gh auth git-credential \"$@\"; }; f", Origin: OriginManagedRuntime}, + }) + if len(records) != 0 { + t.Fatalf("records = %#v, want no ordinary-key override records", records) + } + want := map[string]string{ + "GIT_CONFIG_COUNT": "4", + "GIT_CONFIG_KEY_0": "core.autocrlf", + "GIT_CONFIG_VALUE_0": "input", + "GIT_CONFIG_KEY_1": "notes.augment.mergeStrategy", + "GIT_CONFIG_VALUE_1": "union", + "GIT_CONFIG_KEY_2": "core.hooksPath", + "GIT_CONFIG_VALUE_2": "/profile/hooks", + "GIT_CONFIG_KEY_3": "credential.https://github.com.helper", + } + for key, wantValue := range want { + if got := env[key]; got != wantValue { + t.Errorf("%s = %q, want %q", key, got, wantValue) + } + } +} + +func TestResolve_IndexedSourceStillUsesOrdinaryConflictRules(t *testing.T) { + err := conflictOf(t, []Definition{ + {Key: "PATH", Literal: "/managed", Origin: OriginManagedRuntime}, + {Key: "PATH", Literal: "/profile", Origin: OriginExecutorProfile}, + }) + want := []string{OriginExecutorProfile, OriginManagedRuntime} + if !reflect.DeepEqual(err.Origins, want) { + t.Fatalf("origins = %#v, want %#v", err.Origins, want) + } +} + func TestResolve_ManagedRuntimeBeatsAgentProfile(t *testing.T) { // AC-2 env, _ := mustResolve(t, []Definition{ {Key: "K", Literal: "managed-value", Origin: OriginManagedRuntime}, diff --git a/apps/backend/internal/agent/runtime/lifecycle/manager_launch.go b/apps/backend/internal/agent/runtime/lifecycle/manager_launch.go index efae626401..3b0e4a408f 100644 --- a/apps/backend/internal/agent/runtime/lifecycle/manager_launch.go +++ b/apps/backend/internal/agent/runtime/lifecycle/manager_launch.go @@ -2298,32 +2298,46 @@ func getAttachmentsFromMetadata(execution *AgentExecution) []MessageAttachment { // configureAndStartAgent configures the agent command and starts the agent subprocess. // Returns the effective boot command (full command with adapter args, or base command). func (m *Manager) configureAndStartAgent(ctx context.Context, execution *AgentExecution, approvalPolicy string) (string, error) { - env := execution.RuntimeEnvironment() + runtimeSnapshot := execution.RuntimeEnvironment() metadataEnv := runtimeEnvFromMetadata(execution.MetadataSnapshot()) - if env == nil { - env = metadataEnv + var env map[string]string + if runtimeSnapshot == nil { + env = cloneStringMap(metadataEnv) + if env == nil { + env = make(map[string]string) + } if err := m.mergeAgentProfileEnvForExecution(ctx, execution, env); err != nil { m.updateExecutionError(execution.ID, "failed to resolve agent profile environment: "+err.Error()) return "", fmt.Errorf("resolve agent profile environment: %w", err) } } else { // SetExecutionEnv carries per-run values such as repository credentials. - // Overlay them on the launch snapshot without re-reading profile secrets. - for key, value := range metadataEnv { - env[key] = value + // Compose them with the launch snapshot without re-reading profile + // secrets. Host bridge entries are filtered here, while the normal + // agentctl Configure boundary composes the request with the instance's + // canonical environment and preserves inherited user entries. + var err error + env, err = composeExecutionRuntimeEnvironment(runtimeSnapshot, metadataEnv) + if err != nil { + m.updateExecutionError(execution.ID, "failed to compose agent env: "+err.Error()) + return "", fmt.Errorf("compose agent environment: %w", err) } } if err := spillLargeWakePayloadEnv(env, execution.WorkspacePath, m.logger.Zap()); err != nil { m.updateExecutionError(execution.ID, "failed to prepare agent env: "+err.Error()) return "", fmt.Errorf("failed to prepare agent env: %w", err) } + configureEnv := cloneStringMap(env) + execution.setRuntimeEnvironment(env) client, releaseClient := execution.AcquireAgentCtlClient() defer releaseClient() if client == nil { return "", fmt.Errorf("execution %q has no agentctl client", execution.ID) } - if err := client.ConfigureAgent(ctx, execution.AgentCommand, execution.AgentArgs, env, approvalPolicy, execution.ContinueCommand, execution.ContinueArgs); err != nil { + // Starting with stale agent configuration could expose an old credential + // set, so the subprocess must not start when configuration delivery fails. + if err := client.ConfigureAgent(ctx, execution.AgentCommand, execution.AgentArgs, configureEnv, approvalPolicy, execution.ContinueCommand, execution.ContinueArgs); err != nil { return "", fmt.Errorf("failed to configure agent: %w", err) } diff --git a/apps/backend/internal/agent/runtime/lifecycle/manager_launch_test.go b/apps/backend/internal/agent/runtime/lifecycle/manager_launch_test.go index 05d0469875..8b5a186a9c 100644 --- a/apps/backend/internal/agent/runtime/lifecycle/manager_launch_test.go +++ b/apps/backend/internal/agent/runtime/lifecycle/manager_launch_test.go @@ -541,6 +541,88 @@ func TestBuildEnvForExecution_SeparatesOfficeAndExecutionProfiles(t *testing.T) } } +func TestBuildEnvForExecutionHostGHBridge(t *testing.T) { + mgr := newTestManager(t) + profileInfo := &AgentProfileInfo{ + EnvVars: []settingsmodels.ProfileEnvVar{{Key: "GH_TOKEN", Value: "late-profile-token"}}, + } + req := &LaunchRequest{ + TaskID: "task-1", + SessionID: "session-1", + Env: map[string]string{ + "GIT_CONFIG_COUNT": "2", + "GIT_CONFIG_KEY_0": "notes.augment.mergeStrategy", + "GIT_CONFIG_VALUE_0": "union", + "GIT_CONFIG_KEY_1": "credential.https://github.com.helper", + "GIT_CONFIG_VALUE_1": "!'host tools/gh' auth git-credential", + }, + } + + env, err := mgr.buildEnvForExecution(context.Background(), "exec-1", req, nil, profileInfo) + if err != nil { + t.Fatalf("buildEnvForExecution() error = %v", err) + } + if got := env["GH_TOKEN"]; got != "late-profile-token" { + t.Fatalf("GH_TOKEN = %q, want late profile token", got) + } + if got := env["GIT_CONFIG_COUNT"]; got != "2" { + t.Fatalf("GIT_CONFIG_COUNT = %q, want 2", got) + } + if got := env["GIT_CONFIG_KEY_0"]; got != "notes.augment.mergeStrategy" || env["GIT_CONFIG_VALUE_0"] != "union" { + t.Fatalf("inherited Git config entry 0 = (%q, %q)", got, env["GIT_CONFIG_VALUE_0"]) + } + if got := env["GIT_CONFIG_KEY_1"]; got != "credential.https://github.com.helper" || env["GIT_CONFIG_VALUE_1"] == "" { + t.Fatalf("host Git config entry 1 = (%q, %q)", got, env["GIT_CONFIG_VALUE_1"]) + } +} + +func TestBuildEnvForExecutionHostGHBridge_ComposesStrictProfileBlocks(t *testing.T) { + mgr := newTestManager(t) + req := &LaunchRequest{ + TaskID: "task-1", + SessionID: "session-1", + EnvironmentResolutionRequired: true, + Env: map[string]string{ + "GIT_CONFIG_COUNT": "1", + "GIT_CONFIG_KEY_0": "credential.https://github.com.helper", + "GIT_CONFIG_VALUE_0": "!f() { : kandev-host-gh-bridge; gh auth git-credential \"$@\"; }; f", + }, + EnvironmentDefinitions: []runtimeenv.Definition{ + {Key: "GIT_CONFIG_COUNT", Literal: "2", Origin: runtimeenv.OriginExecutorProfile}, + {Key: "GIT_CONFIG_KEY_0", Literal: "notes.augment.mergeStrategy", Origin: runtimeenv.OriginExecutorProfile}, + {Key: "GIT_CONFIG_VALUE_0", Literal: "union", Origin: runtimeenv.OriginExecutorProfile}, + {Key: "GIT_CONFIG_KEY_1", Literal: "core.hooksPath", Origin: runtimeenv.OriginExecutorProfile}, + {Key: "GIT_CONFIG_VALUE_1", Literal: "/profile/hooks", Origin: runtimeenv.OriginExecutorProfile}, + }, + } + profileInfo := &AgentProfileInfo{EnvVars: []settingsmodels.ProfileEnvVar{ + {Key: "GIT_CONFIG_COUNT", Value: "1"}, + {Key: "GIT_CONFIG_KEY_0", Value: "core.autocrlf"}, + {Key: "GIT_CONFIG_VALUE_0", Value: "input"}, + }} + + env, err := mgr.buildEnvForExecution(context.Background(), "exec-1", req, nil, profileInfo) + if err != nil { + t.Fatalf("buildEnvForExecution() error = %v", err) + } + if got := env["GIT_CONFIG_COUNT"]; got != "4" { + t.Fatalf("GIT_CONFIG_COUNT = %q, want 4", got) + } + wantEntries := []struct{ key, value string }{ + {"core.autocrlf", "input"}, + {"notes.augment.mergeStrategy", "union"}, + {"core.hooksPath", "/profile/hooks"}, + {"credential.https://github.com.helper", "!f() { : kandev-host-gh-bridge; gh auth git-credential \"$@\"; }; f"}, + } + for index, want := range wantEntries { + key := env["GIT_CONFIG_KEY_"+strconv.Itoa(index)] + value := env["GIT_CONFIG_VALUE_"+strconv.Itoa(index)] + if key != want.key || value != want.value { + t.Errorf("Git config entry %d = (%q, %q), want (%q, %q)", index, key, value, want.key, want.value) + } + } +} + func TestBuildEnvForExecution_RejectsLateManagedValuesThatConflictWithRepositorySecrets(t *testing.T) { store := newInMemorySecretStore() for _, key := range []string{"AGENT_MODEL", "AGENTCTL_AUTO_APPROVE_PERMISSIONS", "GOCACHE", "ANTHROPIC_API_KEY"} { @@ -858,6 +940,51 @@ func TestConfigureAndStartAgentUsesRuntimeSnapshotWhenProfileSecretIsUnavailable } } +func TestConfigureAndStartAgentSendsComposedRuntimeEnvironmentAsOverlay(t *testing.T) { + mgr := newTestManager(t) + var configuredEnv map[string]string + var replaced bool + client := newConfigureCaptureAgentctlClient(t, newTestLogger(), &configuredEnv, &replaced) + execution := &AgentExecution{ + ID: "exec-1", + TaskID: "task-1", + SessionID: "session-1", + AgentCommand: "npx -y @agentclientprotocol/codex-acp", + WorkspacePath: t.TempDir(), + metadata: map[string]interface{}{ + "runtime_env": map[string]string{ + "GIT_CONFIG_COUNT": "1", + "GIT_CONFIG_KEY_0": "credential.https://github.com.helper", + "GIT_CONFIG_VALUE_0": "!f() { : kandev-host-gh-bridge; '/new/gh' auth git-credential \"$@\"; }; f", + }, + }, + agentctl: client, + } + execution.setRuntimeEnvironment(map[string]string{ + "GIT_CONFIG_COUNT": "3", + "GIT_CONFIG_KEY_0": "notes.augment.mergeStrategy", + "GIT_CONFIG_VALUE_0": "union", + "GIT_CONFIG_KEY_1": "core.hooksPath", + "GIT_CONFIG_VALUE_1": "/user/hooks", + "GIT_CONFIG_KEY_2": "credential.https://github.com.helper", + "GIT_CONFIG_VALUE_2": "!f() { : kandev-host-gh-bridge; '/old/gh' auth git-credential \"$@\"; }; f", + }) + + if _, err := mgr.configureAndStartAgent(context.Background(), execution, "never"); err != nil { + t.Fatalf("configureAndStartAgent() error = %v", err) + } + if replaced { + t.Fatal("runtime snapshot was sent through complete replacement mode, want overlay mode") + } + if configuredEnv["GIT_CONFIG_COUNT"] != "3" || + configuredEnv["GIT_CONFIG_KEY_0"] != "notes.augment.mergeStrategy" || + configuredEnv["GIT_CONFIG_KEY_1"] != "core.hooksPath" || + configuredEnv["GIT_CONFIG_KEY_2"] != "credential.https://github.com.helper" || + !strings.Contains(configuredEnv["GIT_CONFIG_VALUE_2"], "'/new/gh'") { + t.Fatalf("configured runtime environment = %#v, want one composed replacement block", configuredEnv) + } +} + func TestConfigureAndStartAgent_SendsStructuredArgv(t *testing.T) { mgr := newTestManager(t) var captured []string @@ -955,13 +1082,14 @@ func TestSetExecutionEnv_DoesNotSnapshotProfileEnvVars(t *testing.T) { } } -func newConfigureCaptureAgentctlClient(t *testing.T, log *logger.Logger, captured *map[string]string) *agentctl.Client { +func newConfigureCaptureAgentctlClient(t *testing.T, log *logger.Logger, captured *map[string]string, replaced ...*bool) *agentctl.Client { t.Helper() server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { case "/api/v1/agent/configure": var req struct { - Env map[string]string `json:"env"` + Env map[string]string `json:"env"` + ReplaceEnv bool `json:"replace_env"` } if err := json.NewDecoder(r.Body).Decode(&req); err != nil { t.Errorf("decode configure request: %v", err) @@ -969,6 +1097,9 @@ func newConfigureCaptureAgentctlClient(t *testing.T, log *logger.Logger, capture return } *captured = req.Env + if len(replaced) > 0 && replaced[0] != nil { + *replaced[0] = req.ReplaceEnv + } _, _ = w.Write([]byte(`{"success":true}`)) case "/api/v1/start": _, _ = w.Write([]byte(`{"success":true,"command":"npx -y @agentclientprotocol/codex-acp"}`)) diff --git a/apps/backend/internal/agent/runtime/lifecycle/profile_env.go b/apps/backend/internal/agent/runtime/lifecycle/profile_env.go index 25be23dbc3..9047550706 100644 --- a/apps/backend/internal/agent/runtime/lifecycle/profile_env.go +++ b/apps/backend/internal/agent/runtime/lifecycle/profile_env.go @@ -7,6 +7,7 @@ import ( settingsmodels "github.com/kandev/kandev/internal/agent/settings/models" "github.com/kandev/kandev/internal/gitconfigenv" + "github.com/kandev/kandev/internal/githubauth" ) var ErrProfileSecretUnavailable = errors.New("BLOCKED_PROFILE_SECRET") @@ -62,6 +63,42 @@ func mergeEnvFillMissing(dst, src map[string]string) { } } +// composeExecutionRuntimeEnvironment updates an existing execution snapshot +// with a per-run overlay. Host GitHub helpers are Kandev-owned generated +// entries and are removed before composition so a later request can replace +// or remove them without hiding inherited user configuration. +func composeExecutionRuntimeEnvironment(base, overlay map[string]string) (map[string]string, error) { + removeObsoleteManagedCredentialEnvironment(base) + filtered, err := gitconfigenv.Filter(base, func(index int, entries []gitconfigenv.Entry) bool { + return !githubauth.IsHostGitHubCredentialHelperEntry(entries[index].Key, entries[index].Value) + }) + if err != nil { + return nil, fmt.Errorf("remove generated host GitHub helper: %w", err) + } + return gitconfigenv.Merge(filtered, overlay) +} + +func removeObsoleteManagedCredentialEnvironment(env map[string]string) { + for _, key := range []string{ + githubauth.CredentialBrokerURLEnv, + githubauth.CredentialHelperPathEnv, + githubauth.CredentialCLIShimDirEnv, + githubauth.CredentialCLIBashEnvEnv, + githubauth.CredentialParentBashEnv, + githubauth.CredentialLeaseEnv, + githubauth.CredentialReissueCapabilityEnv, + githubauth.CredentialTaskIDEnv, + githubauth.CredentialSessionIDEnv, + githubauth.CredentialRepositoryEnv, + githubauth.CredentialOwnerEnv, + githubauth.CredentialRepoEnv, + githubauth.CredentialHostEnv, + githubauth.CredentialScopesEnv, + } { + delete(env, key) + } +} + // resolveAgentProfileEnvVars resolves profile env entries. SecretID wins over // Value. A missing secret store or failed reveal aborts the whole profile // environment rather than falling back to a literal value or partial map. diff --git a/apps/backend/internal/agent/runtime/lifecycle/profile_env_test.go b/apps/backend/internal/agent/runtime/lifecycle/profile_env_test.go index e80b341e4b..dc58787e88 100644 --- a/apps/backend/internal/agent/runtime/lifecycle/profile_env_test.go +++ b/apps/backend/internal/agent/runtime/lifecycle/profile_env_test.go @@ -7,6 +7,7 @@ import ( settingsmodels "github.com/kandev/kandev/internal/agent/settings/models" "github.com/kandev/kandev/internal/common/logger" + "github.com/kandev/kandev/internal/githubauth" "github.com/kandev/kandev/internal/secrets" ) @@ -73,6 +74,33 @@ func TestMergeEnvFillMissingComposesIndexedGitConfigMismatchedCount(t *testing.T } } +func TestComposeExecutionRuntimeEnvironmentRemovesObsoleteManagedCredentials(t *testing.T) { + base := map[string]string{ + githubauth.CredentialBrokerURLEnv: "https://broker.example/old", + githubauth.CredentialLeaseEnv: "old-lease", + "GIT_CONFIG_COUNT": "2", + "GIT_CONFIG_KEY_0": "core.hooksPath", + "GIT_CONFIG_VALUE_0": "/user/hooks", + "GIT_CONFIG_KEY_1": "credential.https://github.com.helper", + "GIT_CONFIG_VALUE_1": "!f() { : " + githubauth.HostGitHubCredentialHelperMarker + "; '/old/gh' auth git-credential \"$@\"; }; f", + } + + got, err := composeExecutionRuntimeEnvironment(base, map[string]string{"CURRENT": "yes"}) + if err != nil { + t.Fatalf("composeExecutionRuntimeEnvironment() error = %v", err) + } + if _, present := got[githubauth.CredentialBrokerURLEnv]; present { + t.Fatalf("obsolete broker URL remained: %#v", got) + } + if _, present := got[githubauth.CredentialLeaseEnv]; present { + t.Fatalf("obsolete credential lease remained: %#v", got) + } + if got["CURRENT"] != "yes" || got["GIT_CONFIG_COUNT"] != "1" || + got["GIT_CONFIG_KEY_0"] != "core.hooksPath" || got["GIT_CONFIG_VALUE_0"] != "/user/hooks" { + t.Fatalf("composed runtime environment = %#v, want current value and inherited user config", got) + } +} + func TestResolveAgentProfileEnvVars_SecretAndValue(t *testing.T) { store := newInMemorySecretStore() _ = store.Create(context.Background(), &secrets.SecretWithValue{ diff --git a/apps/backend/internal/agentctl/server/api/server.go b/apps/backend/internal/agentctl/server/api/server.go index db78a0bb20..cd8541cadb 100644 --- a/apps/backend/internal/agentctl/server/api/server.go +++ b/apps/backend/internal/agentctl/server/api/server.go @@ -440,6 +440,7 @@ type AgentConfigureRequest struct { ContinueCommand string `json:"continue_command,omitempty"` // For one-shot agents: command for follow-up prompts ContinueArgs optionalArgs `json:"continue_args"` Env map[string]string `json:"env,omitempty"` + ReplaceEnv bool `json:"replace_env,omitempty"` ApprovalPolicy string `json:"approval_policy,omitempty"` // "untrusted", "on-failure", "on-request", or "never" } @@ -477,11 +478,17 @@ func (s *Server) handleAgentConfigure(c *gin.Context) { return } - if err := s.procMgr.Configure(req.Command, req.AgentArgs.Args, req.AgentArgs.Present, req.Env, req.ApprovalPolicy, req.ContinueCommand, req.ContinueArgs.Args, req.ContinueArgs.Present); err != nil { - s.logger.Error("failed to configure agent", zap.Error(err), zap.String("command", req.Command)) + var configureErr error + if req.ReplaceEnv { + configureErr = s.procMgr.ConfigureWithEnvironment(req.Command, req.AgentArgs.Args, req.AgentArgs.Present, req.Env, req.ApprovalPolicy, req.ContinueCommand, req.ContinueArgs.Args, req.ContinueArgs.Present) + } else { + configureErr = s.procMgr.Configure(req.Command, req.AgentArgs.Args, req.AgentArgs.Present, req.Env, req.ApprovalPolicy, req.ContinueCommand, req.ContinueArgs.Args, req.ContinueArgs.Present) + } + if configureErr != nil { + s.logger.Error("failed to configure agent", zap.Error(configureErr), zap.String("command", req.Command)) c.JSON(http.StatusInternalServerError, AgentConfigureResponse{ Success: false, - Error: err.Error(), + Error: configureErr.Error(), }) return } diff --git a/apps/backend/internal/agentctl/server/config/config_test.go b/apps/backend/internal/agentctl/server/config/config_test.go index 623c80195d..c9b3903dc8 100644 --- a/apps/backend/internal/agentctl/server/config/config_test.go +++ b/apps/backend/internal/agentctl/server/config/config_test.go @@ -324,6 +324,63 @@ func TestCollectAgentEnvPreservesParentIndexedGitConfig(t *testing.T) { } } +func TestCollectAgentEnvHostGHBridge(t *testing.T) { + clearParentIndexedGitConfig(t) + ghPath := filepath.Join(t.TempDir(), "host tools", "gh") + if err := os.MkdirAll(filepath.Dir(ghPath), 0o700); err != nil { + t.Fatalf("create fake gh directory: %v", err) + } + const ghScript = `#!/bin/sh +if [ "$1" = "auth" ] && [ "$2" = "git-credential" ]; then + cat >/dev/null + printf 'username=x-access-token\npassword=%s\n' "$GH_TOKEN" + exit 0 +fi +exit 2 +` + if err := os.WriteFile(ghPath, []byte(ghScript), 0o700); err != nil { + t.Fatalf("write fake gh: %v", err) + } + t.Setenv("GIT_CONFIG_COUNT", "2") + t.Setenv("GIT_CONFIG_KEY_0", "notes.augment.mergeStrategy") + t.Setenv("GIT_CONFIG_VALUE_0", "union") + t.Setenv("GIT_CONFIG_KEY_1", "core.hooksPath") + t.Setenv("GIT_CONFIG_VALUE_1", "/Users/cfl12/.locstat/git/hooks") + + env, err := CollectAgentEnvWithError(map[string]string{ + "GH_TOKEN": "late-profile-token", + "HOME": filepath.Join(t.TempDir(), "home"), + "PATH": "/usr/bin:/bin", + "GIT_CONFIG_COUNT": "1", + "GIT_CONFIG_KEY_0": "credential.https://github.com.helper", + "GIT_CONFIG_VALUE_0": "!'" + ghPath + "' auth git-credential", + "GIT_CONFIG_NOSYSTEM": "1", + }) + if err != nil { + t.Fatalf("CollectAgentEnvWithError() error = %v", err) + } + if got := envSliceValue(env, "GIT_CONFIG_COUNT"); got != "3" { + t.Fatalf("GIT_CONFIG_COUNT = %q, want 3", got) + } + if got := envSliceValue(env, "GIT_CONFIG_KEY_0"); got != "notes.augment.mergeStrategy" || envSliceValue(env, "GIT_CONFIG_VALUE_0") != "union" { + t.Fatalf("inherited Git config entry 0 = (%q, %q)", got, envSliceValue(env, "GIT_CONFIG_VALUE_0")) + } + if got := envSliceValue(env, "GIT_CONFIG_KEY_1"); got != "core.hooksPath" || envSliceValue(env, "GIT_CONFIG_VALUE_1") != "/Users/cfl12/.locstat/git/hooks" { + t.Fatalf("inherited Git config entry 1 = (%q, %q)", got, envSliceValue(env, "GIT_CONFIG_VALUE_1")) + } + + command := exec.Command("git", "credential", "fill") + command.Env = env + command.Stdin = strings.NewReader("protocol=https\nhost=github.com\npath=acme/widgets\n\n") + output, err := command.CombinedOutput() + if err != nil { + t.Fatalf("git credential fill failed: %v\n%s", err, output) + } + if !strings.Contains(string(output), "password=late-profile-token") { + t.Fatalf("credential output = %q, want late profile token", output) + } +} + func TestCollectAgentEnvIgnoresParentIndexedGitConfigBeyondCount(t *testing.T) { // Hosts inherit indexed entries from a parent that later lowered // GIT_CONFIG_COUNT. Git ignores the leftovers, so instance creation must diff --git a/apps/backend/internal/agentctl/server/process/manager.go b/apps/backend/internal/agentctl/server/process/manager.go index 56206be7f5..ad5786a2ec 100644 --- a/apps/backend/internal/agentctl/server/process/manager.go +++ b/apps/backend/internal/agentctl/server/process/manager.go @@ -28,6 +28,7 @@ import ( "github.com/kandev/kandev/internal/common/logger" "github.com/kandev/kandev/internal/common/securityutil" "github.com/kandev/kandev/internal/gitconfigenv" + "github.com/kandev/kandev/internal/githubauth" tools "github.com/kandev/kandev/internal/tools/installer" "go.uber.org/zap" ) @@ -1709,6 +1710,18 @@ func lookupEnvValue(env []string, key string) string { // This must be called before Start() if the instance was created without a command. // continueCommand is optional — when set, the adapter uses it for one-shot follow-up prompts. func (m *Manager) Configure(command string, agentArgs []string, agentArgsPresent bool, env map[string]string, approvalPolicy, continueCommand string, continueArgs []string, continueArgsPresent bool) error { + return m.configure(command, agentArgs, agentArgsPresent, env, approvalPolicy, continueCommand, continueArgs, continueArgsPresent, false) +} + +// ConfigureWithEnvironment sets the agent command and replaces the complete +// effective indexed Git configuration block supplied by env. Ordinary +// instance variables that are absent from env remain available to the agent. +// This must be called before Start() if the instance was created without a command. +func (m *Manager) ConfigureWithEnvironment(command string, agentArgs []string, agentArgsPresent bool, env map[string]string, approvalPolicy, continueCommand string, continueArgs []string, continueArgsPresent bool) error { + return m.configure(command, agentArgs, agentArgsPresent, env, approvalPolicy, continueCommand, continueArgs, continueArgsPresent, true) +} + +func (m *Manager) configure(command string, agentArgs []string, agentArgsPresent bool, env map[string]string, approvalPolicy, continueCommand string, continueArgs []string, continueArgsPresent, replaceEnv bool) error { m.startMu.Lock() defer m.startMu.Unlock() @@ -1734,6 +1747,13 @@ func (m *Manager) Configure(command string, agentArgs []string, agentArgsPresent } } + // Compose the environment before changing any other configuration so a + // malformed indexed Git block leaves the instance fully unchanged. + mergedEnv, err := composeConfiguredAgentEnvironment(m.cfg.AgentEnv, env, replaceEnv) + if err != nil { + return fmt.Errorf("compose configured agent environment: %w", err) + } + m.cfg.AgentCommand = command m.cfg.AgentArgs = args @@ -1751,12 +1771,11 @@ func (m *Manager) Configure(command string, agentArgs []string, agentArgsPresent m.cfg.ContinueArgs = continueArgs } - // Merge additional env vars - if len(env) > 0 { - for k, v := range env { - m.cfg.AgentEnv = append(m.cfg.AgentEnv, fmt.Sprintf("%s=%s", k, v)) - } + m.cfg.AgentEnv = mergedEnv + if m.adapterCfg != nil && m.adapterCfg.OneShotConfig != nil { + m.adapterCfg.OneShotConfig.Env = append([]string(nil), m.cfg.AgentEnv...) } + m.setTrackerGitEnvironment(m.cfg.AgentEnv) m.logger.Info("agent configured", zap.String("command", command), @@ -1768,6 +1787,76 @@ func (m *Manager) Configure(command string, agentArgs []string, agentArgsPresent return nil } +func composeConfiguredAgentEnvironment(current []string, overlay map[string]string, replaceIndexed bool) ([]string, error) { + base := environmentMapFromSlice(current) + removeObsoleteManagedCredentialEnvironment(base) + filtered, err := gitconfigenv.Filter(base, func(index int, entries []gitconfigenv.Entry) bool { + return !githubauth.IsHostGitHubCredentialHelperEntry(entries[index].Key, entries[index].Value) + }) + if err != nil { + return nil, fmt.Errorf("remove generated host GitHub helper: %w", err) + } + if replaceIndexed { + // A complete environment owns the entire indexed block, including an + // empty block. Remove the previous block unconditionally so a logout or + // policy change cannot retain stale generated entries. + filtered, err = gitconfigenv.Filter(filtered, func(int, []gitconfigenv.Entry) bool { return false }) + if err != nil { + return nil, fmt.Errorf("replace indexed Git configuration: %w", err) + } + } + if len(filtered) == 0 && len(overlay) == 0 { + return nil, nil + } + merged, err := gitconfigenv.Merge(filtered, overlay) + if err != nil { + return nil, err + } + keys := make([]string, 0, len(merged)) + for key := range merged { + keys = append(keys, key) + } + sort.Strings(keys) + result := make([]string, 0, len(keys)) + for _, key := range keys { + result = append(result, key+"="+merged[key]) + } + return result, nil +} + +func removeObsoleteManagedCredentialEnvironment(env map[string]string) { + for _, key := range []string{ + githubauth.CredentialBrokerURLEnv, + githubauth.CredentialHelperPathEnv, + githubauth.CredentialCLIShimDirEnv, + githubauth.CredentialCLIBashEnvEnv, + githubauth.CredentialParentBashEnv, + githubauth.CredentialLeaseEnv, + githubauth.CredentialReissueCapabilityEnv, + githubauth.CredentialTaskIDEnv, + githubauth.CredentialSessionIDEnv, + githubauth.CredentialRepositoryEnv, + githubauth.CredentialOwnerEnv, + githubauth.CredentialRepoEnv, + githubauth.CredentialHostEnv, + githubauth.CredentialScopesEnv, + } { + delete(env, key) + } +} + +func environmentMapFromSlice(env []string) map[string]string { + result := make(map[string]string, len(env)) + for _, entry := range env { + eq := strings.IndexByte(entry, '=') + if eq <= 0 { + continue + } + result[entry[:eq]] = entry[eq+1:] + } + return result +} + // createAdapter creates the appropriate protocol adapter based on configuration. // This should be called before starting the process so PrepareEnvironment can run. func (m *Manager) createAdapter() error { diff --git a/apps/backend/internal/agentctl/server/process/manager_temp_test.go b/apps/backend/internal/agentctl/server/process/manager_temp_test.go index 144c226ba3..26300c5c38 100644 --- a/apps/backend/internal/agentctl/server/process/manager_temp_test.go +++ b/apps/backend/internal/agentctl/server/process/manager_temp_test.go @@ -14,6 +14,7 @@ import ( "github.com/kandev/kandev/internal/agentctl/server/config" "github.com/kandev/kandev/internal/agentctl/server/shell" + "github.com/kandev/kandev/internal/githubauth" "github.com/kandev/kandev/pkg/agent" ) @@ -171,6 +172,169 @@ func TestManager_ProcessEnvironmentMergesIndexedGitConfig(t *testing.T) { } } +func TestManagerConfigureReplacesOwnedHostHelperAndPreservesIndexedEnvironment(t *testing.T) { + oldHelper := "!f() { : " + githubauth.HostGitHubCredentialHelperMarker + "; '/old/gh' auth git-credential \"$@\"; }; f" + newHelper := "!f() { : " + githubauth.HostGitHubCredentialHelperMarker + "; '/new/gh' auth git-credential \"$@\"; }; f" + mgr := NewManager(&config.InstanceConfig{ + WorkDir: t.TempDir(), + AgentEnv: []string{ + "GIT_CONFIG_COUNT=3", + "GIT_CONFIG_KEY_0=notes.augment.mergeStrategy", + "GIT_CONFIG_VALUE_0=union", + "GIT_CONFIG_KEY_1=core.hooksPath", + "GIT_CONFIG_VALUE_1=/user/hooks", + "GIT_CONFIG_KEY_2=credential.https://github.com.helper", + "GIT_CONFIG_VALUE_2=" + oldHelper, + }, + }, newTestLogger(t)) + t.Cleanup(mgr.stopWorkspaceTrackers) + + configure := func(env map[string]string) { + t.Helper() + if err := mgr.Configure("echo", nil, false, env, "", "", nil, false); err != nil { + t.Fatalf("Configure() error = %v", err) + } + } + configure(map[string]string{ + "GIT_CONFIG_COUNT": "1", + "GIT_CONFIG_KEY_0": "credential.https://github.com.helper", + "GIT_CONFIG_VALUE_0": newHelper, + }) + + env := environmentMap(mgr.cfg.AgentEnv) + if env["GIT_CONFIG_COUNT"] != "3" || env["GIT_CONFIG_KEY_0"] != "notes.augment.mergeStrategy" || + env["GIT_CONFIG_KEY_1"] != "core.hooksPath" || env["GIT_CONFIG_VALUE_1"] != "/user/hooks" || + env["GIT_CONFIG_KEY_2"] != "credential.https://github.com.helper" || env["GIT_CONFIG_VALUE_2"] != newHelper { + t.Fatalf("configured environment = %#v, want inherited entries plus replacement helper", env) + } + if trackerEnv := environmentMap(mgr.GetWorkspaceTracker().gitCommand(context.Background(), false, "status").Env); trackerEnv["GIT_CONFIG_VALUE_2"] != newHelper { + t.Fatalf("tracker helper = %q, want replacement helper", trackerEnv["GIT_CONFIG_VALUE_2"]) + } + + configure(nil) + env = environmentMap(mgr.cfg.AgentEnv) + if env["GIT_CONFIG_COUNT"] != "2" || env["GIT_CONFIG_KEY_0"] != "notes.augment.mergeStrategy" || + env["GIT_CONFIG_KEY_1"] != "core.hooksPath" || env["GIT_CONFIG_VALUE_1"] != "/user/hooks" { + t.Fatalf("reconfigured environment = %#v, want inherited entries without generated helper", env) + } + if _, present := env["GIT_CONFIG_VALUE_2"]; present { + t.Fatalf("stale generated helper remained after reconfiguration: %#v", env) + } +} + +func TestManagerConfigureWithEnvironmentReplacesCompleteIndexedBlock(t *testing.T) { + oldHelper := "!f() { : " + githubauth.HostGitHubCredentialHelperMarker + "; '/old/gh' auth git-credential \"$@\"; }; f" + newHelper := "!f() { : " + githubauth.HostGitHubCredentialHelperMarker + "; '/new/gh' auth git-credential \"$@\"; }; f" + mgr := NewManager(&config.InstanceConfig{ + WorkDir: t.TempDir(), + AgentEnv: []string{ + "GIT_CONFIG_COUNT=3", + "GIT_CONFIG_KEY_0=notes.augment.mergeStrategy", + "GIT_CONFIG_VALUE_0=union", + "GIT_CONFIG_KEY_1=core.hooksPath", + "GIT_CONFIG_VALUE_1=/user/hooks", + "GIT_CONFIG_KEY_2=credential.https://github.com.helper", + "GIT_CONFIG_VALUE_2=" + oldHelper, + }, + }, newTestLogger(t)) + t.Cleanup(mgr.stopWorkspaceTrackers) + + complete := map[string]string{ + "GIT_CONFIG_COUNT": "3", + "GIT_CONFIG_KEY_0": "notes.augment.mergeStrategy", + "GIT_CONFIG_VALUE_0": "union", + "GIT_CONFIG_KEY_1": "core.hooksPath", + "GIT_CONFIG_VALUE_1": "/user/hooks", + "GIT_CONFIG_KEY_2": "credential.https://github.com.helper", + "GIT_CONFIG_VALUE_2": newHelper, + } + if err := mgr.ConfigureWithEnvironment("echo", nil, false, complete, "", "", nil, false); err != nil { + t.Fatalf("ConfigureWithEnvironment() error = %v", err) + } + env := environmentMap(mgr.cfg.AgentEnv) + if env["GIT_CONFIG_COUNT"] != "3" || env["GIT_CONFIG_KEY_0"] != "notes.augment.mergeStrategy" || + env["GIT_CONFIG_KEY_1"] != "core.hooksPath" || env["GIT_CONFIG_VALUE_1"] != "/user/hooks" || + env["GIT_CONFIG_KEY_2"] != "credential.https://github.com.helper" || env["GIT_CONFIG_VALUE_2"] != newHelper { + t.Fatalf("complete configured environment = %#v, want one complete replacement block", env) + } + + if err := mgr.ConfigureWithEnvironment("echo", nil, false, nil, "", "", nil, false); err != nil { + t.Fatalf("ConfigureWithEnvironment() removal error = %v", err) + } + env = environmentMap(mgr.cfg.AgentEnv) + if len(env["GIT_CONFIG_COUNT"]) != 0 { + t.Fatalf("removed configured environment = %#v, want complete indexed block removed", env) + } + for key := range env { + if strings.HasPrefix(key, "GIT_CONFIG_KEY_") || strings.HasPrefix(key, "GIT_CONFIG_VALUE_") { + t.Fatalf("indexed entry remained after complete removal: %#v", env) + } + } +} + +func TestManagerConfigureLeavesConfigurationUnchangedWhenEnvironmentIsInvalid(t *testing.T) { + mgr := NewManager(&config.InstanceConfig{ + WorkDir: t.TempDir(), + AgentCommand: "old-command", + AgentArgs: []string{"old-command", "--old"}, + ApprovalPolicy: "old-policy", + AgentEnv: []string{ + "KEEP_ME=yes", + "GIT_CONFIG_COUNT=1", + "GIT_CONFIG_KEY_0=core.hooksPath", + "GIT_CONFIG_VALUE_0=/user/hooks", + }, + }, newTestLogger(t)) + t.Cleanup(mgr.stopWorkspaceTrackers) + + err := mgr.ConfigureWithEnvironment( + "new-command", []string{"new-command"}, true, + map[string]string{"GIT_CONFIG_COUNT": "1", "GIT_CONFIG_KEY_0": "missing-value"}, + "new-policy", "", nil, false, + ) + if err == nil { + t.Fatal("ConfigureWithEnvironment() succeeded with malformed indexed Git block") + } + if mgr.cfg.AgentCommand != "old-command" || strings.Join(mgr.cfg.AgentArgs, " ") != "old-command --old" || + mgr.cfg.ApprovalPolicy != "old-policy" { + t.Fatalf("configuration mutated after failed composition: command=%q args=%#v policy=%q", mgr.cfg.AgentCommand, mgr.cfg.AgentArgs, mgr.cfg.ApprovalPolicy) + } + env := environmentMap(mgr.cfg.AgentEnv) + if env["KEEP_ME"] != "yes" || env["GIT_CONFIG_COUNT"] != "1" || env["GIT_CONFIG_VALUE_0"] != "/user/hooks" { + t.Fatalf("environment mutated after failed composition: %#v", env) + } +} + +func TestManagerConfigureRemovesObsoleteManagedCredentialEnvironment(t *testing.T) { + mgr := NewManager(&config.InstanceConfig{ + WorkDir: t.TempDir(), + AgentEnv: []string{ + "KEEP_ME=yes", + "KANDEV_GITHUB_CREDENTIAL_BROKER_URL=https://broker.example/resolve", + "KANDEV_GITHUB_CREDENTIAL_LEASE=stale-lease", + "KANDEV_GITHUB_CLI_SHIM_DIR=/stale/shim", + }, + }, newTestLogger(t)) + t.Cleanup(mgr.stopWorkspaceTrackers) + + if err := mgr.Configure("echo", nil, false, nil, "", "", nil, false); err != nil { + t.Fatalf("Configure() error = %v", err) + } + env := environmentMap(mgr.cfg.AgentEnv) + if env["KEEP_ME"] != "yes" { + t.Fatalf("ordinary environment was removed: %#v", env) + } + for _, key := range []string{ + "KANDEV_GITHUB_CREDENTIAL_BROKER_URL", + "KANDEV_GITHUB_CREDENTIAL_LEASE", + "KANDEV_GITHUB_CLI_SHIM_DIR", + } { + if _, ok := env[key]; ok { + t.Fatalf("obsolete managed credential %s remained: %#v", key, env) + } + } +} + func TestManager_PipedProcessInheritsAgentEnvironment(t *testing.T) { mgr := NewManager(&config.InstanceConfig{ AgentEnv: []string{ diff --git a/apps/backend/internal/agentctl/server/process/workspace_git_env_test.go b/apps/backend/internal/agentctl/server/process/workspace_git_env_test.go index c14b92e0d1..91458d613b 100644 --- a/apps/backend/internal/agentctl/server/process/workspace_git_env_test.go +++ b/apps/backend/internal/agentctl/server/process/workspace_git_env_test.go @@ -4,6 +4,7 @@ import ( "context" "os" "path/filepath" + "strings" "testing" "github.com/kandev/kandev/internal/agentctl/server/config" @@ -61,6 +62,55 @@ func TestManagerTrackerGitEnvironmentUsesInstanceEnvironment(t *testing.T) { } } +func TestManagerHostGHBridgeGitEnvironment(t *testing.T) { + ghPath := filepath.Join(t.TempDir(), "host tools", "gh") + if err := os.MkdirAll(filepath.Dir(ghPath), 0o700); err != nil { + t.Fatalf("create fake gh directory: %v", err) + } + const ghScript = `#!/bin/sh +if [ "$1" = "auth" ] && [ "$2" = "git-credential" ]; then + cat >/dev/null + printf 'username=x-access-token\npassword=%s\n' "$GH_TOKEN" + exit 0 +fi +exit 2 +` + if err := os.WriteFile(ghPath, []byte(ghScript), 0o700); err != nil { + t.Fatalf("write fake gh: %v", err) + } + instanceEnv := []string{ + "GH_TOKEN=late-profile-token", + "HOME=" + filepath.Join(t.TempDir(), "home"), + "PATH=/usr/bin:/bin", + "GIT_CONFIG_NOSYSTEM=1", + "GIT_CONFIG_COUNT=3", + "GIT_CONFIG_KEY_0=notes.augment.mergeStrategy", + "GIT_CONFIG_VALUE_0=union", + "GIT_CONFIG_KEY_1=core.hooksPath", + "GIT_CONFIG_VALUE_1=/Users/cfl12/.locstat/git/hooks", + "GIT_CONFIG_KEY_2=credential.https://github.com.helper", + "GIT_CONFIG_VALUE_2=!" + "'" + ghPath + "' auth git-credential", + } + mgr := NewManager(&config.InstanceConfig{WorkDir: t.TempDir(), AgentEnv: instanceEnv}, newTestLogger(t)) + t.Cleanup(mgr.stopWorkspaceTrackers) + + cmd := mgr.GetWorkspaceTracker().gitCommand(context.Background(), false, "credential", "fill") + env := environmentMap(cmd.Env) + if env["GIT_CONFIG_COUNT"] != "3" || env["GIT_CONFIG_KEY_0"] != "notes.augment.mergeStrategy" || + env["GIT_CONFIG_VALUE_0"] != "union" || env["GIT_CONFIG_KEY_1"] != "core.hooksPath" || + env["GIT_CONFIG_VALUE_1"] != "/Users/cfl12/.locstat/git/hooks" { + t.Fatalf("tracker Git environment changed inherited entries: %#v", env) + } + cmd.Stdin = strings.NewReader("protocol=https\nhost=github.com\npath=acme/widgets\n\n") + output, err := cmd.CombinedOutput() + if err != nil { + t.Fatalf("tracker git credential fill failed: %v\n%s", err, output) + } + if !strings.Contains(string(output), "password=late-profile-token") { + t.Fatalf("credential output = %q, want late profile token", output) + } +} + func TestWorkspaceGitEnvironmentForcesExplicitSSHBatchMode(t *testing.T) { tracker := NewWorkspaceTracker(t.TempDir(), newTestLogger(t)) tracker.SetGitEnvironment([]string{ diff --git a/apps/backend/internal/backendapp/adapters.go b/apps/backend/internal/backendapp/adapters.go index 96003a42d0..62f5f86bb3 100644 --- a/apps/backend/internal/backendapp/adapters.go +++ b/apps/backend/internal/backendapp/adapters.go @@ -584,6 +584,12 @@ func (a *lifecycleAdapter) SetExecutionEnv(ctx context.Context, agentExecutionID return a.mgr.SetExecutionEnv(ctx, agentExecutionID, env) } +// ExecutorProfileEnvForSession resolves the current executor profile +// environment for a prepared workspace before its agent process starts. +func (a *lifecycleAdapter) ExecutorProfileEnvForSession(ctx context.Context, sessionID, taskEnvironmentID string) (map[string]string, error) { + return a.mgr.ExecutorProfileEnvForSession(ctx, sessionID, taskEnvironmentID) +} + // SetMcpMode changes the MCP tool mode on an existing execution's agentctl instance. func (a *lifecycleAdapter) SetMcpMode(ctx context.Context, executionID string, mode string) error { return a.mgr.SetMcpMode(ctx, executionID, mode) @@ -940,6 +946,7 @@ func (a *lifecycleAdapter) ResolveAgentProfile(ctx context.Context, profileID st AutoApprove: info.AutoApprove, DangerouslySkipPermissions: info.DangerouslySkipPermissions, CLIPassthrough: info.CLIPassthrough, + EnvVars: append([]models.ProfileEnvVar(nil), info.EnvVars...), SupportsMCP: info.SupportsMCP, }, nil } diff --git a/apps/backend/internal/gitconfigenv/environment.go b/apps/backend/internal/gitconfigenv/environment.go index fcea513a90..06ed63613c 100644 --- a/apps/backend/internal/gitconfigenv/environment.go +++ b/apps/backend/internal/gitconfigenv/environment.go @@ -44,6 +44,9 @@ func Merge(base, overlay map[string]string) (map[string]string, error) { } } entries := removeBoundaryOverlap(baseEntries, overlayEntries) + if len(entries) > maxEntries { + return nil, fmt.Errorf("combined Git config has %d entries; maximum is %d", len(entries), maxEntries) + } writeEntries(result, entries) return result, nil } @@ -148,6 +151,12 @@ func entriesFrom(env map[string]string) ([]Entry, error) { } func removeBoundaryOverlap(base, overlay []Entry) []Entry { + // A caller may forward a complete snapshot that already starts with the + // current base block and adds a new suffix. Treat that as an already + // composed snapshot instead of duplicating the inherited entries. + if len(overlay) >= len(base) && entriesEqual(overlay[:len(base)], base) { + return append([]Entry{}, overlay...) + } maxOverlap := min(len(base), len(overlay)) for overlap := maxOverlap; overlap > 0; overlap-- { if entriesEqual(base[len(base)-overlap:], overlay[:overlap]) { diff --git a/apps/backend/internal/gitconfigenv/environment_test.go b/apps/backend/internal/gitconfigenv/environment_test.go index 4b6890429e..daa3ae1c33 100644 --- a/apps/backend/internal/gitconfigenv/environment_test.go +++ b/apps/backend/internal/gitconfigenv/environment_test.go @@ -52,6 +52,29 @@ func TestIndexedGitConfigMergeCollapsesOnlyExactBoundaryOverlap(t *testing.T) { } } +func TestIndexedGitConfigMergeRecognizesAlreadyForwardedSnapshot(t *testing.T) { + base := environmentWithEntries( + Entry{Key: "notes.augment.mergeStrategy", Value: "union"}, + Entry{Key: "core.hooksPath", Value: "/user/hooks"}, + ) + forwarded := environmentWithEntries( + Entry{Key: "notes.augment.mergeStrategy", Value: "union"}, + Entry{Key: "core.hooksPath", Value: "/user/hooks"}, + Entry{Key: "credential.https://github.com.helper", Value: "!kandev host helper"}, + ) + merged, err := Merge(base, forwarded) + if err != nil { + t.Fatalf("Merge() error = %v", err) + } + + if got := merged["GIT_CONFIG_COUNT"]; got != "3" { + t.Fatalf("GIT_CONFIG_COUNT = %q, want 3", got) + } + if got := merged["GIT_CONFIG_KEY_2"]; got != "credential.https://github.com.helper" { + t.Fatalf("GIT_CONFIG_KEY_2 = %q, want forwarded helper", got) + } +} + func TestIndexedGitConfigMergeRetainsMeaningfulRepeatedEntries(t *testing.T) { merged, err := Merge(environmentWithEntries( Entry{Key: "url.https://github.com/.insteadOf", Value: "git@github.com:"}, @@ -77,6 +100,20 @@ func TestIndexedGitConfigMergeRejectsMalformedBlock(t *testing.T) { } } +func TestIndexedGitConfigMergeRejectsCombinedEntryLimit(t *testing.T) { + baseEntries := make([]Entry, maxEntries) + for index := range baseEntries { + baseEntries[index] = Entry{Key: "test.key", Value: "value"} + } + + _, err := Merge(environmentWithEntries(baseEntries...), environmentWithEntries( + Entry{Key: "credential.https://github.com.helper", Value: "!gh auth git-credential"}, + )) + if err == nil { + t.Fatal("Merge() error = nil, want combined entry limit error") + } +} + func TestIndexedGitConfigMergeIgnoresEntriesBeyondCount(t *testing.T) { // Observed on a host whose shell hook re-initialized the block at count 2 // without clearing the higher indexes a nested shell had appended. Git diff --git a/apps/backend/internal/githubauth/environment.go b/apps/backend/internal/githubauth/environment.go index 14ad29cf42..a90ee83b10 100644 --- a/apps/backend/internal/githubauth/environment.go +++ b/apps/backend/internal/githubauth/environment.go @@ -18,6 +18,11 @@ const ( CredentialHostEnv = "KANDEV_GITHUB_CREDENTIAL_HOST" CredentialScopesEnv = "KANDEV_GITHUB_CREDENTIAL_SCOPES" + // HostGitHubCredentialHelperMarker identifies helpers generated by the + // local executor bridge. It is intentionally part of the command value so + // cleanup can preserve user-owned helpers with the same gh command shape. + HostGitHubCredentialHelperMarker = "kandev-host-gh-bridge" + ManagedGitCredentialHelper = `!"${KANDEV_GITHUB_CREDENTIAL_HELPER_PATH}" git-credential` LegacyShimGitCredentialHelper = `!"${KANDEV_GITHUB_CLI_SHIM_DIR}/agentctl" git-credential` LegacyGitCredentialHelper = "!agentctl git-credential" @@ -46,3 +51,21 @@ func CanonicalCredentialPath(raw string) string { } return trimmed } + +// IsHostGitHubCredentialHelper reports whether a Git helper command belongs +// to the local host GitHub bridge. The marker is the ownership boundary; +// command shape alone is not sufficient because users may select the same gh +// executable and operation themselves. +func IsHostGitHubCredentialHelper(value string) bool { + return strings.Contains(value, ": "+HostGitHubCredentialHelperMarker+";") +} + +// IsHostGitHubCredentialHelperEntry reports ownership for a host-scoped +// credential helper entry. Callers can use it while filtering Git's indexed +// environment block without removing unrelated hosts or generic helpers. +func IsHostGitHubCredentialHelperEntry(key, value string) bool { + key = strings.ToLower(strings.TrimSpace(key)) + return strings.HasPrefix(key, "credential.https://") && + strings.HasSuffix(key, ".helper") && + IsHostGitHubCredentialHelper(value) +} diff --git a/apps/backend/internal/orchestrator/executor/executor.go b/apps/backend/internal/orchestrator/executor/executor.go index 0d211a3683..497fa16437 100644 --- a/apps/backend/internal/orchestrator/executor/executor.go +++ b/apps/backend/internal/orchestrator/executor/executor.go @@ -482,6 +482,10 @@ type AgentProfileInfo struct { CLIPassthrough bool NativeSessionResume bool // Agent supports ACP session/load for resume SupportsMCP bool + // EnvVars carries profile definitions, including only opaque SecretID + // references for secret-backed values. The executor uses credential-store + // selection variables before probing the optional host bridge. + EnvVars []models.ProfileEnvVar } // LaunchAgentRequest contains parameters for launching an agent @@ -950,6 +954,7 @@ type Executor struct { gitCredentialBrokerURL string githubCredentialPolicyResolver TaskGitCredentialPolicyResolver agentctlBinaryPath string + hostGitHubCredentialProbe hostGitHubCredentialProbe // Configuration retryLimit int @@ -1225,13 +1230,14 @@ type ShellPreferenceProvider interface { // NewExecutor creates a new executor func NewExecutor(agentManager AgentManagerClient, repo executorStore, log *logger.Logger, cfg ExecutorConfig) *Executor { return &Executor{ - agentManager: agentManager, - repo: repo, - secretStore: cfg.SecretStore, - shellPrefs: cfg.ShellPrefs, - logger: log.WithFields(zap.String("component", "executor")), - retryLimit: 3, - retryDelay: 5 * time.Second, + agentManager: agentManager, + repo: repo, + secretStore: cfg.SecretStore, + shellPrefs: cfg.ShellPrefs, + logger: log.WithFields(zap.String("component", "executor")), + retryLimit: 3, + retryDelay: 5 * time.Second, + hostGitHubCredentialProbe: runHostGitHubCredentialProbe, } } diff --git a/apps/backend/internal/orchestrator/executor/executor_credentials.go b/apps/backend/internal/orchestrator/executor/executor_credentials.go index 7aac4f3b95..c2986e77df 100644 --- a/apps/backend/internal/orchestrator/executor/executor_credentials.go +++ b/apps/backend/internal/orchestrator/executor/executor_credentials.go @@ -184,34 +184,126 @@ func (e *Executor) configureGitCredentialBrokerForRepositories( req *LaunchAgentRequest, infos []*repoInfo, ) error { - if len(infos) == 0 { - return nil - } - policy := TaskGitCredentialPolicy{Mode: taskGitCredentialsModeManaged} - if e.githubCredentialPolicyResolver != nil { - resolved, err := e.githubCredentialPolicyResolver.ResolveTaskGitCredentialPolicy(ctx, req.WorkspaceID) - if err != nil { - return fmt.Errorf("resolve task Git credential policy: %w", err) - } - policy = resolved + return e.configureGitCredentialBrokerForRepositoriesWithProfileEnv(ctx, req, infos, nil) +} + +func (e *Executor) configureGitCredentialBrokerForRepositoriesWithProfileEnv( + ctx context.Context, + req *LaunchAgentRequest, + infos []*repoInfo, + profileEnvVars []models.ProfileEnvVar, +) error { + return e.configureGitCredentialBrokerForRepositoriesWithProfileEnvAndBridge( + ctx, req, infos, profileEnvVars, true, + ) +} + +func (e *Executor) configureGitCredentialBrokerForRepositoriesWithProfileEnvAndBridge( + ctx context.Context, + req *LaunchAgentRequest, + infos []*repoInfo, + profileEnvVars []models.ProfileEnvVar, + hostBridgeProfileResolved bool, +) error { + if req == nil { + return errors.New("launch request is required") } if req.Env == nil { req.Env = make(map[string]string) } - if policy.Mode == taskGitCredentialsModeExecutor || req.Env[envGitHubToken] != "" || req.Env[envGHToken] != "" { - if err := removeManagedGitHubCredentials(req); err != nil { - return err - } - clearManagedContributionDestinations(req, infos) + if err := removeHostGitHubCredentialHelpers(req); err != nil { + return err + } + if len(infos) == 0 { return nil } + // Plugin-owned repositories do not need the workspace GitHub policy. Keep + // their opaque lease path independent so a missing GitHub policy cannot + // block an otherwise valid plugin launch. + if !includesGitHubRepository(infos) { + return e.configurePluginGitCredentials(ctx, req, infos, profileEnvVars) + } + policy, err := e.resolveTaskGitCredentialPolicy(ctx, req.WorkspaceID) + if err != nil { + return err + } + if policy.Mode == taskGitCredentialsModeExecutor { + return e.configureExecutorGitCredentials(ctx, req, infos, profileEnvVars, hostBridgeProfileResolved) + } + return e.configureManagedGitCredentials(ctx, req, infos, profileEnvVars) +} + +func (e *Executor) resolveTaskGitCredentialPolicy(ctx context.Context, workspaceID string) (TaskGitCredentialPolicy, error) { + policy := TaskGitCredentialPolicy{Mode: taskGitCredentialsModeManaged} + if e.githubCredentialPolicyResolver == nil { + return policy, nil + } + resolved, err := e.githubCredentialPolicyResolver.ResolveTaskGitCredentialPolicy(ctx, workspaceID) + if err != nil { + return policy, fmt.Errorf("resolve task Git credential policy: %w", err) + } + return resolved, nil +} + +func (e *Executor) configureExecutorGitCredentials( + ctx context.Context, + req *LaunchAgentRequest, + infos []*repoInfo, + profileEnvVars []models.ProfileEnvVar, + hostBridgeProfileResolved bool, +) error { + if err := removeManagedGitHubCredentials(req); err != nil { + return err + } + clearManagedContributionDestinations(req, infos) + if err := e.configurePluginGitCredentials(ctx, req, infos, profileEnvVars); err != nil { + return err + } + if !hostBridgeProfileResolved { + return nil + } + return e.configureHostGitHubCredentialBridgeWithProfileEnv(ctx, req, infos, profileEnvVars) +} + +func (e *Executor) configurePluginGitCredentials( + ctx context.Context, + req *LaunchAgentRequest, + infos []*repoInfo, + profileEnvVars []models.ProfileEnvVar, +) error { if e.gitCredentialIssuer == nil { if hasManagedContributionDestination(infos) { return errors.New("managed contribution destination cannot be activated without the GitHub credential broker") } return nil } - scopes, helpers, err := e.issueGitCredentialScopes(ctx, req, infos, true) + scopes, helpers, err := e.issueGitCredentialScopesWithProfileEnv(ctx, req, infos, false, profileEnvVars) + if err != nil { + return err + } + if len(scopes) == 0 { + return nil + } + return e.configureGitCredentialEnvironment(req, scopes, helpers) +} + +func (e *Executor) configureManagedGitCredentials( + ctx context.Context, + req *LaunchAgentRequest, + infos []*repoInfo, + profileEnvVars []models.ProfileEnvVar, +) error { + if err := removeManagedGitHubCredentials(req); err != nil { + return err + } + clearManagedContributionDestinationsForExplicitTokens(req, infos, profileEnvVars) + if e.gitCredentialIssuer == nil { + if hasManagedContributionDestination(infos) { + return errors.New("managed contribution destination cannot be activated without the GitHub credential broker") + } + return nil + } + scopes, helpers, err := e.issueGitCredentialScopesWithProfileEnv(ctx, req, infos, true, profileEnvVars) if err != nil { return err } @@ -272,24 +364,15 @@ func (e *Executor) preflightManagedGitCredentials( // nothing here will ever attempt to resolve a repository's identity. return nil } - policy := TaskGitCredentialPolicy{Mode: taskGitCredentialsModeManaged} - if e.githubCredentialPolicyResolver != nil { - resolved, err := e.githubCredentialPolicyResolver.ResolveTaskGitCredentialPolicy(ctx, workspaceID) - if err != nil { - return fmt.Errorf("resolve task Git credential policy: %w", err) - } - policy = resolved - } - if policy.Mode == taskGitCredentialsModeExecutor { - return nil - } - if executorProfileHasGitHubToken(execConfig) { - return nil - } taskRepos, err := e.repo.ListTaskRepositories(ctx, taskID) if err != nil { return fmt.Errorf("list task repositories: %w", err) } + type preflightRepository struct { + repository *models.Repository + } + repositories := make([]preflightRepository, 0, len(taskRepos)) + hasGitHub := false for _, taskRepo := range taskRepos { if taskRepo == nil || taskRepo.RepositoryID == "" { continue @@ -298,7 +381,37 @@ func (e *Executor) preflightManagedGitCredentials( if err != nil { return fmt.Errorf("load repository %q: %w", taskRepo.RepositoryID, err) } - if repository == nil || managedGitCredentialProvider(repository, true, nil) == "" { + if repository == nil { + continue + } + repositories = append(repositories, preflightRepository{repository: repository}) + if repositoryCanUseGitHubCredentials(repository) { + hasGitHub = true + } + } + policy := TaskGitCredentialPolicy{Mode: taskGitCredentialsModeManaged} + if hasGitHub && e.githubCredentialPolicyResolver != nil { + resolved, err := e.githubCredentialPolicyResolver.ResolveTaskGitCredentialPolicy(ctx, workspaceID) + if err != nil { + return fmt.Errorf("resolve task Git credential policy: %w", err) + } else { + policy = resolved + } + } + // A remote executor profile with gh_cli_env supplies an explicit credential + // for this launch. Managed identity preflight cannot require a broker + // identity when that executor credential path will be selected. + if executorProfileHasGitHubToken(execConfig) { + return nil + } + for _, entry := range repositories { + repository := entry.repository + providerID := managedGitCredentialProvider(repository, true, nil) + if providerID == "" { + continue + } + if providerID == gitHubProviderID && + (policy.Mode == taskGitCredentialsModeExecutor || executorProfileHasGitHubToken(execConfig)) { continue } if _, _, _, _, err := gitCredentialCloneIdentity(repository, repository.ID); err != nil { @@ -308,6 +421,14 @@ func (e *Executor) preflightManagedGitCredentials( return nil } +func repositoryCanUseGitHubCredentials(repository *models.Repository) bool { + if repository == nil { + return false + } + provider := strings.ToLower(strings.TrimSpace(repository.Provider)) + return provider == gitHubProviderID || (provider == "" && repository.SourceType != sourceTypeLocal) +} + func executorProfileHasGitHubToken(execConfig executorConfig) bool { if !executorNeedsResolvedCredentials(execConfig.ExecutorType) { return false @@ -328,14 +449,21 @@ func (e *Executor) issueGitCredentialScopes( req *LaunchAgentRequest, infos []*repoInfo, githubManaged bool, +) ([]githubCredentialScope, map[string]struct{}, error) { + return e.issueGitCredentialScopesWithProfileEnv(ctx, req, infos, githubManaged, nil) +} + +func (e *Executor) issueGitCredentialScopesWithProfileEnv( + ctx context.Context, + req *LaunchAgentRequest, + infos []*repoInfo, + githubManaged bool, + profileEnvVars []models.ProfileEnvVar, ) ([]githubCredentialScope, map[string]struct{}, error) { // Validate every managed repository before issuing the first lease. This avoids // leaving a partial lease set when a later task binding is invalid. for _, info := range infos { - if info == nil || info.Repository == nil || managedGitCredentialProvider(info.Repository, githubManaged, req.Env) == "" { - continue - } - if _, _, _, _, err := gitCredentialCloneIdentity(info.Repository, info.RepositoryID); err != nil { + if err := validateGitCredentialScopeInfo(info, req, githubManaged, profileEnvVars); err != nil { return nil, nil, err } } @@ -343,34 +471,71 @@ func (e *Executor) issueGitCredentialScopes( scopes := make([]githubCredentialScope, 0, len(infos)*2) helpers := make(map[string]struct{}, len(infos)) for _, info := range infos { - scope, err := e.issueGitCredentialScope(ctx, req, info, githubManaged) + infoScopes, err := e.issueGitCredentialScopesForInfo(ctx, req, info, githubManaged, profileEnvVars) if err != nil { return nil, nil, err } - if scope != nil { - scopes = append(scopes, *scope) + for _, scope := range infoScopes { + scopes = append(scopes, scope) helpers[scope.Host] = struct{}{} } - contributionScope, err := e.issueGitHubContributionCredentialScope(ctx, req, info) - if err != nil { - return nil, nil, err - } - if contributionScope != nil { - scopes = append(scopes, *contributionScope) - helpers[contributionScope.Host] = struct{}{} - } - destinationScope, err := e.issueGitHubContributionDestinationCredentialScope(ctx, req, info) - if err != nil { - return nil, nil, err - } - if destinationScope != nil { - scopes = append(scopes, *destinationScope) - helpers[destinationScope.Host] = struct{}{} - } } return scopes, helpers, nil } +func validateGitCredentialScopeInfo( + info *repoInfo, + req *LaunchAgentRequest, + githubManaged bool, + profileEnvVars []models.ProfileEnvVar, +) error { + if info == nil || info.Repository == nil || + managedGitCredentialProviderForProfile(info.Repository, githubManaged, req.Env, profileEnvVars) == "" { + return nil + } + _, _, _, _, err := gitCredentialCloneIdentity(info.Repository, info.RepositoryID) + return err +} + +func (e *Executor) issueGitCredentialScopesForInfo( + ctx context.Context, + req *LaunchAgentRequest, + info *repoInfo, + githubManaged bool, + profileEnvVars []models.ProfileEnvVar, +) ([]githubCredentialScope, error) { + providerID := "" + if info != nil && info.Repository != nil { + providerID = managedGitCredentialProviderForProfile(info.Repository, githubManaged, req.Env, profileEnvVars) + } + scopes := make([]githubCredentialScope, 0, 3) + scope, err := e.issueGitCredentialScopeWithProfileEnv(ctx, req, info, githubManaged, profileEnvVars) + if err != nil { + return nil, err + } + if scope != nil { + scopes = append(scopes, *scope) + } + if !githubManaged || providerID != gitHubProviderID { + return scopes, nil + } + contributionScope, err := e.issueGitHubContributionCredentialScope(ctx, req, info) + if err != nil { + return nil, err + } + if contributionScope != nil { + scopes = append(scopes, *contributionScope) + } + destinationScope, err := e.issueGitHubContributionDestinationCredentialScope(ctx, req, info) + if err != nil { + return nil, err + } + if destinationScope != nil { + scopes = append(scopes, *destinationScope) + } + return scopes, nil +} + func (e *Executor) configureGitCredentialEnvironment( req *LaunchAgentRequest, scopes []githubCredentialScope, @@ -408,7 +573,7 @@ func (e *Executor) configureGitCredentialEnvironment( func includesGitHubRepository(infos []*repoInfo) bool { for _, info := range infos { - if info != nil && info.Repository != nil && strings.EqualFold(info.Repository.Provider, gitHubProviderID) { + if info != nil && info.Repository != nil && managedGitCredentialProvider(info.Repository, true, nil) == gitHubProviderID { return true } } @@ -493,12 +658,22 @@ func (e *Executor) issueGitCredentialScope( req *LaunchAgentRequest, info *repoInfo, githubManaged bool, +) (*githubCredentialScope, error) { + return e.issueGitCredentialScopeWithProfileEnv(ctx, req, info, githubManaged, nil) +} + +func (e *Executor) issueGitCredentialScopeWithProfileEnv( + ctx context.Context, + req *LaunchAgentRequest, + info *repoInfo, + githubManaged bool, + profileEnvVars []models.ProfileEnvVar, ) (*githubCredentialScope, error) { if info == nil || info.Repository == nil { return nil, nil } repository := info.Repository - providerID := managedGitCredentialProvider(repository, githubManaged, req.Env) + providerID := managedGitCredentialProviderForProfile(repository, githubManaged, req.Env, profileEnvVars) if providerID == "" { return nil, nil } @@ -671,6 +846,18 @@ func (e *Executor) issueGitCredentialLease( } func managedGitCredentialProvider(repository *models.Repository, githubManaged bool, env map[string]string) string { + return managedGitCredentialProviderForProfile(repository, githubManaged, env, nil) +} + +func managedGitCredentialProviderForProfile( + repository *models.Repository, + githubManaged bool, + env map[string]string, + profileEnvVars []models.ProfileEnvVar, +) string { + if repository == nil { + return "" + } if repository.SourceType == sourceTypeLocal && strings.TrimSpace(repository.Provider) == "" { return "" } @@ -684,12 +871,59 @@ func managedGitCredentialProvider(repository *models.Repository, githubManaged b if providerID == gitLabProviderID || providerID == providerAzureDevOps { return "" } - if providerID == gitHubProviderID && (!githubManaged || env[envGitHubToken] != "" || env[envGHToken] != "") { + if providerID == gitHubProviderID && (!githubManaged || hasExplicitGitHubTokenForRepository(env, profileEnvVars, repository)) { return "" } return providerID } +func clearManagedContributionDestinationsForExplicitTokens( + req *LaunchAgentRequest, + infos []*repoInfo, + profileEnvVars []models.ProfileEnvVar, +) { + explicitRepositories := make(map[string]struct{}, len(infos)) + var env map[string]string + if req != nil { + env = req.Env + } + for _, info := range infos { + if info == nil || info.Repository == nil || + managedGitCredentialProvider(info.Repository, true, nil) != gitHubProviderID || + !hasExplicitGitHubTokenForRepository(env, profileEnvVars, info.Repository) { + continue + } + info.ContributionDestination = nil + explicitRepositories[info.RepositoryID] = struct{}{} + } + if req == nil { + return + } + if _, found := explicitRepositories[req.RepositoryID]; found { + req.ContributionDestination = nil + } + for index := range req.Repositories { + if _, found := explicitRepositories[req.Repositories[index].RepositoryID]; found { + req.Repositories[index].ContributionDestination = nil + } + } +} + +func hasExplicitGitHubTokenForRepository( + env map[string]string, + profileEnvVars []models.ProfileEnvVar, + repository *models.Repository, +) bool { + if repository == nil { + return false + } + host, _, _, _, err := gitCredentialCloneIdentity(repository, repository.ID) + if err != nil { + return false + } + return hasExplicitGitHubToken(env, host) || hasExplicitGitHubTokenInProfile(profileEnvVars, host) +} + func gitCredentialCloneIdentity(repository *models.Repository, repositoryID string) (string, string, string, string, error) { if repository == nil { return "", "", "", "", fmt.Errorf("repository %q is required", repositoryID) diff --git a/apps/backend/internal/orchestrator/executor/executor_credentials_preflight_test.go b/apps/backend/internal/orchestrator/executor/executor_credentials_preflight_test.go index 3d2803ca5c..1521441a07 100644 --- a/apps/backend/internal/orchestrator/executor/executor_credentials_preflight_test.go +++ b/apps/backend/internal/orchestrator/executor/executor_credentials_preflight_test.go @@ -133,13 +133,14 @@ func TestPreflightManagedGitCredentialsValidatesEveryBindingInOrder(t *testing.T // TestPreflightManagedGitCredentialsSkipsWhenExecutorModePolicy covers the // workspace policy applicability rule: when the resolved policy routes Git // credentials through the executor rather than the managed broker, no -// repository identity is validated at all - matching -// configureGitCredentialBrokerForRepositories' own unconditional skip. +// GitHub repository identity is validated - matching the shared route's +// executor-mode bypass. Plugin repository bindings remain independently +// validated because executor mode still issues their opaque leases. func TestPreflightManagedGitCredentialsSkipsWhenExecutorModePolicy(t *testing.T) { repo := newMockRepository() seedPreflightTaskRepository(repo, "task-1", "repo-1", &models.Repository{ - ID: "repo-1", SourceType: sourceTypeLocal, Provider: "acme-forge", - RemoteURL: "https://forge.example/acme/widgets.git", + ID: "repo-1", SourceType: sourceTypeLocal, Provider: gitHubProviderID, + RemoteURL: "https://github.com/acme/widgets.git", }) exec := newPreflightTestExecutor(t, repo) exec.SetTaskGitCredentialPolicyResolver(fakeTaskGitCredentialPolicyResolver{ @@ -151,11 +152,25 @@ func TestPreflightManagedGitCredentialsSkipsWhenExecutorModePolicy(t *testing.T) } } +func TestPreflightManagedGitCredentialsSkipsGitHubPolicyForPluginOnlyTask(t *testing.T) { + repo := newMockRepository() + seedPreflightTaskRepository(repo, "task-1", "repo-1", &models.Repository{ + ID: "repo-1", SourceType: sourceTypeLocal, Provider: "bitbucket", + ProviderHost: "https://bitbucket.example", RemoteURL: "https://bitbucket.example/acme/widgets.git", + }) + exec := newPreflightTestExecutor(t, repo) + exec.SetTaskGitCredentialPolicyResolver(fakeTaskGitCredentialPolicyResolver{err: errors.New("GitHub policy unavailable")}) + + if err := exec.PreflightManagedGitCredentials(context.Background(), "workspace-1", "task-1", "", ""); err != nil { + t.Fatalf("PreflightManagedGitCredentials() error = %v, want plugin-only task to bypass GitHub policy", err) + } +} + func TestPrepareSessionSkipsManagedIdentityForRemoteProfileGitHubToken(t *testing.T) { repo := newMockRepository() seedPreflightTaskRepository(repo, "task-1", "repo-1", &models.Repository{ - ID: "repo-1", SourceType: sourceTypeLocal, Provider: "acme-forge", - RemoteURL: "https://forge.example/acme/widgets.git", + ID: "repo-1", SourceType: sourceTypeLocal, Provider: gitHubProviderID, + RemoteURL: "https://github.com/acme/widgets.git", }) repo.executors["exec-ssh"] = &models.Executor{ID: "exec-ssh", Type: models.ExecutorTypeSSH} repo.executorProfiles["profile-token"] = &models.ExecutorProfile{ diff --git a/apps/backend/internal/orchestrator/executor/executor_credentials_test.go b/apps/backend/internal/orchestrator/executor/executor_credentials_test.go index e098c2c594..9d0a782318 100644 --- a/apps/backend/internal/orchestrator/executor/executor_credentials_test.go +++ b/apps/backend/internal/orchestrator/executor/executor_credentials_test.go @@ -504,6 +504,145 @@ func TestManagedGitCredentialProviderTreatsEmptyProviderAsGitHub(t *testing.T) { } } +func TestManagedGitCredentialProviderScopesExplicitTokensByHost(t *testing.T) { + public := &models.Repository{ + ID: "public", Provider: gitHubProviderID, ProviderHost: "https://github.com", + RemoteURL: "https://github.com/acme/public.git", + } + enterprise := &models.Repository{ + ID: "enterprise", Provider: gitHubProviderID, ProviderHost: "https://ghe.example", + RemoteURL: "https://ghe.example/acme/enterprise.git", + } + env := map[string]string{envGHToken: "public-token"} + if got := managedGitCredentialProviderForProfile(public, true, env, nil); got != "" { + t.Fatalf("public provider = %q, want explicit public token to bypass broker", got) + } + if got := managedGitCredentialProviderForProfile(enterprise, true, env, nil); got != gitHubProviderID { + t.Fatalf("enterprise provider = %q, want broker despite public token", got) + } +} + +func TestExplicitGitHubTokenClearsOnlyMatchingContributionRepositories(t *testing.T) { + publicDestination := &models.ContributionDestination{} + enterpriseDestination := &models.ContributionDestination{} + public := &repoInfo{ + RepositoryID: "public", Repository: &models.Repository{ + ID: "public", Provider: gitHubProviderID, ProviderHost: "https://github.com", + RemoteURL: "https://github.com/acme/public.git", + }, ContributionDestination: publicDestination, + } + enterprise := &repoInfo{ + RepositoryID: "enterprise", Repository: &models.Repository{ + ID: "enterprise", Provider: gitHubProviderID, ProviderHost: "https://ghe.example", + RemoteURL: "https://ghe.example/acme/enterprise.git", + }, ContributionDestination: enterpriseDestination, + } + req := &LaunchAgentRequest{ + RepositoryID: "public", + Env: map[string]string{"GH_ENTERPRISE_TOKEN": "enterprise-token"}, + Repositories: []RepoSpec{ + {RepositoryID: "public", ContributionDestination: publicDestination}, + {RepositoryID: "enterprise", ContributionDestination: enterpriseDestination}, + }, + ContributionDestination: publicDestination, + } + + clearManagedContributionDestinationsForExplicitTokens(req, []*repoInfo{public, enterprise}, nil) + if public.ContributionDestination == nil || req.ContributionDestination == nil || req.Repositories[0].ContributionDestination == nil { + t.Fatal("public contribution destination was cleared by an enterprise token") + } + if enterprise.ContributionDestination != nil || req.Repositories[1].ContributionDestination != nil { + t.Fatal("enterprise contribution destination remained with an explicit enterprise token") + } +} + +func TestConfigureManagedGitCredentialsKeepsMixedHostRepositoriesScoped(t *testing.T) { + issuer := &fakeGitHubCredentialLeaseIssuer{lease: gitcredentials.Lease{Token: "enterprise-lease"}} + exec := newTestExecutor(t, &mockAgentManager{}, newMockRepository()) + exec.SetGitHubCredentialBroker(issuer, "https://kandev.example/api/github/credentials/resolve") + exec.SetTaskGitCredentialPolicyResolver(fakeTaskGitCredentialPolicyResolver{ + policy: TaskGitCredentialPolicy{Mode: taskGitCredentialsModeManaged}, + }) + req := &LaunchAgentRequest{ + TaskID: "task-1", WorkspaceID: "workspace-1", SessionID: "session-1", + ExecutorType: string(models.ExecutorTypeRemoteDocker), + Env: map[string]string{envGHToken: "public-token"}, + } + infos := []*repoInfo{ + {RepositoryID: "public", Repository: &models.Repository{ + ID: "public", Provider: gitHubProviderID, ProviderHost: "https://github.com", + RemoteURL: "https://github.com/acme/public.git", + }}, + {RepositoryID: "enterprise", Repository: &models.Repository{ + ID: "enterprise", Provider: gitHubProviderID, ProviderHost: "https://ghe.example", + RemoteURL: "https://ghe.example/acme/enterprise.git", + }}, + } + if err := exec.configureGitCredentialBrokerForRepositories(context.Background(), req, infos); err != nil { + t.Fatalf("configureGitCredentialBrokerForRepositories() error = %v", err) + } + if issuer.calls != 1 || len(issuer.requests) != 1 || issuer.requests[0].Host != "ghe.example" { + t.Fatalf("credential leases = %#v, want one enterprise lease", issuer.requests) + } + if got := req.Env[githubauth.CredentialHostEnv]; got != "ghe.example" { + t.Fatalf("credential host = %q, want enterprise host", got) + } +} + +func TestConfigureGitCredentialsSkipsGitHubPolicyForPluginOnlyLaunch(t *testing.T) { + issuer := &fakeGitHubCredentialLeaseIssuer{lease: gitcredentials.Lease{Token: "plugin-lease"}} + exec := newTestExecutor(t, &mockAgentManager{}, newMockRepository()) + exec.SetGitHubCredentialBroker(issuer, "https://kandev.example/api/github/credentials/resolve") + exec.SetTaskGitCredentialPolicyResolver(fakeTaskGitCredentialPolicyResolver{err: errors.New("GitHub policy unavailable")}) + req := &LaunchAgentRequest{ + TaskID: "task-1", WorkspaceID: "workspace-1", SessionID: "session-1", + ExecutorType: string(models.ExecutorTypeRemoteDocker), Env: map[string]string{}, + } + info := &repoInfo{RepositoryID: "repo-1", Repository: &models.Repository{ + ID: "repo-1", Provider: "bitbucket", ProviderHost: "https://bitbucket.example", + RemoteURL: "https://bitbucket.example/acme/widgets.git", + }} + if err := exec.configureGitCredentialBrokerForRepositories(context.Background(), req, []*repoInfo{info}); err != nil { + t.Fatalf("plugin-only credential configuration failed: %v", err) + } + if issuer.calls != 1 || issuer.request.ProviderID != "bitbucket" { + t.Fatalf("plugin lease = %#v, calls=%d, want one bitbucket lease", issuer.request, issuer.calls) + } +} + +func TestConfigureExecutorGitCredentialsIssuesPluginLeaseBeforeHostBridge(t *testing.T) { + setupHostGHExecutable(t) + issuer := &fakeGitHubCredentialLeaseIssuer{lease: gitcredentials.Lease{Token: "opaque-lease"}} + exec := newTestExecutor(t, &mockAgentManager{}, newMockRepository()) + exec.SetGitHubCredentialBroker(issuer, "https://kandev.example/api/github/credentials/resolve") + exec.SetTaskGitCredentialPolicyResolver(fakeTaskGitCredentialPolicyResolver{ + policy: TaskGitCredentialPolicy{Mode: taskGitCredentialsModeExecutor}, + }) + exec.SetHostGitHubCredentialProbe(func(context.Context, string, string, map[string]string) error { return nil }) + req := &LaunchAgentRequest{ + TaskID: "task-1", WorkspaceID: "workspace-1", SessionID: "session-1", + ExecutorType: string(models.ExecutorTypeWorktree), Env: map[string]string{}, + } + infos := []*repoInfo{ + {RepositoryID: "github", Repository: &models.Repository{ + ID: "github", Provider: gitHubProviderID, RemoteURL: "https://github.com/acme/widgets.git", + }}, + {RepositoryID: "plugin", Repository: &models.Repository{ + ID: "plugin", Provider: "bitbucket", ProviderHost: "https://bitbucket.example", + RemoteURL: "https://bitbucket.example/acme/tools.git", + }}, + } + if err := exec.configureGitCredentialBrokerForRepositories(context.Background(), req, infos); err != nil { + t.Fatalf("executor credential configuration failed: %v", err) + } + if issuer.calls != 1 || issuer.request.ProviderID != "bitbucket" { + t.Fatalf("plugin lease = %#v, calls=%d, want one plugin lease", issuer.request, issuer.calls) + } + if !hasHostGitHubHelper(req.Env) { + t.Fatalf("host bridge helper missing after plugin lease configuration: %#v", req.Env) + } +} + func TestConfigureGitCredentialBrokerSkipsLocalRepositoryWithoutProvider(t *testing.T) { issuer := &fakeGitHubCredentialLeaseIssuer{lease: gitcredentials.Lease{Token: "opaque-lease"}} exec := newTestExecutor(t, &mockAgentManager{}, newMockRepository()) @@ -647,7 +786,7 @@ func TestConfigureGitHubCredentialBrokerDisablesDestinationForExplicitProfileTok exec.SetGitHubCredentialBroker(issuer, "https://kandev.example/api/github/credentials/resolve") destination := testCredentialDestination() req := &LaunchAgentRequest{ - TaskID: "task-1", WorkspaceID: "workspace-1", SessionID: "session-1", + TaskID: "task-1", WorkspaceID: "workspace-1", SessionID: "session-1", RepositoryID: "repo-1", ExecutorType: string(models.ExecutorTypeRemoteDocker), Env: map[string]string{envGHToken: "profile-token"}, ContributionDestination: &destination, @@ -695,6 +834,9 @@ func TestConfigureGitHubCredentialBrokerSkipsExecutorInheritedPolicy(t *testing. exec.SetTaskGitCredentialPolicyResolver(fakeTaskGitCredentialPolicyResolver{ policy: TaskGitCredentialPolicy{Mode: "executor"}, }) + exec.SetHostGitHubCredentialProbe(func(context.Context, string, string, map[string]string) error { + return errors.New("host gh unavailable") + }) req := &LaunchAgentRequest{WorkspaceID: "workspace-1", Env: map[string]string{ githubauth.CredentialBrokerURLEnv: "http://broker.example/resolve", githubauth.CredentialHelperPathEnv: "/opt/kandev/agentctl", diff --git a/apps/backend/internal/orchestrator/executor/executor_execute.go b/apps/backend/internal/orchestrator/executor/executor_execute.go index edd9dddc3a..7a0996dd1c 100644 --- a/apps/backend/internal/orchestrator/executor/executor_execute.go +++ b/apps/backend/internal/orchestrator/executor/executor_execute.go @@ -17,6 +17,7 @@ import ( "github.com/kandev/kandev/internal/agent/runtime/routingerr" "github.com/kandev/kandev/internal/common/constants" "github.com/kandev/kandev/internal/common/subproc" + "github.com/kandev/kandev/internal/gitconfigenv" mcpprofile "github.com/kandev/kandev/internal/mcp/profile" "github.com/kandev/kandev/internal/orchestrator/sessionstate" "github.com/kandev/kandev/internal/repoclone" @@ -1375,6 +1376,12 @@ func (e *Executor) LaunchPreparedSession(ctx context.Context, task *v1.Task, ses if session.TaskID != task.ID { return nil, fmt.Errorf("session does not belong to task") } + if strings.TrimSpace(agentProfileID) == "" { + agentProfileID = strings.TrimSpace(session.ExecutionProfileID) + if agentProfileID == "" { + agentProfileID = strings.TrimSpace(session.AgentProfileID) + } + } if err := e.admitWorktreeRecovery(ctx, task.ID); err != nil { return nil, err } @@ -1507,6 +1514,15 @@ func (e *Executor) LaunchPreparedSession(ctx context.Context, task *v1.Task, ses mergeEnv(req, opts.RouteOverride.Env) } } + profileEnvVars, profileResolved := e.resolveHostGitHubBridgeProfileEnv(ctx, agentProfileID, execCfg.ProfileEnvVars) + if err := e.configureGitCredentialBrokerForRepositoriesWithProfileEnvAndBridge( + ctx, req, allRepos, profileEnvVars, profileResolved, + ); err != nil { + return nil, err + } + if err := e.applyGitCredentialSnapshot(ctx, req, session); err != nil { + return nil, err + } // Apply McpMode from options (takes precedence over session metadata check in buildLaunchAgentRequest) if opts.McpMode != "" { @@ -1586,7 +1602,7 @@ func (e *Executor) LaunchPreparedSession(ctx context.Context, task *v1.Task, ses return nil, fmt.Errorf("check runtime inventory for session %q: %w", sessionID, hasRunningErr) } if hasRunning { - result, existingErr := e.startAgentOnExistingWorkspace(launchCtx, task, session, prompt, startAgent, opts.McpMode, opts.Env, opts.TurnID) + result, existingErr := e.startAgentOnExistingWorkspaceWithRequest(launchCtx, task, session, prompt, startAgent, opts.McpMode, req, opts.TurnID) if !errors.Is(existingErr, ErrStaleExecution) && !errors.Is(existingErr, ErrAgentCommandMissing) { if releaseErr := releaseSelectedWorktreeRecovery(ctx, &recoveryAdmission); releaseErr != nil { return nil, errors.Join(existingErr, fmt.Errorf("release worktree recovery admission: %w", releaseErr)) @@ -2019,13 +2035,6 @@ func (e *Executor) buildLaunchAgentRequest(ctx context.Context, task *v1.Task, s if err != nil { return nil, execConfig, err } - if err := e.configureGitCredentialBrokerForRepositories(ctx, req, allRepos); err != nil { - return nil, execConfig, err - } - if err := e.applyGitCredentialSnapshot(ctx, req, session); err != nil { - return nil, execConfig, err - } - // Multi-repo: when more than one repository is associated with the task, // populate req.Repositories so the lifecycle preparer creates one worktree // per repo. The legacy single-repo top-level fields above stay populated @@ -2281,6 +2290,25 @@ func dockerLocalCloneSource(repositoryPath string) string { // reconciled DB drift; that's now structurally impossible because executors_running // is owned by the lifecycle manager and writes are atomic with executionStore.Add. func (e *Executor) startAgentOnExistingWorkspace(ctx context.Context, task *v1.Task, session *models.TaskSession, prompt string, startAgent bool, mcpMode string, env map[string]string, turnIDs ...string) (*TaskExecution, error) { + request := &LaunchAgentRequest{ + TaskID: task.ID, + WorkspaceID: task.WorkspaceID, + SessionID: session.ID, + Env: cloneStringMap(env), + } + return e.startAgentOnExistingWorkspaceWithRequest(ctx, task, session, prompt, startAgent, mcpMode, request, turnIDs...) +} + +func (e *Executor) startAgentOnExistingWorkspaceWithRequest( + ctx context.Context, + task *v1.Task, + session *models.TaskSession, + prompt string, + startAgent bool, + mcpMode string, + request *LaunchAgentRequest, + turnIDs ...string, +) (*TaskExecution, error) { executionID, err := e.agentManager.GetExecutionIDForSession(ctx, session.ID) if err != nil || executionID == "" { // No execution exists in memory (e.g. backend restarted since workspace was prepared). @@ -2318,7 +2346,7 @@ func (e *Executor) startAgentOnExistingWorkspace(ctx context.Context, task *v1.T } } e.bindPromptTurnID(ctx, session.ID, executionID, turnIDs) - if err := e.configureExistingWorkspace(ctx, task, session, executionID, mcpMode, env); err != nil { + if err := e.configureExistingWorkspace(ctx, task, session, executionID, mcpMode, request); err != nil { return nil, err } @@ -2370,17 +2398,34 @@ func (e *Executor) configureExistingWorkspace( task *v1.Task, session *models.TaskSession, executionID, mcpMode string, - env map[string]string, + request *LaunchAgentRequest, ) error { - credentialReq := &LaunchAgentRequest{WorkspaceID: task.WorkspaceID, Env: cloneStringMap(env)} + if request == nil { + return errors.New("existing workspace launch request is required") + } + credentialReq := &LaunchAgentRequest{ + TaskID: task.ID, + WorkspaceID: task.WorkspaceID, + SessionID: session.ID, + TaskEnvironmentID: request.TaskEnvironmentID, + ExecutorType: request.ExecutorType, + Env: cloneStringMap(request.Env), + } e.injectGitLabWorkspaceCredentials(ctx, credentialReq) - if len(credentialReq.Env) > 0 { - if err := e.agentManager.SetExecutionEnv(ctx, executionID, credentialReq.Env); err != nil { - e.logger.Warn("failed to set execution env for existing workspace", - zap.String("session_id", session.ID), - zap.String("agent_execution_id", executionID), - zap.Error(err)) + if provider, ok := e.agentManager.(executorProfileEnvironmentProvider); ok { + profileEnv, err := provider.ExecutorProfileEnvForSession(ctx, session.ID, credentialReq.TaskEnvironmentID) + if err != nil { + return fmt.Errorf("resolve executor profile environment for existing workspace: %w", err) } + credentialReq.Env, err = mergeExistingWorkspaceProfileEnv(profileEnv, credentialReq.Env) + if err != nil { + return fmt.Errorf("compose executor profile environment for existing workspace: %w", err) + } + } + // A failed delivery leaves the running workspace on its previous credential + // snapshot, so do not continue to agent start and report refreshed state. + if err := e.agentManager.SetExecutionEnv(ctx, executionID, credentialReq.Env); err != nil { + return fmt.Errorf("set execution env for existing workspace: %w", err) } // If config MCP mode is needed, reconfigure the MCP server before starting the agent. @@ -2403,6 +2448,21 @@ func (e *Executor) configureExistingWorkspace( return nil } +type executorProfileEnvironmentProvider interface { + ExecutorProfileEnvForSession(context.Context, string, string) (map[string]string, error) +} + +func mergeExistingWorkspaceProfileEnv(profileEnv, requestEnv map[string]string) (map[string]string, error) { + if len(profileEnv) == 0 { + return cloneStringMap(requestEnv), nil + } + merged, err := gitconfigenv.Merge(profileEnv, requestEnv) + if err != nil { + return nil, err + } + return merged, nil +} + func (e *Executor) bindPromptTurnID(ctx context.Context, sessionID, executionID string, turnIDs []string) { if len(turnIDs) == 0 || turnIDs[0] == "" { return diff --git a/apps/backend/internal/orchestrator/executor/executor_host_gh_bridge.go b/apps/backend/internal/orchestrator/executor/executor_host_gh_bridge.go new file mode 100644 index 0000000000..4df7badb32 --- /dev/null +++ b/apps/backend/internal/orchestrator/executor/executor_host_gh_bridge.go @@ -0,0 +1,482 @@ +package executor + +import ( + "context" + "fmt" + "io" + "os" + "os/exec" + "path/filepath" + "sort" + "strings" + "sync" + "time" + + "github.com/kandev/kandev/internal/common/subproc" + "github.com/kandev/kandev/internal/gitconfigenv" + "github.com/kandev/kandev/internal/gitcredentials" + "github.com/kandev/kandev/internal/githubauth" + "github.com/kandev/kandev/internal/task/models" +) + +const ( + hostGitHubCredentialProbeTimeout = 5 * time.Second + hostGitHubCredentialProbeWorkers = 4 +) + +// hostGitHubCredentialProbe checks whether the host gh account can provide a +// token for one validated GitHub host. It must never return the token itself. +type hostGitHubCredentialProbe func(context.Context, string, string, map[string]string) error + +type hostGitHubProbeResult struct { + index int + available bool +} + +// SetHostGitHubCredentialProbe replaces the host CLI availability check. The +// seam keeps policy tests deterministic while production uses the real gh CLI. +func (e *Executor) SetHostGitHubCredentialProbe(probe hostGitHubCredentialProbe) { + if probe == nil { + e.hostGitHubCredentialProbe = runHostGitHubCredentialProbe + return + } + e.hostGitHubCredentialProbe = probe +} + +func runHostGitHubCredentialProbe(ctx context.Context, executable, host string, env map[string]string) error { + cmd := exec.CommandContext(ctx, executable, "auth", "token", "--hostname", host) + cmd.Env = hostGitHubCommandEnvironment(env) + cmd.Stdout = io.Discard + cmd.Stderr = io.Discard + return subproc.RunGH(ctx, cmd) +} + +func (e *Executor) configureHostGitHubCredentialBridgeWithProfileEnv( + ctx context.Context, + req *LaunchAgentRequest, + infos []*repoInfo, + profileEnvVars []models.ProfileEnvVar, +) error { + if req == nil || !hostGitHubCredentialBridgeEligible(req.ExecutorType) { + return nil + } + if err := ctx.Err(); err != nil { + return err + } + hosts := resolveHostGitHubHosts(infos) + if len(hosts) == 0 { + return nil + } + executable, err := resolveHostGitHubExecutable() + if err != nil { + return nil + } + probe := e.hostGitHubCredentialProbe + if probe == nil { + probe = runHostGitHubCredentialProbe + } + probeEnv, probeOK := e.hostGitHubProbeEnvironment(ctx, req.Env, profileEnvVars) + if !probeOK { + if err := ctx.Err(); err != nil { + return err + } + return nil + } + available, err := e.probeHostGitHubHosts(ctx, req, profileEnvVars, probe, executable, hosts, probeEnv) + if err != nil { + return err + } + if len(available) == 0 { + return nil + } + return appendHostGitHubCredentialHelpers(req, executable, available) +} + +func (e *Executor) probeHostGitHubHosts( + ctx context.Context, + req *LaunchAgentRequest, + profileEnvVars []models.ProfileEnvVar, + probe hostGitHubCredentialProbe, + executable string, + hosts []string, + probeEnv map[string]string, +) ([]string, error) { + if err := ctx.Err(); err != nil { + return nil, err + } + candidates := hostGitHubProbeCandidates(req, profileEnvVars, hosts) + if len(candidates) == 0 { + return nil, nil + } + + workerCount := hostGitHubCredentialProbeWorkers + if workerCount > len(candidates) { + workerCount = len(candidates) + } + probeCtx, cancel := context.WithCancel(ctx) + defer cancel() + jobs := make(chan int, len(candidates)) + results := make(chan hostGitHubProbeResult, len(candidates)) + for _, index := range candidates { + jobs <- index + } + close(jobs) + + var workers sync.WaitGroup + workers.Add(workerCount) + for range workerCount { + go runHostGitHubProbeWorker(probeCtx, &workers, jobs, results, probe, executable, hosts, probeEnv) + } + + availableByIndex := make([]bool, len(hosts)) + completed := 0 + for completed < len(candidates) { + select { + case result := <-results: + availableByIndex[result.index] = result.available + completed++ + case <-ctx.Done(): + cancel() + workers.Wait() + return nil, ctx.Err() + } + } + cancel() + workers.Wait() + + available := make([]string, 0, len(candidates)) + for _, index := range candidates { + if availableByIndex[index] { + available = append(available, hosts[index]) + } + } + return available, nil +} + +func hostGitHubProbeCandidates(req *LaunchAgentRequest, profileEnvVars []models.ProfileEnvVar, hosts []string) []int { + candidates := make([]int, 0, len(hosts)) + for index, host := range hosts { + if hasExplicitGitHubToken(req.Env, host) || hasExplicitGitHubTokenInProfile(profileEnvVars, host) { + continue + } + candidates = append(candidates, index) + } + return candidates +} + +func runHostGitHubProbeWorker( + ctx context.Context, + workers *sync.WaitGroup, + jobs <-chan int, + results chan<- hostGitHubProbeResult, + probe hostGitHubCredentialProbe, + executable string, + hosts []string, + probeEnv map[string]string, +) { + defer workers.Done() + for index := range jobs { + hostCtx, hostCancel := context.WithTimeout(ctx, hostGitHubCredentialProbeTimeout) + probeErr := probe(hostCtx, executable, hosts[index], probeEnv) + timedOut := hostCtx.Err() != nil + hostCancel() + select { + case results <- hostGitHubProbeResult{index: index, available: probeErr == nil && !timedOut}: + case <-ctx.Done(): + return + } + } +} + +// resolveHostGitHubBridgeProfileEnv returns profile definitions in the order +// used by the final resolver: the agent profile is the weaker source and the +// executor profile is the authoritative source. A failed agent-profile lookup +// disables only the optional host bridge, because probing the backend account +// could select a different GitHub login from the one the child will use. +func (e *Executor) resolveHostGitHubBridgeProfileEnv( + ctx context.Context, + agentProfileID string, + executorProfileEnv []models.ProfileEnvVar, +) ([]models.ProfileEnvVar, bool) { + if strings.TrimSpace(agentProfileID) == "" { + return append([]models.ProfileEnvVar(nil), executorProfileEnv...), true + } + if e == nil || e.agentManager == nil { + return append([]models.ProfileEnvVar(nil), executorProfileEnv...), false + } + agentProfile, err := e.agentManager.ResolveAgentProfile(ctx, agentProfileID) + if err != nil || agentProfile == nil { + return append([]models.ProfileEnvVar(nil), executorProfileEnv...), false + } + profileEnv := make([]models.ProfileEnvVar, 0, len(agentProfile.EnvVars)+len(executorProfileEnv)) + profileEnv = append(profileEnv, agentProfile.EnvVars...) + profileEnv = append(profileEnv, executorProfileEnv...) + return profileEnv, true +} + +func (e *Executor) hostGitHubProbeEnvironment( + ctx context.Context, + requestEnv map[string]string, + profileEnvVars []models.ProfileEnvVar, +) (map[string]string, bool) { + if len(profileEnvVars) == 0 { + return requestEnv, true + } + selected := winningGitHubCredentialSelection(profileEnvVars) + overrides, ok := e.resolveHostGitHubProbeOverrides(ctx, requestEnv, selected) + if !ok { + return nil, false + } + if len(overrides) == 0 { + return requestEnv, true + } + probeEnv := cloneStringMap(requestEnv) + if probeEnv == nil { + probeEnv = make(map[string]string, len(overrides)) + } + for key, value := range overrides { + probeEnv[key] = value + } + return probeEnv, true +} + +func winningGitHubCredentialSelection(profileEnvVars []models.ProfileEnvVar) map[string]models.ProfileEnvVar { + selected := make(map[string]models.ProfileEnvVar, len(profileEnvVars)) + for _, envVar := range profileEnvVars { + if !isGitHubCredentialSelectionEnv(envVar.Key) || + (envVar.SecretID == "" && strings.TrimSpace(envVar.Value) == "") { + continue + } + // The effective profile is ordered from weaker agent values to stronger + // executor values. Resolve the winning definition before revealing a + // secret so a stale weaker reference cannot disable a valid override. + selected[envVar.Key] = envVar + } + return selected +} + +func (e *Executor) resolveHostGitHubProbeOverrides( + ctx context.Context, + requestEnv map[string]string, + selected map[string]models.ProfileEnvVar, +) (map[string]string, bool) { + overrides := make(map[string]string, len(selected)) + keys := make([]string, 0, len(selected)) + for key := range selected { + keys = append(keys, key) + } + sort.Strings(keys) + for _, key := range keys { + envVar := selected[key] + // Request values are the effective managed source. Do not reveal a + // profile-backed directory that cannot affect the selected account. + if nonEmptyEnvValue(requestEnv, envVar.Key) { + continue + } + if envVar.SecretID == "" { + if envVar.Value != "" { + overrides[envVar.Key] = envVar.Value + } + continue + } + if e == nil || e.secretStore == nil { + return nil, false + } + value, err := e.revealGlobalSecret(ctx, envVar.SecretID) + if err != nil { + return nil, false + } + overrides[envVar.Key] = value + } + return overrides, true +} + +func isGitHubCredentialSelectionEnv(key string) bool { + switch key { + case "HOME", "GH_CONFIG_DIR", "XDG_CONFIG_HOME": + return true + default: + return false + } +} + +func hasExplicitGitHubTokenInProfile(profileEnvVars []models.ProfileEnvVar, host string) bool { + for _, envVar := range profileEnvVars { + if envVar.SecretID == "" && strings.TrimSpace(envVar.Value) == "" { + continue + } + switch envVar.Key { + case envGHToken, envGitHubToken: + if strings.EqualFold(host, defaultGitHubHost) { + return true + } + case "GH_ENTERPRISE_TOKEN", "GITHUB_ENTERPRISE_TOKEN": + if !strings.EqualFold(host, defaultGitHubHost) { + return true + } + } + } + return false +} + +func hostGitHubCredentialBridgeEligible(executorType string) bool { + switch models.ExecutorType(executorType) { + case models.ExecutorTypeLocal, models.ExecutorTypeWorktree: + return true + default: + return false + } +} + +func resolveHostGitHubHosts(infos []*repoInfo) []string { + hosts := make([]string, 0, len(infos)) + seen := make(map[string]struct{}, len(infos)) + for _, info := range infos { + if info == nil || info.Repository == nil || !hostGitHubRepository(info.Repository) { + continue + } + identity, err := gitcredentials.ResolveRepositoryIdentity(gitcredentials.RepositoryIdentityInput{ + RepositoryID: info.RepositoryID, + Provider: info.Repository.Provider, + ProviderHost: info.Repository.ProviderHost, + ProviderOwner: info.Repository.ProviderOwner, + ProviderName: info.Repository.ProviderName, + RemoteURL: info.Repository.RemoteURL, + }) + if err != nil || !hostGitHubIdentityAllowed(info.Repository, identity.Host) { + continue + } + host := strings.TrimSpace(identity.Host) + key := strings.ToLower(host) + if host == "" { + continue + } + if _, found := seen[key]; found { + continue + } + seen[key] = struct{}{} + hosts = append(hosts, host) + } + return hosts +} + +func hostGitHubRepository(repository *models.Repository) bool { + if repository == nil { + return false + } + provider := strings.ToLower(strings.TrimSpace(repository.Provider)) + if provider == gitHubProviderID { + return true + } + // Empty provider rows retain the public GitHub compatibility fallback for + // managed remote repositories, but a local checkout needs explicit metadata. + return provider == "" && repository.SourceType != sourceTypeLocal +} + +func hostGitHubIdentityAllowed(repository *models.Repository, host string) bool { + if strings.EqualFold(strings.TrimSpace(repository.Provider), gitHubProviderID) { + return true + } + return strings.EqualFold(host, defaultGitHubHost) +} + +func hasExplicitGitHubToken(env map[string]string, host string) bool { + if strings.EqualFold(host, defaultGitHubHost) { + return nonEmptyEnvValue(env, envGHToken) || nonEmptyEnvValue(env, envGitHubToken) + } + return nonEmptyEnvValue(env, "GH_ENTERPRISE_TOKEN") || + nonEmptyEnvValue(env, "GITHUB_ENTERPRISE_TOKEN") +} + +func nonEmptyEnvValue(env map[string]string, key string) bool { + return strings.TrimSpace(env[key]) != "" +} + +func resolveHostGitHubExecutable() (string, error) { + path, err := exec.LookPath("gh") + if err != nil { + return "", err + } + if filepath.IsAbs(path) { + return filepath.Clean(path), nil + } + return filepath.Abs(path) +} + +func appendHostGitHubCredentialHelpers(req *LaunchAgentRequest, executable string, hosts []string) error { + overlay := make(map[string]string, len(hosts)*2) + helper := hostGitHubCredentialHelper(executable) + for _, host := range hosts { + appendGitConfig(overlay, "credential.https://"+host+".helper", helper) + } + merged, err := gitconfigenv.Merge(req.Env, overlay) + if err != nil { + return fmt.Errorf("compose host GitHub credential helper: %w", err) + } + req.Env = merged + return nil +} + +func removeHostGitHubCredentialHelpers(req *LaunchAgentRequest) error { + if req == nil || req.Env == nil { + return nil + } + filtered, err := gitconfigenv.Filter(req.Env, func(index int, entries []gitconfigenv.Entry) bool { + return !isHostGitHubCredentialHelperEntry(entries[index]) + }) + if err != nil { + return fmt.Errorf("remove host GitHub credential helper: %w", err) + } + req.Env = filtered + return nil +} + +func isHostGitHubCredentialHelperEntry(entry gitconfigenv.Entry) bool { + return githubauth.IsHostGitHubCredentialHelperEntry(entry.Key, entry.Value) +} + +func hostGitHubCredentialHelper(executable string) string { + return "!f() { : " + githubauth.HostGitHubCredentialHelperMarker + "; " + shellQuote(executable) + " auth git-credential \"$@\"; }; f" +} + +func isHostGitHubCredentialHelper(value string) bool { + return githubauth.IsHostGitHubCredentialHelper(value) +} + +func shellQuote(value string) string { + return "'" + strings.ReplaceAll(value, "'", "'\"'\"'") + "'" +} + +func hostGitHubCommandEnvironment(overrides map[string]string) []string { + envMap := make(map[string]string, len(overrides)+4) + for _, entry := range os.Environ() { + key, value, found := strings.Cut(entry, "=") + if found && isHostGitHubCommandEnvironmentKey(key) { + envMap[key] = value + } + } + for key, value := range overrides { + if isHostGitHubCommandEnvironmentKey(key) { + envMap[key] = value + } + } + keys := make([]string, 0, len(envMap)) + for key := range envMap { + keys = append(keys, key) + } + sort.Strings(keys) + env := make([]string, 0, len(keys)) + for _, key := range keys { + env = append(env, key+"="+envMap[key]) + } + return env +} + +func isHostGitHubCommandEnvironmentKey(key string) bool { + switch key { + case "PATH", "HOME", "GH_CONFIG_DIR", "XDG_CONFIG_HOME": + return true + default: + return false + } +} diff --git a/apps/backend/internal/orchestrator/executor/executor_host_gh_bridge_profile_test.go b/apps/backend/internal/orchestrator/executor/executor_host_gh_bridge_profile_test.go new file mode 100644 index 0000000000..3d39ea793c --- /dev/null +++ b/apps/backend/internal/orchestrator/executor/executor_host_gh_bridge_profile_test.go @@ -0,0 +1,273 @@ +package executor + +import ( + "context" + "errors" + "path/filepath" + "testing" + "time" + + "github.com/kandev/kandev/internal/task/models" + v1 "github.com/kandev/kandev/pkg/api/v1" +) + +func TestExecutorHostGHBridgeCleanupPreservesUserOwnedGHHelpers(t *testing.T) { + ghPath := setupHostGHExecutable(t) + userHelper := "!'" + filepath.Join(t.TempDir(), "custom gh") + "' auth git-credential" + otherHostHelper := "!'" + filepath.Join(t.TempDir(), "other gh") + "' auth git-credential" + oldGenerated := hostGitHubCredentialHelper("/old/generated/gh") + req := executorHostGHBridgeRequest(indexedGitConfigTestEnvironment( + gitConfigTestEntry{key: "credential.https://github.com.helper", value: userHelper}, + gitConfigTestEntry{key: "credential.https://git.example.helper", value: otherHostHelper}, + gitConfigTestEntry{key: "credential.https://github.com.helper", value: oldGenerated}, + )) + executor := newTestExecutor(t, &mockAgentManager{}, newMockRepository()) + executor.SetTaskGitCredentialPolicyResolver(fakeTaskGitCredentialPolicyResolver{ + policy: TaskGitCredentialPolicy{Mode: taskGitCredentialsModeExecutor}, + }) + executor.SetHostGitHubCredentialProbe(func(context.Context, string, string, map[string]string) error { + return nil + }) + + if err := executor.configureGitCredentialBrokerForRepositories(context.Background(), req, []*repoInfo{executorHostGHBridgeRepository("repo-1", "github.com")}); err != nil { + t.Fatalf("configureGitCredentialBrokerForRepositories() error = %v", err) + } + entries := gitConfigEntriesFromEnvironment(t, req.Env) + if !containsGitConfigEntry(entries, "credential.https://github.com.helper", userHelper) { + t.Fatalf("user GitHub helper was removed: %#v", entries) + } + if !containsGitConfigEntry(entries, "credential.https://git.example.helper", otherHostHelper) { + t.Fatalf("other-host helper was removed: %#v", entries) + } + if !containsGitConfigEntry(entries, "credential.https://github.com.helper", hostGitHubCredentialHelper(ghPath)) { + t.Fatalf("replacement generated helper missing: %#v", entries) + } + if containsGitConfigEntry(entries, "credential.https://github.com.helper", oldGenerated) { + t.Fatalf("obsolete generated helper remained: %#v", entries) + } +} + +func TestExecutorHostGHBridgeCleanupPreservesUserHelperWhenCLIUnavailable(t *testing.T) { + t.Setenv("PATH", t.TempDir()) + userHelper := "!'" + filepath.Join(t.TempDir(), "custom gh") + "' auth git-credential" + req := executorHostGHBridgeRequest(indexedGitConfigTestEnvironment( + gitConfigTestEntry{key: "credential.https://github.com.helper", value: userHelper}, + )) + executor := newTestExecutor(t, &mockAgentManager{}, newMockRepository()) + executor.SetTaskGitCredentialPolicyResolver(fakeTaskGitCredentialPolicyResolver{ + policy: TaskGitCredentialPolicy{Mode: taskGitCredentialsModeExecutor}, + }) + if err := executor.configureGitCredentialBrokerForRepositories(context.Background(), req, []*repoInfo{executorHostGHBridgeRepository("repo-1", "github.com")}); err != nil { + t.Fatalf("configureGitCredentialBrokerForRepositories() error = %v", err) + } + entries := gitConfigEntriesFromEnvironment(t, req.Env) + if !containsGitConfigEntry(entries, "credential.https://github.com.helper", userHelper) { + t.Fatalf("user helper was removed while replacement CLI was unavailable: %#v", entries) + } +} + +func TestExecutorHostGHBridgeProbeUsesProfileCredentialDirectories(t *testing.T) { + setupHostGHExecutable(t) + cases := []struct { + name string + parentKey string + parentValue string + profileKey string + profileValue string + }{ + {name: "HOME", parentKey: "HOME", parentValue: "/backend/home", profileKey: "HOME", profileValue: "/profile/home"}, + {name: "GH_CONFIG_DIR", parentKey: "GH_CONFIG_DIR", parentValue: "/backend/gh", profileKey: "GH_CONFIG_DIR", profileValue: "/profile/gh"}, + } + for _, test := range cases { + t.Run(test.name, func(t *testing.T) { + t.Setenv(test.parentKey, test.parentValue) + executor := newTestExecutor(t, &mockAgentManager{}, newMockRepository()) + executor.SetTaskGitCredentialPolicyResolver(fakeTaskGitCredentialPolicyResolver{ + policy: TaskGitCredentialPolicy{Mode: taskGitCredentialsModeExecutor}, + }) + var probedEnv map[string]string + executor.SetHostGitHubCredentialProbe(func(_ context.Context, _ string, _ string, env map[string]string) error { + probedEnv = cloneStringMap(env) + if env[test.profileKey] != test.profileValue { + return errors.New("selected profile credential store is unavailable") + } + return nil + }) + req := executorHostGHBridgeRequest(nil) + err := executor.configureGitCredentialBrokerForRepositoriesWithProfileEnv( + context.Background(), req, + []*repoInfo{executorHostGHBridgeRepository("repo-1", "github.com")}, + []models.ProfileEnvVar{{Key: test.profileKey, Value: test.profileValue}}, + ) + if err != nil { + t.Fatalf("configureGitCredentialBrokerForRepositoriesWithProfileEnv() error = %v", err) + } + if probedEnv[test.profileKey] != test.profileValue { + t.Fatalf("probe %s = %q, want profile value %q; env=%#v", test.profileKey, probedEnv[test.profileKey], test.profileValue, probedEnv) + } + if !hasHostGitHubHelper(req.Env) { + t.Fatalf("profile credential store did not activate host bridge: %#v", req.Env) + } + }) + } +} + +func TestExecutorHostGHBridgeProbeRevealsOnlyWinningProfileDirectory(t *testing.T) { + executor := newTestExecutor(t, &mockAgentManager{}, newMockRepository()) + executor.secretStore = &mockSecretStore{secrets: map[string]string{ + "executor-home": "/executor/home", + }} + probeEnv, ok := executor.hostGitHubProbeEnvironment(context.Background(), nil, []models.ProfileEnvVar{ + {Key: "HOME", SecretID: "missing-agent-home"}, + {Key: "HOME", SecretID: "executor-home"}, + }) + if !ok { + t.Fatal("hostGitHubProbeEnvironment() failed while resolving the winning profile directory") + } + if got := probeEnv["HOME"]; got != "/executor/home" { + t.Fatalf("probe HOME = %q, want winning executor-profile directory", got) + } +} + +func TestExecutorHostGHBridgeResolvesEffectiveAgentAndExecutorProfiles(t *testing.T) { + setupHostGHExecutable(t) + manager := &mockAgentManager{ + resolveAgentProfileFunc: func(_ context.Context, profileID string) (*AgentProfileInfo, error) { + if profileID != "agent-profile" { + t.Fatalf("resolved profile ID = %q, want agent-profile", profileID) + } + return &AgentProfileInfo{ + EnvVars: []models.ProfileEnvVar{{Key: "HOME", Value: "/agent/home"}}, + }, nil + }, + } + executor := newTestExecutor(t, manager, newMockRepository()) + executor.SetTaskGitCredentialPolicyResolver(fakeTaskGitCredentialPolicyResolver{ + policy: TaskGitCredentialPolicy{Mode: taskGitCredentialsModeExecutor}, + }) + var probedEnv map[string]string + executor.SetHostGitHubCredentialProbe(func(_ context.Context, _ string, _ string, env map[string]string) error { + probedEnv = cloneStringMap(env) + return nil + }) + + profileEnv, resolved := executor.resolveHostGitHubBridgeProfileEnv( + context.Background(), "agent-profile", + []models.ProfileEnvVar{{Key: "GH_CONFIG_DIR", Value: "/executor/gh"}}, + ) + if !resolved { + t.Fatal("profile environment was not resolved") + } + req := executorHostGHBridgeRequest(map[string]string{"HOME": "/request/home"}) + if err := executor.configureGitCredentialBrokerForRepositoriesWithProfileEnvAndBridge( + context.Background(), req, + []*repoInfo{executorHostGHBridgeRepository("repo-1", "github.com")}, + profileEnv, resolved, + ); err != nil { + t.Fatalf("configureGitCredentialBrokerForRepositoriesWithProfileEnvAndBridge() error = %v", err) + } + if probedEnv["HOME"] != "/request/home" { + t.Fatalf("probe HOME = %q, want request value", probedEnv["HOME"]) + } + if probedEnv["GH_CONFIG_DIR"] != "/executor/gh" { + t.Fatalf("probe GH_CONFIG_DIR = %q, want executor-profile value", probedEnv["GH_CONFIG_DIR"]) + } + if !hasHostGitHubHelper(req.Env) { + t.Fatalf("effective profile stores did not activate host bridge: %#v", req.Env) + } +} + +func TestLaunchPreparedSessionProbesEffectiveProfileCredentialStore(t *testing.T) { + setupHostGHExecutable(t) + cases := []struct { + name string + agentEnv []models.ProfileEnvVar + executorEnv []models.ProfileEnvVar + selectionKey string + selectionPath string + }{ + { + name: "agent profile HOME", + agentEnv: []models.ProfileEnvVar{{Key: "HOME", Value: "/profile/home"}}, + selectionKey: "HOME", + selectionPath: "/profile/home", + }, + { + name: "executor profile GH_CONFIG_DIR", + executorEnv: []models.ProfileEnvVar{{Key: "GH_CONFIG_DIR", Value: "/profile/gh"}}, + selectionKey: "GH_CONFIG_DIR", + selectionPath: "/profile/gh", + }, + } + for _, test := range cases { + t.Run(test.name, func(t *testing.T) { + t.Setenv(test.selectionKey, "/backend/credential-store") + repo := newMockRepository() + const taskID = "task-effective-profile-store" + const sessionID = "session-effective-profile-store" + repo.repositories["repo-1"] = &models.Repository{ + ID: "repo-1", + Name: "widgets", + SourceType: sourceTypeLocal, + LocalPath: t.TempDir(), + Provider: gitHubProviderID, + ProviderOwner: "acme", + ProviderName: "widgets", + RemoteURL: "https://github.com/acme/widgets.git", + } + repo.taskRepositories["task-repo-1"] = &models.TaskRepository{ + ID: "task-repo-1", TaskID: taskID, RepositoryID: "repo-1", Position: 0, + } + repo.executors["executor-1"] = &models.Executor{ + ID: "executor-1", Type: models.ExecutorTypeWorktree, Status: models.ExecutorStatusActive, + } + repo.executorProfiles["executor-profile"] = &models.ExecutorProfile{ + ID: "executor-profile", ExecutorID: "executor-1", EnvVars: test.executorEnv, + } + repo.sessions[sessionID] = &models.TaskSession{ + ID: sessionID, TaskID: taskID, AgentProfileID: "agent-profile", + ExecutorID: "executor-1", ExecutorProfileID: "executor-profile", + State: models.TaskSessionStateCreated, StartedAt: time.Now(), UpdatedAt: time.Now(), + } + + var probedEnv map[string]string + manager := &mockAgentManager{ + resolveAgentProfileFunc: func(_ context.Context, profileID string) (*AgentProfileInfo, error) { + if profileID != "agent-profile" { + t.Fatalf("resolved profile ID = %q, want agent-profile", profileID) + } + return &AgentProfileInfo{ProfileID: profileID, EnvVars: test.agentEnv}, nil + }, + launchAgentFunc: func(_ context.Context, req *LaunchAgentRequest) (*LaunchAgentResponse, error) { + if !hasHostGitHubHelper(req.Env) { + t.Fatalf("production launch request has no host helper: %#v", req.Env) + } + return &LaunchAgentResponse{AgentExecutionID: "execution-1", Status: v1.AgentStatusStarting}, nil + }, + } + executor := newTestExecutor(t, manager, repo) + executor.SetTaskGitCredentialPolicyResolver(fakeTaskGitCredentialPolicyResolver{ + policy: TaskGitCredentialPolicy{Mode: taskGitCredentialsModeExecutor}, + }) + executor.SetHostGitHubCredentialProbe(func(_ context.Context, _ string, _ string, env map[string]string) error { + probedEnv = cloneStringMap(env) + if env[test.selectionKey] != test.selectionPath { + return errors.New("probe selected the wrong credential store") + } + return nil + }) + + _, err := executor.LaunchPreparedSession(context.Background(), &v1.Task{ + ID: taskID, WorkspaceID: "workspace-1", Title: "effective profile store", + }, sessionID, LaunchOptions{ + AgentProfileID: "agent-profile", ExecutorID: "executor-1", StartAgent: false, + }) + if err != nil { + t.Fatalf("LaunchPreparedSession() error = %v", err) + } + if probedEnv[test.selectionKey] != test.selectionPath { + t.Fatalf("probe %s = %q, want %q; env=%#v", test.selectionKey, probedEnv[test.selectionKey], test.selectionPath, probedEnv) + } + }) + } +} diff --git a/apps/backend/internal/orchestrator/executor/executor_host_gh_bridge_test.go b/apps/backend/internal/orchestrator/executor/executor_host_gh_bridge_test.go new file mode 100644 index 0000000000..3be846a837 --- /dev/null +++ b/apps/backend/internal/orchestrator/executor/executor_host_gh_bridge_test.go @@ -0,0 +1,821 @@ +package executor + +import ( + "context" + "errors" + "os" + osExec "os/exec" + "path/filepath" + "sort" + "strconv" + "strings" + "sync" + "testing" + "time" + + runtimeenv "github.com/kandev/kandev/internal/agent/runtime/environment" + "github.com/kandev/kandev/internal/gitconfigenv" + "github.com/kandev/kandev/internal/gitcredentials" + "github.com/kandev/kandev/internal/task/models" + v1 "github.com/kandev/kandev/pkg/api/v1" +) + +func TestExecutorHostGHBridgeCredentialFill(t *testing.T) { + ghPath := filepath.Join(t.TempDir(), "host tools", "gh") + if err := os.MkdirAll(filepath.Dir(ghPath), 0o700); err != nil { + t.Fatalf("create fake gh directory: %v", err) + } + const ghScript = `#!/bin/sh +if [ "$1" = "auth" ] && [ "$2" = "token" ]; then + exit 0 +fi +if [ "$1" = "auth" ] && [ "$2" = "git-credential" ]; then + cat >/dev/null + printf 'username=x-access-token\npassword=host-token\n' + exit 0 +fi +exit 2 +` + if err := os.WriteFile(ghPath, []byte(ghScript), 0o700); err != nil { + t.Fatalf("write fake gh: %v", err) + } + t.Setenv("PATH", filepath.Dir(ghPath)+string(os.PathListSeparator)+os.Getenv("PATH")) + + executor := newTestExecutor(t, &mockAgentManager{}, newMockRepository()) + executor.SetTaskGitCredentialPolicyResolver(fakeTaskGitCredentialPolicyResolver{ + policy: TaskGitCredentialPolicy{Mode: taskGitCredentialsModeExecutor}, + }) + req := &LaunchAgentRequest{ + TaskID: "task-1", + WorkspaceID: "workspace-1", + SessionID: "session-1", + ExecutorType: string(models.ExecutorTypeWorktree), + Env: map[string]string{ + "GIT_CONFIG_COUNT": "2", + "GIT_CONFIG_KEY_0": "notes.augment.mergeStrategy", + "GIT_CONFIG_VALUE_0": "union", + "GIT_CONFIG_KEY_1": "core.hooksPath", + "GIT_CONFIG_VALUE_1": "/Users/cfl12/.locstat/git/hooks", + }, + } + info := &repoInfo{ + RepositoryID: "repo-1", + Repository: &models.Repository{ + Provider: "github", + ProviderOwner: "acme", + ProviderName: "widgets", + RemoteURL: "https://github.com/acme/widgets.git", + }, + } + + if err := executor.configureGitCredentialBrokerForRepositories(context.Background(), req, []*repoInfo{info}); err != nil { + t.Fatalf("configureGitCredentialBrokerForRepositories() error = %v", err) + } + if got := req.Env["GIT_CONFIG_COUNT"]; got != "3" { + t.Fatalf("GIT_CONFIG_COUNT = %q, want 3", got) + } + if got := req.Env["GIT_CONFIG_KEY_0"]; got != "notes.augment.mergeStrategy" { + t.Fatalf("GIT_CONFIG_KEY_0 = %q, want inherited notes entry", got) + } + if got := req.Env["GIT_CONFIG_VALUE_0"]; got != "union" { + t.Fatalf("GIT_CONFIG_VALUE_0 = %q, want inherited notes value", got) + } + if got := req.Env["GIT_CONFIG_KEY_1"]; got != "core.hooksPath" { + t.Fatalf("GIT_CONFIG_KEY_1 = %q, want inherited hooks entry", got) + } + if got := req.Env["GIT_CONFIG_VALUE_1"]; got != "/Users/cfl12/.locstat/git/hooks" { + t.Fatalf("GIT_CONFIG_VALUE_1 = %q, want inherited hooks value", got) + } + + env := isolatedGitEnvironment(t, req.Env) + command := osExec.Command("git", "credential", "fill") + command.Env = env + command.Stdin = strings.NewReader("protocol=https\nhost=github.com\npath=acme/widgets\n\n") + output, err := command.CombinedOutput() + if err != nil { + t.Fatalf("git credential fill failed: %v\n%s", err, output) + } + if !strings.Contains(string(output), "username=x-access-token") || + !strings.Contains(string(output), "password=host-token") { + t.Fatalf("credential output = %q, want fake host helper credentials", output) + } +} + +func TestExecutorHostGHBridgePreflightComposesIndexedProfileBlock(t *testing.T) { + executor := newTestExecutor(t, &mockAgentManager{}, newMockRepository()) + req := executorHostGHBridgeRequest(indexedGitConfigTestEnvironment( + gitConfigTestEntry{key: "credential.https://github.com.helper", value: hostGitHubCredentialHelper("/host/gh")}, + )) + profileEnv := []models.ProfileEnvVar{ + {Key: "GIT_CONFIG_COUNT", Value: "2"}, + {Key: "GIT_CONFIG_KEY_0", Value: "notes.augment.mergeStrategy"}, + {Key: "GIT_CONFIG_VALUE_0", Value: "union"}, + {Key: "GIT_CONFIG_KEY_1", Value: "core.hooksPath"}, + {Key: "GIT_CONFIG_VALUE_1", Value: "/profile/hooks"}, + } + + if err := executor.resolveLaunchEnvironment(context.Background(), req, profileEnv, nil); err != nil { + t.Fatalf("resolveLaunchEnvironment() error = %v", err) + } + indexedDefinitions := 0 + for _, definition := range req.EnvironmentDefinitions { + if gitconfigenv.IsIndexedKey(definition.Key) { + indexedDefinitions++ + } + if definition.Origin != runtimeenv.OriginManagedRuntime && definition.Origin != runtimeenv.OriginExecutorProfile { + t.Fatalf("unexpected definition origin %q", definition.Origin) + } + } + if indexedDefinitions != 8 { + t.Fatalf("indexed environment definitions = %d, want request and profile indexed blocks", indexedDefinitions) + } +} + +func TestExecutorHostGHBridgePreservesIndexedConfig(t *testing.T) { + setupHostGHExecutable(t) + + tests := []struct { + name string + env map[string]string + wantCount string + wantErrorText string + wantEntries []gitConfigTestEntry + }{ + { + name: "absent block", + wantCount: "1", + wantEntries: []gitConfigTestEntry{ + {key: "credential.https://github.com.helper"}, + }, + }, + { + name: "empty block", + env: map[string]string{ + "GIT_CONFIG_COUNT": "0", + }, + wantCount: "1", + wantEntries: []gitConfigTestEntry{ + {key: "credential.https://github.com.helper"}, + }, + }, + { + name: "meaningful repeated helpers", + env: indexedGitConfigTestEnvironment( + gitConfigTestEntry{key: "credential.https://github.com.helper", value: "!first-helper"}, + gitConfigTestEntry{key: "credential.https://github.com.helper", value: "!second-helper"}, + ), + wantCount: "3", + wantEntries: []gitConfigTestEntry{ + {key: "credential.https://github.com.helper", value: "!first-helper"}, + {key: "credential.https://github.com.helper", value: "!second-helper"}, + {key: "credential.https://github.com.helper"}, + }, + }, + { + name: "malformed block", + env: map[string]string{ + "GIT_CONFIG_COUNT": "1", + "GIT_CONFIG_KEY_0": "core.hooksPath", + }, + wantErrorText: "entry 0", + }, + { + name: "combined count limit", + env: indexedGitConfigTestEnvironment(func() []gitConfigTestEntry { + entries := make([]gitConfigTestEntry, 0, 256) + for index := 0; index < 256; index++ { + entries = append(entries, gitConfigTestEntry{key: "test.key", value: "value"}) + } + return entries + }()...), + wantErrorText: "maximum is 256", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + executor := newTestExecutor(t, &mockAgentManager{}, newMockRepository()) + executor.SetTaskGitCredentialPolicyResolver(fakeTaskGitCredentialPolicyResolver{ + policy: TaskGitCredentialPolicy{Mode: taskGitCredentialsModeExecutor}, + }) + executor.SetHostGitHubCredentialProbe(func(context.Context, string, string, map[string]string) error { + return nil + }) + req := executorHostGHBridgeRequest(test.env) + info := executorHostGHBridgeRepository("repo-1", "github.com") + + err := executor.configureGitCredentialBrokerForRepositories(context.Background(), req, []*repoInfo{info}) + if test.wantErrorText != "" { + if err == nil || !strings.Contains(err.Error(), test.wantErrorText) { + t.Fatalf("error = %v, want %q", err, test.wantErrorText) + } + return + } + if err != nil { + t.Fatalf("configureGitCredentialBrokerForRepositories() error = %v", err) + } + if got := req.Env["GIT_CONFIG_COUNT"]; got != test.wantCount { + t.Fatalf("GIT_CONFIG_COUNT = %q, want %q", got, test.wantCount) + } + for index, want := range test.wantEntries { + if got := req.Env["GIT_CONFIG_KEY_"+strconv.Itoa(index)]; got != want.key { + t.Errorf("GIT_CONFIG_KEY_%d = %q, want %q", index, got, want.key) + } + gotValue := req.Env["GIT_CONFIG_VALUE_"+strconv.Itoa(index)] + if index == len(test.wantEntries)-1 && isHostGitHubCredentialHelper(gotValue) { + continue + } + if gotValue != want.value { + t.Errorf("GIT_CONFIG_VALUE_%d = %q, want %q", index, gotValue, want.value) + } + } + }) + } +} + +func TestExecutorHostGHBridgeCredentialPrecedence(t *testing.T) { + setupHostGHExecutable(t) + + tests := []struct { + name string + policy TaskGitCredentialPolicy + env map[string]string + repository *models.Repository + brokerError error + wantProbeCalls int + wantHelper bool + wantBrokerCalls int + wantExistingEntry string + }{ + { + name: "request public token", + policy: TaskGitCredentialPolicy{Mode: taskGitCredentialsModeExecutor}, + env: map[string]string{envGHToken: "request-token"}, + wantProbeCalls: 0, + }, + { + name: "request enterprise token", + policy: TaskGitCredentialPolicy{Mode: taskGitCredentialsModeExecutor}, + env: map[string]string{"GH_ENTERPRISE_TOKEN": "enterprise-token"}, + repository: &models.Repository{Provider: gitHubProviderID, ProviderHost: "https://ghe.example"}, + }, + { + name: "existing user helper remains earlier", + policy: TaskGitCredentialPolicy{Mode: taskGitCredentialsModeExecutor}, + env: indexedGitConfigTestEnvironment(gitConfigTestEntry{key: "credential.https://github.com.helper", value: "!user-helper"}), + wantProbeCalls: 1, + wantHelper: true, + wantExistingEntry: "!user-helper", + }, + { + name: "managed broker failure has no host fallback", + policy: TaskGitCredentialPolicy{Mode: taskGitCredentialsModeManaged}, + brokerError: errors.New("broker unavailable"), + wantProbeCalls: 0, + wantBrokerCalls: 1, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + executor := newTestExecutor(t, &mockAgentManager{}, newMockRepository()) + issuer := &fakeGitHubCredentialLeaseIssuer{lease: gitcredentials.Lease{Token: "opaque-lease"}, err: test.brokerError} + executor.SetGitHubCredentialBroker(issuer, "https://kandev.example/api/github/credentials/resolve") + executor.SetTaskGitCredentialPolicyResolver(fakeTaskGitCredentialPolicyResolver{policy: test.policy}) + probeCalls := 0 + executor.SetHostGitHubCredentialProbe(func(context.Context, string, string, map[string]string) error { + probeCalls++ + return nil + }) + repository := test.repository + if repository == nil { + repository = &models.Repository{ + Provider: gitHubProviderID, + ProviderHost: "https://github.com", + ProviderOwner: "acme", + ProviderName: "widgets", + RemoteURL: "https://github.com/acme/widgets.git", + } + } + req := executorHostGHBridgeRequest(test.env) + err := executor.configureGitCredentialBrokerForRepositories(context.Background(), req, []*repoInfo{{RepositoryID: "repo-1", Repository: repository}}) + if test.brokerError != nil { + if err == nil || !strings.Contains(err.Error(), "broker unavailable") { + t.Fatalf("error = %v, want broker failure", err) + } + } else if err != nil { + t.Fatalf("configureGitCredentialBrokerForRepositories() error = %v", err) + } + if probeCalls != test.wantProbeCalls { + t.Fatalf("probe calls = %d, want %d", probeCalls, test.wantProbeCalls) + } + if issuer.calls != test.wantBrokerCalls { + t.Fatalf("broker calls = %d, want %d", issuer.calls, test.wantBrokerCalls) + } + if hasHostGitHubHelper(req.Env) != test.wantHelper { + t.Fatalf("host helper present = %v, want %v; env=%#v", hasHostGitHubHelper(req.Env), test.wantHelper, req.Env) + } + if test.wantExistingEntry != "" && req.Env["GIT_CONFIG_VALUE_0"] != test.wantExistingEntry { + t.Fatalf("existing helper = %q, want %q", req.Env["GIT_CONFIG_VALUE_0"], test.wantExistingEntry) + } + }) + } +} + +func TestExecutorHostGHBridgeCleanupPreservesUserHelpersWhenReplacementUnavailable(t *testing.T) { + t.Setenv("PATH", t.TempDir()) + executor := newTestExecutor(t, &mockAgentManager{}, newMockRepository()) + executor.SetTaskGitCredentialPolicyResolver(fakeTaskGitCredentialPolicyResolver{ + policy: TaskGitCredentialPolicy{Mode: taskGitCredentialsModeExecutor}, + }) + userHelper := "!f() { '/opt/custom/gh' auth git-credential \"$@\"; }; f" + generatedHelper := hostGitHubCredentialHelper("/old/gh") + req := executorHostGHBridgeRequest(indexedGitConfigTestEnvironment( + gitConfigTestEntry{key: "credential.https://github.com.helper", value: userHelper}, + gitConfigTestEntry{key: "credential.https://ghe.example.helper", value: userHelper}, + gitConfigTestEntry{key: "credential.https://github.com.helper", value: generatedHelper}, + )) + + if err := executor.configureGitCredentialBrokerForRepositories( + context.Background(), req, []*repoInfo{executorHostGHBridgeRepository("repo-1", "github.com")}, + ); err != nil { + t.Fatalf("configureGitCredentialBrokerForRepositories() error = %v", err) + } + + entries := gitConfigEntriesFromEnvironment(t, req.Env) + if len(entries) != 2 { + t.Fatalf("configured Git helpers = %d, want two user entries: %#v", len(entries), entries) + } + if !containsGitConfigEntry(entries, "credential.https://github.com.helper", userHelper) || + !containsGitConfigEntry(entries, "credential.https://ghe.example.helper", userHelper) { + t.Fatalf("user-owned helpers were not preserved: %#v", entries) + } + if containsGitConfigEntry(entries, "credential.https://github.com.helper", generatedHelper) { + t.Fatalf("generated helper remained after cleanup: %#v", entries) + } +} + +func TestExplicitGitHubTokensAreScopedToTheirGitHubHost(t *testing.T) { + tests := []struct { + name string + host string + env map[string]string + want bool + }{ + {name: "public token for public host", host: "github.com", env: map[string]string{envGHToken: "public"}, want: true}, + {name: "enterprise token for enterprise host", host: "ghe.example", env: map[string]string{"GH_ENTERPRISE_TOKEN": "enterprise"}, want: true}, + {name: "public token does not bypass enterprise host", host: "ghe.example", env: map[string]string{envGHToken: "public"}}, + {name: "enterprise token does not bypass public host", host: "github.com", env: map[string]string{"GH_ENTERPRISE_TOKEN": "enterprise"}}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + if got := hasExplicitGitHubToken(test.env, test.host); got != test.want { + t.Fatalf("hasExplicitGitHubToken(%q) = %v, want %v", test.host, got, test.want) + } + }) + } +} + +func TestExecutorHostGHBridgeEligibility(t *testing.T) { + t.Run("local and worktree only", func(t *testing.T) { + setupHostGHExecutable(t) + for _, executorType := range []models.ExecutorType{models.ExecutorTypeLocal, models.ExecutorTypeWorktree} { + t.Run(string(executorType), func(t *testing.T) { + executor := newTestExecutor(t, &mockAgentManager{}, newMockRepository()) + executor.SetTaskGitCredentialPolicyResolver(fakeTaskGitCredentialPolicyResolver{policy: TaskGitCredentialPolicy{Mode: taskGitCredentialsModeExecutor}}) + calls := 0 + executor.SetHostGitHubCredentialProbe(func(context.Context, string, string, map[string]string) error { + calls++ + return nil + }) + req := executorHostGHBridgeRequest(nil) + req.ExecutorType = string(executorType) + if err := executor.configureGitCredentialBrokerForRepositories(context.Background(), req, []*repoInfo{{RepositoryID: "repo-1", Repository: &models.Repository{Provider: gitHubProviderID, RemoteURL: "https://github.com/acme/widgets.git"}}}); err != nil { + t.Fatalf("configureGitCredentialBrokerForRepositories() error = %v", err) + } + if calls != 1 { + t.Fatalf("probe calls = %d, want 1", calls) + } + }) + } + }) + + for _, executorType := range []models.ExecutorType{ + models.ExecutorTypeLocalDocker, + models.ExecutorTypeRemoteDocker, + models.ExecutorTypeSprites, + models.ExecutorTypeSSH, + models.ExecutorTypeKubernetes, + models.ExecutorTypeMockRemote, + "unknown", + } { + t.Run(string(executorType), func(t *testing.T) { + executor := newTestExecutor(t, &mockAgentManager{}, newMockRepository()) + executor.SetTaskGitCredentialPolicyResolver(fakeTaskGitCredentialPolicyResolver{policy: TaskGitCredentialPolicy{Mode: taskGitCredentialsModeExecutor}}) + calls := 0 + executor.SetHostGitHubCredentialProbe(func(context.Context, string, string, map[string]string) error { + calls++ + return nil + }) + req := executorHostGHBridgeRequest(nil) + req.ExecutorType = string(executorType) + if err := executor.configureGitCredentialBrokerForRepositories(context.Background(), req, []*repoInfo{{RepositoryID: "repo-1", Repository: &models.Repository{Provider: gitHubProviderID, RemoteURL: "https://github.com/acme/widgets.git"}}}); err != nil { + t.Fatalf("configureGitCredentialBrokerForRepositories() error = %v", err) + } + if calls != 0 { + t.Fatalf("probe calls = %d, want 0", calls) + } + }) + } + + t.Run("mixed repositories deduplicate trusted hosts", func(t *testing.T) { + setupHostGHExecutable(t) + executor := newTestExecutor(t, &mockAgentManager{}, newMockRepository()) + executor.SetTaskGitCredentialPolicyResolver(fakeTaskGitCredentialPolicyResolver{policy: TaskGitCredentialPolicy{Mode: taskGitCredentialsModeExecutor}}) + var hosts []string + var hostsMu sync.Mutex + executor.SetHostGitHubCredentialProbe(func(_ context.Context, _ string, host string, _ map[string]string) error { + hostsMu.Lock() + hosts = append(hosts, host) + hostsMu.Unlock() + return nil + }) + req := executorHostGHBridgeRequest(nil) + infos := []*repoInfo{ + {RepositoryID: "public-1", Repository: &models.Repository{Provider: gitHubProviderID, RemoteURL: "https://github.com/acme/one.git"}}, + {RepositoryID: "public-2", Repository: &models.Repository{Provider: gitHubProviderID, RemoteURL: "git@github.com:acme/two.git"}}, + {RepositoryID: "enterprise", Repository: &models.Repository{Provider: gitHubProviderID, ProviderHost: "https://ghe.example", RemoteURL: "https://ghe.example/acme/three.git"}}, + {RepositoryID: "gitlab", Repository: &models.Repository{Provider: gitLabProviderID, RemoteURL: "https://gitlab.example/acme/four.git"}}, + {RepositoryID: "local-ambiguous", Repository: &models.Repository{SourceType: sourceTypeLocal, RemoteURL: "https://github.com/acme/five.git"}}, + {RepositoryID: "invalid", Repository: &models.Repository{Provider: gitHubProviderID, ProviderOwner: "acme", ProviderName: "six", RemoteURL: "https://github.com/other/six.git"}}, + } + if err := executor.configureGitCredentialBrokerForRepositories(context.Background(), req, infos); err != nil { + t.Fatalf("configureGitCredentialBrokerForRepositories() error = %v", err) + } + hostsMu.Lock() + defer hostsMu.Unlock() + if strings.Join(hosts, ",") != "github.com,ghe.example" { + t.Fatalf("probed hosts = %v, want [github.com ghe.example]", hosts) + } + }) + + t.Run("unavailable CLI skips bridge", func(t *testing.T) { + t.Setenv("PATH", t.TempDir()) + executor := newTestExecutor(t, &mockAgentManager{}, newMockRepository()) + executor.SetTaskGitCredentialPolicyResolver(fakeTaskGitCredentialPolicyResolver{policy: TaskGitCredentialPolicy{Mode: taskGitCredentialsModeExecutor}}) + calls := 0 + executor.SetHostGitHubCredentialProbe(func(context.Context, string, string, map[string]string) error { + calls++ + return nil + }) + req := executorHostGHBridgeRequest(nil) + if err := executor.configureGitCredentialBrokerForRepositories(context.Background(), req, []*repoInfo{{RepositoryID: "repo-1", Repository: &models.Repository{Provider: gitHubProviderID, RemoteURL: "https://github.com/acme/widgets.git"}}}); err != nil { + t.Fatalf("configureGitCredentialBrokerForRepositories() error = %v", err) + } + if calls != 0 || hasHostGitHubHelper(req.Env) { + t.Fatalf("unavailable CLI still activated bridge: calls=%d env=%#v", calls, req.Env) + } + }) +} + +func TestExecutorHostGHBridgeProbeTimeoutAndCancellation(t *testing.T) { + setupHostGHExecutable(t) + baseInfo := []*repoInfo{executorHostGHBridgeRepository("repo-1", "github.com")} + + t.Run("probe timeout is optional", func(t *testing.T) { + executor := newTestExecutor(t, &mockAgentManager{}, newMockRepository()) + executor.SetTaskGitCredentialPolicyResolver(fakeTaskGitCredentialPolicyResolver{policy: TaskGitCredentialPolicy{Mode: taskGitCredentialsModeExecutor}}) + executor.SetHostGitHubCredentialProbe(func(ctx context.Context, _, _ string, _ map[string]string) error { + if _, ok := ctx.Deadline(); !ok { + t.Fatal("probe context has no deadline") + } + return context.DeadlineExceeded + }) + req := executorHostGHBridgeRequest(nil) + if err := executor.configureGitCredentialBrokerForRepositories(context.Background(), req, baseInfo); err != nil { + t.Fatalf("configureGitCredentialBrokerForRepositories() error = %v", err) + } + if hasHostGitHubHelper(req.Env) { + t.Fatalf("timed out probe activated bridge: %#v", req.Env) + } + }) + + t.Run("caller cancellation is returned", func(t *testing.T) { + executor := newTestExecutor(t, &mockAgentManager{}, newMockRepository()) + executor.SetTaskGitCredentialPolicyResolver(fakeTaskGitCredentialPolicyResolver{policy: TaskGitCredentialPolicy{Mode: taskGitCredentialsModeExecutor}}) + ctx, cancel := context.WithCancel(context.Background()) + executor.SetHostGitHubCredentialProbe(func(_ context.Context, _, _ string, _ map[string]string) error { + cancel() + return context.Canceled + }) + err := executor.configureGitCredentialBrokerForRepositories(ctx, executorHostGHBridgeRequest(nil), baseInfo) + if !errors.Is(err, context.Canceled) { + t.Fatalf("error = %v, want context cancellation", err) + } + }) +} + +func TestHostGitHubCommandEnvironmentUsesOnlyCredentialSelectionInputs(t *testing.T) { + t.Setenv("PATH", "/host/bin") + t.Setenv("HOME", "/host/home") + env := hostGitHubCommandEnvironment(map[string]string{ + "GH_CONFIG_DIR": "/profile/gh", + "XDG_CONFIG_HOME": "/profile/config", + "GH_TOKEN": "secret-token", + "DATABASE_URL": "postgres://secret", + }) + + values := make(map[string]string, len(env)) + for _, entry := range env { + key, value, ok := strings.Cut(entry, "=") + if !ok { + t.Fatalf("environment entry %q has no separator", entry) + } + values[key] = value + } + for key := range values { + if !isHostGitHubCommandEnvironmentKey(key) { + t.Fatalf("probe environment contains unrelated key %q", key) + } + } + for key, want := range map[string]string{ + "PATH": "/host/bin", + "HOME": "/host/home", + "GH_CONFIG_DIR": "/profile/gh", + "XDG_CONFIG_HOME": "/profile/config", + } { + if got := values[key]; got != want { + t.Errorf("probe environment %s = %q, want %q", key, got, want) + } + } + if _, ok := values["GH_TOKEN"]; ok { + t.Fatal("probe environment exposed GH_TOKEN") + } + if _, ok := values["DATABASE_URL"]; ok { + t.Fatal("probe environment exposed DATABASE_URL") + } +} + +func TestExecutorHostGHBridgeProbesHostsConcurrentlyAndPreservesOrder(t *testing.T) { + setupHostGHExecutable(t) + executor := newTestExecutor(t, &mockAgentManager{}, newMockRepository()) + executor.SetTaskGitCredentialPolicyResolver(fakeTaskGitCredentialPolicyResolver{ + policy: TaskGitCredentialPolicy{Mode: taskGitCredentialsModeExecutor}, + }) + hosts := []string{"github.com", "ghe-one.example", "ghe-two.example", "ghe-three.example"} + started := make(chan string, len(hosts)) + release := make(chan struct{}) + executor.SetHostGitHubCredentialProbe(func(_ context.Context, _, host string, _ map[string]string) error { + started <- host + <-release + return nil + }) + infos := make([]*repoInfo, 0, len(hosts)) + for index, host := range hosts { + infos = append(infos, executorHostGHBridgeRepository(strconv.Itoa(index), host)) + } + req := executorHostGHBridgeRequest(nil) + done := make(chan error, 1) + go func() { + done <- executor.configureGitCredentialBrokerForRepositories(context.Background(), req, infos) + }() + + for range hosts { + select { + case <-started: + case <-time.After(2 * time.Second): + close(release) + t.Fatal("host probes did not run concurrently") + } + } + close(release) + if err := <-done; err != nil { + t.Fatalf("configureGitCredentialBrokerForRepositories() error = %v", err) + } + + entries := gitConfigEntriesFromEnvironment(t, req.Env) + if len(entries) != len(hosts) { + t.Fatalf("configured Git helpers = %d, want %d: %#v", len(entries), len(hosts), entries) + } + for index, host := range hosts { + wantKey := "credential.https://" + host + ".helper" + if entries[index].key != wantKey || !isHostGitHubCredentialHelper(entries[index].value) { + t.Fatalf("helper %d = %#v, want host %q", index, entries[index], host) + } + } +} + +func TestExecutorHostGHBridgePreparedWorkspace(t *testing.T) { + var captured map[string]string + var gotSessionID, gotEnvironmentID string + manager := &mockAgentManager{ + executorProfileEnvFunc: func(_ context.Context, sessionID, taskEnvironmentID string) (map[string]string, error) { + gotSessionID = sessionID + gotEnvironmentID = taskEnvironmentID + return map[string]string{envGHToken: "profile-token"}, nil + }, + setExecutionEnvFunc: func(_ context.Context, _ string, env map[string]string) error { + captured = env + return nil + }, + } + executor := newTestExecutor(t, manager, newMockRepository()) + request := &LaunchAgentRequest{ + TaskID: "task-1", + WorkspaceID: "workspace-1", + SessionID: "session-1", + TaskEnvironmentID: "environment-1", + ExecutorType: string(models.ExecutorTypeWorktree), + Env: indexedGitConfigTestEnvironment( + gitConfigTestEntry{key: "notes.augment.mergeStrategy", value: "union"}, + gitConfigTestEntry{key: "credential.https://github.com.helper", value: hostGitHubCredentialHelper("/tmp/host tools/gh")}, + ), + } + session := &models.TaskSession{ID: "session-1", TaskID: "task-1"} + + err := executor.configureExistingWorkspace( + context.Background(), + &v1.Task{ID: "task-1", WorkspaceID: "workspace-1"}, + session, + "execution-1", + "", + request, + ) + if err != nil { + t.Fatalf("configureExistingWorkspace() error = %v", err) + } + if gotSessionID != session.ID || gotEnvironmentID != request.TaskEnvironmentID { + t.Fatalf("profile environment lookup = (%q, %q), want (%q, %q)", gotSessionID, gotEnvironmentID, session.ID, request.TaskEnvironmentID) + } + if captured[envGHToken] != "profile-token" { + t.Fatalf("prepared workspace token = %q, want current profile token", captured[envGHToken]) + } + if captured["GIT_CONFIG_COUNT"] != "2" || captured["GIT_CONFIG_KEY_0"] != "notes.augment.mergeStrategy" || + captured["GIT_CONFIG_VALUE_0"] != "union" || !isHostGitHubCredentialHelper(captured["GIT_CONFIG_VALUE_1"]) { + t.Fatalf("prepared workspace Git environment = %#v", captured) + } +} + +func TestExecutorHostGHBridgePreparedWorkspacePropagatesEnvironmentDeliveryFailure(t *testing.T) { + manager := &mockAgentManager{ + setExecutionEnvFunc: func(context.Context, string, map[string]string) error { + return errors.New("agentctl unavailable") + }, + } + executor := newTestExecutor(t, manager, newMockRepository()) + err := executor.configureExistingWorkspace( + context.Background(), + &v1.Task{ID: "task-1", WorkspaceID: "workspace-1"}, + &models.TaskSession{ID: "session-1", TaskID: "task-1"}, + "execution-1", + "", + &LaunchAgentRequest{WorkspaceID: "workspace-1", Env: map[string]string{"GH_TOKEN": "token"}}, + ) + if err == nil || !strings.Contains(err.Error(), "agentctl unavailable") { + t.Fatalf("configureExistingWorkspace() error = %v, want environment delivery failure", err) + } +} + +func TestExecutorHostGHBridgeRepeatedPreparationReplacesGeneratedEntry(t *testing.T) { + setupHostGHExecutable(t) + executor := newTestExecutor(t, &mockAgentManager{}, newMockRepository()) + executor.SetTaskGitCredentialPolicyResolver(fakeTaskGitCredentialPolicyResolver{ + policy: TaskGitCredentialPolicy{Mode: taskGitCredentialsModeExecutor}, + }) + probes := 0 + executor.SetHostGitHubCredentialProbe(func(context.Context, string, string, map[string]string) error { + probes++ + return nil + }) + repositories := []*repoInfo{executorHostGHBridgeRepository("repo-1", "github.com")} + request := executorHostGHBridgeRequest(nil) + if err := executor.configureGitCredentialBrokerForRepositories(context.Background(), request, repositories); err != nil { + t.Fatalf("first configuration: %v", err) + } + if err := executor.configureGitCredentialBrokerForRepositories(context.Background(), request, repositories); err != nil { + t.Fatalf("second configuration: %v", err) + } + if probes != 2 { + t.Fatalf("probe calls = %d, want one fresh probe per preparation", probes) + } + if got := request.Env["GIT_CONFIG_COUNT"]; got != "1" { + t.Fatalf("GIT_CONFIG_COUNT = %q, want one generated helper", got) + } + if !hasHostGitHubHelper(request.Env) { + t.Fatalf("repeated preparation removed the generated helper: %#v", request.Env) + } +} + +type gitConfigTestEntry struct { + key string + value string +} + +func indexedGitConfigTestEnvironment(entries ...gitConfigTestEntry) map[string]string { + env := map[string]string{"GIT_CONFIG_COUNT": strconv.Itoa(len(entries))} + for index, entry := range entries { + env["GIT_CONFIG_KEY_"+strconv.Itoa(index)] = entry.key + env["GIT_CONFIG_VALUE_"+strconv.Itoa(index)] = entry.value + } + return env +} + +func gitConfigEntriesFromEnvironment(t *testing.T, env map[string]string) []gitConfigTestEntry { + t.Helper() + count, err := strconv.Atoi(env["GIT_CONFIG_COUNT"]) + if err != nil { + t.Fatalf("GIT_CONFIG_COUNT = %q: %v", env["GIT_CONFIG_COUNT"], err) + } + entries := make([]gitConfigTestEntry, 0, count) + for index := 0; index < count; index++ { + entries = append(entries, gitConfigTestEntry{ + key: env["GIT_CONFIG_KEY_"+strconv.Itoa(index)], + value: env["GIT_CONFIG_VALUE_"+strconv.Itoa(index)], + }) + } + return entries +} + +func containsGitConfigEntry(entries []gitConfigTestEntry, key, value string) bool { + for _, entry := range entries { + if entry.key == key && entry.value == value { + return true + } + } + return false +} + +func executorHostGHBridgeRequest(env map[string]string) *LaunchAgentRequest { + return &LaunchAgentRequest{ + TaskID: "task-1", + WorkspaceID: "workspace-1", + SessionID: "session-1", + ExecutorType: string(models.ExecutorTypeWorktree), + Env: env, + } +} + +func executorHostGHBridgeRepository(id, host string) *repoInfo { + return &repoInfo{ + RepositoryID: id, + Repository: &models.Repository{ + Provider: gitHubProviderID, + ProviderOwner: "acme", + ProviderName: "widgets", + ProviderHost: "https://" + host, + RemoteURL: "https://" + host + "/acme/widgets.git", + }, + } +} + +func setupHostGHExecutable(t *testing.T) string { + t.Helper() + ghPath := filepath.Join(t.TempDir(), "host tools", "gh") + if err := os.MkdirAll(filepath.Dir(ghPath), 0o700); err != nil { + t.Fatalf("create fake gh directory: %v", err) + } + if err := os.WriteFile(ghPath, []byte("#!/bin/sh\nexit 0\n"), 0o700); err != nil { + t.Fatalf("write fake gh: %v", err) + } + t.Setenv("PATH", filepath.Dir(ghPath)+string(os.PathListSeparator)+os.Getenv("PATH")) + return ghPath +} + +func hasHostGitHubHelper(env map[string]string) bool { + count, _ := strconv.Atoi(env["GIT_CONFIG_COUNT"]) + for index := 0; index < count; index++ { + if isHostGitHubCredentialHelper(env["GIT_CONFIG_VALUE_"+strconv.Itoa(index)]) { + return true + } + } + return false +} + +func isolatedGitEnvironment(t *testing.T, overrides map[string]string) []string { + t.Helper() + envMap := make(map[string]string) + for _, entry := range os.Environ() { + key, value, ok := strings.Cut(entry, "=") + if !ok || strings.HasPrefix(key, "GIT_CONFIG_") { + continue + } + envMap[key] = value + } + envMap["HOME"] = filepath.Join(t.TempDir(), "home") + envMap["PATH"] = "/usr/bin:/bin" + envMap["GIT_CONFIG_NOSYSTEM"] = "1" + for key, value := range overrides { + envMap[key] = value + } + + keys := make([]string, 0, len(envMap)) + for key := range envMap { + keys = append(keys, key) + } + sort.Strings(keys) + env := make([]string, 0, len(keys)) + for _, key := range keys { + env = append(env, key+"="+envMap[key]) + } + return env +} diff --git a/apps/backend/internal/orchestrator/executor/executor_kubernetes_parity_test.go b/apps/backend/internal/orchestrator/executor/executor_kubernetes_parity_test.go index 13c0729967..2712217d47 100644 --- a/apps/backend/internal/orchestrator/executor/executor_kubernetes_parity_test.go +++ b/apps/backend/internal/orchestrator/executor/executor_kubernetes_parity_test.go @@ -36,8 +36,8 @@ func TestKubernetesCredentialBrokerRequiresHTTPS(t *testing.T) { func TestKubernetesProfileTokenSkipsManagedCredentialPreflight(t *testing.T) { repo := newMockRepository() seedPreflightTaskRepository(repo, "task-1", "repo-1", &models.Repository{ - ID: "repo-1", SourceType: sourceTypeLocal, Provider: "acme-forge", - RemoteURL: "https://forge.example/acme/widgets.git", + ID: "repo-1", SourceType: sourceTypeLocal, Provider: gitHubProviderID, + RemoteURL: "https://github.com/acme/widgets.git", }) repo.executors["exec-k8s"] = &models.Executor{ ID: "exec-k8s", Type: models.ExecutorTypeKubernetes, diff --git a/apps/backend/internal/orchestrator/executor/executor_mocks_test.go b/apps/backend/internal/orchestrator/executor/executor_mocks_test.go index 805a7ccb4e..23bc616775 100644 --- a/apps/backend/internal/orchestrator/executor/executor_mocks_test.go +++ b/apps/backend/internal/orchestrator/executor/executor_mocks_test.go @@ -29,6 +29,7 @@ type mockAgentManager struct { resolveAgentProfileFunc func(ctx context.Context, profileID string) (*AgentProfileInfo, error) setExecutionDescriptionFunc func(ctx context.Context, agentExecutionID string, description string) error setExecutionEnvFunc func(ctx context.Context, agentExecutionID string, env map[string]string) error + executorProfileEnvFunc func(ctx context.Context, sessionID, taskEnvironmentID string) (map[string]string, error) getExecutionIDForSessionFunc func(ctx context.Context, sessionID string) (string, error) listExecutionsForTaskFunc func(taskID string) []lifecycle.ExecutionReference isAgentCommandConfiguredFunc func(agentExecutionID string) bool @@ -78,6 +79,13 @@ func (m *mockAgentManager) SetExecutionEnv(ctx context.Context, executionID stri return nil } +func (m *mockAgentManager) ExecutorProfileEnvForSession(ctx context.Context, sessionID, taskEnvironmentID string) (map[string]string, error) { + if m.executorProfileEnvFunc != nil { + return m.executorProfileEnvFunc(ctx, sessionID, taskEnvironmentID) + } + return nil, nil +} + func (m *mockAgentManager) SetMcpMode(_ context.Context, _ string, _ string) error { return nil } diff --git a/apps/backend/internal/orchestrator/executor/executor_resume.go b/apps/backend/internal/orchestrator/executor/executor_resume.go index fe1891af3e..176eb91ee1 100644 --- a/apps/backend/internal/orchestrator/executor/executor_resume.go +++ b/apps/backend/internal/orchestrator/executor/executor_resume.go @@ -1316,8 +1316,11 @@ func (e *Executor) buildResumeRequestAtCredentialBoundaryWithOptions( if err := e.applyResumeWorkspaceFolders(ctx, task.ID, req); err != nil { return nil, "", execConfig, existingEnv, existingRunning, err } - if err := e.configureResumeGitHubCredentials( - ctx, req, session, allRepos, beforeCredentialLease, + profileEnvVars, profileResolved := e.resolveHostGitHubBridgeProfileEnv( + ctx, req.AgentProfileID, execConfig.ProfileEnvVars, + ) + if err := e.configureResumeGitHubCredentialsWithProfileEnvAndBridge( + ctx, req, session, allRepos, beforeCredentialLease, profileEnvVars, profileResolved, ); err != nil { return nil, "", execConfig, existingEnv, existingRunning, err } @@ -1523,13 +1526,29 @@ func (e *Executor) configureResumeGitHubCredentials( session *models.TaskSession, repositories []*repoInfo, beforeCredentialLease func() error, +) error { + return e.configureResumeGitHubCredentialsWithProfileEnvAndBridge( + ctx, req, session, repositories, beforeCredentialLease, nil, true, + ) +} + +func (e *Executor) configureResumeGitHubCredentialsWithProfileEnvAndBridge( + ctx context.Context, + req *LaunchAgentRequest, + session *models.TaskSession, + repositories []*repoInfo, + beforeCredentialLease func() error, + profileEnvVars []models.ProfileEnvVar, + hostBridgeProfileResolved bool, ) error { if beforeCredentialLease != nil { if err := beforeCredentialLease(); err != nil { return err } } - if err := e.configureGitCredentialBrokerForRepositories(ctx, req, repositories); err != nil { + if err := e.configureGitCredentialBrokerForRepositoriesWithProfileEnvAndBridge( + ctx, req, repositories, profileEnvVars, hostBridgeProfileResolved, + ); err != nil { return err } return e.applyGitCredentialSnapshot(ctx, req, session) diff --git a/docs/plans/executor-host-gh-bridge/plan.md b/docs/plans/executor-host-gh-bridge/plan.md new file mode 100644 index 0000000000..bd63fc23a6 --- /dev/null +++ b/docs/plans/executor-host-gh-bridge/plan.md @@ -0,0 +1,181 @@ +--- +created: 2026-09-12 +status: implemented +requirements: + - REQ-INTEGRATIONS-GITHUB-AUTHENTICATION-002 +system_design: + - ../../specs/integrations/system-design/github-authentication-02.md +legacy_specs: [] +--- + +# Fix plan: Executor host GitHub CLI bridge + +## Overview + +[Issue #3072](https://github.com/kdlbs/kandev/issues/3072) requests HTTPS Git access through the host GitHub CLI in executor mode. +The issue is assigned to `carlosflorencio`. +The integration system owns this repair because it owns task credential selection. +The requirement and technical design lacked the optional HTTPS bridge. +This package adds that behavior to the existing authentication capability. + +Implementation is complete. Tasks 01, 02, and 03 were executed in sequence in this session. +The production route, runtime propagation, regression coverage, and public guidance are included. +The review follow-up also closes source-aware indexed resolution, complete-environment +reconfiguration, generated-helper ownership, and profile credential-store selection. + +## Confirmed root cause + +Investigated repository head: `49df3794bc15d610988acc674637384ce8f1d4be`. +The canonical issue was open with no comments or image attachments during investigation. + +In `executor_credentials.go`, `configureGitCredentialBrokerForRepositories` returns immediately after executor-mode managed-credential cleanup. +It never registers a host GitHub CLI helper. +An HTTPS origin therefore requires a separately configured Git helper, even when the CLI can supply a token. +Transport selection does not register that helper. + +A temporary `TestRepro3072HostGHBridge` drove the real executor policy branch with a fake authenticated CLI. +A real `git credential fill` then failed with: + +```text +fatal: could not read Username for 'https://github.com': terminal prompts disabled +``` + +The same test composed a hypothetical helper through `gitconfigenv.Merge`. +Credential lookup then succeeded, both inherited entries survived, and another host still failed. +The probe used fake credentials, isolated Git configuration, and no network. +The temporary test was removed after evidence collection. + +## Scope + +### In scope + +- Optional host CLI credential availability probe and helper for Local/Worktree executor mode. +- Attached-host selection, explicit-token precedence, and managed-mode exclusion. +- Ordered indexed configuration across actual launch, resume, prepared-workspace, and child-process boundaries. +- Focused regression coverage and public explanation of the resulting behavior. + +### Out of scope + +- Global `gh auth setup-git`, user Git configuration writes, or agent prompt changes. +- SSH key creation, SSH/HTTPS remote rewriting, or additional repository permissions. +- New workspace automation bindings, broker changes, UI controls, migrations, or remote credential forwarding. +- Changes to sibling issues #3069, #3070, and #3071. + +## Technical approach + +The [requirement](../../specs/integrations/requirements/github-authentication.md) defines criteria 002.1 through 002.7. +The [design](../../specs/integrations/system-design/github-authentication-02.md#executor-host-cli-bridge) defines eligibility, lookup, composition, and runtime propagation. + +Task 01 adds the optional route beside `configureGitCredentialBrokerForRepositories`. +Use a small injectable runner and `subproc.RunGH` with discarded command output. +Use the existing repository identity resolver and `gitconfigenv.Merge`. +Existing Git helpers remain earlier in the chain, so the new helper fills an authentication gap. + +Task 02 closes the full-launch, resume, and prepared-workspace paths. +The strict resolver composes the indexed Git block by source after all managed definitions exist. +Lifecycle sends a composed runtime snapshot through agentctl's normal overlay mode; the indexed +merge recognizes an already-forwarded snapshot, while complete replacement remains an explicit API +for callers that own the full indexed block. Reconfiguration removes obsolete Kandev-generated +ordinary credential variables and host helpers before composing the next snapshot. +It tests profile environment selection before probing and the final agentctl child environment. +Managed helper failure must never invoke the host CLI. +Task 03 documents the completed behavior and synchronizes results. + +The existing task Git policy ADR already owns ordered composition and managed isolation. +No new ADR is needed for this implementation of executor-visible credential access. +The related clone-transport and task-terminal packages remain historical delivery records. +This package changes neither their completed scope nor their recorded test results. + +## Tests + +| Criteria | Planned test and file | +| --- | --- | +| 002.1, 002.2 | `TestExecutorHostGHBridgeCredentialFill` in `executor_host_gh_bridge_test.go`: real executor route, real Git, fake CLI. Initially fails because no helper exists. | +| 002.2 | `TestExecutorHostGHBridgePreservesIndexedConfig`: the supplied two-entry block, empty/absent block, duplicate helpers, malformed block, and combined count boundary. | +| 002.3 | `TestExecutorHostGHBridgeCredentialPrecedence`: request tokens, late profile tokens, enterprise tokens, existing user helper, and managed broker failure. | +| 002.4, 002.5 | `TestExecutorHostGHBridgeEligibility`: explicit local types, all remote types, mixed hosts/providers, unavailable CLI, timeout, and cancellation. | +| 002.6 | `TestExecutorHostGHBridgeLaunchResume` and `TestExecutorHostGHBridgePreparedWorkspace`: host availability changes, policy transition, and repeated preparation. | +| 002.2, 002.3, 002.7 | `TestCollectAgentEnvHostGHBridge` in agentctl `config_test.go`: inherited block plus task overlay, no global file changes, no secret output. | +| 002.3, 002.6 | `TestBuildEnvForExecutionHostGHBridge` in lifecycle `manager_launch_test.go`: late explicit token and environment replacement. | +| 002.2, 002.6 | `TestManagerConfigureReplacesOwnedHostHelperAndPreservesIndexedEnvironment` and `TestManagerConfigureWithEnvironmentReplacesCompleteIndexedBlock` in process `manager_temp_test.go`: overlay and complete replacement behavior for agent, tracker, and shell/process consumers. | +| 002.3, 002.5 | `TestExecutorHostGHBridgeProbeUsesProfileCredentialDirectories`, `TestExecutorHostGHBridgeResolvesEffectiveAgentAndExecutorProfiles`, and `TestLaunchPreparedSessionProbesEffectiveProfileCredentialStore`: profile HOME/GH_CONFIG_DIR selection through the production prepared-session entry point, request precedence, and no-token probe behavior. | + +All executor test paths above are under `apps/backend/internal/orchestrator/executor/`. +Task 02 also covers the real subprocess helper under a login shell that replaces `PATH`. + +## E2E tests + +The end-to-end boundary is Git credential resolution in the effective task environment. +Use real `git credential fill` through the actual policy and environment composition paths with an isolated fake CLI. +This checks the user-visible authentication outcome without an artificial browser test. +No Playwright project or rendered UI change is required. +A live GitHub fetch/push is not part of automated validation and is not claimed by the reproduction. + +## Work orders + +- [x] [Task 01: Add the optional host CLI route](task-01-host-cli-route.md) +- [x] [Task 02: Complete runtime propagation](task-02-runtime-propagation.md) +- [x] [Task 03: Document executor HTTPS access](task-03-document-https-access.md) + +## Verification results + +Diagnostic evidence: + +- `go test -tags fts5 ./internal/orchestrator/executor -run '^TestRepro3072HostGHBridge$' -count=1 -v`: passed. + The test asserted the current failure and the successful hypothetical helper composition. +- `go test ./internal/gitconfigenv -count=1`: passed. +- GitHub assignment: re-read issue assignees and confirmed `carlosflorencio`. + +Design validation: + +- `python3 scripts/list-docs.py validate`: passed (264 decisions, 818 specifications). +- `python3 scripts/lint-spec-files.test.py`: passed (36 tests). +- `python3 scripts/lint-spec-files.py --all`: passed. +- `git diff --check -- docs/specs docs/plans/executor-host-gh-bridge`: passed. +- Work-order requirement IDs, acceptance IDs, relative links, and existing source paths: checked. +- `git status --short -- docs/plans/executor-host-gh-bridge`: confirmed the new package is present. +Implementation validation: + +- The final affected-package run passed with `-count=1` for `internal/agent/runtime/agentctl`, + `internal/agent/runtime/environment`, `internal/agent/runtime/lifecycle`, + `internal/agentctl/server/api`, `internal/agentctl/server/config`, + `internal/agentctl/server/process`, `internal/backendapp`, `internal/gitconfigenv`, + and `internal/orchestrator/executor`. +- `make -C apps/backend lint` passed with zero issues on the final tree. +- `make -C apps/backend build` passed for the host and remote runtime binaries. +- The strict production-shaped test `TestBuildEnvForExecutionHostGHBridge_ComposesStrictProfileBlocks` + composes agent-profile, executor-profile, and managed Git blocks while preserving each indexed entry. +- `TestSwitchSessionForStepUsesReusableSessionExecutorProfileForCredentialAdmission` + verifies that an explicit remote-profile `gh_cli_env` credential keeps reusable-session admission + valid while managed identity preflight remains host-aware for launches that use the broker. +- `TestConfigureAndStartAgentSendsComposedRuntimeEnvironmentAsOverlay` verifies that lifecycle sends + one composed snapshot through the normal configure path; process-manager tests cover reconfiguration, + tracker, one-shot, shell/process propagation, user hooks/notes, complete-block removal, and removal + of obsolete marker-owned helpers. `TestComposeExecutionRuntimeEnvironmentRemovesObsoleteManagedCredentials` + also covers stale ordinary broker variables at the lifecycle boundary. +- `TestLaunchPreparedSessionProbesEffectiveProfileCredentialStore` exercises HOME and GH_CONFIG_DIR + mismatches through the production prepared-session entry point. The profile tests also cover both + profile sources, request precedence, unavailable replacement behavior, and no token output. +- `node --test scripts/validate-public-docs.test.mjs` passed (62 tests), and + `node scripts/validate-public-docs.mjs` validated 46 published pages. +- `python3 scripts/list-docs.py validate`, `python3 scripts/lint-spec-files.py --all`, + `git diff --check`, and changed-file `gofmt` checks passed. +- An earlier full backend-suite run passed all changed packages but returned nonzero for existing + process-tree probe tests in `internal/agentctl/server/process/probe` and office FTS migration + tests in `internal/office/repository/sqlite`; the final validation above is the current scoped run. + +## Risks + +- CLI token availability does not prove repository permission or network reachability. +- A backend service can have a different home directory or credential store from the user's terminal. +- Later profile variables and reused executor environments can change credential precedence. +- Shell startup can replace `PATH`. The helper must resolve the intended host executable safely. +- Existing user helpers can return stale credentials before the optional fallback. This repair preserves their precedence. + +## External references + +- [Git configuration protocol](https://git-scm.com/docs/git-config#Documentation/git-config.txt-GIT_CONFIG_COUNT): indexed entries and runtime configuration. +- [Git credential helper behavior](https://git-scm.com/docs/gitcredentials): helper ordering and URL contexts. +- [GitHub CLI token command](https://cli.github.com/manual/gh_auth_token): host-specific token retrieval. + +- [GitHub CLI environment precedence](https://cli.github.com/manual/gh_help_environment): explicit tokens and CLI configuration directory. diff --git a/docs/plans/executor-host-gh-bridge/task-01-host-cli-route.md b/docs/plans/executor-host-gh-bridge/task-01-host-cli-route.md new file mode 100644 index 0000000000..3ca05c46d2 --- /dev/null +++ b/docs/plans/executor-host-gh-bridge/task-01-host-cli-route.md @@ -0,0 +1,135 @@ +--- +id: "01-host-cli-route" +title: "Add the optional host CLI route" +status: complete +wave: 1 +depends_on: [] +plan: "plan.md" +requirements: + - REQ-INTEGRATIONS-GITHUB-AUTHENTICATION-002 +acceptance_criteria: + - AC-INTEGRATIONS-GITHUB-AUTHENTICATION-002.1 + - AC-INTEGRATIONS-GITHUB-AUTHENTICATION-002.2 + - AC-INTEGRATIONS-GITHUB-AUTHENTICATION-002.3 + - AC-INTEGRATIONS-GITHUB-AUTHENTICATION-002.4 + - AC-INTEGRATIONS-GITHUB-AUTHENTICATION-002.5 + - AC-INTEGRATIONS-GITHUB-AUTHENTICATION-002.6 + - AC-INTEGRATIONS-GITHUB-AUTHENTICATION-002.7 +system_design: + - ../../specs/integrations/system-design/github-authentication-02.md +--- + +# Task 01: Add the optional host CLI route + +## Summary + +Register an optional host GitHub CLI helper for eligible Local/Worktree tasks in executor mode. +Preserve existing Git configuration and explicit credential precedence. + +## In scope + +- Add a small injected availability runner in `executor_host_gh_bridge.go` and wire its default through the executor. +- Extend the executor-policy branch in `configureGitCredentialBrokerForRepositories` after managed cleanup. +- Resolve and deduplicate eligible GitHub hosts from attached repository metadata through the existing identity resolver. +- Apply explicit executor-type and token gates before the optional probe. +- Execute the bounded probe without a shell, discard token output, and preserve caller cancellation. +- Compose one optional helper per host with `gitconfigenv.Merge`, after existing helpers and without a reset. +- Validate the resulting indexed block, including the combined count limit. +- Use a safely quoted absolute host CLI executable so the helper survives a changed child `PATH`. +- Mark generated helpers with an unambiguous Kandev ownership marker and remove only marker-owned entries. +- Resolve agent and executor profile credential-store variables before probing, while preserving request precedence. + +## Out of scope + +Runtime entry-point changes belong to Task 02. Remote credentials, global Git writes, and broker redesign are excluded. + +## Acceptance + +1. The real-route credential-fill regression fails before implementation and passes after the correction. +2. Eligibility, token precedence, probe failures, and indexed configuration cases pass with no token output or global writes. +3. Repeated configuration preserves meaningful user entries and does not accumulate the generated helper. + +## TDD cases + +Add `executor_host_gh_bridge_test.go` with the tests named in the plan. +Use a fake `gh` binary and real `git credential fill` in an isolated temporary home. +Clear credential-related inherited variables in the fixture without printing them. + +Start with these exact inherited values: + +```text +GIT_CONFIG_COUNT=2 +GIT_CONFIG_KEY_0=notes.augment.mergeStrategy +GIT_CONFIG_VALUE_0=union +GIT_CONFIG_KEY_1=core.hooksPath +GIT_CONFIG_VALUE_1=/Users/cfl12/.locstat/git/hooks +``` + +After one helper is appended, assert count 3 and unchanged entries 0 and 1. +Also test inherited host entries outside `req.Env`, so final composition cannot silently duplicate the host block. +Include mixed repositories: repeated eligible host, explicit enterprise host, GitLab host, and ambiguous local metadata. +Assert zero probes for managed mode, request-token overrides, unknown executor types, and every remote executor type. +Distinguish probe timeout from caller cancellation. +Use fake tokens throughout the tests. + +## Verification + +From the repository root: + +```bash +(cd apps/backend && go test -tags fts5 ./internal/orchestrator/executor -run 'TestExecutorHostGHBridge|TestConfigureGitHubCredentialBroker|TestConfigureGitCredentialBroker' -count=1) +(cd apps/backend && go test ./internal/gitconfigenv -count=1) +git diff --check +``` + +## Files likely touched + +- `apps/backend/internal/orchestrator/executor/executor_host_gh_bridge.go` (new) +- `apps/backend/internal/orchestrator/executor/executor_host_gh_bridge_test.go` (new) +- `apps/backend/internal/orchestrator/executor/executor_credentials.go` +- `apps/backend/internal/orchestrator/executor/executor_credentials_test.go` +- `apps/backend/internal/orchestrator/executor/executor.go` + +Reuse `internal/gitconfigenv`, `internal/gitcredentials`, and `internal/common/subproc` without unrelated refactoring. + +## Dependencies + +None. + +## Risks + +Probe success is token availability only. Existing helper credentials retain their original precedence. +Malformed optional repository identities must not become trusted hosts. + +## Parallelism + +`sequential` + +## Inputs + +- Authentication requirement 002 and the design's “Executor host CLI bridge” section. +- `executor_credentials_test.go`: `TestConfigureGitHubCredentialBrokerSkipsExecutorInheritedPolicy`. +- `gitconfigenv/environment_test.go`: ordered composition and boundary-overlap tests. +- `repoclone/protocol.go`: the existing bounded host CLI command pattern. + +## Results + +- Added the optional host `gh` route for explicit Local and Worktree executor + requests, with validated host selection, token gates, bounded no-shell probes, + safe absolute helper commands, profile-aware credential-store selection, and managed-mode isolation. +- Preserved inherited indexed Git entries and meaningful repeated helpers while + removing only marker-owned stale bridge entries and enforcing the combined 256-entry limit. +- Added regressions for quoted user-owned `gh` helpers, unrelated hosts, unavailable + replacement CLIs, agent/executor profile directory mismatches, and request-level precedence. +- Added host-specific token precedence coverage for mixed public and enterprise hosts, + a bounded concurrent probe with deterministic helper order, and an allowlisted probe + environment that excludes token and unrelated process secrets. +- `TestLaunchPreparedSessionProbesEffectiveProfileCredentialStore` covers the HOME and + GH_CONFIG_DIR mismatch cases through the production prepared-session entry point. +- `TestSwitchSessionForStepUsesReusableSessionExecutorProfileForCredentialAdmission` passes with + an explicit remote-profile `gh_cli_env` credential, preserving reusable-session admission while + broker preflight remains scoped to launches that need managed identity validation. +- `(cd apps/backend && go test -tags fts5 ./internal/orchestrator/executor -run 'TestExecutorHostGHBridge|TestConfigureGitHubCredentialBroker|TestConfigureGitCredentialBroker' -count=1)` passed. +- `(cd apps/backend && go test ./internal/gitconfigenv -count=1)` passed. +- `go test ./internal/orchestrator/executor -count=1` and `go test ./internal/gitconfigenv -count=1` passed in the final affected-package run. +- `git diff --check` passed. diff --git a/docs/plans/executor-host-gh-bridge/task-02-runtime-propagation.md b/docs/plans/executor-host-gh-bridge/task-02-runtime-propagation.md new file mode 100644 index 0000000000..41db41e655 --- /dev/null +++ b/docs/plans/executor-host-gh-bridge/task-02-runtime-propagation.md @@ -0,0 +1,147 @@ +--- +id: "02-runtime-propagation" +title: "Complete runtime propagation" +status: complete +wave: 2 +depends_on: ["01-host-cli-route"] +plan: "plan.md" +requirements: + - REQ-INTEGRATIONS-GITHUB-AUTHENTICATION-002 +acceptance_criteria: + - AC-INTEGRATIONS-GITHUB-AUTHENTICATION-002.2 + - AC-INTEGRATIONS-GITHUB-AUTHENTICATION-002.3 + - AC-INTEGRATIONS-GITHUB-AUTHENTICATION-002.4 + - AC-INTEGRATIONS-GITHUB-AUTHENTICATION-002.5 + - AC-INTEGRATIONS-GITHUB-AUTHENTICATION-002.6 + - AC-INTEGRATIONS-GITHUB-AUTHENTICATION-002.7 +system_design: + - ../../specs/integrations/system-design/github-authentication-02.md +--- + +# Task 02: Complete runtime propagation + +## Summary + +Carry the bridge through each local launch path and the final child environment. +Prove that a reused executor or late profile token cannot defeat credential precedence. +Compose indexed Git definitions at the strict source-aware boundary and replace stale generated +entries at the agentctl configure/start boundary. + +## In scope + +- Cover full launch and `configureResumeGitHubCredentials` through the shared credential route. +- Extend prepared-workspace configuration with the resolved executor identity and repository set before agent start. +- Reuse preparation's resolved repositories instead of repeating materialization or remote reconciliation. +- Replace obsolete generated entries when the host, token, or policy changes on resume or prepared start. +- Preserve environment delivery errors instead of reporting successful bridge activation after failed delivery. +- Exercise lifecycle profile merges and `CollectAgentEnvWithError` using the supplied multi-entry host environment. +- Cover the same effective Git environment for agent processes and existing task shell/process consumers. +- Treat complete lifecycle snapshots and request-only configure calls as separate environment contracts. +- Remove obsolete marker-owned entries when a later preparation has no bridge, while preserving user entries. + +## Out of scope + +No new UI, snapshot schema, remote credential forwarding, or unrelated generic environment refactor. + +## Acceptance + +1. Launch, resume, and prepared start each pass with current eligibility and no duplicate indexed entries. +2. Late profile tokens remain the effective credential, and managed broker failure never invokes the host helper. +3. Real Git succeeds through the final environment without Git file changes, including a login shell that replaces `PATH`. + +## TDD cases + +Add the plan's launch/resume/prepared-workspace and child-environment tests before changing each path. +Use a spy host CLI to assert invocation counts and credential source without exposing real secrets. +Test logout between preparations, executor-to-managed transition, a newly explicit token, and failed environment delivery. +Test the bridge with a successful user helper, a failed managed helper, and a token supplied by a late profile merge. +For a failed explicit token, assert that no stored-login retry occurs. +Test `HOME` and `GH_CONFIG_DIR` overrides so probe/helper account selection stays consistent. + +For process-level evidence, use real Git in an isolated home with a fake CLI. +Pass inherited and task blocks through the actual agentctl environment collector. +Assert unchanged hooks and notes, one generated helper, and no changed Git configuration files. +Use an executable path with spaces and a login shell that replaces `PATH`. +Keep platform-specific shell tests explicitly gated, while portable policy and composition tests run everywhere. + +## Verification + +From the repository root: + +```bash +(cd apps/backend && go test -tags fts5 ./internal/orchestrator/executor -run 'TestExecutorHostGHBridge|TestConfigureGitHubCredentialBroker|TestConfigureGitCredentialBroker' -count=1) +(cd apps/backend && go test -tags fts5 ./internal/agent/runtime/lifecycle -run 'TestBuildEnvForExecutionHostGHBridge|TestMergeEnvFillMissing' -count=1) +(cd apps/backend && go test -tags fts5 ./internal/agentctl/server/config -run 'TestCollectAgentEnv.*(HostGHBridge|GitConfig)' -count=1) +(cd apps/backend && go test -tags fts5 ./internal/agentctl/server/process -run 'Test.*(HostGHBridge|GitEnv)' -count=1) +git diff --check +``` + +## Files likely touched + +- `apps/backend/internal/orchestrator/executor/executor_execute.go` +- `apps/backend/internal/orchestrator/executor/executor_resume.go` +- `apps/backend/internal/orchestrator/executor/executor_host_gh_bridge_test.go` +- `apps/backend/internal/agent/runtime/lifecycle/manager_launch_test.go` +- `apps/backend/internal/agent/runtime/lifecycle/profile_env.go` +- `apps/backend/internal/agentctl/server/config/config.go` +- `apps/backend/internal/agentctl/server/config/config_test.go` +- `apps/backend/internal/agentctl/server/process/workspace_git_env_test.go` + +Only change runtime composition where the failing boundary tests require it. +Read `apps/backend/internal/agentctl/AGENTS.md` before agentctl implementation. + +## Dependencies + +Task 01. + +## Risks + +Early request maps do not contain every eventual profile variable. +Prepared-workspace starts can bypass the full credential route or retain an old generated environment. + +## Parallelism + +`sequential` + +## Inputs + +- Authentication criteria 002.2 through 002.7 and the design's runtime propagation section. +- `configureExistingWorkspace`, `configureResumeGitHubCredentials`, and `buildEnvForExecution`. +- `profile_env.go`, `CollectAgentEnvWithError`, and nearby indexed Git configuration regressions. + +## Results + +- Carried the effective bridge environment through initial launch, resume, and + prepared-workspace agent starts. +- Composed agent-profile, executor-profile, managed-runtime, and repository Git + blocks at strict resolution, then sent the resulting lifecycle snapshot through + agentctl's normal overlay mode. The indexed merge recognizes an already-forwarded + snapshot and does not append inherited entries twice. +- Refreshed agent and executor profile credential-store values before the host probe, + preserved request precedence, replaced stale marker-owned entries, and returned + environment delivery errors. +- The process manager tests verify request-only overlay reconfiguration, complete + replacement for callers that own the full indexed block, and invalid-config atomicity. + The overlay path preserves user hooks/notes and removes generated helpers when the + next request supplies none. Agent, tracker, one-shot, shell, and task processes use + the resulting canonical environment. +- `TestComposeExecutionRuntimeEnvironmentRemovesObsoleteManagedCredentials` verifies + lifecycle composition also removes stale ordinary broker variables before forwarding + a request-only reconfiguration. +- The lifecycle test verifies that a prepared execution with an existing runtime + snapshot sends one composed indexed block through normal Configure. The Kubernetes + restart path uses the same overlay contract, and lifecycle spills the environment once. +- `TestBuildEnvForExecutionHostGHBridge_ComposesStrictProfileBlocks` verifies the + production strict resolver composes agent-profile, executor-profile, and managed + indexed Git definitions while retaining the supplied entries. +- `TestLaunchPreparedSessionProbesEffectiveProfileCredentialStore` verifies both + profile-directory mismatch directions through the production prepared-session route. +- `(cd apps/backend && go test -tags fts5 ./internal/orchestrator/executor -run 'TestExecutorHostGHBridge|TestConfigureGitHubCredentialBroker|TestConfigureGitCredentialBroker' -count=1)` passed. +- `(cd apps/backend && go test -tags fts5 ./internal/agent/runtime/lifecycle -run 'TestBuildEnvForExecutionHostGHBridge|TestMergeEnvFillMissing' -count=1)` passed. +- `(cd apps/backend && go test -tags fts5 ./internal/agentctl/server/config -run 'TestCollectAgentEnv.*(HostGHBridge|GitConfig)' -count=1)` passed. +- `(cd apps/backend && go test -tags fts5 ./internal/agentctl/server/process -run 'Test.*(HostGHBridge|GitEnv)' -count=1)` passed. +- The final affected-package run passed with `-count=1` for the executor, lifecycle, + agentctl client/API/config/process, backendapp, environment, and `gitconfigenv` packages. +- `make -C apps/backend lint` passed with zero issues on the final tree. +- `make -C apps/backend build` passed. +- `git diff --check` passed. diff --git a/docs/plans/executor-host-gh-bridge/task-03-document-https-access.md b/docs/plans/executor-host-gh-bridge/task-03-document-https-access.md new file mode 100644 index 0000000000..dec2c60e6a --- /dev/null +++ b/docs/plans/executor-host-gh-bridge/task-03-document-https-access.md @@ -0,0 +1,99 @@ +--- +id: "03-document-https-access" +title: "Document executor HTTPS access" +status: complete +wave: 3 +depends_on: ["02-runtime-propagation"] +plan: "plan.md" +requirements: + - REQ-INTEGRATIONS-GITHUB-AUTHENTICATION-002 +acceptance_criteria: + - AC-INTEGRATIONS-GITHUB-AUTHENTICATION-002.1 + - AC-INTEGRATIONS-GITHUB-AUTHENTICATION-002.3 + - AC-INTEGRATIONS-GITHUB-AUTHENTICATION-002.4 + - AC-INTEGRATIONS-GITHUB-AUTHENTICATION-002.5 + - AC-INTEGRATIONS-GITHUB-AUTHENTICATION-002.7 +system_design: + - ../../specs/integrations/system-design/github-authentication-02.md +--- + +# Task 03: Document executor HTTPS access + +## Summary + +Explain the implemented HTTPS fallback and its limits in the existing public integration guide. +Record the completed work-order results without claiming live GitHub access from fake-credential tests. + +## In scope + +- Update the task credential policy explanation in `docs/public/integrations.md`. +- Explain Local/Worktree scope, available host CLI credentials, explicit token precedence, and preserved Git configuration. +- State that remote executors still need their own credentials and that a CLI login does not grant additional repository permissions. +- Explain that this behavior uses the backend service account's visible CLI configuration. +- Synchronize the authentication design and package results with the final implementation. + +## Out of scope + +No new documentation page, UI copy, screenshots, or translation keys. Result sections are +updated when review validation changes the recorded implementation behavior. + +## Acceptance + +1. Public instructions describe the completed behavior and retain the managed-mode isolation contract. +2. Documentation validators pass, and all work-order results contain actual command outcomes. + +## Verification + +From the repository root: + +```bash +node --test scripts/validate-public-docs.test.mjs +node scripts/validate-public-docs.mjs +python3 scripts/list-docs.py validate +python3 scripts/lint-spec-files.py --all +git diff --check +``` + +## Files likely touched + +- `docs/public/integrations.md` +- `docs/specs/integrations/requirements/github-authentication.md` +- `docs/specs/integrations/system-design/github-authentication-01.md` +- `docs/specs/integrations/system-design/github-authentication-02.md` +- `docs/plans/executor-host-gh-bridge/plan.md` +- This package's work-order Results sections. + +## Dependencies + +Tasks 01 and 02. + +## Risks + +A desktop terminal login can differ from credentials visible to a backend service. +The guide must not promise that a token can authenticate SSH or access every repository. + +## Parallelism + +`sequential` + +## Inputs + +- The implemented behavior and recorded results from Tasks 01 and 02. +- The existing public integration guide's task credential policy section. +- `/docs-maintainer` and `/simple-english`. + +## Results + +- Updated `docs/public/integrations.md` with the Local and Worktree host `gh` + fallback, backend service-account scope including the required `gh auth login` + account, host-specific explicit token precedence, preserved Git configuration, + remote-executor limits, and permission boundary. +- Synchronized the implementation plan and all work-order statuses with the + completed production and regression work, including the strict resolver, + configure-boundary composition, marker ownership, and profile-aware probing. +- `node --test scripts/validate-public-docs.test.mjs` passed with 62 tests. +- `node scripts/validate-public-docs.mjs` validated 46 published docs pages. +- `python3 scripts/list-docs.py validate` passed for 264 decisions and 818 specifications. +- `python3 scripts/lint-spec-files.test.py` passed all 36 tests. +- `python3 scripts/lint-spec-files.py --all` passed. +- `git diff --check` passed. diff --git a/docs/public/integrations.md b/docs/public/integrations.md index e93c6b5274..2893b947d1 100644 --- a/docs/public/integrations.md +++ b/docs/public/integrations.md @@ -153,8 +153,17 @@ path on desktop hover or focus and in a touch-accessible drawer on mobile. shim; stored PATs and App private keys are not exposed to the task. An executor-profile `GH_TOKEN` or `GITHUB_TOKEN` takes precedence. - **Inherit executor Git credentials** does not install Kandev's helper or `gh` shim. Local and - Worktree tasks use host Git credentials. Docker, SSH, and cloud tasks use credentials configured - in the executor. + Worktree tasks use host Git credentials. For GitHub HTTPS remotes, Kandev checks the host + account's `gh` login for each attached GitHub host. When `gh auth token` succeeds, Kandev adds a + temporary HTTPS helper to the task environment. The helper uses the GitHub CLI configuration + visible to the Kandev backend service account. Run `gh auth login` as the OS account that runs + the backend; a login in another desktop terminal is not enough. The host bridge does not write + global Git configuration or persist helper or token state. An explicit `GH_TOKEN` or + `GITHUB_TOKEN` takes precedence over stored CLI credentials. For GitHub Enterprise hosts, + `GH_ENTERPRISE_TOKEN` and `GITHUB_ENTERPRISE_TOKEN` have the same effect. If `gh` is unavailable + or not authenticated for a host, the task keeps its other inherited Git and SSH credentials. + Docker, SSH, and cloud tasks use credentials configured in the executor. A CLI login authenticates + that account, but it does not grant repository permissions. - A disconnected workspace can select **Inherit executor Git credentials** and save only the task-access setting. It does not need a PAT or GitHub App. - The policy applies to newly launched task processes. After a launch or resume, use **New diff --git a/docs/specs/integrations/requirements/github-authentication.md b/docs/specs/integrations/requirements/github-authentication.md index 31b49fc262..f7d8fb4095 100644 --- a/docs/specs/integrations/requirements/github-authentication.md +++ b/docs/specs/integrations/requirements/github-authentication.md @@ -31,9 +31,28 @@ GitHub credentials must not silently cross workspace boundaries. A local workspa - **AC-INTEGRATIONS-GITHUB-AUTHENTICATION-001.10:** Every Local or Worktree launch and resume shall re-evaluate the inherited clone protocol. The system shall reconcile the Kandev-managed checkout before task preparation continues. This behavior shall not require a backend restart. Kandev shall not rewrite the origin of a user-managed local checkout. - **AC-INTEGRATIONS-GITHUB-AUTHENTICATION-001.11:** When a Local or Worktree task launches or resumes, the system shall inspect each attached Kandev-managed GitHub checkout before the agent starts. This behavior includes a prepared workspace that the task reuses. - **AC-INTEGRATIONS-GITHUB-AUTHENTICATION-001.12:** The system shall reconcile each managed checkout to the canonical transport for the current task policy. It shall not rewrite an already-canonical origin or a user-managed local checkout. - - **AC-INTEGRATIONS-GITHUB-AUTHENTICATION-001.13:** The public guide shall explain historical managed defaults, preserved upgrade policies, and recovery through explicit task-access selection. It shall distinguish managed checkouts, user-managed checkouts, and remote executors. +### REQ-INTEGRATIONS-GITHUB-AUTHENTICATION-002: Executor HTTPS access through host GitHub CLI + +**Intent:** Local and Worktree tasks can use an available host GitHub CLI credential for HTTPS Git operations. +This extends executor inheritance without changing workspace automation identity or managed task access. + +#### Acceptance criteria + +- **AC-INTEGRATIONS-GITHUB-AUTHENTICATION-002.1:** In executor mode, Local and Worktree tasks shall use an available host CLI credential for each eligible attached GitHub host. HTTPS credential lookup shall work without a separate global credential-helper setup. +- **AC-INTEGRATIONS-GITHUB-AUTHENTICATION-002.2:** The bridge shall preserve every valid inherited Git configuration entry, its value, and its order. This includes multiple entries for hooks and notes, and meaningful repeated helper entries. +- **AC-INTEGRATIONS-GITHUB-AUTHENTICATION-002.3:** Explicit profile or executor tokens shall retain precedence over the host login. Managed task authentication shall never fall back to the host bridge after a broker failure. +- **AC-INTEGRATIONS-GITHUB-AUTHENTICATION-002.4:** The bridge shall apply only to eligible attached GitHub hosts on Local and Worktree executors. Remote executors, unrelated hosts, and other providers shall receive no host credential bridge. +- **AC-INTEGRATIONS-GITHUB-AUTHENTICATION-002.5:** When the host CLI is absent, unauthenticated, or exceeds its lookup deadline, task preparation shall continue without the optional bridge. Existing Git helpers and SSH behavior shall remain available. Caller cancellation shall remain cancellation. +- **AC-INTEGRATIONS-GITHUB-AUTHENTICATION-002.6:** Initial launch, resume, and prepared-workspace agent start shall apply the current eligibility rules before task commands run. Repeated preparation shall not accumulate bridge entries. +- **AC-INTEGRATIONS-GITHUB-AUTHENTICATION-002.7:** The bridge shall change only the task execution environment. It shall not write Git configuration files, persist tokens, change workspace automation identity, or rewrite repository remotes. + +#### Exclusions + +This requirement does not provide repository permissions, create an SSH key, or validate token permissions through a network request. +It does not change managed repository materialization, credential snapshots, UI controls, or credential routing for remote executors. + ## System design The migrated technical source is split into [part 1](../system-design/github-authentication-01.md), [part 2](../system-design/github-authentication-02.md), [part 3](../system-design/github-authentication-03.md). diff --git a/docs/specs/integrations/system-design/github-authentication-01.md b/docs/specs/integrations/system-design/github-authentication-01.md index e3f83b08af..947d6fce16 100644 --- a/docs/specs/integrations/system-design/github-authentication-01.md +++ b/docs/specs/integrations/system-design/github-authentication-01.md @@ -275,7 +275,7 @@ The non-secret operational settings row adds `task_git_credentials_mode`, with a | Value | Behavior | | --- | --- | | `managed` | Inject the workspace broker contract for attached GitHub repositories unless an explicit executor-profile token overrides it. Existing missing/invalid values continue to normalize here for upgrade compatibility. | -| `executor` | Default persisted for newly created workspaces. Inject no Kandev GitHub helper or `gh` shim; use credentials available where the selected executor runs. | +| `executor` | Default persisted for newly created workspaces. Inject no broker helper or managed `gh` shim. Local/Worktree HTTPS can use the optional host CLI bridge described in part 2. Remote credentials remain executor-owned. | Missing or invalid persisted values normalize to `managed`. Workspace-settings copy includes this policy because it is operational configuration, not authentication material. diff --git a/docs/specs/integrations/system-design/github-authentication-02.md b/docs/specs/integrations/system-design/github-authentication-02.md index 22301a8fae..6ad6383742 100644 --- a/docs/specs/integrations/system-design/github-authentication-02.md +++ b/docs/specs/integrations/system-design/github-authentication-02.md @@ -3,6 +3,7 @@ status: draft system: integrations requirements: - REQ-INTEGRATIONS-GITHUB-AUTHENTICATION-001 + - REQ-INTEGRATIONS-GITHUB-AUTHENTICATION-002 created: 2026-07-19 owners: - Kandev @@ -18,6 +19,7 @@ This design preserves the technical source detail for `REQ-INTEGRATIONS-GITHUB-A | Requirement | Design section | | --- | --- | | `REQ-INTEGRATIONS-GITHUB-AUTHENTICATION-001` | [Migrated source detail](#migrated-source-detail) | +| `REQ-INTEGRATIONS-GITHUB-AUTHENTICATION-002` | [Executor host CLI bridge](#executor-host-cli-bridge) | ## Migrated source detail @@ -251,3 +253,106 @@ registration and never creates a global default. snapshot. Desktop supports hover and keyboard focus; coarse-pointer/mobile users open the same information in a 44px-target drawer. Unknown inherited/profile actors are explicitly labeled runtime-selected. + + +## Executor host CLI bridge + +This section implements `REQ-INTEGRATIONS-GITHUB-AUTHENTICATION-002` and its criteria 002.1 through 002.7. +The integration system owns the credential policy, even though executor and agentctl code carry the environment. +The [implementation package](../../../plans/executor-host-gh-bridge/plan.md) records delivery status. + +### Eligibility and credential precedence + +`Executor.configureGitCredentialBrokerForRepositories` owns the policy branch shared by full launch and resume. +The executor branch removes managed credentials, then considers an optional host helper. +Only explicit Local and Worktree executor types qualify. An unknown type does not imply Local. +The branch never runs for Docker, SSH, Sprites, Kubernetes, or other remote execution. + +For each attached GitHub repository, resolve its host through `gitcredentials.ResolveRepositoryIdentity`. +Use provider identity and persisted remote metadata. Do not discover new hosts from arbitrary checkout remotes. +The existing public GitHub compatibility fallback remains available. +An explicit enterprise provider host must pass the existing identity validation. +Invalid or ambiguous optional bridge identities are skipped without weakening managed admission. +A local-source repository qualifies only when its recorded metadata establishes a GitHub identity. +Deduplicate hosts within each preparation operation. + +For `github.com`, a non-empty explicit `GH_TOKEN` or `GITHUB_TOKEN` in the request bypasses probing and bridge injection for that host. +For a non-public host, only a non-empty `GH_ENTERPRISE_TOKEN` or `GITHUB_ENTERPRISE_TOKEN` bypasses probing and bridge injection for that host. +Mixed-host launches evaluate these token variables independently for each attached host. +Later profile merges must preserve the same effective token precedence. +The helper invokes ordinary `gh`, which selects explicit environment tokens before stored login credentials. +An invalid explicit token must not trigger a retry with a stored host login. +Tests must exercise late profile tokens at the child-process boundary, not only inspect the early request map. + +Managed mode retains its existing helper reset and fail-closed behavior. +No host helper can follow the managed reset/helper pair in the effective Git configuration. +A policy transition rebuilds the environment from its source layers instead of retaining a prior generated bridge. +The existing credential snapshot remains `executor` / `executor_selected`, with no inferred actor. + +### Optional host probe + +Add a small, injectable command runner beside executor credential routing. +Resolve the real host `gh` executable before managed shim activation. +Invoke `gh auth token --hostname ` without a shell through `subproc.RunGH`. +Discard stdout and stderr. The command result exposes only availability, never token bytes. +Use a five-second operation deadline and honor earlier caller cancellation. +Deduplicate probes per host for that operation. Do not cache a result across launches or resumes. +Command failure or its own timeout skips the optional bridge. Caller cancellation aborts preparation normally. +A successful token command establishes token availability, not repository access or token validity on GitHub. + +The probe resolves the selected profile's `HOME`, `GH_CONFIG_DIR`, and `XDG_CONFIG_HOME` values before it runs. +The helper receives the same values in the final effective child environment, so a profile override cannot +silently probe one account and execute Git against another account. A secret-backed directory value is +resolved through the global secret store for the probe and is never logged or placed in the command. +Use a safely quoted absolute helper executable when a child shell can replace `PATH`. +Do not embed a token or unvalidated host text in a shell command. + +### Indexed configuration and runtime propagation + +For each eligible host, append one URL-scoped helper with the semantics of +`credential.https://.helper = !gh auth git-credential`. +The generated shell function contains the unambiguous `kandev-host-gh-bridge` ownership marker. +Existing user helpers remain earlier in the chain. Do not insert an empty reset for this optional fallback. +Compose the helper block through `gitconfigenv.Merge`, never a map overlay or a hardcoded count. +Keep the existing malformed-block error behavior and preserve meaningful duplicate entries. +Validate the combined count against the existing 256-entry limit before dispatch. +Complete replacement and the agentctl composition boundary remove obsolete host bridge helpers carrying the +Kandev marker, including when no replacement is available. A user-selected quoted `gh` command and an +unrelated host helper remain according to ownership. Do not premerge the entire host environment into a +request that agentctl will merge with that same host environment again. + +The strict source-aware resolver treats `GIT_CONFIG_COUNT`, `GIT_CONFIG_KEY_*`, and +`GIT_CONFIG_VALUE_*` as one indexed block per environment source. It composes agent-profile, +executor-profile, managed-runtime, and repository blocks in source order while ordinary variables +retain the existing conflict, secret, and tier rules. The final lifecycle boundary resolves this +composition after every managed definition has been added. + +`configureResumeGitHubCredentials` reuses the shared policy branch. +The prepared-workspace path through `configureExistingWorkspace` also needs the resolved executor identity and repository set. +It must apply the refreshed environment before `SetExecutionEnv` and agent start. +Reuse the repository set already resolved for preparation instead of cloning or reconciling it a second time. +An environment delivery failure must prevent a claim that the refreshed environment is active. + +The agentctl configure boundary has two explicit modes. The existing API mode composes a request-only +overlay with the instance block, removing only marker-owned bridge entries first. Lifecycle launch and +Kubernetes restart pass a composed runtime snapshot through this overlay mode; the indexed merge recognizes +an already-forwarded snapshot and does not append it to itself. Complete-environment mode remains available +to callers that supply the complete indexed block, including an intentional empty block. Both modes update +the agent environment, one-shot adapter, workspace tracker, task shells, and task-scoped processes from the +same canonical slice. +A reused executor must replace obsolete generated entries when a policy, token, or host eligibility changes, +including when a later preparation supplies no bridge at all. +A login-shell test covers executable paths with spaces and a replaced `PATH`. +No global Git file, repository configuration file, token cache, or workspace connection is modified. + +### Compatibility and evidence + +This extends the existing executor-inheritance contract in +[the task Git policy decision](../../../decisions/2026-07-27-task-git-credential-policy.md). +It does not create a new credential source or change the managed security boundary. +The existing workspace-creation identity rules remain unchanged. + +A real Git subprocess with a fake host CLI supplies deterministic end-to-end credential evidence. +It covers success, a foreign host, an explicit token, and a failed managed helper. +No live credential or network request is needed for these regressions. +An authenticated login can still lack repository permissions, and a later logout can still make an operation fail.