From 15788438b41e2e37e78484ce9276397b5543fde0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Flor=C3=AAncio?= Date: Sat, 15 Aug 2026 23:51:39 +0100 Subject: [PATCH] feat: route managed Improve Kandev contributions to bound forks (#2569) --- .../cmd/agentctl/github_cli_shim_test.go | 36 ++ .../backend/cmd/agentctl/github_credential.go | 29 +- apps/backend/cmd/mock-agent/handler.go | 7 +- apps/backend/cmd/mock-agent/scenarios.go | 57 ++- apps/backend/cmd/mock-agent/scenarios_test.go | 23 + .../config/workflows/improve-kandev.yml | 45 +- apps/backend/config/workflows/loader_test.go | 53 ++ .../agentctl/client_workspace_sources.go | 11 +- .../agent/runtime/agentctl/control.go | 3 +- .../agent/runtime/lifecycle/container.go | 32 +- .../agent/runtime/lifecycle/env_preparer.go | 109 ++-- .../runtime/lifecycle/env_preparer_local.go | 5 +- .../lifecycle/env_preparer_worktree.go | 49 +- .../runtime/lifecycle/executor_backend.go | 109 ++-- .../runtime/lifecycle/executor_docker.go | 40 +- .../runtime/lifecycle/executor_sprites.go | 3 + .../lifecycle/executor_sprites_operations.go | 26 +- .../agent/runtime/lifecycle/executor_ssh.go | 3 + .../lifecycle/executor_ssh_operations.go | 19 +- .../runtime/lifecycle/executor_ssh_scripts.go | 7 + .../lifecycle/executor_ssh_scripts_test.go | 28 ++ .../runtime/lifecycle/executor_standalone.go | 31 +- .../runtime/lifecycle/manager_execution.go | 5 + .../agent/runtime/lifecycle/manager_launch.go | 149 ++++-- .../runtime/lifecycle/manager_launch_test.go | 28 ++ .../runtime/lifecycle/manager_passthrough.go | 80 ++- .../manager_passthrough_launch_test.go | 23 + .../runtime/lifecycle/preparer_script.go | 16 +- .../runtime/lifecycle/preparer_script_test.go | 38 +- .../internal/agent/runtime/lifecycle/types.go | 109 ++-- .../runtime/lifecycle/types_repospecs_test.go | 27 +- .../lifecycle/workspace_materialization.go | 24 +- .../server/api/workspace_materialize.go | 90 +++- .../server/api/workspace_materialize_test.go | 10 + .../internal/agentctl/server/config/config.go | 64 ++- .../agentctl/server/instance/instance.go | 7 +- .../agentctl/server/instance/manager.go | 45 +- .../internal/agentctl/server/process/git.go | 131 ++++- .../server/process/git_pr_providers.go | 11 +- .../server/process/git_pr_providers_test.go | 104 ++++ .../agentctl/server/process/git_test.go | 45 ++ .../agentctl/server/process/manager.go | 6 + .../agentctl/server/process/vscode_test.go | 14 +- .../process/workspace_content_search.go | 6 + .../process/workspace_content_search_test.go | 27 + apps/backend/internal/backendapp/adapters.go | 42 +- apps/backend/internal/backendapp/helpers.go | 3 + apps/backend/internal/backendapp/services.go | 280 ++++++++++- .../backendapp/services_github_broker_test.go | 120 ++++- .../backend/internal/gitcredentials/broker.go | 40 +- .../internal/github/controller_auth.go | 19 +- .../internal/github/credential_broker.go | 99 +++- .../internal/github/credential_broker_test.go | 50 +- apps/backend/internal/github/gh_client.go | 89 +++- .../internal/github/gh_client_reads_test.go | 91 +++- apps/backend/internal/github/mock_client.go | 89 ++++ apps/backend/internal/github/models.go | 20 + apps/backend/internal/github/noop_client.go | 16 + apps/backend/internal/github/pat_client.go | 58 +++ .../internal/github/pat_client_test.go | 29 ++ apps/backend/internal/github/poller.go | 8 + .../backend/internal/github/repository_api.go | 65 +++ .../github/service_automation_auth.go | 95 +++- apps/backend/internal/github/service_fork.go | 475 ++++++++++++++++++ .../internal/github/service_fork_test.go | 417 +++++++++++++++ .../github/service_pr_branch_fallback_test.go | 159 ++++++ .../internal/github/service_pr_watch.go | 27 +- .../github/service_pr_watch_batched.go | 20 +- apps/backend/internal/github/store.go | 10 + apps/backend/internal/improvekandev/github.go | 14 + .../backend/internal/improvekandev/handler.go | 192 +++++-- .../internal/improvekandev/handler_test.go | 96 +++- .../mcp/handlers/clarification_pause_test.go | 4 + .../internal/mcp/handlers/parent_question.go | 31 +- .../mcp/handlers/parent_question_test.go | 17 +- .../office/testharness/routes_office.go | 34 +- .../orchestrator/event_handlers_github.go | 31 +- .../event_handlers_github_test.go | 3 + .../orchestrator/executor/executor.go | 66 +-- .../executor/executor_credentials.go | 172 ++++++- .../executor/executor_credentials_test.go | 133 ++++- .../orchestrator/executor/executor_execute.go | 22 +- .../orchestrator/executor/executor_resume.go | 41 +- .../executor/executor_resume_test.go | 37 ++ .../internal/scriptengine/providers.go | 56 +++ .../internal/scriptengine/providers_test.go | 30 ++ .../task/models/contribution_destination.go | 149 ++++++ .../models/contribution_destination_test.go | 179 +++++++ .../task/models/remote_contribution.go | 64 ++- apps/backend/internal/task/service/service.go | 117 +++-- .../service_contribution_destination_test.go | 183 +++++++ .../internal/task/service/service_requests.go | 6 +- .../internal/task/service/service_tasks.go | 55 ++ .../internal/task/statussummary/projector.go | 1 - .../internal/worktree/manager_contribution.go | 49 ++ .../internal/worktree/manager_lifecycle.go | 6 + .../manager_remote_contribution_test.go | 10 + apps/backend/internal/worktree/worktree.go | 8 +- apps/web/app/page.tsx | 11 +- .../improve-kandev-dialog-create.tsx | 32 +- .../improve-kandev-dialog-model.test.ts | 36 +- .../components/improve-kandev-dialog-model.ts | 43 +- .../review/review-repository-identity.ts | 44 +- .../web/components/sentry/sentry-settings.tsx | 5 +- apps/web/e2e/fixtures/docker-probe.ts | 118 ++++- apps/web/e2e/fixtures/docker-test-base.ts | 45 +- apps/web/e2e/fixtures/office-fixture.ts | 7 + apps/web/e2e/fixtures/test-base.ts | 69 ++- apps/web/e2e/helpers/api-client.ts | 59 ++- apps/web/e2e/helpers/multi-pr-review.ts | 17 + apps/web/e2e/helpers/plugin-fixture.ts | 5 + apps/web/e2e/helpers/session-store.ts | 17 + apps/web/e2e/pages/jira-settings-page.ts | 5 + apps/web/e2e/pages/sentry-settings-page.ts | 13 +- apps/web/e2e/pages/session-page.ts | 15 +- apps/web/e2e/tests/chat/busy-signal.spec.ts | 11 +- .../chat/mobile-auto-scroll-toggle.spec.ts | 2 + .../e2e/tests/chat/mobile-subagent.spec.ts | 6 +- .../tests/chat/model-selector-error.spec.ts | 1 + .../e2e/tests/chat/shell-output-echo.spec.ts | 5 + apps/web/e2e/tests/chat/subagent.spec.ts | 60 ++- .../tests/docker/storage-maintenance.spec.ts | 16 +- .../e2e/tests/git/git-changes-panel.spec.ts | 30 +- apps/web/e2e/tests/improve-kandev.spec.ts | 65 ++- .../tests/integrations/jira-settings.spec.ts | 50 +- .../integrations/linear-settings.spec.ts | 1 + .../integrations/sentry-settings.spec.ts | 1 + .../layout/preview-tab-session-switch.spec.ts | 24 +- .../e2e/tests/mobile-improve-kandev.spec.ts | 14 +- .../tests/office/comment-run-status.spec.ts | 9 + .../e2e/tests/office/scheduler-events.spec.ts | 40 +- .../web/e2e/tests/pr/pr-push-autolink.spec.ts | 1 + .../review/mobile-review-multi-pr.spec.ts | 27 +- .../task-workspace-content-search.spec.ts | 57 +-- .../tests/session/port-forward-dialog.spec.ts | 2 + .../e2e/tests/session/session-layout.spec.ts | 6 +- .../tests/settings/secrets-copy-move.spec.ts | 76 ++- .../tests/system/storage-maintenance.spec.ts | 1 + .../tests/task/add-workspace-sources.spec.ts | 23 +- .../task/archive-freezes-task-state.spec.ts | 28 +- .../task/create-task-branch-selector.spec.ts | 4 +- ...e-create-task-new-local-repository.spec.ts | 22 + .../mobile-plan-toolbar-implement.spec.ts | 13 +- .../task/mobile-task-listing-display.spec.ts | 36 ++ .../tests/task/plan-toolbar-implement.spec.ts | 8 +- .../task/sidebar-settled-spinner.spec.ts | 24 +- .../task-create-prompt-autocomplete.spec.ts | 13 +- .../tests/task/task-switcher-status.spec.ts | 6 + .../e2e/tests/terminal/terminal-agent.spec.ts | 13 + .../tests/terminal/terminal-new-tab.spec.ts | 6 +- .../workflow/workflow-automation.spec.ts | 2 +- .../workflow-cycle-guardrails.spec.ts | 5 +- .../tests/workflow/workflow-queue-helpers.ts | 17 +- .../tests/workflow/workflow-settings.spec.ts | 2 +- ...se-all-workflow-snapshots-inflight.test.ts | 127 +++++ .../kanban/use-all-workflow-snapshots.ts | 121 +++-- .../use-workspace-content-search.test.ts | 48 +- .../session/use-workspace-content-search.ts | 58 ++- .../settings/use-settings-data.test.tsx | 82 +++ .../domains/settings/use-settings-data.ts | 69 ++- .../workspace/use-repositories.test.ts | 16 +- .../domains/workspace/use-repositories.ts | 22 +- .../workspace/use-repository-branches.test.ts | 20 + .../workspace/use-repository-branches.ts | 34 +- .../web/lib/api/domains/improve-kandev-api.ts | 25 +- apps/web/lib/ssr/mapper.test.ts | 13 + apps/web/lib/ssr/mapper.ts | 1 + apps/web/src/locales/en/common.json | 6 + apps/web/src/locales/pseudo/common.json | 6 + apps/web/src/locales/pt-pt/common.json | 6 + ...2026-08-12-task-bound-fork-destinations.md | 132 +++++ docs/decisions/INDEX.md | 1 + .../plans/improve-kandev-managed-fork/plan.md | 227 +++++++++ ...destination-model-and-github-resolution.md | 93 ++++ ...k-02-creation-preparation-and-bootstrap.md | 91 ++++ ...03-runtime-credentials-and-fork-routing.md | 95 ++++ ...ask-04-workflow-ui-integration-and-docs.md | 99 ++++ docs/public/git-operations.md | 17 +- docs/public/integrations.md | 10 + docs/specs/improve-kandev/spec.md | 135 ++++- .../integrations/github-authentication.md | 22 +- 181 files changed, 7639 insertions(+), 1116 deletions(-) create mode 100644 apps/backend/cmd/mock-agent/scenarios_test.go create mode 100644 apps/backend/internal/github/repository_api.go create mode 100644 apps/backend/internal/github/service_fork.go create mode 100644 apps/backend/internal/github/service_fork_test.go create mode 100644 apps/backend/internal/github/service_pr_branch_fallback_test.go create mode 100644 apps/backend/internal/task/models/contribution_destination.go create mode 100644 apps/backend/internal/task/models/contribution_destination_test.go create mode 100644 apps/backend/internal/task/service/service_contribution_destination_test.go create mode 100644 apps/web/hooks/domains/settings/use-settings-data.test.tsx create mode 100644 docs/decisions/2026-08-12-task-bound-fork-destinations.md create mode 100644 docs/plans/improve-kandev-managed-fork/plan.md create mode 100644 docs/plans/improve-kandev-managed-fork/task-01-destination-model-and-github-resolution.md create mode 100644 docs/plans/improve-kandev-managed-fork/task-02-creation-preparation-and-bootstrap.md create mode 100644 docs/plans/improve-kandev-managed-fork/task-03-runtime-credentials-and-fork-routing.md create mode 100644 docs/plans/improve-kandev-managed-fork/task-04-workflow-ui-integration-and-docs.md diff --git a/apps/backend/cmd/agentctl/github_cli_shim_test.go b/apps/backend/cmd/agentctl/github_cli_shim_test.go index 522f068ef6..1e3a260e35 100644 --- a/apps/backend/cmd/agentctl/github_cli_shim_test.go +++ b/apps/backend/cmd/agentctl/github_cli_shim_test.go @@ -260,6 +260,42 @@ func TestGitHubCLIShimSelectsRepositoryLease(t *testing.T) { } } +func TestGitHubCLIShimAcceptsShortRepositoryPathForGitHubLease(t *testing.T) { + var got githubBrokerResolveRequest + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if err := json.NewDecoder(r.Body).Decode(&got); err != nil { + t.Errorf("decode request: %v", err) + } + _, _ = io.WriteString(w, `{"username":"x-access-token","password":"backend-token"}`) + })) + t.Cleanup(server.Close) + env := githubCredentialTestEnv(server.URL) + env["PATH"] = "/shim:/usr/bin" + env["GH_REPO"] = "acme/backend" + env[envGitHubCredentialScopes] = `[ + {"lease":"backend-lease","task_id":"task-1","session_id":"session-1","repository_id":"repo-2","owner":"acme","repo":"backend","host":"github.com","path":"/acme/backend.git","provider_id":"github"} + ]` + var childToken string + err := runGitHubCLIShim( + context.Background(), []string{"pr", "list"}, strings.NewReader(""), io.Discard, io.Discard, + lookupEnv(env), func() []string { return envMap(env) }, server.Client(), "/shim", + func(string, string) (string, error) { return "/usr/bin/gh", nil }, + func(_ context.Context, _ string, _ []string, childEnv []string, _ io.Reader, _, _ io.Writer) error { + childToken = envValue(childEnv, "GH_TOKEN") + return nil + }, + ) + if err != nil { + t.Fatalf("runGitHubCLIShim() error = %v", err) + } + if got.Path != "/acme/backend.git" { + t.Fatalf("broker path = %q, want canonical GitHub clone path", got.Path) + } + if childToken != "backend-token" { + t.Fatalf("child GH_TOKEN = %q, want backend-token", childToken) + } +} + func TestParseGitHubCLIRepository(t *testing.T) { tests := []struct { name string diff --git a/apps/backend/cmd/agentctl/github_credential.go b/apps/backend/cmd/agentctl/github_credential.go index 6dbe12a0e2..87a8d17c78 100644 --- a/apps/backend/cmd/agentctl/github_credential.go +++ b/apps/backend/cmd/agentctl/github_credential.go @@ -17,14 +17,16 @@ import ( ) type githubBrokerResolveRequest struct { - Lease string `json:"lease"` - TaskID string `json:"task_id"` - SessionID string `json:"session_id"` - RepositoryID string `json:"repository_id"` - Owner string `json:"owner"` - Repo string `json:"repo"` - Host string `json:"host"` - Path string `json:"path,omitempty"` + Lease string `json:"lease"` + TaskID string `json:"task_id"` + SessionID string `json:"session_id"` + RepositoryID string `json:"repository_id"` + Owner string `json:"owner"` + Repo string `json:"repo"` + Host string `json:"host"` + Path string `json:"path,omitempty"` + ProviderID string `json:"provider_id,omitempty"` + ParentProviderID string `json:"parent_provider_id,omitempty"` } type githubBrokerCredential struct { @@ -235,8 +237,8 @@ func readGitCredentialInput(input io.Reader) (map[string]string, error) { } // gitCredentialRequestForInput validates the Git credential helper request -// against its issued lease scope, then carries the exact requested repository -// path through to the broker. Git uses owner/repo fields on this compatibility +// against its issued lease scope, then carries the issued repository path +// through to the broker. Git uses owner/repo fields on this compatibility // endpoint, so Repo retains any provider namespace after the first segment. func gitCredentialRequestForInput(input map[string]string, scope githubBrokerResolveRequest) (githubBrokerResolveRequest, error) { protocol := strings.TrimSpace(input["protocol"]) @@ -256,9 +258,10 @@ func gitCredentialRequestForInput(input map[string]string, scope githubBrokerRes return githubBrokerResolveRequest{}, fmt.Errorf("git repository does not match credential lease scope") } if scope.Path != "" { - if path != scope.Path { + if canonicalGitCredentialPath(path) != canonicalGitCredentialPath(scope.Path) { return githubBrokerResolveRequest{}, fmt.Errorf("git repository does not match credential lease scope") } + path = scope.Path } else { legacyPath := strings.TrimSuffix(strings.Trim(path, "/"), ".git") owner, repo, found := strings.Cut(legacyPath, "/") @@ -271,3 +274,7 @@ func gitCredentialRequestForInput(input map[string]string, scope githubBrokerRes scope.Path = path return scope, nil } + +func canonicalGitCredentialPath(path string) string { + return strings.TrimSuffix(strings.Trim(path, "/"), ".git") +} diff --git a/apps/backend/cmd/mock-agent/handler.go b/apps/backend/cmd/mock-agent/handler.go index 738cb5028f..1a2499055c 100644 --- a/apps/backend/cmd/mock-agent/handler.go +++ b/apps/backend/cmd/mock-agent/handler.go @@ -320,7 +320,7 @@ var kandevSystemRegex = regexp.MustCompile(`[\s\S]*?:` plus the title, body, and draft options. - `gh` will fork `kdlbs/kandev` to your account if a fork does not - already exist, rename the existing `origin` remote to `upstream`, - and point `origin` at `/kandev`. The command is a no-op - on subsequent runs, so it is safe to re-run. - - Verify with `git remote -v`: `origin` must point at your fork and - `upstream` at `kdlbs/kandev`. Skip this step only if you confirmed - via `gh api repos/kdlbs/kandev --jq .permissions.push` that you - already have push access to the upstream repo. + Executor-owned credentials are a separate compatibility path. It may + use the executor's own publication setup only when the task has no + server-authored managed destination. In that case, verify `gh auth + status`, then run `gh repo fork kdlbs/kandev --remote + --remote-name=origin` when the executor does not already have a + writable fork. Verify that `origin` points at the executor-owned fork + and that the canonical repository remains available as `upstream`. + Never select this path to recover from a managed preparation failure or + a missing managed push remote. STEP 3: INVOKE THE PR SKILL Use the `pr` skill (see `.claude/skills/pr/SKILL.md`): - - Push the current branch to `origin` (your fork, after step 2). + - Push the current branch through the prepared route from step 2. - Open a pull request against `main` in `kdlbs/kandev` with a clear title and description summarising the change and how it was tested. - `gh pr create` auto-detects the cross-fork setup. + Use the explicit repository and fork head from step 2. - Reference the original task title in the PR description. STEP 4: REPORT diff --git a/apps/backend/config/workflows/loader_test.go b/apps/backend/config/workflows/loader_test.go index fb90b1949b..9572112f64 100644 --- a/apps/backend/config/workflows/loader_test.go +++ b/apps/backend/config/workflows/loader_test.go @@ -316,6 +316,59 @@ func TestLoadTemplates_ReportKandevIssuePromptContract(t *testing.T) { } } +func TestLoadTemplates_ImproveKandevManagedPublicationPromptContract(t *testing.T) { + templates, err := LoadTemplates() + if err != nil { + t.Fatalf("LoadTemplates() returned error: %v", err) + } + + var improve *models.WorkflowTemplate + for _, tmpl := range templates { + if tmpl.ID == "improve-kandev" { + improve = tmpl + break + } + } + if improve == nil { + t.Fatal("improve-kandev template not found") + } + if len(improve.Steps) != 3 { + t.Fatalf("improve-kandev steps = %d, want 3", len(improve.Steps)) + } + + prStep := improve.Steps[2] + normalizedPrompt := strings.Join(strings.Fields(prStep.Prompt), " ") + for _, required := range []string{ + "Managed workspace credentials", + "origin` remains the canonical `kdlbs/kandev`", + "ordinary `git push`", + "gh pr create --repo kdlbs/kandev --base main", + ":", + "Executor-owned credentials are a separate compatibility path", + "Never select this path to recover from a managed preparation failure", + "gh repo fork kdlbs/kandev", + "executor-owned credentials", + "--remote-name=origin", + } { + if !strings.Contains(normalizedPrompt, required) { + t.Errorf("managed publication prompt must contain %q", required) + } + } + managedPrompt := normalizedPrompt + if executorSection := strings.Index(managedPrompt, "Executor-owned credentials"); executorSection >= 0 { + managedPrompt = managedPrompt[:executorSection] + } + for _, forbidden := range []string{ + "gh repo fork", + "remote-name=origin", + "rename the existing `origin`", + } { + if strings.Contains(managedPrompt, forbidden) { + t.Errorf("managed publication prompt must not contain %q", forbidden) + } + } +} + // TestLoadTemplates_PRReviewMRAutomationInstruction is AC30: the pr-review // template's review step must instruct the agent to enable lifecycle // notifications on whichever provider the task's linked review target is diff --git a/apps/backend/internal/agent/runtime/agentctl/client_workspace_sources.go b/apps/backend/internal/agent/runtime/agentctl/client_workspace_sources.go index bf765e1ae9..c49eabc39a 100644 --- a/apps/backend/internal/agent/runtime/agentctl/client_workspace_sources.go +++ b/apps/backend/internal/agent/runtime/agentctl/client_workspace_sources.go @@ -14,11 +14,12 @@ import ( // request accepted by agentctl. It intentionally has no credential field; // lifecycle wiring supplies Git credentials separately when that is needed. type MaterializeRepositoryRequest struct { - RepositoryURL string `json:"repository_url"` - Destination string `json:"destination"` - BaseBranch string `json:"base_branch"` - CheckoutBranch string `json:"checkout_branch,omitempty"` - RemoteContribution *models.RemoteContribution `json:"remote_contribution,omitempty"` + RepositoryURL string `json:"repository_url"` + Destination string `json:"destination"` + BaseBranch string `json:"base_branch"` + CheckoutBranch string `json:"checkout_branch,omitempty"` + RemoteContribution *models.RemoteContribution `json:"remote_contribution,omitempty"` + ContributionDestination *models.ContributionDestination `json:"contribution_destination,omitempty"` } // MaterializeRepositoryResponse reports the adopted workspace subdirectory. diff --git a/apps/backend/internal/agent/runtime/agentctl/control.go b/apps/backend/internal/agent/runtime/agentctl/control.go index e0f8242700..66df235500 100644 --- a/apps/backend/internal/agent/runtime/agentctl/control.go +++ b/apps/backend/internal/agent/runtime/agentctl/control.go @@ -79,7 +79,8 @@ type CreateInstanceRequest struct { // RemoteContributions maps an agentctl workspace repository subpath to the // server-authored contribution binding for that checkout. The empty key is // the workspace root. - RemoteContributions map[string]models.RemoteContribution `json:"remote_contributions,omitempty"` + RemoteContributions map[string]models.RemoteContribution `json:"remote_contributions,omitempty"` + ContributionDestinations map[string]models.ContributionDestination `json:"contribution_destinations,omitempty"` // WorkspaceSourceRoots are canonical host roots explicitly attached to the // workspace. Agentctl permits file operations through links only beneath // these roots. diff --git a/apps/backend/internal/agent/runtime/lifecycle/container.go b/apps/backend/internal/agent/runtime/lifecycle/container.go index 805a71492f..d2e06e05b8 100644 --- a/apps/backend/internal/agent/runtime/lifecycle/container.go +++ b/apps/backend/internal/agent/runtime/lifecycle/container.go @@ -64,8 +64,9 @@ type ContainerConfig struct { // BaseBranches maps RepositoryName → base branch ref; forwarded into // agentctl's CreateInstanceRequest so each WorkspaceTracker resolves // diff stats against the task-recorded base. - BaseBranches map[string]string - RemoteContributions map[string]models.RemoteContribution + BaseBranches map[string]string + RemoteContributions map[string]models.RemoteContribution + ContributionDestinations map[string]models.ContributionDestination } func boolPtr(v bool) *bool { @@ -99,19 +100,20 @@ func buildContainerCreateInstanceRequest( config.AutoApprovePermissions, config.AutoApprovePermissionsOverride, ), - AutoStart: false, - McpServers: config.McpServers, - SessionID: config.SessionID, - DisableAskQuestion: disableAskQuestion, - AssumeMcpSse: assumeMcpSse, - AssumeMcpHttp: assumeMcpHttp, - McpMode: config.McpMode, - McpProviders: config.McpProviders, - McpProfile: config.McpProfile, - RequiresProcessKill: requiresProcessKill, - StripEnv: stripEnv, - BaseBranches: config.BaseBranches, - RemoteContributions: config.RemoteContributions, + AutoStart: false, + McpServers: config.McpServers, + SessionID: config.SessionID, + DisableAskQuestion: disableAskQuestion, + AssumeMcpSse: assumeMcpSse, + AssumeMcpHttp: assumeMcpHttp, + McpMode: config.McpMode, + McpProviders: config.McpProviders, + McpProfile: config.McpProfile, + RequiresProcessKill: requiresProcessKill, + StripEnv: stripEnv, + BaseBranches: config.BaseBranches, + RemoteContributions: config.RemoteContributions, + ContributionDestinations: config.ContributionDestinations, } } diff --git a/apps/backend/internal/agent/runtime/lifecycle/env_preparer.go b/apps/backend/internal/agent/runtime/lifecycle/env_preparer.go index 82a5b18b68..4f42c91524 100644 --- a/apps/backend/internal/agent/runtime/lifecycle/env_preparer.go +++ b/apps/backend/internal/agent/runtime/lifecycle/env_preparer.go @@ -26,22 +26,23 @@ const ( // carried at the top level. When EnvPrepareRequest.Repositories is non-empty, // each entry produces one prepared worktree under the shared TaskDirName. type RepoPrepareSpec struct { - RepositoryID string - RepositoryPath string - RepoName string - BaseBranch string - DefaultBranch string // Repository's default_branch, used as fallback when BaseBranch is missing - CheckoutBranch string - PRNumber int // GitHub PR number when CheckoutBranch is a PR head; enables refs/pull//head fetch for fork PRs. - RemoteContribution *models.RemoteContribution - WorktreeID string - WorktreeBranch string - WorktreeBranchPrefix string - WorktreeBranchTemplate string - WorktreeBranchTicket string - PullBeforeWorktree bool - RemoteSyncHandled bool - RepoSetupScript string + RepositoryID string + RepositoryPath string + RepoName string + BaseBranch string + DefaultBranch string // Repository's default_branch, used as fallback when BaseBranch is missing + CheckoutBranch string + PRNumber int // GitHub PR number when CheckoutBranch is a PR head; enables refs/pull//head fetch for fork PRs. + RemoteContribution *models.RemoteContribution + WorktreeID string + WorktreeBranch string + WorktreeBranchPrefix string + WorktreeBranchTemplate string + WorktreeBranchTicket string + PullBeforeWorktree bool + RemoteSyncHandled bool + RepoSetupScript string + ContributionDestination *models.ContributionDestination // BranchSlug, when set, suffixes the worktree path as // {RepoName}-{BranchSlug} so two specs sharing a RepositoryID don't collide // on disk. @@ -54,25 +55,26 @@ type RepoPrepareSpec struct { // EnvPrepareRequest contains the parameters for environment preparation. type EnvPrepareRequest struct { - TaskID string - WorkspaceID string - SessionID string - TaskTitle string - ExecutionID string - ExecutorType executor.Name - WorkspacePath string - RepositoryPath string - RepositoryID string - UseWorktree bool - SetupScript string - RepoSetupScript string // Repository-level setup script (e.g. "make install") - BaseBranch string - DefaultBranch string // Repository's default_branch, used as fallback when BaseBranch is missing - CheckoutBranch string - PRNumber int // GitHub PR number when CheckoutBranch is a PR head; enables refs/pull//head fetch for fork PRs. - RemoteContribution *models.RemoteContribution - WorktreeID string - WorktreeBranch string + TaskID string + WorkspaceID string + SessionID string + TaskTitle string + ExecutionID string + ExecutorType executor.Name + WorkspacePath string + RepositoryPath string + RepositoryID string + UseWorktree bool + SetupScript string + RepoSetupScript string // Repository-level setup script (e.g. "make install") + BaseBranch string + DefaultBranch string // Repository's default_branch, used as fallback when BaseBranch is missing + CheckoutBranch string + PRNumber int // GitHub PR number when CheckoutBranch is a PR head; enables refs/pull//head fetch for fork PRs. + RemoteContribution *models.RemoteContribution + ContributionDestination *models.ContributionDestination + WorktreeID string + WorktreeBranch string WorktreeBranchPrefix string WorktreeBranchTemplate string @@ -109,24 +111,25 @@ func (r *EnvPrepareRequest) RepoSpecs() []RepoPrepareSpec { return nil } return []RepoPrepareSpec{{ - RepositoryID: r.RepositoryID, - RepositoryPath: r.RepositoryPath, - RepoName: r.RepoName, - BaseBranch: r.BaseBranch, - DefaultBranch: r.DefaultBranch, - CheckoutBranch: r.CheckoutBranch, - PRNumber: r.PRNumber, - RemoteContribution: r.RemoteContribution, - WorktreeID: r.WorktreeID, - WorktreeBranch: r.WorktreeBranch, - WorktreeBranchPrefix: r.WorktreeBranchPrefix, - WorktreeBranchTemplate: r.WorktreeBranchTemplate, - WorktreeBranchTicket: r.WorktreeBranchTicket, - PullBeforeWorktree: r.PullBeforeWorktree, - RemoteSyncHandled: r.RemoteSyncHandled, - RepoSetupScript: r.RepoSetupScript, - BranchSlug: r.BranchSlug, - BranchIdentitySlug: r.BranchIdentitySlug, + RepositoryID: r.RepositoryID, + RepositoryPath: r.RepositoryPath, + RepoName: r.RepoName, + BaseBranch: r.BaseBranch, + DefaultBranch: r.DefaultBranch, + CheckoutBranch: r.CheckoutBranch, + PRNumber: r.PRNumber, + RemoteContribution: r.RemoteContribution, + WorktreeID: r.WorktreeID, + WorktreeBranch: r.WorktreeBranch, + WorktreeBranchPrefix: r.WorktreeBranchPrefix, + WorktreeBranchTemplate: r.WorktreeBranchTemplate, + WorktreeBranchTicket: r.WorktreeBranchTicket, + PullBeforeWorktree: r.PullBeforeWorktree, + RemoteSyncHandled: r.RemoteSyncHandled, + RepoSetupScript: r.RepoSetupScript, + BranchSlug: r.BranchSlug, + BranchIdentitySlug: r.BranchIdentitySlug, + ContributionDestination: r.ContributionDestination, }} } diff --git a/apps/backend/internal/agent/runtime/lifecycle/env_preparer_local.go b/apps/backend/internal/agent/runtime/lifecycle/env_preparer_local.go index c5fffd089f..a6cb5e727d 100644 --- a/apps/backend/internal/agent/runtime/lifecycle/env_preparer_local.go +++ b/apps/backend/internal/agent/runtime/lifecycle/env_preparer_local.go @@ -60,7 +60,10 @@ func (p *LocalPreparer) Prepare(ctx context.Context, req *EnvPrepareRequest, onP if workspacePath == "" { workspacePath = req.RepositoryPath } - resolvedScript := resolvePreparerSetupScript(req, workspacePath) + resolvedScript, err := resolvePreparerSetupScript(req, workspacePath) + if err != nil { + return nil, fmt.Errorf("resolve setup script: %w", err) + } // CheckoutBranch (PR head) takes priority over BaseBranch when both set. effectiveBranch := req.CheckoutBranch diff --git a/apps/backend/internal/agent/runtime/lifecycle/env_preparer_worktree.go b/apps/backend/internal/agent/runtime/lifecycle/env_preparer_worktree.go index 598b67d776..c0e1b0db07 100644 --- a/apps/backend/internal/agent/runtime/lifecycle/env_preparer_worktree.go +++ b/apps/backend/internal/agent/runtime/lifecycle/env_preparer_worktree.go @@ -121,7 +121,10 @@ func (p *WorktreePreparer) Prepare(ctx context.Context, req *EnvPrepareRequest, // isScriptEffectivelyEmpty. scriptReq := *req // shallow copy — Env map is shared (read-only in runSetupScriptStep) scriptReq.RepoSetupScript = "" - resolvedScript := resolvePreparerSetupScript(&scriptReq, workspacePath) + resolvedScript, err := resolvePreparerSetupScript(&scriptReq, workspacePath) + if err != nil { + return nil, fmt.Errorf("resolve setup script: %w", err) + } if resolvedScript != "" { totalSteps++ steps = runSetupScriptStep(ctx, &scriptReq, workspacePath, resolvedScript, stepIdx, totalSteps, onProgress, steps, p.logger) @@ -266,27 +269,28 @@ func (p *WorktreePreparer) createWorktreeWithSync( // manager's CreateRequest. Progress callbacks are wired by the caller. func buildWorktreeCreateRequest(req *EnvPrepareRequest) worktree.CreateRequest { return worktree.CreateRequest{ - TaskID: req.TaskID, - WorkspaceID: req.WorkspaceID, - SessionID: req.SessionID, - TaskTitle: req.TaskTitle, - RepositoryID: req.RepositoryID, - RepositoryPath: req.RepositoryPath, - BaseBranch: req.BaseBranch, - FallbackBaseBranch: req.DefaultBranch, - CheckoutBranch: req.CheckoutBranch, - PRNumber: req.PRNumber, - RemoteContribution: req.RemoteContribution, - WorktreeBranchPrefix: req.WorktreeBranchPrefix, - WorktreeBranchTemplate: req.WorktreeBranchTemplate, - WorktreeBranchTicket: req.WorktreeBranchTicket, - PullBeforeWorktree: req.PullBeforeWorktree, - RemoteSyncHandled: req.RemoteSyncHandled, - WorktreeID: req.WorktreeID, - TaskDirName: req.TaskDirName, - RepoName: req.RepoName, - BranchSlug: req.BranchSlug, - BranchIdentitySlug: req.BranchIdentitySlug, + TaskID: req.TaskID, + WorkspaceID: req.WorkspaceID, + SessionID: req.SessionID, + TaskTitle: req.TaskTitle, + RepositoryID: req.RepositoryID, + RepositoryPath: req.RepositoryPath, + BaseBranch: req.BaseBranch, + FallbackBaseBranch: req.DefaultBranch, + CheckoutBranch: req.CheckoutBranch, + PRNumber: req.PRNumber, + RemoteContribution: req.RemoteContribution, + WorktreeBranchPrefix: req.WorktreeBranchPrefix, + WorktreeBranchTemplate: req.WorktreeBranchTemplate, + WorktreeBranchTicket: req.WorktreeBranchTicket, + PullBeforeWorktree: req.PullBeforeWorktree, + RemoteSyncHandled: req.RemoteSyncHandled, + WorktreeID: req.WorktreeID, + TaskDirName: req.TaskDirName, + RepoName: req.RepoName, + BranchSlug: req.BranchSlug, + BranchIdentitySlug: req.BranchIdentitySlug, + ContributionDestination: req.ContributionDestination, // Export resolved executor-profile env vars into the repository setup // script so tokens (e.g. an npm auth token) are available during // install. Set for both single-repo and multi-repo launches, which both @@ -488,6 +492,7 @@ func (p *WorktreePreparer) prepareOneRepo( subReq.CheckoutBranch = spec.CheckoutBranch subReq.PRNumber = spec.PRNumber subReq.RemoteContribution = spec.RemoteContribution + subReq.ContributionDestination = spec.ContributionDestination subReq.WorktreeID = spec.WorktreeID subReq.WorktreeBranchPrefix = spec.WorktreeBranchPrefix subReq.WorktreeBranchTemplate = spec.WorktreeBranchTemplate diff --git a/apps/backend/internal/agent/runtime/lifecycle/executor_backend.go b/apps/backend/internal/agent/runtime/lifecycle/executor_backend.go index 8e9d95fe29..21d430130a 100644 --- a/apps/backend/internal/agent/runtime/lifecycle/executor_backend.go +++ b/apps/backend/internal/agent/runtime/lifecycle/executor_backend.go @@ -54,6 +54,42 @@ func validateRemoteContributions(values map[string]models.RemoteContribution) (m return validated, nil } +func contributionDestinationsFromMetadata(metadata map[string]interface{}) (map[string]models.ContributionDestination, error) { + if metadata == nil { + return nil, nil + } + raw, ok := metadata[MetadataKeyContributionDestinations] + if !ok || raw == nil { + return nil, nil + } + if typed, ok := raw.(map[string]models.ContributionDestination); ok { + return validateContributionDestinations(typed) + } + data, err := json.Marshal(raw) + if err != nil { + return nil, fmt.Errorf("encode contribution destinations: %w", err) + } + var decoded map[string]models.ContributionDestination + if err := json.Unmarshal(data, &decoded); err != nil { + return nil, fmt.Errorf("decode contribution destinations: %w", err) + } + return validateContributionDestinations(decoded) +} + +func validateContributionDestinations(values map[string]models.ContributionDestination) (map[string]models.ContributionDestination, error) { + if len(values) == 0 { + return nil, nil + } + validated := make(map[string]models.ContributionDestination, len(values)) + for key, destination := range values { + if err := destination.Validate(); err != nil { + return nil, fmt.Errorf("validate contribution destination %q: %w", key, err) + } + validated[key] = destination + } + return validated, nil +} + // Runtime abstracts the agent execution environment (Docker, Standalone, K8s, SSH, etc.) // Each runtime is responsible for creating and managing agentctl instances. // Agent subprocess launching is handled separately via agentctl client methods. @@ -112,18 +148,19 @@ const ( // MetadataKeyBaseBranches stores a map[string]string (RepositoryName → // base branch ref) for per-repo diff-stat resolution inside agentctl. // The empty key "" applies to the root / single-repo tracker. - MetadataKeyBaseBranches = "base_branches" - MetadataKeyRemoteContributions = "remote_contributions" - MetadataKeyIsRemote = "is_remote" - MetadataKeyRemoteAuthHome = "remote_auth_target_home" - MetadataKeyGitUserName = "git_user_name" - MetadataKeyGitUserEmail = "git_user_email" - MetadataKeyImageTagOverride = "image_tag_override" - MetadataKeyContainerID = "container_id" - MetadataKeySpriteName = "sprite_name" - MetadataKeySpriteState = "sprite_state" - MetadataKeySpriteCreatedAt = "sprite_created_at" - MetadataKeyLocalPort = "local_port" + MetadataKeyBaseBranches = "base_branches" + MetadataKeyRemoteContributions = "remote_contributions" + MetadataKeyContributionDestinations = "contribution_destinations" + MetadataKeyIsRemote = "is_remote" + MetadataKeyRemoteAuthHome = "remote_auth_target_home" + MetadataKeyGitUserName = "git_user_name" + MetadataKeyGitUserEmail = "git_user_email" + MetadataKeyImageTagOverride = "image_tag_override" + MetadataKeyContainerID = "container_id" + MetadataKeySpriteName = "sprite_name" + MetadataKeySpriteState = "sprite_state" + MetadataKeySpriteCreatedAt = "sprite_created_at" + MetadataKeyLocalPort = "local_port" // MetadataKeyModelOverride holds a user-requested model that overrides the // agent profile's configured model on the next launch. Set by SetSessionModel @@ -190,20 +227,21 @@ var persistentMetadataKeys = map[string]bool{ MetadataKeyIsRemote: true, // Executor profile / auth config - MetadataKeyCleanupScript: true, - MetadataKeyRepoSetupScript: true, - MetadataKeyRemoteAuthHome: true, - MetadataKeyGitUserName: true, - MetadataKeyGitUserEmail: true, - "remote_credentials": true, - "remote_auth_secrets": true, - "executor_mcp_policy": true, - "sprites_network_policy_rules": true, - "executor_profile_id": true, - MetadataKeyImageTagOverride: true, - MetadataKeyContainerID: true, - MetadataKeyWorktreeBranch: true, - MetadataKeyRemoteContributions: true, + MetadataKeyCleanupScript: true, + MetadataKeyRepoSetupScript: true, + MetadataKeyRemoteAuthHome: true, + MetadataKeyGitUserName: true, + MetadataKeyGitUserEmail: true, + "remote_credentials": true, + "remote_auth_secrets": true, + "executor_mcp_policy": true, + "sprites_network_policy_rules": true, + "executor_profile_id": true, + MetadataKeyImageTagOverride: true, + MetadataKeyContainerID: true, + MetadataKeyWorktreeBranch: true, + MetadataKeyRemoteContributions: true, + MetadataKeyContributionDestinations: true, } // persistentMetadataPrefixes lists key prefixes that should persist. @@ -360,15 +398,16 @@ type ExecutorCreateRequest struct { // RemoteContributions carries validated, credential-free bindings to the // runtime/agentctl boundary. Keys use the same workspace subpath convention // as BaseBranches; the empty key is the workspace root. - RemoteContributions map[string]models.RemoteContribution - McpServers []McpServerConfig - AgentConfig agents.Agent // Agent type info needed by runtimes - PreviousExecutionID string // Non-empty when reconnecting to a previous execution - McpMode string // MCP tool mode: "task" (default), "config", or "office" - McpProviders []string // Normalized provider capabilities attached to the task - McpProfile *mcpprofile.Context - AuthToken string // Previously handshaken agentctl token for reconnects - BootstrapNonce string // Stored nonce for re-handshake after container restart + RemoteContributions map[string]models.RemoteContribution + ContributionDestinations map[string]models.ContributionDestination + McpServers []McpServerConfig + AgentConfig agents.Agent // Agent type info needed by runtimes + PreviousExecutionID string // Non-empty when reconnecting to a previous execution + McpMode string // MCP tool mode: "task" (default), "config", or "office" + McpProviders []string // Normalized provider capabilities attached to the task + McpProfile *mcpprofile.Context + AuthToken string // Previously handshaken agentctl token for reconnects + BootstrapNonce string // Stored nonce for re-handshake after container restart // OnProgress is an optional callback for streaming preparation progress. // Executors that perform multi-step setup (e.g. Sprites, remote Docker) can diff --git a/apps/backend/internal/agent/runtime/lifecycle/executor_docker.go b/apps/backend/internal/agent/runtime/lifecycle/executor_docker.go index c8eff5559b..374771a208 100644 --- a/apps/backend/internal/agent/runtime/lifecycle/executor_docker.go +++ b/apps/backend/internal/agent/runtime/lifecycle/executor_docker.go @@ -166,6 +166,9 @@ func (r *DockerExecutor) CreateInstance(ctx context.Context, req *ExecutorCreate if _, err := validateRemoteContributions(req.RemoteContributions); err != nil { return nil, err } + if _, err := validateContributionDestinations(req.ContributionDestinations); err != nil { + return nil, err + } dockerClient, containerMgr, err := r.ensureClient() if err != nil { return nil, fmt.Errorf("docker unavailable: %w", err) @@ -274,6 +277,7 @@ func (r *DockerExecutor) buildContainerLaunchConfig(req *ExecutorCreateRequest) LocalClonePath: localCloneMountPath(req.Metadata), BaseBranches: getMetadataStringMap(req.Metadata, MetadataKeyBaseBranches), RemoteContributions: req.RemoteContributions, + ContributionDestinations: req.ContributionDestinations, }, nil } @@ -560,20 +564,21 @@ func buildReconnectCreateInstanceRequest(req *ExecutorCreateRequest, instanceID req.AutoApprovePermissions, req.AutoApprovePermissionsOverride, ), - AutoStart: false, - McpServers: req.McpServers, - McpProviders: req.McpProviders, - McpProfile: req.McpProfile, - SessionID: req.SessionID, - TaskID: req.TaskID, - DisableAskQuestion: disableAskQuestion, - AssumeMcpSse: assumeMcpSse, - AssumeMcpHttp: assumeMcpHttp, - McpMode: req.McpMode, - RequiresProcessKill: requiresProcessKill, - StripEnv: stripEnv, - BaseBranches: getMetadataStringMap(req.Metadata, MetadataKeyBaseBranches), - RemoteContributions: req.RemoteContributions, + AutoStart: false, + McpServers: req.McpServers, + McpProviders: req.McpProviders, + McpProfile: req.McpProfile, + SessionID: req.SessionID, + TaskID: req.TaskID, + DisableAskQuestion: disableAskQuestion, + AssumeMcpSse: assumeMcpSse, + AssumeMcpHttp: assumeMcpHttp, + McpMode: req.McpMode, + RequiresProcessKill: requiresProcessKill, + StripEnv: stripEnv, + BaseBranches: getMetadataStringMap(req.Metadata, MetadataKeyBaseBranches), + RemoteContributions: req.RemoteContributions, + ContributionDestinations: req.ContributionDestinations, } } @@ -796,6 +801,13 @@ func (r *DockerExecutor) resolvePrepareScript(req *ExecutorCreateRequest) (strin } script += contributionScript } + if destination, ok := req.ContributionDestinations[""]; ok { + destinationScript, err := scriptengine.ContributionDestinationSetupScript(&destination) + if err != nil { + return "", err + } + script += destinationScript + } resolver := scriptengine.NewResolver(). WithProvider(scriptengine.WorkspaceProvider(dockerWorkspacePath)). diff --git a/apps/backend/internal/agent/runtime/lifecycle/executor_sprites.go b/apps/backend/internal/agent/runtime/lifecycle/executor_sprites.go index d92b44e238..e33fbc1629 100644 --- a/apps/backend/internal/agent/runtime/lifecycle/executor_sprites.go +++ b/apps/backend/internal/agent/runtime/lifecycle/executor_sprites.go @@ -125,6 +125,9 @@ func (r *SpritesExecutor) CreateInstance(ctx context.Context, req *ExecutorCreat if _, err := validateRemoteContributions(req.RemoteContributions); err != nil { return nil, err } + if _, err := validateContributionDestinations(req.ContributionDestinations); err != nil { + return nil, err + } token := req.Env["SPRITES_API_TOKEN"] if token == "" { return nil, fmt.Errorf("SPRITES_API_TOKEN not set in execution environment (configure it in the executor profile)") diff --git a/apps/backend/internal/agent/runtime/lifecycle/executor_sprites_operations.go b/apps/backend/internal/agent/runtime/lifecycle/executor_sprites_operations.go index f5b1f04fee..6170163f25 100644 --- a/apps/backend/internal/agent/runtime/lifecycle/executor_sprites_operations.go +++ b/apps/backend/internal/agent/runtime/lifecycle/executor_sprites_operations.go @@ -312,6 +312,13 @@ func (r *SpritesExecutor) resolvePrepareScript(req *ExecutorCreateRequest) (stri } script += contributionScript } + if destination, ok := req.ContributionDestinations[""]; ok { + destinationScript, err := scriptengine.ContributionDestinationSetupScript(&destination) + if err != nil { + return "", err + } + script += destinationScript + } installScripts := r.collectAgentInstallScripts(req) @@ -442,15 +449,16 @@ func spriteCreateInstanceRequest(req *ExecutorCreateRequest) agentctl.CreateInst req.AutoApprovePermissions, req.AutoApprovePermissionsOverride, ), - McpServers: req.McpServers, - McpMode: req.McpMode, - McpProviders: req.McpProviders, - McpProfile: req.McpProfile, - RequiresProcessKill: requiresProcessKillFromReq(req), - StripEnv: stripEnvFromReq(req), - BaseBranches: getMetadataStringMap(req.Metadata, MetadataKeyBaseBranches), - RemoteContributions: req.RemoteContributions, - Env: cloneStringMap(req.Env), + McpServers: req.McpServers, + McpMode: req.McpMode, + McpProviders: req.McpProviders, + McpProfile: req.McpProfile, + RequiresProcessKill: requiresProcessKillFromReq(req), + StripEnv: stripEnvFromReq(req), + BaseBranches: getMetadataStringMap(req.Metadata, MetadataKeyBaseBranches), + RemoteContributions: req.RemoteContributions, + ContributionDestinations: req.ContributionDestinations, + Env: cloneStringMap(req.Env), } } diff --git a/apps/backend/internal/agent/runtime/lifecycle/executor_ssh.go b/apps/backend/internal/agent/runtime/lifecycle/executor_ssh.go index c60b83d43f..b5c3b0d954 100644 --- a/apps/backend/internal/agent/runtime/lifecycle/executor_ssh.go +++ b/apps/backend/internal/agent/runtime/lifecycle/executor_ssh.go @@ -199,6 +199,9 @@ func (r *SSHExecutor) CreateInstance(ctx context.Context, req *ExecutorCreateReq if _, err := validateRemoteContributions(req.RemoteContributions); err != nil { return nil, fmt.Errorf("ssh: validate remote contributions: %w", err) } + if _, err := validateContributionDestinations(req.ContributionDestinations); err != nil { + return nil, fmt.Errorf("ssh: validate contribution destinations: %w", err) + } target, err := r.targetFromMetadata(req.Metadata) if err != nil { diff --git a/apps/backend/internal/agent/runtime/lifecycle/executor_ssh_operations.go b/apps/backend/internal/agent/runtime/lifecycle/executor_ssh_operations.go index e447f2f7a6..bd95fe635f 100644 --- a/apps/backend/internal/agent/runtime/lifecycle/executor_ssh_operations.go +++ b/apps/backend/internal/agent/runtime/lifecycle/executor_ssh_operations.go @@ -629,15 +629,16 @@ func buildSSHCreateInstanceRequest( req.AutoApprovePermissions, req.AutoApprovePermissionsOverride, ), - McpServers: req.McpServers, - McpMode: req.McpMode, - McpProviders: req.McpProviders, - McpProfile: req.McpProfile, - RequiresProcessKill: requiresProcessKillFromReq(req), - StripEnv: stripEnvFromReq(req), - BaseBranches: getMetadataStringMap(req.Metadata, MetadataKeyBaseBranches), - RemoteContributions: req.RemoteContributions, - Env: sshRemoteContributionEnv(req, agentctlBin), + McpServers: req.McpServers, + McpMode: req.McpMode, + McpProviders: req.McpProviders, + McpProfile: req.McpProfile, + RequiresProcessKill: requiresProcessKillFromReq(req), + StripEnv: stripEnvFromReq(req), + BaseBranches: getMetadataStringMap(req.Metadata, MetadataKeyBaseBranches), + RemoteContributions: req.RemoteContributions, + ContributionDestinations: req.ContributionDestinations, + Env: sshRemoteContributionEnv(req, agentctlBin), } } diff --git a/apps/backend/internal/agent/runtime/lifecycle/executor_ssh_scripts.go b/apps/backend/internal/agent/runtime/lifecycle/executor_ssh_scripts.go index d8ba81d1df..a04554aba7 100644 --- a/apps/backend/internal/agent/runtime/lifecycle/executor_ssh_scripts.go +++ b/apps/backend/internal/agent/runtime/lifecycle/executor_ssh_scripts.go @@ -60,6 +60,13 @@ func (r *SSHExecutor) resolvePrepareScript(req *ExecutorCreateRequest, workspace } script += "\n" + sshRemoteContributionScript(workspacePath, targetURL, &binding) } + if destination, ok := req.ContributionDestinations[""]; ok { + destinationScript, err := scriptengine.ContributionDestinationSetupScriptAt(&destination, workspacePath) + if err != nil { + return "", err + } + script += "\n" + destinationScript + } return r.resolveSSHScript(req, workspacePath, agentctlBin, script) } diff --git a/apps/backend/internal/agent/runtime/lifecycle/executor_ssh_scripts_test.go b/apps/backend/internal/agent/runtime/lifecycle/executor_ssh_scripts_test.go index 3c703c6ac2..fc4a594215 100644 --- a/apps/backend/internal/agent/runtime/lifecycle/executor_ssh_scripts_test.go +++ b/apps/backend/internal/agent/runtime/lifecycle/executor_ssh_scripts_test.go @@ -11,6 +11,7 @@ import ( "golang.org/x/crypto/ssh" "github.com/kandev/kandev/internal/common/logger" + "github.com/kandev/kandev/internal/task/models" ) func TestSSHResolvePrepareScriptUsesWorkspaceProviders(t *testing.T) { @@ -47,6 +48,33 @@ func TestSSHResolvePrepareScriptUsesWorkspaceProviders(t *testing.T) { } } +func TestSSHResolvePrepareScriptUsesRemoteWorkspaceForContributionDestination(t *testing.T) { + destination := models.ContributionDestination{ + Version: models.ContributionDestinationVersion, + Provider: models.ContributionDestinationProviderGitHub, + SourceRepository: models.ContributionDestinationRepository{ + Host: "github.com", Path: "kdlbs/kandev", ProviderID: "100", RemoteURL: "https://github.com/kdlbs/kandev.git", + }, + TargetRepository: models.ContributionDestinationRepository{ + Host: "github.com", Path: "alice/kandev", ProviderID: "200", RemoteURL: "https://github.com/alice/kandev.git", + }, + } + + script, err := (&SSHExecutor{}).resolvePrepareScript(&ExecutorCreateRequest{ + Metadata: map[string]interface{}{"repository_clone_url": "https://github.com/kdlbs/kandev.git"}, + ContributionDestinations: map[string]models.ContributionDestination{"": destination}, + }, "/remote/task", "/remote/bin/agentctl") + if err != nil { + t.Fatalf("resolvePrepareScript() error = %v", err) + } + if !strings.Contains(script, "cd '/remote/task'") { + t.Fatalf("destination setup did not use remote workspace:\n%s", script) + } + if strings.Contains(script, "cd '/workspace'") { + t.Fatalf("destination setup used the local default workspace:\n%s", script) + } +} + func TestSSHDefaultPrepareScriptExecutesCheckoutAndSetup(t *testing.T) { if _, err := exec.LookPath("git"); err != nil { t.Skip("git not available") diff --git a/apps/backend/internal/agent/runtime/lifecycle/executor_standalone.go b/apps/backend/internal/agent/runtime/lifecycle/executor_standalone.go index 5169cc9baf..f562fb7258 100644 --- a/apps/backend/internal/agent/runtime/lifecycle/executor_standalone.go +++ b/apps/backend/internal/agent/runtime/lifecycle/executor_standalone.go @@ -100,21 +100,22 @@ func buildStandaloneCreateInstanceRequest( req.AutoApprovePermissions, req.AutoApprovePermissionsOverride, ), - AutoStart: false, - McpServers: req.McpServers, - SessionID: req.SessionID, - TaskID: req.TaskID, - DisableAskQuestion: disableAskQuestion, - AssumeMcpSse: assumeMcpSse, - AssumeMcpHttp: assumeMcpHttp, - McpMode: req.McpMode, - McpProviders: req.McpProviders, - McpProfile: req.McpProfile, - RequiresProcessKill: requiresProcessKill, - StripEnv: stripEnv, - BaseBranches: getMetadataStringMap(req.Metadata, MetadataKeyBaseBranches), - RemoteContributions: req.RemoteContributions, - WorkspaceSourceRoots: req.WorkspaceSourceRoots, + AutoStart: false, + McpServers: req.McpServers, + SessionID: req.SessionID, + TaskID: req.TaskID, + DisableAskQuestion: disableAskQuestion, + AssumeMcpSse: assumeMcpSse, + AssumeMcpHttp: assumeMcpHttp, + McpMode: req.McpMode, + McpProviders: req.McpProviders, + McpProfile: req.McpProfile, + RequiresProcessKill: requiresProcessKill, + StripEnv: stripEnv, + BaseBranches: getMetadataStringMap(req.Metadata, MetadataKeyBaseBranches), + RemoteContributions: req.RemoteContributions, + ContributionDestinations: req.ContributionDestinations, + WorkspaceSourceRoots: req.WorkspaceSourceRoots, } } diff --git a/apps/backend/internal/agent/runtime/lifecycle/manager_execution.go b/apps/backend/internal/agent/runtime/lifecycle/manager_execution.go index 5ec47825be..4b06a84848 100644 --- a/apps/backend/internal/agent/runtime/lifecycle/manager_execution.go +++ b/apps/backend/internal/agent/runtime/lifecycle/manager_execution.go @@ -685,6 +685,10 @@ func (m *Manager) prepareExecutionCreateRequest( if err != nil { return nil, err } + contributionDestinations, err := contributionDestinationsFromMetadata(metadata) + if err != nil { + return nil, err + } autoApprove := false var autoApproveOverride *bool if profileInfo != nil { @@ -713,6 +717,7 @@ func (m *Manager) prepareExecutionCreateRequest( AuthToken: m.revealRuntimeSecret(ctx, info.Metadata, MetadataKeyAuthTokenSecret), BootstrapNonce: m.revealRuntimeSecret(ctx, info.Metadata, MetadataKeyBootstrapNonceSecret), RemoteContributions: remoteContributions, + ContributionDestinations: contributionDestinations, }, profileInfo: profileInfo, }, nil diff --git a/apps/backend/internal/agent/runtime/lifecycle/manager_launch.go b/apps/backend/internal/agent/runtime/lifecycle/manager_launch.go index 2d7bf2654b..4c5cf11445 100644 --- a/apps/backend/internal/agent/runtime/lifecycle/manager_launch.go +++ b/apps/backend/internal/agent/runtime/lifecycle/manager_launch.go @@ -219,6 +219,55 @@ func collectRemoteContributions(req *LaunchRequest) (map[string]models.RemoteCon return bindings, nil } +// collectContributionDestinations projects server-authored managed fork +// destinations using the same workspace-subpath keys as remote contributions. +func collectContributionDestinations(req *LaunchRequest) (map[string]models.ContributionDestination, error) { + if req == nil { + return nil, nil + } + specs := req.RepoSpecs() + if len(specs) == 0 { + if req.ContributionDestination == nil { + return nil, nil + } + if err := req.ContributionDestination.Validate(); err != nil { + return nil, fmt.Errorf("validate contribution destination: %w", err) + } + return map[string]models.ContributionDestination{"": *req.ContributionDestination}, nil + } + destinations := make(map[string]models.ContributionDestination) + for index, spec := range specs { + if spec.ContributionDestination == nil { + continue + } + if err := spec.ContributionDestination.Validate(); err != nil { + return nil, fmt.Errorf("validate contribution destination for repository %q: %w", spec.RepoName, err) + } + key := "" + if index > 0 { + key = baseBranchMetadataKey(spec) + } + if existing, ok := destinations[key]; ok { + if !sameContributionDestinationTarget(existing, *spec.ContributionDestination) { + return nil, fmt.Errorf("multiple contribution destinations target workspace repository %q", key) + } + continue + } + destinations[key] = *spec.ContributionDestination + } + if len(destinations) == 0 { + return nil, nil + } + return destinations, nil +} + +func sameContributionDestinationTarget(left, right models.ContributionDestination) bool { + return strings.EqualFold(left.TargetRepository.Host, right.TargetRepository.Host) && + left.TargetRepository.Path == right.TargetRepository.Path && + left.TargetRepository.ProviderID == right.TargetRepository.ProviderID && + left.TargetRepository.RemoteURL == right.TargetRepository.RemoteURL +} + // collectBaseBranches builds the per-repo {RepositoryName → base_branch} // map that agentctl reads to scope diff stats. Single-repo legacy launches // are recorded under the empty key "" so single-repo trackers (which have @@ -715,6 +764,13 @@ func (m *Manager) launchBuildExecutorRequest(ctx context.Context, executionID st if len(remoteContributions) > 0 { metadata[MetadataKeyRemoteContributions] = remoteContributions } + contributionDestinations, err := collectContributionDestinations(reqWithWorktree) + if err != nil { + return nil, nil, nil, err + } + if len(contributionDestinations) > 0 { + metadata[MetadataKeyContributionDestinations] = contributionDestinations + } var autoApproveOverride *bool if profileInfo != nil { @@ -747,6 +803,7 @@ func (m *Manager) launchBuildExecutorRequest(ctx context.Context, executionID st BootstrapNonce: m.revealRuntimeSecret(ctx, metadata, MetadataKeyBootstrapNonceSecret), OnProgress: onProgress, RemoteContributions: remoteContributions, + ContributionDestinations: contributionDestinations, } launchCtx, launchCancel := withLaunchPhaseTimeout(ctx) @@ -847,34 +904,35 @@ func (m *Manager) runEnvironmentPreparerWithProgress( func buildEnvPrepareRequest(req *LaunchRequest, workspacePath string, execName executor.Name) *EnvPrepareRequest { repoSetupScript, _ := req.Metadata[MetadataKeyRepoSetupScript].(string) prepReq := &EnvPrepareRequest{ - TaskID: req.TaskID, - WorkspaceID: req.WorkspaceID, - SessionID: req.SessionID, - TaskTitle: req.TaskTitle, - ExecutorType: execName, - WorkspacePath: workspacePath, - RepositoryPath: req.RepositoryPath, - RepositoryID: req.RepositoryID, - UseWorktree: req.UseWorktree, - WorktreeID: req.WorktreeID, - SetupScript: req.SetupScript, - RepoSetupScript: repoSetupScript, - BaseBranch: req.BaseBranch, - DefaultBranch: req.DefaultBranch, - CheckoutBranch: req.CheckoutBranch, - PRNumber: req.PRNumber, - RemoteContribution: req.RemoteContribution, - WorktreeBranch: getMetadataString(req.Metadata, MetadataKeyWorktreeBranch), - WorktreeBranchPrefix: req.WorktreeBranchPrefix, - WorktreeBranchTemplate: req.WorktreeBranchTemplate, - WorktreeBranchTicket: req.WorktreeBranchTicket, - PullBeforeWorktree: req.PullBeforeWorktree, - RemoteSyncHandled: req.RemoteSyncHandled, - TaskDirName: req.TaskDirName, - RepoName: req.RepoName, - BranchSlug: req.BranchSlug, - BranchIdentitySlug: req.BranchIdentitySlug, - Env: req.Env, + TaskID: req.TaskID, + WorkspaceID: req.WorkspaceID, + SessionID: req.SessionID, + TaskTitle: req.TaskTitle, + ExecutorType: execName, + WorkspacePath: workspacePath, + RepositoryPath: req.RepositoryPath, + RepositoryID: req.RepositoryID, + UseWorktree: req.UseWorktree, + WorktreeID: req.WorktreeID, + SetupScript: req.SetupScript, + RepoSetupScript: repoSetupScript, + BaseBranch: req.BaseBranch, + DefaultBranch: req.DefaultBranch, + CheckoutBranch: req.CheckoutBranch, + PRNumber: req.PRNumber, + RemoteContribution: req.RemoteContribution, + ContributionDestination: req.ContributionDestination, + WorktreeBranch: getMetadataString(req.Metadata, MetadataKeyWorktreeBranch), + WorktreeBranchPrefix: req.WorktreeBranchPrefix, + WorktreeBranchTemplate: req.WorktreeBranchTemplate, + WorktreeBranchTicket: req.WorktreeBranchTicket, + PullBeforeWorktree: req.PullBeforeWorktree, + RemoteSyncHandled: req.RemoteSyncHandled, + TaskDirName: req.TaskDirName, + RepoName: req.RepoName, + BranchSlug: req.BranchSlug, + BranchIdentitySlug: req.BranchIdentitySlug, + Env: req.Env, } // Multi-repo: forward the repo list when the launch request carries one. // Each per-repo entry inherits the request-level RepoSetupScript when its @@ -887,23 +945,24 @@ func buildEnvPrepareRequest(req *LaunchRequest, workspacePath string, execName e setup = repoSetupScript } specs = append(specs, RepoPrepareSpec{ - RepositoryID: r.RepositoryID, - RepositoryPath: r.RepositoryPath, - RepoName: r.RepoName, - BaseBranch: r.BaseBranch, - DefaultBranch: r.DefaultBranch, - CheckoutBranch: r.CheckoutBranch, - PRNumber: r.PRNumber, - RemoteContribution: r.RemoteContribution, - WorktreeID: r.WorktreeID, - WorktreeBranchPrefix: r.WorktreeBranchPrefix, - WorktreeBranchTemplate: r.WorktreeBranchTemplate, - WorktreeBranchTicket: r.WorktreeBranchTicket, - PullBeforeWorktree: r.PullBeforeWorktree, - RemoteSyncHandled: r.RemoteSyncHandled, - RepoSetupScript: setup, - BranchSlug: r.BranchSlug, - BranchIdentitySlug: r.BranchIdentitySlug, + RepositoryID: r.RepositoryID, + RepositoryPath: r.RepositoryPath, + RepoName: r.RepoName, + BaseBranch: r.BaseBranch, + DefaultBranch: r.DefaultBranch, + CheckoutBranch: r.CheckoutBranch, + PRNumber: r.PRNumber, + RemoteContribution: r.RemoteContribution, + WorktreeID: r.WorktreeID, + WorktreeBranchPrefix: r.WorktreeBranchPrefix, + WorktreeBranchTemplate: r.WorktreeBranchTemplate, + WorktreeBranchTicket: r.WorktreeBranchTicket, + PullBeforeWorktree: r.PullBeforeWorktree, + RemoteSyncHandled: r.RemoteSyncHandled, + RepoSetupScript: setup, + BranchSlug: r.BranchSlug, + BranchIdentitySlug: r.BranchIdentitySlug, + ContributionDestination: r.ContributionDestination, }) } prepReq.Repositories = specs 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 492de496bb..5cd6718516 100644 --- a/apps/backend/internal/agent/runtime/lifecycle/manager_launch_test.go +++ b/apps/backend/internal/agent/runtime/lifecycle/manager_launch_test.go @@ -298,6 +298,34 @@ func TestBuildAgentCommand_CommandPrefix(t *testing.T) { }) } +func TestCollectContributionDestinationsRejectsDistinctIdentitiesWithSamePath(t *testing.T) { + first := lifecycleTestContributionDestination("200") + second := lifecycleTestContributionDestination("201") + req := &LaunchRequest{Repositories: []RepoLaunchSpec{ + {RepoName: "primary"}, + {RepoName: "shared", BaseBranch: "main", ContributionDestination: &first}, + {RepoName: "shared", BaseBranch: "main", ContributionDestination: &second}, + }} + + _, err := collectContributionDestinations(req) + if err == nil { + t.Fatal("collectContributionDestinations accepted distinct target identities with the same path") + } +} + +func lifecycleTestContributionDestination(providerID string) models.ContributionDestination { + return models.ContributionDestination{ + Version: models.ContributionDestinationVersion, + Provider: models.ContributionDestinationProviderGitHub, + SourceRepository: models.ContributionDestinationRepository{ + Host: "github.com", Path: "kdlbs/kandev", ProviderID: "100", RemoteURL: "https://github.com/kdlbs/kandev.git", + }, + TargetRepository: models.ContributionDestinationRepository{ + Host: "github.com", Path: "alice/kandev", ProviderID: providerID, RemoteURL: "https://github.com/alice/kandev.git", + }, + } +} + func TestBuildAgentCommand_RejectsInvalidBuiltArgv(t *testing.T) { mgr := newTestManager(t) tests := []struct { diff --git a/apps/backend/internal/agent/runtime/lifecycle/manager_passthrough.go b/apps/backend/internal/agent/runtime/lifecycle/manager_passthrough.go index a0cfd28097..dd782774bd 100644 --- a/apps/backend/internal/agent/runtime/lifecycle/manager_passthrough.go +++ b/apps/backend/internal/agent/runtime/lifecycle/manager_passthrough.go @@ -2,6 +2,7 @@ package lifecycle import ( "context" + "errors" "fmt" "maps" "os" @@ -22,6 +23,8 @@ import ( v1 "github.com/kandev/kandev/pkg/api/v1" ) +var errPassthroughProcessReplaced = errors.New("passthrough process was replaced") + // MarkPassthroughRunning marks a passthrough execution as running when user submits input. // This is called when Enter key is detected in the terminal handler. // It updates the execution status and publishes an AgentRunning event. @@ -743,9 +746,11 @@ func (m *Manager) startPassthroughSession(ctx context.Context, execution *AgentE return err } + execution.passthroughLifecycleMu.Lock() execution.PassthroughProcessID = processInfo.ID execution.PassthroughStartedAt = time.Now() execution.passthroughLaunchUsedResume = false + execution.passthroughLifecycleMu.Unlock() m.logger.Info("passthrough session started", zap.String("execution_id", execution.ID), @@ -852,6 +857,9 @@ func (m *Manager) resumePassthroughCommand(ctx context.Context, execution *Agent // without --resume, effectively clearing the agent's conversation context. // The workflow step prompt is delivered afterwards via stdin (autoStartPassthroughPrompt). func (m *Manager) restartPassthroughProcess(ctx context.Context, execution *AgentExecution) error { + execution.passthroughLifecycleMu.Lock() + defer execution.passthroughLifecycleMu.Unlock() + m.logger.Info("restarting passthrough process for context reset", zap.String("execution_id", execution.ID), zap.String("session_id", execution.SessionID), @@ -920,11 +928,33 @@ func (m *Manager) restartPassthroughProcess(ctx context.Context, execution *Agen // If the agent supports resume, it uses the resume flag to continue the last conversation. // Otherwise, it starts a fresh CLI session with the same profile settings. func (m *Manager) ResumePassthroughSession(ctx context.Context, sessionID string) error { + return m.resumePassthroughSession(ctx, sessionID, "") +} + +func (m *Manager) passthroughProcessMatches(execution *AgentExecution, processID string) bool { + if execution == nil || processID == "" { + return false + } + execution.passthroughLifecycleMu.Lock() + defer execution.passthroughLifecycleMu.Unlock() + return execution.PassthroughProcessID == processID +} + +// resumePassthroughSession is the shared resume path for user reconnects and +// delayed exit recovery. expectedProcessID is set by exit recovery so an old +// callback cannot replace a process installed by a workflow reset. +func (m *Manager) resumePassthroughSession(ctx context.Context, sessionID, expectedProcessID string) error { execution, exists := m.executionStore.GetBySessionID(sessionID) if !exists { return fmt.Errorf("%w: %s", ErrNoExecutionForSession, sessionID) } + execution.passthroughLifecycleMu.Lock() + defer execution.passthroughLifecycleMu.Unlock() + if expectedProcessID != "" && execution.PassthroughProcessID != expectedProcessID { + return errPassthroughProcessReplaced + } + resolved, err := m.resolvePassthroughAgent(ctx, execution) if err != nil { return err @@ -1147,6 +1177,13 @@ func (m *Manager) handlePassthroughExit(execution *AgentExecution, status *agent return } + if !m.passthroughProcessMatches(execution, status.ProcessID) { + m.logger.Debug("skipping stale passthrough auto-restart", + zap.String("session_id", sessionID), + zap.String("exited_process_id", status.ProcessID)) + return + } + // Wait a bit for the old process to be cleaned up from the process map time.Sleep(cleanupDelay) @@ -1159,6 +1196,13 @@ func (m *Manager) handlePassthroughExit(execution *AgentExecution, status *agent return } + if !m.passthroughProcessMatches(execution, status.ProcessID) { + m.logger.Debug("skipping stale passthrough auto-restart after cleanup", + zap.String("session_id", sessionID), + zap.String("exited_process_id", status.ProcessID)) + return + } + interactiveRunner := m.GetInteractiveRunner() if interactiveRunner == nil { m.logger.Debug("no interactive runner available for auto-restart", @@ -1197,14 +1241,14 @@ func (m *Manager) handlePassthroughExit(execution *AgentExecution, status *agent // Resume-failed flags have already been flipped synchronously in // handlePassthroughStatus so any concurrent WS reconnect that // races this goroutine sees the new values immediately. - m.attemptResumeFallback(execution, interactiveRunner, sessionID, exitCode, uptime) + m.attemptResumeFallbackForProcess(execution, interactiveRunner, sessionID, status.ProcessID, exitCode, uptime) return } m.notifyFastFailExit(interactiveRunner, sessionID, uptime, exitCode, fastFailWindow) return } - m.attemptPassthroughRestart(execution, interactiveRunner, sessionID, exitCode, restartDelay) + m.attemptPassthroughRestartForProcess(execution, interactiveRunner, sessionID, status.ProcessID, exitCode, restartDelay) } // attemptResumeFallback recovers from a fast-failed resume launch by relaunching @@ -1215,6 +1259,19 @@ func (m *Manager) handlePassthroughExit(execution *AgentExecution, status *agent // continued failure we surface the existing red banner so they can fix their // profile. func (m *Manager) attemptResumeFallback(execution *AgentExecution, runner *process.InteractiveRunner, sessionID string, exitCode int, uptime time.Duration) { + m.attemptResumeFallbackForProcess(execution, runner, sessionID, "", exitCode, uptime) +} + +func (m *Manager) attemptResumeFallbackForProcess(execution *AgentExecution, runner *process.InteractiveRunner, sessionID, expectedProcessID string, exitCode int, uptime time.Duration) { + execution.passthroughLifecycleMu.Lock() + defer execution.passthroughLifecycleMu.Unlock() + if expectedProcessID != "" && execution.PassthroughProcessID != expectedProcessID { + m.logger.Debug("skipping stale passthrough resume fallback", + zap.String("session_id", sessionID), + zap.String("exited_process_id", expectedProcessID)) + return + } + m.logger.Info("passthrough resume launch fast-failed, retrying without resume flag", zap.String("session_id", sessionID), zap.String("execution_id", execution.ID), @@ -1289,6 +1346,17 @@ func (m *Manager) attemptResumeFallback(execution *AgentExecution, runner *proce // restart delay, re-checks shutdown/WebSocket, and resumes the session. // Reconnects the existing WebSocket to the new process on success. func (m *Manager) attemptPassthroughRestart(execution *AgentExecution, runner *process.InteractiveRunner, sessionID string, exitCode int, restartDelay time.Duration) { + m.attemptPassthroughRestartForProcess(execution, runner, sessionID, "", exitCode, restartDelay) +} + +func (m *Manager) attemptPassthroughRestartForProcess(execution *AgentExecution, runner *process.InteractiveRunner, sessionID, expectedProcessID string, exitCode int, restartDelay time.Duration) { + if expectedProcessID != "" && !m.passthroughProcessMatches(execution, expectedProcessID) { + m.logger.Debug("skipping stale passthrough restart", + zap.String("session_id", sessionID), + zap.String("exited_process_id", expectedProcessID)) + return + } + m.logger.Info("passthrough process exited with active WebSocket, attempting auto-restart", zap.String("session_id", sessionID), zap.Int("exit_code", exitCode)) @@ -1316,7 +1384,13 @@ func (m *Manager) attemptPassthroughRestart(execution *AgentExecution, runner *p return } - if err := m.ResumePassthroughSession(context.Background(), sessionID); err != nil { + if err := m.resumePassthroughSession(context.Background(), sessionID, expectedProcessID); err != nil { + if errors.Is(err, errPassthroughProcessReplaced) { + m.logger.Debug("skipping stale passthrough restart after process replacement", + zap.String("session_id", sessionID), + zap.String("exited_process_id", expectedProcessID)) + return + } m.logger.Error("failed to auto-restart passthrough session", zap.String("session_id", sessionID), zap.Error(err)) diff --git a/apps/backend/internal/agent/runtime/lifecycle/manager_passthrough_launch_test.go b/apps/backend/internal/agent/runtime/lifecycle/manager_passthrough_launch_test.go index 8ecacc0eb2..9f23194f01 100644 --- a/apps/backend/internal/agent/runtime/lifecycle/manager_passthrough_launch_test.go +++ b/apps/backend/internal/agent/runtime/lifecycle/manager_passthrough_launch_test.go @@ -309,6 +309,7 @@ func TestAttemptPassthroughRestartAbortsWhenTerminalDisconnects(t *testing.T) { func TestHandlePassthroughExitFastFailsResumeLaunch(t *testing.T) { mgr, runner, execution := newPassthroughRunnerManager(t) execution.WorkspacePath = missingWorkspace(t) + execution.PassthroughProcessID = "pty-1" recorder := &terminalRecorder{} runner.TrackSessionWebSocket(execution.SessionID, recorder) @@ -330,6 +331,7 @@ func TestHandlePassthroughExitFastFailsResumeLaunch(t *testing.T) { func TestHandlePassthroughExitFastFailsFreshLaunch(t *testing.T) { mgr, runner, execution := newPassthroughRunnerManager(t) + execution.PassthroughProcessID = "pty-1" recorder := &terminalRecorder{} runner.TrackSessionWebSocket(execution.SessionID, recorder) @@ -372,6 +374,7 @@ func TestHandlePassthroughExitSkipsWithoutTerminal(t *testing.T) { // delay cannot push a genuine fast fail outside the window. func TestHandlePassthroughExitUsesStatusTimestampNotWallClock(t *testing.T) { mgr, runner, execution := newPassthroughRunnerManager(t) + execution.PassthroughProcessID = "pty-1" recorder := &terminalRecorder{} runner.TrackSessionWebSocket(execution.SessionID, recorder) @@ -389,6 +392,26 @@ func TestHandlePassthroughExitUsesStatusTimestampNotWallClock(t *testing.T) { "uptime must be measured from the exit event, not from time.Now()") } +func TestHandlePassthroughExitSkipsReplacedProcess(t *testing.T) { + mgr, runner, execution := newPassthroughRunnerManager(t) + recorder := &terminalRecorder{} + runner.TrackSessionWebSocket(execution.SessionID, recorder) + execution.PassthroughProcessID = "pty-new" + + exitCode := 1 + startedAt := time.Now().Add(-time.Hour) + mgr.handlePassthroughExit(execution, &agentctltypes.ProcessStatusUpdate{ + SessionID: execution.SessionID, + ProcessID: "pty-old", + Status: agentctltypes.ProcessStatusExited, + ExitCode: &exitCode, + Timestamp: time.Now(), + }, startedAt, false) + + require.Empty(t, recorder.text(), "a delayed exit from a replaced process must not restart the active PTY") + require.Equal(t, "pty-new", execution.PassthroughProcessID) +} + // TestStopPassthroughProcessLeavesExecutionStateToItsCaller pins that a failed // runner stop is swallowed AND that this helper never edits the execution: // StopAgentWithReason still needs the process ID to build the ExecutorInstance diff --git a/apps/backend/internal/agent/runtime/lifecycle/preparer_script.go b/apps/backend/internal/agent/runtime/lifecycle/preparer_script.go index bce90b4753..677faa763a 100644 --- a/apps/backend/internal/agent/runtime/lifecycle/preparer_script.go +++ b/apps/backend/internal/agent/runtime/lifecycle/preparer_script.go @@ -2,6 +2,7 @@ package lifecycle import ( "context" + "fmt" "path/filepath" "strings" @@ -16,13 +17,20 @@ import ( // prepareScriptLogger is a package-level logger for setup script resolution. var prepareScriptLogger = logger.Default().WithFields(zap.String("component", "prepare-script")) -func resolvePreparerSetupScript(req *EnvPrepareRequest, workspacePath string) string { +func resolvePreparerSetupScript(req *EnvPrepareRequest, workspacePath string) (string, error) { script := strings.TrimSpace(req.SetupScript) if script == "" { script = defaultPreparerSetupScript(req) } + if req.ContributionDestination != nil { + destinationScript, err := scriptengine.ContributionDestinationSetupScriptAt(req.ContributionDestination, workspacePath) + if err != nil { + return "", fmt.Errorf("resolve contribution destination setup: %w", err) + } + script += destinationScript + } if script == "" { - return "" + return "", nil } metadata := map[string]any{ @@ -60,9 +68,9 @@ func resolvePreparerSetupScript(req *EnvPrepareRequest, workspacePath string) st zap.String("executor_type", string(req.ExecutorType)), zap.Bool("use_worktree", req.UseWorktree), zap.Bool("has_explicit_script", strings.TrimSpace(req.SetupScript) != "")) - return "" + return "", nil } - return resolved + return resolved, nil } // isScriptEffectivelyEmpty returns true when a script contains only a shebang, diff --git a/apps/backend/internal/agent/runtime/lifecycle/preparer_script_test.go b/apps/backend/internal/agent/runtime/lifecycle/preparer_script_test.go index db05700431..31b3a8f3c8 100644 --- a/apps/backend/internal/agent/runtime/lifecycle/preparer_script_test.go +++ b/apps/backend/internal/agent/runtime/lifecycle/preparer_script_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/kandev/kandev/internal/agent/executor" + "github.com/kandev/kandev/internal/task/models" ) func TestResolvePreparerSetupScript_LocalFallbackCommentOnly(t *testing.T) { @@ -13,7 +14,10 @@ func TestResolvePreparerSetupScript_LocalFallbackCommentOnly(t *testing.T) { RepositoryPath: "/tmp/my-repo", } - got := resolvePreparerSetupScript(req, "/tmp/my-repo") + got, err := resolvePreparerSetupScript(req, "/tmp/my-repo") + if err != nil { + t.Fatalf("resolvePreparerSetupScript() error = %v", err) + } if got != "" { t.Fatalf("expected comment-only default script to be treated as empty, got %q", got) } @@ -49,7 +53,10 @@ func TestResolvePreparerSetupScript_LocalWithRepoSetupScript(t *testing.T) { RepoSetupScript: "make install", } - got := resolvePreparerSetupScript(req, "/tmp/my-repo") + got, err := resolvePreparerSetupScript(req, "/tmp/my-repo") + if err != nil { + t.Fatalf("resolvePreparerSetupScript() error = %v", err) + } if got == "" { t.Fatal("expected non-empty script when repo setup script is set") } @@ -66,7 +73,10 @@ func TestResolvePreparerSetupScript_WorktreeWithRepoSetupScript(t *testing.T) { RepoSetupScript: "npm ci", } - got := resolvePreparerSetupScript(req, "/tmp/worktrees/wt-1") + got, err := resolvePreparerSetupScript(req, "/tmp/worktrees/wt-1") + if err != nil { + t.Fatalf("resolvePreparerSetupScript() error = %v", err) + } if got == "" { t.Fatal("expected non-empty script when repo setup script is set") } @@ -82,7 +92,10 @@ func TestResolvePreparerSetupScript_UsesExplicitScript(t *testing.T) { SetupScript: "echo {{repository.path}}", } - got := resolvePreparerSetupScript(req, "/tmp/my-repo") + got, err := resolvePreparerSetupScript(req, "/tmp/my-repo") + if err != nil { + t.Fatalf("resolvePreparerSetupScript() error = %v", err) + } // Data placeholders resolve to a self-contained single-quoted shell token // (security: shellQuote) — functionally identical for echo, safe if the // value ever carried shell metacharacters. @@ -108,7 +121,10 @@ func TestResolvePreparerSetupScript_WorktreePlaceholders(t *testing.T) { }, "\n"), } - got := resolvePreparerSetupScript(req, "/tmp/worktrees/wt-123") + got, err := resolvePreparerSetupScript(req, "/tmp/worktrees/wt-123") + if err != nil { + t.Fatalf("resolvePreparerSetupScript() error = %v", err) + } // Data placeholders resolve to self-contained single-quoted tokens // (shellQuote); worktree.id is a kandev UUID and stays unquoted. expected := []string{ @@ -127,3 +143,15 @@ func TestResolvePreparerSetupScript_WorktreePlaceholders(t *testing.T) { t.Fatalf("expected worktree placeholders to be resolved, got %q", got) } } + +func TestResolvePreparerSetupScriptPropagatesContributionDestinationError(t *testing.T) { + req := &EnvPrepareRequest{ + ExecutorType: executor.NameStandalone, + RepositoryPath: "/tmp/my-repo", + ContributionDestination: &models.ContributionDestination{}, + } + + if _, err := resolvePreparerSetupScript(req, "/tmp/my-repo"); err == nil { + t.Fatal("resolvePreparerSetupScript accepted an invalid contribution destination") + } +} diff --git a/apps/backend/internal/agent/runtime/lifecycle/types.go b/apps/backend/internal/agent/runtime/lifecycle/types.go index 2067871399..812d14c45e 100644 --- a/apps/backend/internal/agent/runtime/lifecycle/types.go +++ b/apps/backend/internal/agent/runtime/lifecycle/types.go @@ -112,8 +112,12 @@ type AgentExecution struct { IsPassthrough bool // Passthrough mode info (CLI passthrough without ACP) - PassthroughProcessID string // Process ID in the interactive runner (empty if not in passthrough mode) - PassthroughStartedAt time.Time // When the current passthrough process was launched; used to detect fast-fail exits and skip auto-restart loops + // passthroughLifecycleMu serializes process replacement paths. A workflow + // context reset and a delayed exit auto-restart must not both replace the + // same PTY, or the execution can point at the wrong process. + passthroughLifecycleMu sync.Mutex + PassthroughProcessID string // Process ID in the interactive runner (empty if not in passthrough mode) + PassthroughStartedAt time.Time // When the current passthrough process was launched; used to detect fast-fail exits and skip auto-restart loops // passthroughLaunchUsedResume is true if the current passthrough process was // launched via ResumePassthroughSession with the resume flag attached. The // fast-fail handler reads this to decide whether to retry once with a fresh @@ -598,24 +602,25 @@ func (ae *AgentExecution) EndSessionSpan() { // the top level. When LaunchRequest.Repositories is set, each entry produces // one prepared worktree under the shared TaskDirName. type RepoLaunchSpec struct { - RepositoryID string - RepositoryPath string - RepositoryURL string // Clone URL for remote executors that need to clone - RepoName string // Repository name used as subdirectory inside TaskDirName - BaseBranch string - DefaultBranch string // Repository's default_branch, used as fallback when BaseBranch is missing - CheckoutBranch string - PRNumber int // GitHub PR number when CheckoutBranch is a PR head; enables refs/pull//head fetch for fork PRs. - RemoteContribution *models.RemoteContribution - WorktreeID string // Existing worktree ID to reuse (skip creation if set) - WorktreeBranchPrefix string - WorktreeBranchTemplate string - WorktreeBranchTicket string - PullBeforeWorktree bool - RemoteSyncHandled bool - RepoSetupScript string // Repository-level setup script (optional) - RepoCleanupScript string // Repository-level cleanup script (optional) - CopyFiles string // Comma-separated paths/globs to copy from the source repo (gitignored .env / config files) + RepositoryID string + RepositoryPath string + RepositoryURL string // Clone URL for remote executors that need to clone + RepoName string // Repository name used as subdirectory inside TaskDirName + BaseBranch string + DefaultBranch string // Repository's default_branch, used as fallback when BaseBranch is missing + CheckoutBranch string + PRNumber int // GitHub PR number when CheckoutBranch is a PR head; enables refs/pull//head fetch for fork PRs. + RemoteContribution *models.RemoteContribution + WorktreeID string // Existing worktree ID to reuse (skip creation if set) + WorktreeBranchPrefix string + WorktreeBranchTemplate string + WorktreeBranchTicket string + PullBeforeWorktree bool + RemoteSyncHandled bool + RepoSetupScript string // Repository-level setup script (optional) + RepoCleanupScript string // Repository-level cleanup script (optional) + CopyFiles string // Comma-separated paths/globs to copy from the source repo (gitignored .env / config files) + ContributionDestination *models.ContributionDestination // BranchSlug, when set, suffixes the worktree directory as // {RepoName}-{BranchSlug} so multi-branch tasks (same repo, multiple // branches) don't collide. @@ -732,20 +737,21 @@ type LaunchRequest struct { CopyFiles string // Worktree configuration - UseWorktree bool // Whether to use a Git worktree for isolation - WorktreeID string // Existing worktree ID to reuse (skip creation if set) - RepositoryID string // Repository ID for worktree tracking - RepositoryPath string // Path to the main repository (for worktree creation) - BaseBranch string // Base branch for the worktree (e.g., "main") - DefaultBranch string // Repository's default_branch, used as fallback when BaseBranch is missing - CheckoutBranch string // Branch to fetch and checkout after worktree creation (e.g., PR head branch) - PRNumber int // GitHub PR number when CheckoutBranch is a PR head; enables refs/pull//head fetch for fork PRs. - RemoteContribution *models.RemoteContribution - WorktreeBranchPrefix string // Branch prefix for worktree branches - WorktreeBranchTemplate string // Branch name template for worktree branches - WorktreeBranchTicket string // External ticket value for branch templates - PullBeforeWorktree bool // Whether to pull from remote before creating the worktree - RemoteSyncHandled bool // Authenticated provider refresh already completed + UseWorktree bool // Whether to use a Git worktree for isolation + WorktreeID string // Existing worktree ID to reuse (skip creation if set) + RepositoryID string // Repository ID for worktree tracking + RepositoryPath string // Path to the main repository (for worktree creation) + BaseBranch string // Base branch for the worktree (e.g., "main") + DefaultBranch string // Repository's default_branch, used as fallback when BaseBranch is missing + CheckoutBranch string // Branch to fetch and checkout after worktree creation (e.g., PR head branch) + PRNumber int // GitHub PR number when CheckoutBranch is a PR head; enables refs/pull//head fetch for fork PRs. + RemoteContribution *models.RemoteContribution + WorktreeBranchPrefix string // Branch prefix for worktree branches + WorktreeBranchTemplate string // Branch name template for worktree branches + WorktreeBranchTicket string // External ticket value for branch templates + PullBeforeWorktree bool // Whether to pull from remote before creating the worktree + RemoteSyncHandled bool // Authenticated provider refresh already completed + ContributionDestination *models.ContributionDestination // Task directory mode: place worktree at ~/.kandev/tasks/{TaskDirName}/{RepoName}/ TaskDirName string // Semantic task directory name (e.g. "fix-bug_ab12") @@ -779,23 +785,24 @@ func (r *LaunchRequest) RepoSpecs() []RepoLaunchSpec { return nil } return []RepoLaunchSpec{{ - RepositoryID: r.RepositoryID, - RepositoryPath: r.RepositoryPath, - RepoName: r.RepoName, - BaseBranch: r.BaseBranch, - DefaultBranch: r.DefaultBranch, - CheckoutBranch: r.CheckoutBranch, - PRNumber: r.PRNumber, - RemoteContribution: r.RemoteContribution, - WorktreeID: r.WorktreeID, - WorktreeBranchPrefix: r.WorktreeBranchPrefix, - WorktreeBranchTemplate: r.WorktreeBranchTemplate, - WorktreeBranchTicket: r.WorktreeBranchTicket, - PullBeforeWorktree: r.PullBeforeWorktree, - RemoteSyncHandled: r.RemoteSyncHandled, - CopyFiles: r.CopyFiles, - BranchSlug: r.BranchSlug, - BranchIdentitySlug: r.BranchIdentitySlug, + RepositoryID: r.RepositoryID, + RepositoryPath: r.RepositoryPath, + RepoName: r.RepoName, + BaseBranch: r.BaseBranch, + DefaultBranch: r.DefaultBranch, + CheckoutBranch: r.CheckoutBranch, + PRNumber: r.PRNumber, + RemoteContribution: r.RemoteContribution, + ContributionDestination: r.ContributionDestination, + WorktreeID: r.WorktreeID, + WorktreeBranchPrefix: r.WorktreeBranchPrefix, + WorktreeBranchTemplate: r.WorktreeBranchTemplate, + WorktreeBranchTicket: r.WorktreeBranchTicket, + PullBeforeWorktree: r.PullBeforeWorktree, + RemoteSyncHandled: r.RemoteSyncHandled, + CopyFiles: r.CopyFiles, + BranchSlug: r.BranchSlug, + BranchIdentitySlug: r.BranchIdentitySlug, }} } diff --git a/apps/backend/internal/agent/runtime/lifecycle/types_repospecs_test.go b/apps/backend/internal/agent/runtime/lifecycle/types_repospecs_test.go index 294ae32e4e..9f8f69e243 100644 --- a/apps/backend/internal/agent/runtime/lifecycle/types_repospecs_test.go +++ b/apps/backend/internal/agent/runtime/lifecycle/types_repospecs_test.go @@ -29,18 +29,20 @@ func TestLaunchRequest_RepoSpecs_ReturnsExplicitListWhenSet(t *testing.T) { func TestLaunchRequest_RepoSpecs_SynthesizesFromLegacyFields(t *testing.T) { binding := models.RemoteContribution{CanonicalURL: "https://github.com/acme/widget/pull/7"} + destination := models.ContributionDestination{Provider: "github"} req := &LaunchRequest{ - RepositoryID: "repo-x", - RepositoryPath: "/x", - BaseBranch: "main", - CheckoutBranch: "feature/y", - WorktreeID: "wt-1", - WorktreeBranchPrefix: "feat/", - PullBeforeWorktree: true, - RepoName: "x", - BranchSlug: "feature-y", - BranchIdentitySlug: "feature-y", - RemoteContribution: &binding, + RepositoryID: "repo-x", + RepositoryPath: "/x", + BaseBranch: "main", + CheckoutBranch: "feature/y", + WorktreeID: "wt-1", + WorktreeBranchPrefix: "feat/", + PullBeforeWorktree: true, + RepoName: "x", + BranchSlug: "feature-y", + BranchIdentitySlug: "feature-y", + RemoteContribution: &binding, + ContributionDestination: &destination, } specs := req.RepoSpecs() if len(specs) != 1 { @@ -57,6 +59,9 @@ func TestLaunchRequest_RepoSpecs_SynthesizesFromLegacyFields(t *testing.T) { if got.RemoteContribution != &binding { t.Errorf("remote contribution = %#v, want %#v", got.RemoteContribution, &binding) } + if got.ContributionDestination != &destination { + t.Errorf("contribution destination = %#v, want %#v", got.ContributionDestination, &destination) + } } func TestLaunchRequest_RepoSpecs_NilForRepoLessLaunch(t *testing.T) { diff --git a/apps/backend/internal/agent/runtime/lifecycle/workspace_materialization.go b/apps/backend/internal/agent/runtime/lifecycle/workspace_materialization.go index 1a238ffe20..6c6fb2b3be 100644 --- a/apps/backend/internal/agent/runtime/lifecycle/workspace_materialization.go +++ b/apps/backend/internal/agent/runtime/lifecycle/workspace_materialization.go @@ -16,11 +16,12 @@ import ( // prepared for a running remote workspace. It deliberately carries only a // credential-free locator; executor launch environments provide Git auth. type WorkspaceRepositoryMaterialization struct { - RepositoryURL string - Destination string - BaseBranch string - CheckoutBranch string - RemoteContribution *models.RemoteContribution + RepositoryURL string + Destination string + BaseBranch string + CheckoutBranch string + RemoteContribution *models.RemoteContribution + ContributionDestination *models.ContributionDestination } const workspaceMaterializationRollbackTimeout = 10 * time.Second @@ -49,7 +50,7 @@ func remoteWorkspaceProjectionFromLaunch(req *LaunchRequest) ([]WorkspaceReposit if name == "" || branchSlug == "" { return nil, fmt.Errorf("remote repository %q has unsafe runtime name", spec.RepoName) } - projection = append(projection, WorkspaceRepositoryMaterialization{RepositoryURL: spec.RepositoryURL, Destination: name + "-" + branchSlug, BaseBranch: spec.BaseBranch, CheckoutBranch: spec.CheckoutBranch, RemoteContribution: spec.RemoteContribution}) + projection = append(projection, WorkspaceRepositoryMaterialization{RepositoryURL: spec.RepositoryURL, Destination: name + "-" + branchSlug, BaseBranch: spec.BaseBranch, CheckoutBranch: spec.CheckoutBranch, RemoteContribution: spec.RemoteContribution, ContributionDestination: spec.ContributionDestination}) } return projection, nil } @@ -181,11 +182,12 @@ func materializeWorkspaceRepositoriesWithoutRescan(ctx context.Context, client w created := make([]WorkspaceRepositoryMaterialization, 0, len(repositories)) for _, repository := range repositories { response, err := client.MaterializeRepository(ctx, agentctl.MaterializeRepositoryRequest{ - RepositoryURL: repository.RepositoryURL, - Destination: repository.Destination, - BaseBranch: repository.BaseBranch, - CheckoutBranch: repository.CheckoutBranch, - RemoteContribution: repository.RemoteContribution, + RepositoryURL: repository.RepositoryURL, + Destination: repository.Destination, + BaseBranch: repository.BaseBranch, + CheckoutBranch: repository.CheckoutBranch, + RemoteContribution: repository.RemoteContribution, + ContributionDestination: repository.ContributionDestination, }) if err != nil { rollbackErr := rollbackMaterializedWorkspaceRepositories(ctx, client, created) diff --git a/apps/backend/internal/agentctl/server/api/workspace_materialize.go b/apps/backend/internal/agentctl/server/api/workspace_materialize.go index 87ec9b5065..e63d0b9486 100644 --- a/apps/backend/internal/agentctl/server/api/workspace_materialize.go +++ b/apps/backend/internal/agentctl/server/api/workspace_materialize.go @@ -23,11 +23,12 @@ import ( // agentctl workspace. RepositoryURL must be a credential-free Git locator; // destination is always a direct child of the current workspace root. type MaterializeRepositoryRequest struct { - RepositoryURL string `json:"repository_url"` - Destination string `json:"destination"` - BaseBranch string `json:"base_branch"` - CheckoutBranch string `json:"checkout_branch,omitempty"` - RemoteContribution *models.RemoteContribution `json:"remote_contribution,omitempty"` + RepositoryURL string `json:"repository_url"` + Destination string `json:"destination"` + BaseBranch string `json:"base_branch"` + CheckoutBranch string `json:"checkout_branch,omitempty"` + RemoteContribution *models.RemoteContribution `json:"remote_contribution,omitempty"` + ContributionDestination *models.ContributionDestination `json:"contribution_destination,omitempty"` } // MaterializeRepositoryResponse deliberately contains no remote locator so a @@ -79,8 +80,14 @@ func (s *Server) handleWorkspaceMaterializeRepository(c *gin.Context) { return } } + if req.ContributionDestination != nil { + if err := req.ContributionDestination.Validate(); err != nil { + c.JSON(http.StatusBadRequest, MaterializeRepositoryResponse{Error: "invalid contribution destination"}) + return + } + } - reused, err := materializeRepository(c.Request.Context(), req.RepositoryURL, destination, req.BaseBranch, req.CheckoutBranch, req.RemoteContribution) + reused, err := materializeRepositoryWithDestination(c.Request.Context(), req.RepositoryURL, destination, req.BaseBranch, req.CheckoutBranch, req.RemoteContribution, req.ContributionDestination) if err != nil { if errors.Is(err, errMaterializeCollision) { c.JSON(http.StatusConflict, MaterializeRepositoryResponse{Error: "destination already exists"}) @@ -213,7 +220,15 @@ func materializeRepository(ctx context.Context, locator, destination, baseBranch if len(bindings) > 0 { binding = bindings[0] } - if reused, err := matchingCheckout(ctx, destination, locator, baseBranch, checkoutBranch, binding); err != nil || reused { + return materializeRepositoryInternal(ctx, locator, destination, baseBranch, checkoutBranch, binding, nil) +} + +func materializeRepositoryWithDestination(ctx context.Context, locator, destination, baseBranch, checkoutBranch string, binding *models.RemoteContribution, contributionDestination *models.ContributionDestination) (bool, error) { + return materializeRepositoryInternal(ctx, locator, destination, baseBranch, checkoutBranch, binding, contributionDestination) +} + +func materializeRepositoryInternal(ctx context.Context, locator, destination, baseBranch, checkoutBranch string, binding *models.RemoteContribution, contributionDestination *models.ContributionDestination) (bool, error) { + if reused, err := matchingCheckoutWithDestination(ctx, destination, locator, baseBranch, checkoutBranch, binding, contributionDestination); err != nil || reused { return reused, err } // codeql[go/path-injection] destination is a direct child of the canonical workspace root; Lstat rejects links before use. @@ -240,6 +255,11 @@ func materializeRepository(ctx context.Context, locator, destination, baseBranch } else if err := checkoutMaterializedBranch(ctx, checkout, baseBranch, checkoutBranch); err != nil { return false, err } + if contributionDestination != nil { + if err := configureContributionDestination(ctx, checkout, contributionDestination); err != nil { + return false, err + } + } // codeql[go/path-injection] checkout is newly created beneath the trusted workspace root; destination is its direct child. if err := os.Rename(checkout, destination); err != nil { if os.IsExist(err) { @@ -250,6 +270,62 @@ func materializeRepository(ctx context.Context, locator, destination, baseBranch return false, nil } +func matchingCheckoutWithDestination(ctx context.Context, destination, locator, baseBranch, checkoutBranch string, binding *models.RemoteContribution, contributionDestination *models.ContributionDestination) (bool, error) { + reused, err := matchingCheckout(ctx, destination, locator, baseBranch, checkoutBranch, binding) + if err != nil || !reused || contributionDestination == nil { + return reused, err + } + return true, configureContributionDestination(ctx, destination, contributionDestination) +} + +func configureContributionDestination(ctx context.Context, checkout string, destination *models.ContributionDestination) error { + if destination == nil { + return nil + } + if err := destination.Validate(); err != nil { + return err + } + remoteName := destination.ContributionRemoteName() + configured, err := materializeGitOutput(ctx, "-C", checkout, "config", "--get", "remote."+remoteName+".url") + if err == nil { + if strings.TrimSpace(configured) != destination.TargetRepository.RemoteURL { + return errors.New("contribution destination identity conflict") + } + } else if _, err := materializeGitOutput(ctx, "-C", checkout, "remote", "add", remoteName, destination.TargetRepository.RemoteURL); err != nil { + return errors.New("contribution destination could not be configured") + } + pushURLs, pushErr := materializeGitOutput(ctx, "-C", checkout, "config", "--get-all", "remote."+remoteName+".pushurl") + if pushErr == nil && !contributionDestinationPushURLsMatch(pushURLs, destination.TargetRepository.RemoteURL) { + return errors.New("contribution destination push identity conflict") + } + if pushErr != nil { + if _, err := materializeGitOutput(ctx, "-C", checkout, "config", "--add", "remote."+remoteName+".pushurl", destination.TargetRepository.RemoteURL); err != nil { + return errors.New("contribution destination push URL could not be configured") + } + } + branch, err := materializeGitOutput(ctx, "-C", checkout, "branch", "--show-current") + if err != nil || strings.TrimSpace(branch) == "" { + return errors.New("contribution destination branch could not be identified") + } + if _, err := materializeGitOutput(ctx, "-C", checkout, "config", "branch."+strings.TrimSpace(branch)+".pushRemote", remoteName); err != nil { + return errors.New("contribution destination push remote could not be configured") + } + return nil +} + +func contributionDestinationPushURLsMatch(configured, target string) bool { + urls := strings.Split(strings.TrimSpace(configured), "\n") + if len(urls) == 0 || (len(urls) == 1 && urls[0] == "") { + return false + } + for _, configuredURL := range urls { + if strings.TrimSpace(configuredURL) != target { + return false + } + } + return true +} + func checkoutMaterializedBranch(ctx context.Context, checkout, baseBranch, checkoutBranch string) error { branch := checkoutBranch if branch == "" { diff --git a/apps/backend/internal/agentctl/server/api/workspace_materialize_test.go b/apps/backend/internal/agentctl/server/api/workspace_materialize_test.go index 2e79b07700..234153f4e1 100644 --- a/apps/backend/internal/agentctl/server/api/workspace_materialize_test.go +++ b/apps/backend/internal/agentctl/server/api/workspace_materialize_test.go @@ -19,6 +19,16 @@ import ( "github.com/kandev/kandev/internal/task/models" ) +func TestContributionDestinationPushURLsMatchAcceptsRepeatedTarget(t *testing.T) { + targetURL := "https://github.com/agent/kandev.git" + if !contributionDestinationPushURLsMatch(targetURL+"\n"+targetURL, targetURL) { + t.Fatal("repeated identical contribution push URLs were rejected") + } + if contributionDestinationPushURLsMatch(targetURL+"\nhttps://github.com/other/kandev.git", targetURL) { + t.Fatal("mixed contribution push URLs were accepted") + } +} + func TestMaterializeRepository_ClonesIntoWorkspaceDestination(t *testing.T) { origin := createMaterializeOrigin(t) workDir := t.TempDir() diff --git a/apps/backend/internal/agentctl/server/config/config.go b/apps/backend/internal/agentctl/server/config/config.go index 3b6c58e06b..987add00aa 100644 --- a/apps/backend/internal/agentctl/server/config/config.go +++ b/apps/backend/internal/agentctl/server/config/config.go @@ -261,7 +261,8 @@ type InstanceConfig struct { // RemoteContributions maps workspace repository subpaths to the // server-authored contribution binding used for source-routed writes. - RemoteContributions map[string]models.RemoteContribution + RemoteContributions map[string]models.RemoteContribution + ContributionDestinations map[string]models.ContributionDestination // WorkspaceSourceRoots are canonical durable source roots permitted for // linked workspace file operations. @@ -459,6 +460,9 @@ func applyOverrides(cfg *InstanceConfig, overrides *InstanceOverrides) { if len(overrides.RemoteContributions) > 0 { cfg.RemoteContributions = cloneRemoteContributions(overrides.RemoteContributions) } + if len(overrides.ContributionDestinations) > 0 { + cfg.ContributionDestinations = cloneContributionDestinations(overrides.ContributionDestinations) + } if overrides.WorkspaceSourceRoots != nil { cfg.WorkspaceSourceRoots = append([]string(nil), overrides.WorkspaceSourceRoots...) } @@ -483,29 +487,30 @@ func applyApprovalOverrides(cfg *InstanceConfig, overrides *InstanceOverrides) { // InstanceOverrides allows overriding default values when creating an instance type InstanceOverrides struct { - InstanceID string - Protocol agent.Protocol - AgentCommand string - WorkDir string - AutoStart *bool - Env []string - AutoApprovePermissions *bool - ApprovalPolicy string - AgentType string - McpServers []McpServerConfig - SessionID string - TaskID string - DisableAskQuestion bool - AssumeMcpSse bool - AssumeMcpHttp bool - McpMode string - McpProviders []string - McpProfile *mcpprofile.Context - RequiresProcessKill bool - StripEnv []string - BaseBranches map[string]string - RemoteContributions map[string]models.RemoteContribution - WorkspaceSourceRoots []string + InstanceID string + Protocol agent.Protocol + AgentCommand string + WorkDir string + AutoStart *bool + Env []string + AutoApprovePermissions *bool + ApprovalPolicy string + AgentType string + McpServers []McpServerConfig + SessionID string + TaskID string + DisableAskQuestion bool + AssumeMcpSse bool + AssumeMcpHttp bool + McpMode string + McpProviders []string + McpProfile *mcpprofile.Context + RequiresProcessKill bool + StripEnv []string + BaseBranches map[string]string + RemoteContributions map[string]models.RemoteContribution + ContributionDestinations map[string]models.ContributionDestination + WorkspaceSourceRoots []string } func cloneRemoteContributions(values map[string]models.RemoteContribution) map[string]models.RemoteContribution { @@ -519,6 +524,17 @@ func cloneRemoteContributions(values map[string]models.RemoteContribution) map[s return cloned } +func cloneContributionDestinations(values map[string]models.ContributionDestination) map[string]models.ContributionDestination { + if len(values) == 0 { + return nil + } + cloned := make(map[string]models.ContributionDestination, len(values)) + for key, value := range values { + cloned[key] = value + } + return cloned +} + // ParseCommand splits a command string into arguments func ParseCommand(cmd string) []string { return strings.Fields(cmd) diff --git a/apps/backend/internal/agentctl/server/instance/instance.go b/apps/backend/internal/agentctl/server/instance/instance.go index e0e26497bb..18936af669 100644 --- a/apps/backend/internal/agentctl/server/instance/instance.go +++ b/apps/backend/internal/agentctl/server/instance/instance.go @@ -184,9 +184,10 @@ type CreateRequest struct { // Each WorkspaceTracker reads its entry at startup and uses it as the // first candidate when resolving BaseCommit / Ahead / Behind. Empty // disables the override. - BaseBranches map[string]string `json:"base_branches,omitempty"` - RemoteContributions map[string]models.RemoteContribution `json:"remote_contributions,omitempty"` - WorkspaceSourceRoots []string `json:"workspace_source_roots,omitempty"` + BaseBranches map[string]string `json:"base_branches,omitempty"` + RemoteContributions map[string]models.RemoteContribution `json:"remote_contributions,omitempty"` + ContributionDestinations map[string]models.ContributionDestination `json:"contribution_destinations,omitempty"` + WorkspaceSourceRoots []string `json:"workspace_source_roots,omitempty"` } // CreateResponse contains the result of creating a new agent instance. diff --git a/apps/backend/internal/agentctl/server/instance/manager.go b/apps/backend/internal/agentctl/server/instance/manager.go index a26e40c1dc..d81d8cbd7a 100644 --- a/apps/backend/internal/agentctl/server/instance/manager.go +++ b/apps/backend/internal/agentctl/server/instance/manager.go @@ -160,28 +160,29 @@ func (m *Manager) CreateInstance(ctx context.Context, req *CreateRequest) (*Crea zap.String("workspace_path", req.WorkspacePath)) overrides := &config.InstanceOverrides{ - InstanceID: id, - Protocol: agent.Protocol(req.Protocol), - AgentCommand: agentCmd, - WorkDir: req.WorkspacePath, - AutoStart: &autoStart, - Env: agentEnv, - AutoApprovePermissions: req.AutoApprovePermissions, - AgentType: req.AgentType, - McpServers: mcpServers, - SessionID: req.SessionID, - TaskID: req.TaskID, - DisableAskQuestion: req.DisableAskQuestion, - AssumeMcpSse: req.AssumeMcpSse, - AssumeMcpHttp: req.AssumeMcpHttp, - McpMode: req.McpMode, - McpProviders: req.McpProviders, - McpProfile: req.McpProfile, - RequiresProcessKill: req.RequiresProcessKill, - StripEnv: req.StripEnv, - BaseBranches: req.BaseBranches, - RemoteContributions: req.RemoteContributions, - WorkspaceSourceRoots: req.WorkspaceSourceRoots, + InstanceID: id, + Protocol: agent.Protocol(req.Protocol), + AgentCommand: agentCmd, + WorkDir: req.WorkspacePath, + AutoStart: &autoStart, + Env: agentEnv, + AutoApprovePermissions: req.AutoApprovePermissions, + AgentType: req.AgentType, + McpServers: mcpServers, + SessionID: req.SessionID, + TaskID: req.TaskID, + DisableAskQuestion: req.DisableAskQuestion, + AssumeMcpSse: req.AssumeMcpSse, + AssumeMcpHttp: req.AssumeMcpHttp, + McpMode: req.McpMode, + McpProviders: req.McpProviders, + McpProfile: req.McpProfile, + RequiresProcessKill: req.RequiresProcessKill, + StripEnv: req.StripEnv, + BaseBranches: req.BaseBranches, + RemoteContributions: req.RemoteContributions, + ContributionDestinations: req.ContributionDestinations, + WorkspaceSourceRoots: req.WorkspaceSourceRoots, } m.logger.Info("CreateInstance: applying overrides", diff --git a/apps/backend/internal/agentctl/server/process/git.go b/apps/backend/internal/agentctl/server/process/git.go index ca2531e116..14f9dc1c85 100644 --- a/apps/backend/internal/agentctl/server/process/git.go +++ b/apps/backend/internal/agentctl/server/process/git.go @@ -46,9 +46,11 @@ type GitOperator struct { // repoName is the multi-repo subpath this operator runs in (e.g. "kandev"). // Empty for the workspace-root operator. Stamped on emitted commit // notifications so the frontend can group commits per repo. - repoName string - remoteContribution *models.RemoteContribution - remoteContributionErr error + repoName string + remoteContribution *models.RemoteContribution + remoteContributionErr error + contributionDestination *models.ContributionDestination + contributionDestinationErr error mu sync.Mutex // Prevents concurrent git operations inProgress bool @@ -91,6 +93,18 @@ func (g *GitOperator) setRemoteContribution(binding *models.RemoteContribution) g.remoteContribution = © } +func (g *GitOperator) setContributionDestination(destination *models.ContributionDestination) { + if destination == nil { + return + } + if err := destination.Validate(); err != nil { + g.contributionDestinationErr = fmt.Errorf("invalid contribution destination: %w", err) + return + } + copy := *destination + g.contributionDestination = © +} + func (g *GitOperator) validateContributionRemote(ctx context.Context) error { if g.remoteContribution == nil { return nil @@ -116,6 +130,27 @@ func (g *GitOperator) validateContributionRemote(ctx context.Context) error { return nil } +func (g *GitOperator) validateContributionDestinationRemote(ctx context.Context) error { + if g.contributionDestination == nil { + return nil + } + remoteName := g.contributionDestination.ContributionRemoteName() + urlsOutput, err := g.runGitCommand(ctx, "config", "--get-all", "remote."+remoteName+".pushurl") + if err != nil { + return errors.New("contribution destination remote is unavailable") + } + urls := strings.Split(strings.TrimSpace(urlsOutput), "\n") + if len(urls) == 0 || urls[0] == "" { + return errors.New("contribution destination remote has no configured push URL") + } + for _, configured := range urls { + if strings.TrimSpace(configured) != g.contributionDestination.TargetRepository.RemoteURL { + return errors.New("contribution destination push URL does not match the validated target") + } + } + return nil +} + func (g *GitOperator) environmentValues() []string { if g.environment == nil { return os.Environ() @@ -364,6 +399,10 @@ func (g *GitOperator) Pull(ctx context.Context, rebase bool) (*GitOperationResul result.Error = g.remoteContributionErr.Error() return result, nil } + if g.contributionDestinationErr != nil { + result.Error = g.contributionDestinationErr.Error() + return result, nil + } if g.remoteContribution != nil { if err := g.validateContributionRemote(ctx); err != nil { result.Error = err.Error() @@ -401,7 +440,6 @@ func (g *GitOperator) Pull(ctx context.Context, rebase bool) (*GitOperationResul } return result, nil } - result.Success = true g.logger.Info("pull completed", zap.String("branch", pullBranch), zap.String("remote", remote), zap.Bool("rebase", rebase)) return result, nil @@ -421,7 +459,17 @@ func (g *GitOperator) Push(ctx context.Context, force bool, setUpstream bool) (* result.Error = g.remoteContributionErr.Error() return result, nil } - if g.remoteContribution != nil && force { + if g.contributionDestinationErr != nil { + result.Error = g.contributionDestinationErr.Error() + return result, nil + } + if g.contributionDestination != nil { + if err := g.validateContributionDestinationRemote(ctx); err != nil { + result.Error = err.Error() + return result, nil + } + } + if (g.remoteContribution != nil || g.contributionDestination != nil) && force { result.Error = "force push is not allowed for a remote contribution" return result, nil } @@ -440,7 +488,11 @@ func (g *GitOperator) Push(ctx context.Context, force bool, setUpstream bool) (* shouldSetUpstream := setUpstream || g.getUpstreamRef(ctx) == "" remote := "origin" refspec := branch - if g.remoteContribution != nil { + if g.contributionDestination != nil { + remote = g.contributionDestination.ContributionRemoteName() + refspec = branch + shouldSetUpstream = setUpstream + } else if g.remoteContribution != nil { remote = g.remoteContribution.ContributionRemoteName() refspec = "HEAD:refs/heads/" + g.remoteContribution.HeadBranch shouldSetUpstream = setUpstream @@ -485,6 +537,30 @@ func (g *GitOperator) PushPreflight(ctx context.Context) (*GitOperationResult, e result.Error = g.remoteContributionErr.Error() return result, nil } + if g.contributionDestinationErr != nil { + result.Error = g.contributionDestinationErr.Error() + return result, nil + } + if g.contributionDestination != nil { + if err := g.validateContributionDestinationRemote(ctx); err != nil { + result.Error = err.Error() + return result, nil + } + branch, err := g.getCurrentBranch(ctx) + if err != nil { + result.Error = err.Error() + return result, nil + } + output, err := g.runGitCommand(ctx, "push", "--dry-run", g.contributionDestination.ContributionRemoteName(), branch) + result.Output = output + if err != nil { + result.Error = err.Error() + return result, nil + } + result.Success = true + g.logger.Info("contribution destination push preflight completed", zap.String("branch", branch)) + return result, nil + } if g.remoteContribution == nil { result.Success = true return result, nil @@ -1161,6 +1237,13 @@ func (g *GitOperator) CreatePR(ctx context.Context, title, body, baseBranch stri g.logger.Info("updated existing remote contribution", zap.String("branch", branch), zap.String("provider", result.Provider)) return result, nil } + if g.contributionDestinationErr != nil { + result.Error = g.contributionDestinationErr.Error() + return result, nil + } + if g.contributionDestination != nil { + return g.createManagedContributionPR(ctx, title, body, baseBranch, draft) + } branch, err := g.getCurrentBranch(ctx) if err != nil { @@ -1227,6 +1310,42 @@ func (g *GitOperator) CreatePR(ctx context.Context, title, body, baseBranch stri } } +func (g *GitOperator) createManagedContributionPR( + ctx context.Context, + title, body, baseBranch string, + draft bool, +) (*PRCreateResult, error) { + result := &PRCreateResult{} + originURL, err := g.getOriginRemoteURL(ctx) + if err != nil { + result.Error = err.Error() + return result, nil + } + if g.detectPRProvider(originURL) != prProviderGitHub { + result.Error = fmt.Sprintf("managed contribution destination requires a GitHub origin: %s", redactRemoteURL(originURL)) + return result, nil + } + if err := g.validateContributionDestinationRemote(ctx); err != nil { + result.Error = err.Error() + return result, nil + } + branch, err := g.getCurrentBranch(ctx) + if err != nil { + result.Error = fmt.Sprintf("failed to get current branch: %s", err.Error()) + return result, nil + } + output, err := g.runGitCommand(ctx, "push", g.contributionDestination.ContributionRemoteName(), "HEAD:refs/heads/"+branch) + if err != nil { + result.Error = fmt.Sprintf("failed to push contribution destination: %s", g.sanitizePRFailure(output, title, body)) + result.Output = g.sanitizeGitPushOutput(output) + return result, nil + } + result.Provider = string(prProviderGitHub) + result.BranchPushed = true + created, createErr := g.createGitHubPR(ctx, result, branch, title, body, baseBranch, draft) + return finalizePRCreationAfterPush(created, createErr) +} + func finalizePRCreationAfterPush(result *PRCreateResult, createErr error) (*PRCreateResult, error) { if result == nil { result = &PRCreateResult{} diff --git a/apps/backend/internal/agentctl/server/process/git_pr_providers.go b/apps/backend/internal/agentctl/server/process/git_pr_providers.go index d12b2afa3d..7849ec6f39 100644 --- a/apps/backend/internal/agentctl/server/process/git_pr_providers.go +++ b/apps/backend/internal/agentctl/server/process/git_pr_providers.go @@ -438,7 +438,16 @@ func (g *GitOperator) createGitHubPR( branch, title, body, baseBranch string, draft bool, ) (*PRCreateResult, error) { - args := []string{"pr", prCreateSubcommand, repositoryFlagTitle, title, repositoryFlagBody, body, repositoryFlagHead, branch} + head := branch + args := []string{"pr", prCreateSubcommand, repositoryFlagTitle, title, repositoryFlagBody, body} + if g.contributionDestination != nil { + parts := strings.Split(g.contributionDestination.TargetRepository.Path, "/") + if len(parts) == 2 { + head = parts[0] + ":" + branch + args = append(args, "--repo", g.contributionDestination.SourceRepository.Path) + } + } + args = append(args, repositoryFlagHead, head) if cleanBase := cleanBaseBranch(baseBranch); cleanBase != "" { args = append(args, "--base", cleanBase) } diff --git a/apps/backend/internal/agentctl/server/process/git_pr_providers_test.go b/apps/backend/internal/agentctl/server/process/git_pr_providers_test.go index a86b4b883c..af94d7bd17 100644 --- a/apps/backend/internal/agentctl/server/process/git_pr_providers_test.go +++ b/apps/backend/internal/agentctl/server/process/git_pr_providers_test.go @@ -14,6 +14,8 @@ import ( "strings" "sync/atomic" "testing" + + taskmodels "github.com/kandev/kandev/internal/task/models" ) func prepareGitLabPRRepo(t *testing.T, remoteURL string) (*GitOperator, string) { @@ -75,6 +77,108 @@ func TestGitOperatorCreatePR_GitLabRESTUsesJSONAndProjectDefaultBranch(t *testin } } +func TestGitOperatorCreatePR_ContributionDestinationUsesCanonicalTargetAndForkHead(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("shell wrapper test is Unix-only") + } + repoDir, cleanup := setupTestRepo(t) + t.Cleanup(cleanup) + targetDir := t.TempDir() + runGit(t, targetDir, "init", "--bare", "--initial-branch=main") + runGit(t, repoDir, "checkout", "-b", "feature/managed-destination") + runGit(t, repoDir, "remote", "set-url", "origin", "https://github.com/kdlbs/kandev.git") + writeFile(t, repoDir, "managed-destination.txt", "managed destination\n") + runGit(t, repoDir, "add", ".") + runGit(t, repoDir, "commit", "-m", "managed destination") + + destination := &taskmodels.ContributionDestination{ + Version: taskmodels.ContributionDestinationVersion, + Provider: taskmodels.ContributionDestinationProviderGitHub, + SourceRepository: taskmodels.ContributionDestinationRepository{ + Host: "github.com", Path: "kdlbs/kandev", ProviderID: "100", RemoteURL: "https://github.com/kdlbs/kandev.git", + }, + TargetRepository: taskmodels.ContributionDestinationRepository{ + Host: "github.com", Path: "agent/kandev", ProviderID: "200", RemoteURL: "https://github.com/agent/kandev.git", + }, + } + if err := destination.Validate(); err != nil { + t.Fatalf("destination.Validate() = %v", err) + } + runGit(t, repoDir, "config", "url."+targetDir+".insteadOf", destination.TargetRepository.RemoteURL) + runGit(t, repoDir, "remote", "add", destination.ContributionRemoteName(), destination.TargetRepository.RemoteURL) + runGit(t, repoDir, "remote", "set-url", "--push", destination.ContributionRemoteName(), destination.TargetRepository.RemoteURL) + + scriptDir := t.TempDir() + ghArgsPath := filepath.Join(scriptDir, "gh-args.txt") + writeExecutable(t, filepath.Join(scriptDir, "gh"), fmt.Sprintf("#!/bin/sh\nprintf '%%s\\n' \"$@\" > %q\nprintf 'https://github.com/kdlbs/kandev/pull/42\\n'\n", ghArgsPath)) + t.Setenv("PATH", scriptDir+string(os.PathListSeparator)+os.Getenv("PATH")) + + operator := NewGitOperator(repoDir, newTestLogger(t), nil) + operator.setContributionDestination(destination) + result, err := operator.CreatePR(context.Background(), "Managed title", "Managed body", "main", true) + if err != nil || !result.Success { + t.Fatalf("CreatePR = %+v, err = %v", result, err) + } + if got := strings.TrimSpace(runGit(t, targetDir, "rev-parse", "refs/heads/feature/managed-destination")); got == "" { + t.Fatal("managed destination branch was not pushed") + } + if got := strings.TrimSpace(runGit(t, repoDir, "remote", "get-url", "origin")); got == "" { + t.Fatal("canonical origin was removed or rewritten") + } + + wantArgs := []string{ + "pr", "create", "--title", "Managed title", "--body", "Managed body", + "--repo", "kdlbs/kandev", "--head", "agent:feature/managed-destination", "--base", "main", "--draft", + } + if got := readScriptArgs(t, ghArgsPath); strings.Join(got, "\n") != strings.Join(wantArgs, "\n") { + t.Fatalf("gh args = %q, want %q", got, wantArgs) + } +} + +func TestGitOperatorCreatePR_ContributionDestinationRejectsNonGitHubOrigin(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("shell wrapper test is Unix-only") + } + repoDir, cleanup := setupTestRepo(t) + t.Cleanup(cleanup) + targetDir := t.TempDir() + runGit(t, targetDir, "init", "--bare", "--initial-branch=main") + runGit(t, repoDir, "remote", "set-url", "origin", "https://gitlab.com/kdlbs/kandev.git") + runGit(t, repoDir, "remote", "add", "contrib-destination", "https://github.com/agent/kandev.git") + runGit(t, repoDir, "remote", "set-url", "--push", "contrib-destination", "https://github.com/agent/kandev.git") + runGit(t, repoDir, "checkout", "-b", "feature/managed-gitlab-origin") + writeFile(t, repoDir, "managed-gitlab-origin.txt", "managed destination\n") + runGit(t, repoDir, "add", ".") + runGit(t, repoDir, "commit", "-m", "managed destination") + + destination := &taskmodels.ContributionDestination{ + Version: taskmodels.ContributionDestinationVersion, + Provider: taskmodels.ContributionDestinationProviderGitHub, + SourceRepository: taskmodels.ContributionDestinationRepository{ + Host: "github.com", Path: "kdlbs/kandev", ProviderID: "100", RemoteURL: "https://github.com/kdlbs/kandev.git", + }, + TargetRepository: taskmodels.ContributionDestinationRepository{ + Host: "github.com", Path: "agent/kandev", ProviderID: "200", RemoteURL: "https://github.com/agent/kandev.git", + }, + } + if err := destination.Validate(); err != nil { + t.Fatalf("destination.Validate() = %v", err) + } + runGit(t, repoDir, "remote", "rename", "contrib-destination", destination.ContributionRemoteName()) + operator := NewGitOperator(repoDir, newTestLogger(t), nil) + operator.setContributionDestination(destination) + result, err := operator.CreatePR(context.Background(), "Managed title", "Managed body", "main", false) + if err != nil { + t.Fatalf("CreatePR returned error: %v", err) + } + if result.Success || !strings.Contains(result.Error, "GitHub origin") { + t.Fatalf("CreatePR = %+v, want a GitHub-origin rejection", result) + } + if _, statErr := os.Stat(filepath.Join(targetDir, "refs", "heads", "feature", "managed-gitlab-origin")); !os.IsNotExist(statErr) { + t.Fatalf("destination branch was pushed despite origin rejection, stat error = %v", statErr) + } +} + func TestGitOperatorCreatePR_GitLabRESTRetryReusesExistingMR(t *testing.T) { var posts atomic.Int32 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { diff --git a/apps/backend/internal/agentctl/server/process/git_test.go b/apps/backend/internal/agentctl/server/process/git_test.go index 00d547ba92..ce7ff59fd8 100644 --- a/apps/backend/internal/agentctl/server/process/git_test.go +++ b/apps/backend/internal/agentctl/server/process/git_test.go @@ -184,6 +184,51 @@ func TestGitOperatorRemoteContributionPullsFromSourceRemote(t *testing.T) { } } +func TestGitOperatorContributionDestinationPushesWithoutChangingPullRemote(t *testing.T) { + repoDir, cleanup := setupTestRepo(t) + defer cleanup() + + targetDir := t.TempDir() + runGit(t, targetDir, "init", "--bare") + runGit(t, repoDir, "remote", "add", "contrib-destination", targetDir) + runGit(t, repoDir, "checkout", "-b", "feature/destination") + writeFile(t, repoDir, "destination.txt", "destination\n") + runGit(t, repoDir, "add", ".") + runGit(t, repoDir, "commit", "-m", "destination") + + destination := &taskmodels.ContributionDestination{ + Version: taskmodels.ContributionDestinationVersion, + Provider: taskmodels.ContributionDestinationProviderGitHub, + SourceRepository: taskmodels.ContributionDestinationRepository{ + Host: "github.com", Path: "kdlbs/kandev", ProviderID: "100", RemoteURL: "https://github.com/kdlbs/kandev.git", + }, + TargetRepository: taskmodels.ContributionDestinationRepository{ + Host: "github.com", Path: "agent/kandev", ProviderID: "200", RemoteURL: "https://github.com/agent/kandev.git", + }, + } + if err := destination.Validate(); err != nil { + t.Fatal(err) + } + runGit(t, repoDir, "config", "url."+targetDir+".insteadOf", destination.TargetRepository.RemoteURL) + // The model derives the actual deterministic remote name. Rename the test + // remote after binding so the operator exercises the server-authored name. + runGit(t, repoDir, "remote", "rename", "contrib-destination", destination.ContributionRemoteName()) + runGit(t, repoDir, "remote", "set-url", "--push", destination.ContributionRemoteName(), destination.TargetRepository.RemoteURL) + + operator := NewGitOperator(repoDir, newTestLogger(t), nil) + operator.setContributionDestination(destination) + pushed, err := operator.Push(context.Background(), false, false) + if err != nil || !pushed.Success { + t.Fatalf("Push = %+v, err = %v", pushed, err) + } + if got := strings.TrimSpace(runGit(t, targetDir, "rev-parse", "refs/heads/feature/destination")); got == "" { + t.Fatal("destination branch was not pushed") + } + if got := strings.TrimSpace(runGit(t, repoDir, "remote", "get-url", "origin")); got == "" { + t.Fatal("origin remote was removed or rewritten") + } +} + func TestGitOperatorPush_PreservesExistingUpstream(t *testing.T) { repoDir, cleanup := setupTestRepo(t) defer cleanup() diff --git a/apps/backend/internal/agentctl/server/process/manager.go b/apps/backend/internal/agentctl/server/process/manager.go index bdd8bdc8c5..713d4acec6 100644 --- a/apps/backend/internal/agentctl/server/process/manager.go +++ b/apps/backend/internal/agentctl/server/process/manager.go @@ -921,6 +921,9 @@ func (m *Manager) GitOperator() *GitOperator { if binding, ok := m.cfg.RemoteContributions[""]; ok { m.gitOperator.setRemoteContribution(&binding) } + if destination, ok := m.cfg.ContributionDestinations[""]; ok { + m.gitOperator.setContributionDestination(&destination) + } } return m.gitOperator } @@ -958,6 +961,9 @@ func (m *Manager) GitOperatorFor(subpath string) (*GitOperator, error) { if binding, ok := m.cfg.RemoteContributions[cleaned]; ok { op.setRemoteContribution(&binding) } + if destination, ok := m.cfg.ContributionDestinations[cleaned]; ok { + op.setContributionDestination(&destination) + } m.gitOperatorsBySubpath[cleaned] = op return op, nil } diff --git a/apps/backend/internal/agentctl/server/process/vscode_test.go b/apps/backend/internal/agentctl/server/process/vscode_test.go index f5a89ec190..f526be033d 100644 --- a/apps/backend/internal/agentctl/server/process/vscode_test.go +++ b/apps/backend/internal/agentctl/server/process/vscode_test.go @@ -16,20 +16,12 @@ import ( "github.com/stretchr/testify/require" ) -func TestAllocatePort(t *testing.T) { - port, err := allocatePort() - require.NoError(t, err) - assert.Greater(t, port, 0) - assert.Less(t, port, 65536) -} - -func TestAllocatePort_Unique(t *testing.T) { - ports := make(map[int]bool) +func TestAllocatePort_MultipleCalls(t *testing.T) { for i := 0; i < 5; i++ { port, err := allocatePort() require.NoError(t, err) - assert.False(t, ports[port], "port %d allocated twice", port) - ports[port] = true + assert.Greater(t, port, 0) + assert.Less(t, port, 65536) } } diff --git a/apps/backend/internal/agentctl/server/process/workspace_content_search.go b/apps/backend/internal/agentctl/server/process/workspace_content_search.go index a37e0ae469..b34348de15 100644 --- a/apps/backend/internal/agentctl/server/process/workspace_content_search.go +++ b/apps/backend/internal/agentctl/server/process/workspace_content_search.go @@ -61,6 +61,12 @@ func (m *Manager) SearchWorkspaceContent( if query == "" { return response, nil } + // Repository materialization can add sibling worktrees after the manager + // starts. Reconcile before taking the tracker snapshot so a search cannot + // permanently return a partial repository graph from that startup race. + if err := m.RescanRepositories(ctx, ""); err != nil { + return nil, err + } for _, tracker := range m.contentSearchTrackers() { if err := ctx.Err(); err != nil { diff --git a/apps/backend/internal/agentctl/server/process/workspace_content_search_test.go b/apps/backend/internal/agentctl/server/process/workspace_content_search_test.go index 77aa4202cc..9557d335f3 100644 --- a/apps/backend/internal/agentctl/server/process/workspace_content_search_test.go +++ b/apps/backend/internal/agentctl/server/process/workspace_content_search_test.go @@ -241,6 +241,33 @@ func TestManagerSearchWorkspaceContentGroupsResultsByRepository(t *testing.T) { } } +func TestManagerSearchWorkspaceContentDiscoversRepositoryAddedAfterStartup(t *testing.T) { + taskRoot := t.TempDir() + firstRepo := filepath.Join(taskRoot, "alpha") + initGitRepoAt(t, firstRepo) + writeFile(t, firstRepo, "match.txt", "late repository token\n") + + manager := NewManager(&config.InstanceConfig{WorkDir: taskRoot}, newTestLogger(t)) + t.Cleanup(manager.stopWorkspaceTrackers) + + secondRepo := filepath.Join(taskRoot, "beta") + initGitRepoAt(t, secondRepo) + writeFile(t, secondRepo, "match.txt", "late repository token\n") + + response, err := manager.SearchWorkspaceContent(context.Background(), "token", 1) + if err != nil { + t.Fatalf("SearchWorkspaceContent returned error: %v", err) + } + if len(response.Results) != 2 { + t.Fatalf("result count = %d, want one per repo: %#v", len(response.Results), response.Results) + } + if response.Results[0].RepositoryName != "alpha" || + response.Results[1].RepositoryName != "beta" { + t.Fatalf("repository order = %q, %q; want alpha, beta", + response.Results[0].RepositoryName, response.Results[1].RepositoryName) + } +} + func TestManagerSearchWorkspaceContentClampsLimitPerRepository(t *testing.T) { repoDir, cleanup := setupTestRepo(t) t.Cleanup(cleanup) diff --git a/apps/backend/internal/backendapp/adapters.go b/apps/backend/internal/backendapp/adapters.go index ff73b03f21..301fe66ac4 100644 --- a/apps/backend/internal/backendapp/adapters.go +++ b/apps/backend/internal/backendapp/adapters.go @@ -222,6 +222,7 @@ func buildLifecycleLaunchRequest( CheckoutBranch: req.CheckoutBranch, PRNumber: req.PRNumber, RemoteContribution: req.RemoteContribution, + ContributionDestination: req.ContributionDestination, WorktreeBranchPrefix: req.WorktreeBranchPrefix, WorktreeBranchTemplate: req.WorktreeBranchTemplate, WorktreeBranchTicket: req.WorktreeBranchTicket, @@ -271,26 +272,27 @@ func lifecycleRepoLaunchSpecs(repos []executor.RepoSpec) []lifecycle.RepoLaunchS specs := make([]lifecycle.RepoLaunchSpec, 0, len(repos)) for _, r := range repos { specs = append(specs, lifecycle.RepoLaunchSpec{ - RepositoryID: r.RepositoryID, - RepositoryPath: r.RepositoryPath, - RepositoryURL: r.RepositoryURL, - RepoName: r.RepoName, - BaseBranch: r.BaseBranch, - DefaultBranch: r.DefaultBranch, - CheckoutBranch: r.CheckoutBranch, - PRNumber: r.PRNumber, - RemoteContribution: r.RemoteContribution, - WorktreeID: r.WorktreeID, - WorktreeBranchPrefix: r.WorktreeBranchPrefix, - WorktreeBranchTemplate: r.WorktreeBranchTemplate, - WorktreeBranchTicket: r.WorktreeBranchTicket, - PullBeforeWorktree: r.PullBeforeWorktree, - RemoteSyncHandled: r.RemoteSyncHandled, - RepoSetupScript: r.RepoSetupScript, - RepoCleanupScript: r.RepoCleanupScript, - CopyFiles: r.CopyFiles, - BranchSlug: r.BranchSlug, - BranchIdentitySlug: r.BranchIdentitySlug, + RepositoryID: r.RepositoryID, + RepositoryPath: r.RepositoryPath, + RepositoryURL: r.RepositoryURL, + RepoName: r.RepoName, + BaseBranch: r.BaseBranch, + DefaultBranch: r.DefaultBranch, + CheckoutBranch: r.CheckoutBranch, + PRNumber: r.PRNumber, + RemoteContribution: r.RemoteContribution, + ContributionDestination: r.ContributionDestination, + WorktreeID: r.WorktreeID, + WorktreeBranchPrefix: r.WorktreeBranchPrefix, + WorktreeBranchTemplate: r.WorktreeBranchTemplate, + WorktreeBranchTicket: r.WorktreeBranchTicket, + PullBeforeWorktree: r.PullBeforeWorktree, + RemoteSyncHandled: r.RemoteSyncHandled, + RepoSetupScript: r.RepoSetupScript, + RepoCleanupScript: r.RepoCleanupScript, + CopyFiles: r.CopyFiles, + BranchSlug: r.BranchSlug, + BranchIdentitySlug: r.BranchIdentitySlug, }) } return specs diff --git a/apps/backend/internal/backendapp/helpers.go b/apps/backend/internal/backendapp/helpers.go index db3e2eef22..2e94b237ce 100644 --- a/apps/backend/internal/backendapp/helpers.go +++ b/apps/backend/internal/backendapp/helpers.go @@ -1234,6 +1234,9 @@ func registerSecondaryRoutes( } } ikHandler := improvekandev.NewHandler(p.taskSvc, p.repoCloner, ghCopier, resolveDefaultWorkspace, p.version, p.log) + if p.services.GitHub != nil { + ikHandler.SetManagedGitHubForkProber(p.services.GitHub) + } ikHandler.SetTemporaryArtifactRegistry(p.temporaryArtifacts) if p.systemSvc != nil { ikHandler.SetLogBundles(p.systemSvc.LogBundles) diff --git a/apps/backend/internal/backendapp/services.go b/apps/backend/internal/backendapp/services.go index aa2bf69e05..ffad7d46cc 100644 --- a/apps/backend/internal/backendapp/services.go +++ b/apps/backend/internal/backendapp/services.go @@ -4,7 +4,9 @@ import ( "context" "errors" "fmt" + "net/url" "path/filepath" + "strconv" "strings" "time" @@ -23,7 +25,6 @@ import ( "github.com/kandev/kandev/internal/db" editorservice "github.com/kandev/kandev/internal/editors/service" "github.com/kandev/kandev/internal/events/bus" - "github.com/kandev/kandev/internal/gitcredentials" "github.com/kandev/kandev/internal/github" "github.com/kandev/kandev/internal/gitlab" "github.com/kandev/kandev/internal/integrations/secretadapter" @@ -52,6 +53,11 @@ import ( "google.golang.org/grpc/status" ) +const ( + canonicalKandevOwner = "kdlbs" + canonicalKandevName = "kandev" +) + func provideServices(cfg *config.Config, log *logger.Logger, repos *Repositories, dbPool *db.Pool, eventBus bus.EventBus, agentRegistry *registry.Registry, version string) (*Services, *agentsettingscontroller.Controller, error) { // Load custom TUI agents from DB into registry before discovery loadCustomTUIAgents(context.Background(), repos, agentRegistry, log) @@ -149,6 +155,10 @@ func provideServices(cfg *config.Config, log *logger.Logger, repos *Repositories if githubSvc != nil { taskSvc.SetTaskStatusSummaryPRReader(&githubTaskStatusSummaryPRReader{gh: githubSvc}) githubSvc.SetPromptResolver(promptSvc) + taskSvc.SetContributionDestinationPreparer(&githubContributionDestinationPreparer{service: githubSvc, taskSvc: taskSvc}) + if brokerErr := githubSvc.ConfigureCredentialBroker(&githubBrokerScopeAuthorizer{repo: repos.Task, provider: githubSvc}); brokerErr != nil { + log.Warn("GitHub credential broker initialization failed", zap.Error(brokerErr)) + } } gitlabSvc, gitlabCleanup := initGitLabService(dbPool, eventBus, repos.Secrets, log) if gitlabSvc != nil { @@ -284,6 +294,126 @@ func reserveBuiltinMentionIdentities( pluginService.SetReservedReferenceIdentities(identities) } +type githubContributionDestinationPreparer struct { + service *github.Service + taskSvc *taskservice.Service +} + +func (p *githubContributionDestinationPreparer) PrepareContributionDestination( + ctx context.Context, + req *taskservice.CreateTaskRequest, + workflow *taskmodels.Workflow, + repositories []*taskmodels.Repository, +) error { + if p == nil || p.service == nil || req == nil || workflow == nil || + workflow.WorkflowTemplateID == nil || *workflow.WorkflowTemplateID != "improve-kandev" { + return nil + } + policy, err := p.service.DescribeTaskGitCredentialPolicy(ctx, req.WorkspaceID) + if err != nil { + return fmt.Errorf("resolve Improve Kandev GitHub credential policy: %w", err) + } + if policy.Mode != github.TaskGitCredentialsModeManaged { + return nil + } + for index := range req.Repositories { + if err := p.prepareRepository(ctx, req, repositories, index); err != nil { + return err + } + } + return nil +} + +func (p *githubContributionDestinationPreparer) prepareRepository( + ctx context.Context, + req *taskservice.CreateTaskRequest, + repositories []*taskmodels.Repository, + index int, +) error { + input := &req.Repositories[index] + if input.RemoteContribution != nil || input.ContributionDestination != nil || + !isCanonicalKandevRepositoryInput(input, repositoryAt(repositories, index)) { + return nil + } + resolution, err := p.service.ResolveContributionForkForWorkspace( + ctx, req.WorkspaceID, canonicalKandevOwner, canonicalKandevName, true, + ) + if err != nil { + return fmt.Errorf("prepare Improve Kandev contribution destination: %w", err) + } + if resolution.Repository == nil || resolution.Repository.ID <= 0 { + return fmt.Errorf("prepare Improve Kandev contribution destination: canonical provider ID is missing") + } + providerRepoID := strconv.FormatInt(resolution.Repository.ID, 10) + destination := resolution.Destination + if destination != nil && strings.TrimSpace(destination.SourceRepository.ProviderID) != providerRepoID { + return fmt.Errorf("prepare Improve Kandev contribution destination: canonical provider ID is inconsistent") + } + if err := p.reconcileProviderRepoID(ctx, repositoryAt(repositories, index), providerRepoID); err != nil { + return err + } + input.ProviderRepoID = providerRepoID + input.ContributionDestination = destination + return nil +} + +func (p *githubContributionDestinationPreparer) reconcileProviderRepoID( + ctx context.Context, + repository *taskmodels.Repository, + providerRepoID string, +) error { + if repository == nil { + return nil + } + if repository.ProviderRepoID != "" && !strings.EqualFold(repository.ProviderRepoID, providerRepoID) { + return fmt.Errorf("prepare Improve Kandev contribution destination: canonical provider ID changed") + } + if repository.ProviderRepoID == "" && p.taskSvc != nil { + if _, err := p.taskSvc.UpdateRepository(ctx, repository.ID, &taskservice.UpdateRepositoryRequest{ProviderRepoID: &providerRepoID}); err != nil { + return fmt.Errorf("backfill Improve Kandev canonical provider ID: %w", err) + } + repository.ProviderRepoID = providerRepoID + } + return nil +} + +func repositoryAt(repositories []*taskmodels.Repository, index int) *taskmodels.Repository { + if index < 0 || index >= len(repositories) { + return nil + } + return repositories[index] +} + +func isCanonicalKandevRepositoryInput( + input *taskservice.TaskRepositoryInput, + repository *taskmodels.Repository, +) bool { + if repository != nil { + return repository.Provider == "github" && + isPublicGitHubProviderHost(repository.ProviderHost) && + repository.ProviderOwner == canonicalKandevOwner && repository.ProviderName == canonicalKandevName + } + return input != nil && input.Provider == "github" && + isPublicGitHubProviderHost(input.ProviderHost) && + input.ProviderOwner == canonicalKandevOwner && input.ProviderName == canonicalKandevName +} + +func isPublicGitHubProviderHost(raw string) bool { + raw = strings.TrimSpace(raw) + if raw == "" { + return false + } + if !strings.Contains(raw, "://") { + raw = "https://" + raw + } + parsed, err := url.Parse(raw) + if err != nil || parsed.User != nil || parsed.Hostname() == "" { + return false + } + return strings.EqualFold(parsed.Hostname(), "github.com") && parsed.Port() == "" && + (parsed.Path == "" || parsed.Path == "/") && parsed.RawQuery == "" && parsed.Fragment == "" +} + type githubBrokerScopeAuthorizer struct { repo interface { GetTask(context.Context, string) (*taskmodels.Task, error) @@ -291,18 +421,34 @@ type githubBrokerScopeAuthorizer struct { GetRepository(context.Context, string) (*taskmodels.Repository, error) ListTaskRepositories(context.Context, string) ([]*taskmodels.TaskRepository, error) } + provider interface { + VerifyContributionDestinationForWorkspace( + context.Context, string, string, string, string, string, string, string, + ) error + } } func (a *githubBrokerScopeAuthorizer) AuthorizeGitHubRepository( ctx context.Context, workspaceID, taskID, sessionID, repositoryID, owner, repoName string, ) error { - if err := a.AuthorizeGitCredential(ctx, gitcredentials.Scope{ - ProviderID: gitCredentialGitHubProviderID, WorkspaceID: workspaceID, TaskID: taskID, SessionID: sessionID, - RepositoryID: repositoryID, Host: gitCredentialGitHubHost, Path: "/" + owner + "/" + repoName + ".git", - }); err == nil { - return nil - } + return a.authorizeGitHubRepository(ctx, workspaceID, taskID, sessionID, repositoryID, owner, repoName, "", "", false) +} + +func (a *githubBrokerScopeAuthorizer) AuthorizeGitHubRepositoryWithIdentity( + ctx context.Context, + workspaceID, taskID, sessionID, repositoryID, owner, repoName, providerID, parentProviderID string, +) error { + return a.authorizeGitHubRepository( + ctx, workspaceID, taskID, sessionID, repositoryID, owner, repoName, providerID, parentProviderID, true, + ) +} + +func (a *githubBrokerScopeAuthorizer) authorizeGitHubRepository( + ctx context.Context, + workspaceID, taskID, sessionID, repositoryID, owner, repoName, providerID, parentProviderID string, + strictIdentity bool, +) error { if a == nil || a.repo == nil { return fmt.Errorf("task repository is unavailable") } @@ -313,20 +459,128 @@ func (a *githubBrokerScopeAuthorizer) AuthorizeGitHubRepository( if err != nil { return err } - if link == nil { - return fmt.Errorf("repository identity does not match lease scope") - } repository, err := a.repo.GetRepository(ctx, repositoryID) if err != nil { return err } - if repository == nil || repository.WorkspaceID != workspaceID || !strings.EqualFold(repository.Provider, "github") { + if repository == nil || repository.WorkspaceID != workspaceID || + !strings.EqualFold(repository.Provider, "github") || !isPublicGitHubProviderHost(repository.ProviderHost) { + return fmt.Errorf("repository identity does not match lease scope") + } + if strings.EqualFold(repository.ProviderOwner, owner) && strings.EqualFold(repository.ProviderName, repoName) { + return authorizeCanonicalGitHubRepository(repository, providerID, parentProviderID) + } + if link == nil { return fmt.Errorf("repository identity does not match lease scope") } binding, found, err := taskmodels.LoadRemoteContribution(link.Metadata) if err != nil { return fmt.Errorf("validate remote contribution scope: %w", err) } + if handled, destinationErr := a.authorizeContributionDestination( + ctx, workspaceID, repository, link, owner, repoName, providerID, parentProviderID, strictIdentity, + ); handled { + return destinationErr + } + return authorizeRemoteContribution(binding, found, owner, repoName, providerID, strictIdentity) +} + +func authorizeCanonicalGitHubRepository( + repository *taskmodels.Repository, + providerID, parentProviderID string, +) error { + if providerID != "" && !strings.EqualFold(repository.ProviderRepoID, providerID) { + return fmt.Errorf("repository provider identity does not match lease scope") + } + if parentProviderID != "" { + return fmt.Errorf("repository parent identity does not match lease scope") + } + return nil +} + +func (a *githubBrokerScopeAuthorizer) authorizeContributionDestination( + ctx context.Context, + workspaceID string, + repository *taskmodels.Repository, + link *taskmodels.TaskRepository, + owner, repoName, providerID, parentProviderID string, + strictIdentity bool, +) (bool, error) { + destination, found, err := taskmodels.LoadContributionDestination(link.Metadata) + if err != nil { + return true, fmt.Errorf("validate contribution destination scope: %w", err) + } + if !found || destination.Provider != taskmodels.ContributionDestinationProviderGitHub || + !strings.EqualFold(destination.TargetRepository.Host, "github.com") { + return false, nil + } + if !contributionDestinationScopeMatches( + destination, repository, owner, repoName, providerID, parentProviderID, strictIdentity, + ) { + return false, nil + } + if !strictIdentity { + return true, nil + } + return true, a.verifyContributionDestination( + ctx, workspaceID, destination, owner, repoName, + ) +} + +func contributionDestinationScopeMatches( + destination taskmodels.ContributionDestination, + repository *taskmodels.Repository, + owner, repoName, providerID, parentProviderID string, + strictIdentity bool, +) bool { + parts := strings.Split(destination.TargetRepository.Path, "/") + canonical := strings.TrimSpace(repository.ProviderOwner) + "/" + strings.TrimSpace(repository.ProviderName) + if len(parts) != 2 || !strings.EqualFold(parts[0], owner) || !strings.EqualFold(parts[1], repoName) || + !strings.EqualFold(destination.SourceRepository.Path, canonical) { + return false + } + if repository.ProviderRepoID != "" && + !strings.EqualFold(destination.SourceRepository.ProviderID, repository.ProviderRepoID) { + return false + } + if !strictIdentity { + return true + } + return providerID != "" && parentProviderID != "" && + strings.EqualFold(destination.TargetRepository.ProviderID, providerID) && + strings.EqualFold(destination.SourceRepository.ProviderID, parentProviderID) && + strings.TrimSpace(repository.ProviderRepoID) != "" && + strings.EqualFold(destination.SourceRepository.ProviderID, repository.ProviderRepoID) +} + +func (a *githubBrokerScopeAuthorizer) verifyContributionDestination( + ctx context.Context, + workspaceID string, + destination taskmodels.ContributionDestination, + owner, repoName string, +) error { + if a.provider == nil { + return fmt.Errorf("contribution destination provider verifier is unavailable") + } + sourceParts := strings.Split(destination.SourceRepository.Path, "/") + if len(sourceParts) != 2 { + return fmt.Errorf("contribution destination provider identity does not match lease scope") + } + if err := a.provider.VerifyContributionDestinationForWorkspace( + ctx, workspaceID, sourceParts[0], sourceParts[1], destination.SourceRepository.ProviderID, + owner, repoName, destination.TargetRepository.ProviderID, + ); err != nil { + return fmt.Errorf("contribution destination provider identity does not match lease scope") + } + return nil +} + +func authorizeRemoteContribution( + binding taskmodels.RemoteContribution, + found bool, + owner, repoName, providerID string, + strictIdentity bool, +) error { if !found || binding.Provider != taskmodels.RemoteContributionProviderGitHub || !binding.CollaborationAllowed || !strings.EqualFold(binding.SourceRepository.Host, "github.com") { return fmt.Errorf("repository identity does not match lease scope") @@ -335,6 +589,10 @@ func (a *githubBrokerScopeAuthorizer) AuthorizeGitHubRepository( if len(parts) != 2 || !strings.EqualFold(parts[0], owner) || !strings.EqualFold(parts[1], repoName) { return fmt.Errorf("repository identity does not match lease scope") } + if strictIdentity && binding.SourceRepository.ProviderID != "" && + !strings.EqualFold(binding.SourceRepository.ProviderID, providerID) { + return fmt.Errorf("repository provider identity does not match lease scope") + } return nil } diff --git a/apps/backend/internal/backendapp/services_github_broker_test.go b/apps/backend/internal/backendapp/services_github_broker_test.go index df0ba249d4..de4ffe9993 100644 --- a/apps/backend/internal/backendapp/services_github_broker_test.go +++ b/apps/backend/internal/backendapp/services_github_broker_test.go @@ -11,6 +11,7 @@ import ( "github.com/kandev/kandev/internal/gitcredentials" "github.com/kandev/kandev/internal/repoclone" taskmodels "github.com/kandev/kandev/internal/task/models" + taskservice "github.com/kandev/kandev/internal/task/service" "github.com/kandev/kandev/pkg/pluginsdk" ) @@ -30,6 +31,18 @@ type recordingPluginCredentialRemote struct { binding string } +type fakeContributionDestinationVerifier struct { + err error + calls int +} + +func (f *fakeContributionDestinationVerifier) VerifyContributionDestinationForWorkspace( + context.Context, string, string, string, string, string, string, string, +) error { + f.calls++ + return f.err +} + func (r *recordingPluginCredentialRemote) GetGitCredentialBinding( _ context.Context, request *pluginsdk.GitCredentialBindingRequest, @@ -198,7 +211,8 @@ func TestGitHubBrokerScopeAuthorizerValidatesSessionOwnershipAndState(t *testing session: &taskmodels.TaskSession{ID: "session-1", TaskID: "task-1", State: taskmodels.TaskSessionStateRunning}, repository: &taskmodels.Repository{ ID: "repository-1", WorkspaceID: "workspace-1", Provider: "github", - ProviderOwner: "kdlbs", ProviderName: kandevName, + ProviderHost: "https://github.com", + ProviderOwner: "kdlbs", ProviderName: "kandev", }, links: []*taskmodels.TaskRepository{{TaskID: "task-1", RepositoryID: "repository-1"}}, } @@ -249,7 +263,7 @@ func TestGitHubBrokerScopeAuthorizerAllowsOnlyTheBoundContributionFork(t *testin repo := &fakeGitHubBrokerTaskRepository{ task: &taskmodels.Task{ID: "task-fork", WorkspaceID: "workspace-fork"}, session: &taskmodels.TaskSession{ID: "session-fork", TaskID: "task-fork", State: taskmodels.TaskSessionStateRunning}, - repository: &taskmodels.Repository{ID: "repository-fork", WorkspaceID: "workspace-fork", Provider: "github", ProviderOwner: "acme", ProviderName: "widget"}, + repository: &taskmodels.Repository{ID: "repository-fork", WorkspaceID: "workspace-fork", Provider: "github", ProviderHost: "https://github.com", ProviderOwner: "acme", ProviderName: "widget"}, links: []*taskmodels.TaskRepository{{TaskID: "task-fork", RepositoryID: "repository-fork", Metadata: metadata}}, } authorizer := &githubBrokerScopeAuthorizer{repo: repo} @@ -275,3 +289,105 @@ func TestGitHubBrokerScopeAuthorizerAllowsOnlyTheBoundContributionFork(t *testin t.Fatalf("non-collaborative fork error = %v, want identity denial", err) } } + +func TestGitHubBrokerScopeAuthorizerAllowsOnlyTheBoundContributionDestination(t *testing.T) { + destination := taskmodels.ContributionDestination{ + Version: taskmodels.ContributionDestinationVersion, + Provider: taskmodels.ContributionDestinationProviderGitHub, + SourceRepository: taskmodels.ContributionDestinationRepository{ + Host: "github.com", Path: "kdlbs/kandev", ProviderID: "100", RemoteURL: "https://github.com/kdlbs/kandev.git", + }, + TargetRepository: taskmodels.ContributionDestinationRepository{ + Host: "github.com", Path: "automation/kandev", ProviderID: "200", RemoteURL: "https://github.com/automation/kandev.git", + }, + } + metadata := map[string]interface{}{} + if err := taskmodels.PutContributionDestination(metadata, &destination); err != nil { + t.Fatalf("PutContributionDestination() error = %v", err) + } + repo := &fakeGitHubBrokerTaskRepository{ + task: &taskmodels.Task{ID: "task-destination", WorkspaceID: "workspace-destination"}, + session: &taskmodels.TaskSession{ID: "session-destination", TaskID: "task-destination", State: taskmodels.TaskSessionStateRunning}, + repository: &taskmodels.Repository{ID: "repository-destination", WorkspaceID: "workspace-destination", Provider: "github", ProviderHost: "https://github.com", ProviderRepoID: "999", ProviderOwner: "kdlbs", ProviderName: "kandev"}, + links: []*taskmodels.TaskRepository{{TaskID: "task-destination", RepositoryID: "repository-destination", Metadata: metadata}}, + } + verifier := &fakeContributionDestinationVerifier{} + authorizer := &githubBrokerScopeAuthorizer{repo: repo, provider: verifier} + + if err := authorizer.AuthorizeGitHubRepository( + context.Background(), "workspace-destination", "task-destination", "session-destination", "repository-destination", "automation", "kandev", + ); err == nil || !strings.Contains(err.Error(), "identity does not match") { + t.Fatal("stale canonical provider ID was accepted for a contribution destination") + } + + repo.repository.ProviderRepoID = "100" + if err := authorizer.AuthorizeGitHubRepository(context.Background(), "workspace-destination", "task-destination", "session-destination", "repository-destination", "automation", "kandev"); err != nil { + t.Fatalf("bound destination was denied: %v", err) + } + if err := authorizer.AuthorizeGitHubRepositoryWithIdentity( + context.Background(), "workspace-destination", "task-destination", "session-destination", "repository-destination", "automation", "kandev", "200", "100", + ); err != nil { + t.Fatalf("bound destination identity was denied: %v", err) + } + if verifier.calls != 1 { + t.Fatalf("provider verification calls = %d, want 1", verifier.calls) + } + if err := authorizer.AuthorizeGitHubRepositoryWithIdentity( + context.Background(), "workspace-destination", "task-destination", "session-destination", "repository-destination", "automation", "kandev", "201", "100", + ); err == nil || !strings.Contains(err.Error(), "identity does not match") { + t.Fatal("delete/recreate target with a different provider ID was accepted") + } + if err := authorizer.AuthorizeGitHubRepositoryWithIdentity( + context.Background(), "workspace-destination", "task-destination", "session-destination", "repository-destination", "automation", "kandev", "200", "101", + ); err == nil || !strings.Contains(err.Error(), "identity does not match") { + t.Fatal("target with a different parent provider ID was accepted") + } + if err := authorizer.AuthorizeGitHubRepository(context.Background(), "workspace-destination", "task-destination", "session-destination", "repository-destination", "automation", "other"); err == nil || !strings.Contains(err.Error(), "identity does not match") { + t.Fatalf("unbound destination error = %v, want identity denial", err) + } +} + +func TestIsCanonicalKandevRepositoryInputRequiresPublicGitHubHost(t *testing.T) { + for _, host := range []string{"", "https://github.enterprise.example"} { + input := &taskservice.TaskRepositoryInput{ + Provider: "github", ProviderHost: host, ProviderOwner: "kdlbs", ProviderName: "kandev", + } + if isCanonicalKandevRepositoryInput(input, nil) { + t.Fatalf("host %q was accepted as the public canonical GitHub repository", host) + } + } + if !isCanonicalKandevRepositoryInput(&taskservice.TaskRepositoryInput{ + Provider: "github", ProviderHost: "https://github.com", ProviderOwner: "kdlbs", ProviderName: "kandev", + }, nil) { + t.Fatal("public GitHub canonical repository was rejected") + } +} + +func TestGitHubBrokerScopeAuthorizerRejectsNonPublicProviderHostsForDestination(t *testing.T) { + destination := taskmodels.ContributionDestination{ + Version: taskmodels.ContributionDestinationVersion, + Provider: taskmodels.ContributionDestinationProviderGitHub, + SourceRepository: taskmodels.ContributionDestinationRepository{ + Host: "github.com", Path: "kdlbs/kandev", ProviderID: "100", RemoteURL: "https://github.com/kdlbs/kandev.git", + }, + TargetRepository: taskmodels.ContributionDestinationRepository{ + Host: "github.com", Path: "automation/kandev", ProviderID: "200", RemoteURL: "https://github.com/automation/kandev.git", + }, + } + metadata := map[string]interface{}{} + if err := taskmodels.PutContributionDestination(metadata, &destination); err != nil { + t.Fatalf("PutContributionDestination() = %v", err) + } + for _, host := range []string{"", "https://github.enterprise.example"} { + repo := &fakeGitHubBrokerTaskRepository{ + task: &taskmodels.Task{ID: "task-host", WorkspaceID: "workspace-host"}, + session: &taskmodels.TaskSession{ID: "session-host", TaskID: "task-host", State: taskmodels.TaskSessionStateRunning}, + repository: &taskmodels.Repository{ID: "repository-host", WorkspaceID: "workspace-host", Provider: "github", ProviderHost: host, ProviderOwner: "kdlbs", ProviderName: "kandev"}, + links: []*taskmodels.TaskRepository{{TaskID: "task-host", RepositoryID: "repository-host", Metadata: metadata}}, + } + authorizer := &githubBrokerScopeAuthorizer{repo: repo} + if err := authorizer.AuthorizeGitHubRepository(context.Background(), "workspace-host", "task-host", "session-host", "repository-host", "automation", "kandev"); err == nil { + t.Fatalf("provider host %q was accepted for a managed destination", host) + } + } +} diff --git a/apps/backend/internal/gitcredentials/broker.go b/apps/backend/internal/gitcredentials/broker.go index 48f31d1be0..997063e7aa 100644 --- a/apps/backend/internal/gitcredentials/broker.go +++ b/apps/backend/internal/gitcredentials/broker.go @@ -32,25 +32,30 @@ var ( // Scope is the host-verified identity a lease may redeem for. Path is the // exact HTTPS remote path supplied to a provider resolver. type Scope struct { - ProviderID string - WorkspaceID string - TaskID string - SessionID string - RepositoryID string - Host string - Path string - TTL time.Duration + ProviderID string + WorkspaceID string + TaskID string + SessionID string + RepositoryID string + Host string + Path string + IdentityProviderID string + ParentProviderID string + CredentialBinding string + TTL time.Duration } // Redemption is helper-supplied, non-secret lease context. ProviderID and // WorkspaceID are deliberately absent: the opaque lease selects those fields. type Redemption struct { - Lease string - TaskID string - SessionID string - RepositoryID string - Host string - Path string + Lease string + TaskID string + SessionID string + RepositoryID string + Host string + Path string + IdentityProviderID string + ParentProviderID string } // Lease is safe to pass only to the credential helper. Its token is never @@ -362,6 +367,9 @@ func normalizeScope(scope Scope) (Scope, error) { scope.SessionID = strings.TrimSpace(scope.SessionID) scope.RepositoryID = strings.TrimSpace(scope.RepositoryID) scope.Host = strings.ToLower(strings.TrimSpace(scope.Host)) + scope.IdentityProviderID = strings.TrimSpace(scope.IdentityProviderID) + scope.ParentProviderID = strings.TrimSpace(scope.ParentProviderID) + scope.CredentialBinding = strings.TrimSpace(scope.CredentialBinding) path, err := normalizePath(scope.Path) if err != nil { return Scope{}, err @@ -398,7 +406,9 @@ func matchesRedemption(scope Scope, request Redemption) bool { scope.SessionID == strings.TrimSpace(request.SessionID) && scope.RepositoryID == strings.TrimSpace(request.RepositoryID) && strings.EqualFold(scope.Host, strings.TrimSpace(request.Host)) && - scope.Path == path + scope.Path == path && + scope.IdentityProviderID == strings.TrimSpace(request.IdentityProviderID) && + scope.ParentProviderID == strings.TrimSpace(request.ParentProviderID) } func boundedTTL(requested time.Duration) time.Duration { diff --git a/apps/backend/internal/github/controller_auth.go b/apps/backend/internal/github/controller_auth.go index c27788eccb..3bf56d4fe8 100644 --- a/apps/backend/internal/github/controller_auth.go +++ b/apps/backend/internal/github/controller_auth.go @@ -242,14 +242,16 @@ func (c *Controller) httpGitHubAppWebhook(ctx *gin.Context) { func (c *Controller) httpResolveCredentialLease(ctx *gin.Context) { var request struct { - Lease string `json:"lease"` - TaskID string `json:"task_id"` - SessionID string `json:"session_id"` - RepositoryID string `json:"repository_id"` - Owner string `json:"owner"` - Repo string `json:"repo"` - Host string `json:"host"` - Path string `json:"path"` + Lease string `json:"lease"` + TaskID string `json:"task_id"` + SessionID string `json:"session_id"` + RepositoryID string `json:"repository_id"` + Owner string `json:"owner"` + Repo string `json:"repo"` + Host string `json:"host"` + Path string `json:"path"` + ProviderID string `json:"provider_id"` + ParentProviderID string `json:"parent_provider_id"` } if err := ctx.ShouldBindJSON(&request); err != nil { ctx.JSON(http.StatusBadRequest, gin.H{"code": "github_invalid_request", "error": "invalid payload"}) @@ -258,6 +260,7 @@ func (c *Controller) httpResolveCredentialLease(ctx *gin.Context) { credential, err := c.service.ResolveGitHubCredential(ctx.Request.Context(), BrokerCredentialRequest{ Lease: request.Lease, TaskID: request.TaskID, SessionID: request.SessionID, RepositoryID: request.RepositoryID, Owner: request.Owner, Repo: request.Repo, Host: request.Host, Path: request.Path, + ProviderID: request.ProviderID, ParentProviderID: request.ParentProviderID, }) if err != nil { writeGitHubAuthError(ctx, err) diff --git a/apps/backend/internal/github/credential_broker.go b/apps/backend/internal/github/credential_broker.go index 926b08b848..e2898c9ff8 100644 --- a/apps/backend/internal/github/credential_broker.go +++ b/apps/backend/internal/github/credential_broker.go @@ -8,6 +8,7 @@ import ( "time" "github.com/kandev/kandev/internal/gitcredentials" + taskmodels "github.com/kandev/kandev/internal/task/models" ) const gitHubTokenUsername = "x-access-token" @@ -29,15 +30,29 @@ type BrokerScopeAuthorizer interface { ) error } +// BrokerScopeAuthorizerWithIdentity extends the task scope check with the +// provider identity that was used to issue the lease. The optional interface +// keeps legacy test and compatibility authorizers source-compatible while the +// production authorizer enforces stable repository IDs. +type BrokerScopeAuthorizerWithIdentity interface { + AuthorizeGitHubRepositoryWithIdentity( + ctx context.Context, + workspaceID, taskID, sessionID, repositoryID, owner, repo, providerID, parentProviderID string, + ) error +} + type CredentialLeaseRequest struct { - WorkspaceID string - TaskID string - SessionID string - RepositoryID string - Owner string - Repo string - Host string - TTL time.Duration + WorkspaceID string + TaskID string + SessionID string + RepositoryID string + Owner string + Repo string + Host string + ProviderID string + ParentProviderID string + DestinationBinding *taskmodels.ContributionDestinationCredentialBinding + TTL time.Duration } type CredentialLease struct { @@ -46,14 +61,16 @@ type CredentialLease struct { } type BrokerCredentialRequest struct { - Lease string - TaskID string - SessionID string - RepositoryID string - Owner string - Repo string - Host string - Path string + Lease string + TaskID string + SessionID string + RepositoryID string + Owner string + Repo string + Host string + Path string + ProviderID string + ParentProviderID string } type BrokerCredential struct { @@ -99,10 +116,19 @@ func (b *CredentialBroker) Issue(ctx context.Context, req CredentialLeaseRequest if b == nil || b.broker == nil { return nil, ErrGitHubNotConfigured } - lease, err := b.broker.Issue(ctx, gitcredentials.Scope{ + scope := gitcredentials.Scope{ ProviderID: githubProviderName, WorkspaceID: req.WorkspaceID, TaskID: req.TaskID, SessionID: req.SessionID, RepositoryID: req.RepositoryID, Host: req.Host, Path: githubCredentialPath(req.Owner, req.Repo), TTL: req.TTL, - }) + IdentityProviderID: req.ProviderID, ParentProviderID: req.ParentProviderID, + } + if req.DestinationBinding != nil { + encodedBinding, err := json.Marshal(req.DestinationBinding) + if err != nil { + return nil, fmt.Errorf("encode contribution destination credential binding: %w", err) + } + scope.CredentialBinding = string(encodedBinding) + } + lease, err := b.broker.Issue(ctx, scope) if err != nil { return nil, err } @@ -119,7 +145,7 @@ func (b *CredentialBroker) Resolve(ctx context.Context, req BrokerCredentialRequ } credential, err := b.broker.Redeem(ctx, gitcredentials.Redemption{ Lease: req.Lease, TaskID: req.TaskID, SessionID: req.SessionID, RepositoryID: req.RepositoryID, - Host: req.Host, Path: path, + Host: req.Host, Path: path, IdentityProviderID: req.ProviderID, ParentProviderID: req.ParentProviderID, }) if err != nil { return nil, err @@ -165,6 +191,12 @@ func (a gitHubScopeAuthorizer) AuthorizeGitCredential(ctx context.Context, scope if err != nil { return err } + if identityAuthorizer, ok := a.authorizer.(BrokerScopeAuthorizerWithIdentity); ok { + return identityAuthorizer.AuthorizeGitHubRepositoryWithIdentity( + ctx, scope.WorkspaceID, scope.TaskID, scope.SessionID, scope.RepositoryID, owner, repo, + scope.IdentityProviderID, scope.ParentProviderID, + ) + } return a.authorizer.AuthorizeGitHubRepository( ctx, scope.WorkspaceID, scope.TaskID, scope.SessionID, scope.RepositoryID, owner, repo, ) @@ -187,6 +219,15 @@ func (p *gitHubCredentialProvider) Binding(ctx context.Context, scope gitcredent if err != nil { return "", err } + if rawBinding := strings.TrimSpace(scope.CredentialBinding); rawBinding != "" { + var binding taskmodels.ContributionDestinationCredentialBinding + if err := json.Unmarshal([]byte(rawBinding), &binding); err != nil { + return "", fmt.Errorf("%w: invalid destination credential binding", ErrCredentialScopeDenied) + } + if err := binding.Validate(); err != nil || !destinationBindingMatchesConnection(&binding, connection, appGeneration) { + return "", fmt.Errorf("%w: destination credential binding does not match the active workspace connection", ErrCredentialScopeDenied) + } + } return githubCredentialBindingFor(connection, appGeneration) } @@ -274,3 +315,23 @@ func githubOwnerRepo(path string) (string, string, error) { } return owner, repo, nil } + +func destinationBindingMatchesConnection( + binding *taskmodels.ContributionDestinationCredentialBinding, + connection *WorkspaceConnection, + appCredentialGeneration int64, +) bool { + if binding == nil || connection == nil || binding.Source != string(connection.Source) || + binding.CredentialGeneration != connection.CredentialGeneration || + binding.AppRegistrationID != connection.AppRegistrationID { + return false + } + if connection.Source == ConnectionSourceGitHubAppInstallation { + return connection.InstallationID != nil && binding.InstallationID == *connection.InstallationID && + binding.AppCredentialGeneration == appCredentialGeneration + } + if binding.Login != "" && !strings.EqualFold(binding.Login, connection.Login) { + return false + } + return binding.AppCredentialGeneration == 0 && binding.InstallationID == 0 +} diff --git a/apps/backend/internal/github/credential_broker_test.go b/apps/backend/internal/github/credential_broker_test.go index 17e5f90933..a8040370e2 100644 --- a/apps/backend/internal/github/credential_broker_test.go +++ b/apps/backend/internal/github/credential_broker_test.go @@ -5,12 +5,15 @@ import ( "errors" "fmt" "testing" + + taskmodels "github.com/kandev/kandev/internal/task/models" ) type fakeBrokerAuthorizer struct { - err error - calls int - sessionIDs []string + err error + calls int + sessionIDs []string + identityProviderIDs []string } type fakeBrokerInstallationProvider struct { @@ -70,6 +73,14 @@ func (a *fakeBrokerAuthorizer) AuthorizeGitHubRepository( return a.err } +func (a *fakeBrokerAuthorizer) AuthorizeGitHubRepositoryWithIdentity( + ctx context.Context, + workspaceID, taskID, sessionID, repositoryID, owner, repo, providerID, parentProviderID string, +) error { + a.identityProviderIDs = append(a.identityProviderIDs, providerID+"/"+parentProviderID) + return a.AuthorizeGitHubRepository(ctx, workspaceID, taskID, sessionID, repositoryID, owner, repo) +} + func newPATCredentialBroker(t *testing.T) (*CredentialBroker, *WorkspaceConnection, *fakeBrokerAuthorizer) { t.Helper() connection := &WorkspaceConnection{ @@ -181,6 +192,39 @@ func TestCredentialBrokerGenerationChangeRevokesLease(t *testing.T) { } } +func TestCredentialBrokerRejectsDestinationLeaseForChangedWorkspaceConnection(t *testing.T) { + broker, connection, authorizer := newPATCredentialBroker(t) + binding := &taskmodels.ContributionDestinationCredentialBinding{ + Source: string(ConnectionSourcePAT), + Login: "octocat", + CredentialGeneration: 7, + } + req := brokerLeaseRequest() + req.ProviderID = "200" + req.ParentProviderID = "100" + req.DestinationBinding = binding + + lease, err := broker.Issue(context.Background(), req) + if err != nil { + t.Fatalf("Issue with matching binding: %v", err) + } + if got := authorizer.identityProviderIDs; len(got) != 1 || got[0] != "200/100" { + t.Fatalf("issued identity checks = %v, want [200/100]", got) + } + connection.Login = "different-user" + connection.CredentialGeneration++ + + if _, err := broker.Issue(context.Background(), req); !errors.Is(err, ErrCredentialScopeDenied) { + t.Fatalf("Issue with changed connection error = %v, want scope denial", err) + } + redemption := brokerCredentialRequest(lease.Token) + redemption.ProviderID = "200" + redemption.ParentProviderID = "100" + if _, err := broker.Resolve(context.Background(), redemption); !errors.Is(err, ErrCredentialLeaseRevoked) { + t.Fatalf("Resolve with changed connection error = %v, want lease revoked", err) + } +} + func TestCredentialBrokerAppRegistrationChangeRevokesLease(t *testing.T) { installationID := int64(42) connection := &WorkspaceConnection{ diff --git a/apps/backend/internal/github/gh_client.go b/apps/backend/internal/github/gh_client.go index de49579b87..c4d65ef782 100644 --- a/apps/backend/internal/github/gh_client.go +++ b/apps/backend/internal/github/gh_client.go @@ -287,23 +287,41 @@ func (c *GHClient) GetIssue(ctx context.Context, owner, repo string, number int) } func (c *GHClient) FindPRByBranch(ctx context.Context, owner, repo, branch string) (*PR, error) { + return c.findPRByHead(ctx, owner, repo, branch, branch, "", "") +} + +func (c *GHClient) FindPRByHead(ctx context.Context, owner, repo, headOwner, headRepo, branch string) (*PR, error) { + return c.findPRByHead(ctx, owner, repo, branch, branch, headOwner, headRepo) +} + +func (c *GHClient) findPRByHead(ctx context.Context, owner, repo, head, branchForError, headOwner, headRepo string) (*PR, error) { + limit := "1" + if headOwner != "" { + // --head accepts only a branch name. Fetch enough matches to select the + // requested fork after gh returns PRs from the whole fork network. + limit = "100" + } out, err := c.run(ctx, "pr", "list", "--repo", fmt.Sprintf("%s/%s", owner, repo), - "--head", branch, + "--head", head, "--state", "open", - "--json", "number,title,url,state,headRefName,headRefOid,baseRefName,author,isDraft,mergeable,mergeStateStatus,additions,deletions,createdAt,updatedAt", - "--limit", "1") + "--json", "number,title,url,state,headRefName,headRefOid,baseRefName,author,isDraft,mergeable,mergeStateStatus,additions,deletions,createdAt,updatedAt,headRepository,headRepositoryOwner", + "--limit", limit) if err != nil { - return nil, fmt.Errorf("find PR by branch %q: %w", branch, err) + return nil, fmt.Errorf("find PR by branch %q: %w", branchForError, err) } var prs []ghPR if err := json.Unmarshal([]byte(out), &prs); err != nil { return nil, fmt.Errorf("parse PR list: %w", err) } - if len(prs) == 0 { - return nil, nil + for i := range prs { + pr := convertGHPR(&prs[i], owner, repo) + if (headOwner == "" && headRepo == "") || + sameRepositoryIdentity(pr.HeadRepoOwner, pr.HeadRepoName, headOwner, headRepo) { + return pr, nil + } } - return convertGHPR(&prs[0], owner, repo), nil + return nil, nil } func (c *GHClient) ListAuthoredPRs(ctx context.Context, owner, repo string) ([]*PR, error) { @@ -509,6 +527,63 @@ func (c *GHClient) HasRepositoryAccess(ctx context.Context, owner, repo string) return strings.TrimSpace(out) != "", nil } +// GetRepository returns the selected gh account's repository identity and +// permissions for managed contribution-destination preparation. +func (c *GHClient) GetRepository(ctx context.Context, owner, repo string) (*GitHubRepository, error) { + out, err := c.run(ctx, "api", fmt.Sprintf("/repos/%s/%s", owner, repo)) + if err != nil { + if isNotFoundErr(err) { + return nil, fmt.Errorf("get repository %s/%s: %w", owner, repo, &GitHubAPIError{ + StatusCode: http.StatusNotFound, + Endpoint: fmt.Sprintf("/repos/%s/%s", owner, repo), + Body: err.Error(), + }) + } + return nil, fmt.Errorf("get repository %s/%s: %w", owner, repo, err) + } + var raw githubRepositoryResponse + if err := json.Unmarshal([]byte(out), &raw); err != nil { + return nil, fmt.Errorf("decode repository %s/%s: %w", owner, repo, err) + } + return projectGitHubRepository(raw), nil +} + +// ListRepositoryForks lists the canonical repository's fork network. The +// network is authoritative for finding an existing fork whose name changed +// after creation, so callers must not assume the canonical repository name. +func (c *GHClient) ListRepositoryForks(ctx context.Context, owner, repo string) ([]*GitHubRepository, error) { + out, err := c.run(ctx, "api", fmt.Sprintf("repos/%s/%s/forks?per_page=100", owner, repo), "--paginate", "--slurp") + if err != nil { + return nil, fmt.Errorf("list repository forks for %s/%s: %w", owner, repo, err) + } + var pages [][]githubRepositoryResponse + if err := json.Unmarshal([]byte(out), &pages); err != nil { + return nil, fmt.Errorf("decode repository forks for %s/%s: %w", owner, repo, err) + } + forks := make([]*GitHubRepository, 0) + for _, page := range pages { + for _, raw := range page { + forks = append(forks, projectGitHubRepository(raw)) + } + } + return forks, nil +} + +// CreateFork creates a fork for the selected named gh account. The service +// resolver verifies the returned identity after GitHub finishes provisioning. +func (c *GHClient) CreateFork(ctx context.Context, owner, repo string) (*GitHubRepository, error) { + args := []string{"api", fmt.Sprintf("/repos/%s/%s/forks", owner, repo), "-X", "POST", "--input", "-"} + out, err := c.runWithStdin(ctx, []byte(`{}`), args...) + if err != nil { + return nil, fmt.Errorf("create fork for %s/%s: %w", owner, repo, err) + } + var raw githubRepositoryResponse + if err := json.Unmarshal([]byte(out), &raw); err != nil { + return nil, fmt.Errorf("decode created fork for %s/%s: %w", owner, repo, err) + } + return projectGitHubRepository(raw), nil +} + // buildAccessibleReposGHArgs constructs the `gh api /user/repos?...` argv for // ListAccessibleRepos. Keeping it pure makes the endpoint/query construction // unit-testable without spawning gh. Callers must clamp `limit` via diff --git a/apps/backend/internal/github/gh_client_reads_test.go b/apps/backend/internal/github/gh_client_reads_test.go index 6f3e5545a7..204f80d344 100644 --- a/apps/backend/internal/github/gh_client_reads_test.go +++ b/apps/backend/internal/github/gh_client_reads_test.go @@ -100,6 +100,38 @@ func TestGHClient_GetIssue_OtherFailureIsWrapped(t *testing.T) { } } +func TestGHClient_GetRepository_NotFoundBecomesTypedError(t *testing.T) { + newFakeGH(t, ghResponse{Prefix: "api /repos/acme/widget", Stderr: "gh: 404 Not Found", Exit: 1}) + repository, err := NewGHClient().GetRepository(context.Background(), "acme", "widget") + if repository != nil { + t.Errorf("repository = %#v, want nil", repository) + } + var apiErr *GitHubAPIError + if !errors.As(err, &apiErr) || apiErr.StatusCode != http.StatusNotFound { + t.Fatalf("err = %v, want a *GitHubAPIError with 404", err) + } + if apiErr.Endpoint != "/repos/acme/widget" { + t.Errorf("endpoint = %q", apiErr.Endpoint) + } +} + +func TestGHClient_ListRepositoryForksUsesForkNetwork(t *testing.T) { + calls := newFakeGH(t, ghResponse{ + Prefix: "api repos/acme/widget/forks?per_page=100", + Stdout: `[[{"id":200,"full_name":"alice/widget-renamed","name":"widget-renamed","owner":{"login":"alice"},"fork":true,"parent":{"id":100,"full_name":"acme/widget"},"permissions":{"push":true}}]]`, + }) + forks, err := NewGHClient().ListRepositoryForks(context.Background(), "acme", "widget") + if err != nil { + t.Fatalf("ListRepositoryForks: %v", err) + } + assertGHArgv(t, calls(t), 0, []string{ + "api", "repos/acme/widget/forks?per_page=100", "--paginate", "--slurp", + }) + if len(forks) != 1 || forks[0].FullName != "alice/widget-renamed" || forks[0].ParentID != 100 { + t.Fatalf("forks = %#v", forks) + } +} + func TestGHClient_GetPR_NotFoundBecomesTypedError(t *testing.T) { newFakeGH(t, ghResponse{Prefix: "pr view", Stderr: "gh: HTTP 404: Not Found", Exit: 1}) pr, err := NewGHClient().GetPR(context.Background(), "acme", "widget", 7) @@ -140,7 +172,7 @@ func TestGHClient_FindPRByBranch(t *testing.T) { assertGHArgv(t, calls(t), 0, []string{ "pr", "list", "--repo", "acme/widget", "--head", "feature", "--state", "open", "--json", "number,title,url,state,headRefName,headRefOid,baseRefName,author,isDraft," + - "mergeable,mergeStateStatus,additions,deletions,createdAt,updatedAt", + "mergeable,mergeStateStatus,additions,deletions,createdAt,updatedAt,headRepository,headRepositoryOwner", "--limit", "1", }) if pr == nil { @@ -160,6 +192,63 @@ func TestGHClient_FindPRByBranch(t *testing.T) { } } +func TestGHClient_FindPRByHead_UsesExactSourceRepository(t *testing.T) { + calls := newFakeGH(t, ghResponse{ + Prefix: "pr list", + Stdout: `[{ + "number":12,"title":"fork PR","url":"https://github.com/kdlbs/kandev/pull/12", + "state":"OPEN","headRefName":"feature","headRefOid":"abc123","baseRefName":"main", + "author":{"login":"alice"},"isDraft":false,"mergeable":"MERGEABLE", + "mergeStateStatus":"CLEAN","headRepository":{"id":"200","name":"kandev","nameWithOwner":"alice/kandev"}, + "headRepositoryOwner":{"login":"alice"} + }]`, + }) + + pr, err := NewGHClient().FindPRByHead(context.Background(), "kdlbs", "kandev", "alice", "kandev", "feature") + if err != nil { + t.Fatalf("FindPRByHead: %v", err) + } + assertGHArgv(t, calls(t), 0, []string{ + "pr", "list", "--repo", "kdlbs/kandev", "--head", "feature", "--state", "open", + "--json", "number,title,url,state,headRefName,headRefOid,baseRefName,author,isDraft," + + "mergeable,mergeStateStatus,additions,deletions,createdAt,updatedAt,headRepository,headRepositoryOwner", + "--limit", "100", + }) + if pr == nil || pr.HeadRepoOwner != "alice" || pr.HeadRepoName != "kandev" { + t.Fatalf("PR = %#v, want source alice/kandev", pr) + } +} + +func TestGHClient_FindPRByHead_ScansAllMatchingBranches(t *testing.T) { + calls := newFakeGH(t, ghResponse{ + Prefix: "pr list", + Stdout: `[ + {"number":11,"title":"other fork","url":"https://github.com/kdlbs/kandev/pull/11", + "state":"OPEN","headRefName":"feature","headRefOid":"other","baseRefName":"main", + "headRepository":{"name":"other-kandev","nameWithOwner":"alice/other-kandev"}, + "headRepositoryOwner":{"login":"alice"}}, + {"number":12,"title":"requested fork","url":"https://github.com/kdlbs/kandev/pull/12", + "state":"OPEN","headRefName":"feature","headRefOid":"requested","baseRefName":"main", + "headRepository":{"name":"kandev","nameWithOwner":"alice/kandev"}, + "headRepositoryOwner":{"login":"alice"}} + ]`, + }) + + pr, err := NewGHClient().FindPRByHead(context.Background(), "kdlbs", "kandev", "alice", "kandev", "feature") + if err != nil { + t.Fatalf("FindPRByHead: %v", err) + } + assertGHArgv(t, calls(t), 0, []string{ + "pr", "list", "--repo", "kdlbs/kandev", "--head", "feature", "--state", "open", + "--json", "number,title,url,state,headRefName,headRefOid,baseRefName,author,isDraft," + + "mergeable,mergeStateStatus,additions,deletions,createdAt,updatedAt,headRepository,headRepositoryOwner", + "--limit", "100", + }) + if pr == nil || pr.Number != 12 || pr.HeadRepoOwner != "alice" { + t.Fatalf("PR = %#v, want the requested fork PR", pr) + } +} + func TestGHClient_FindPRByBranch_NoMatchReturnsNilWithoutError(t *testing.T) { newFakeGH(t, ghResponse{Prefix: "pr list", Stdout: `[]`}) pr, err := NewGHClient().FindPRByBranch(context.Background(), "acme", "widget", "feature") diff --git a/apps/backend/internal/github/mock_client.go b/apps/backend/internal/github/mock_client.go index 8f26e78f13..e8170971c4 100644 --- a/apps/backend/internal/github/mock_client.go +++ b/apps/backend/internal/github/mock_client.go @@ -116,6 +116,7 @@ type MockClient struct { requestedReviews []requestedReviewers mergedPRs []mergedPR mergeMethods map[repoKey]RepoMergeMethods + repositoryDetails map[repoKey]*GitHubRepository gists map[string]mockGist deletedGists []string nextGistID int @@ -162,6 +163,7 @@ func NewMockClient() *MockClient { prCommitsFailures: make(map[prKey]int), commitDetails: make(map[commitDetailKey]PRCommitDetail), mergeMethods: make(map[repoKey]RepoMergeMethods), + repositoryDetails: make(map[repoKey]*GitHubRepository), gists: make(map[string]mockGist), repoFiles: make(map[repoKey][]repoFileEntry), } @@ -218,6 +220,17 @@ func (m *MockClient) FindPRByBranch(_ context.Context, owner, repo, branch strin return pr, nil } +func (m *MockClient) FindPRByHead(ctx context.Context, owner, repo, headOwner, headRepo, branch string) (*PR, error) { + pr, err := m.FindPRByBranch(ctx, owner, repo, branch) + if err != nil || pr == nil { + return pr, err + } + if !sameRepositoryIdentity(pr.HeadRepoOwner, pr.HeadRepoName, headOwner, headRepo) { + return nil, nil + } + return pr, nil +} + // FindPRByBranchCallCount returns how many times FindPRByBranch has been // called. Used by tests asserting detection-probe throttling. func (m *MockClient) FindPRByBranchCallCount() int { @@ -411,6 +424,73 @@ func (m *MockClient) HasRepositoryAccess(_ context.Context, owner, repo string) return false, nil } +// GetRepository returns an explicitly seeded repository identity. The +// lightweight repo-search fixture remains separate so existing autocomplete +// tests do not accidentally grant write access. +func (m *MockClient) GetRepository(_ context.Context, owner, repo string) (*GitHubRepository, error) { + m.mu.RLock() + defer m.mu.RUnlock() + if m.reposUnavailable { + return nil, ErrNoClient + } + repository, ok := m.repositoryDetails[repoKey{owner, repo}] + if !ok { + return nil, &GitHubAPIError{StatusCode: 404, Endpoint: "/repos/" + owner + "/" + repo} + } + copy := *repository + return ©, nil +} + +func (m *MockClient) ListRepositoryForks(_ context.Context, owner, repo string) ([]*GitHubRepository, error) { + m.mu.RLock() + defer m.mu.RUnlock() + if m.reposUnavailable { + return nil, ErrNoClient + } + parent, ok := m.repositoryDetails[repoKey{owner, repo}] + if !ok { + return nil, &GitHubAPIError{StatusCode: 404, Endpoint: "/repos/" + owner + "/" + repo} + } + forks := make([]*GitHubRepository, 0) + for _, repository := range m.repositoryDetails { + if repository == nil || !repository.Fork || repository.ParentID != parent.ID { + continue + } + forks = append(forks, copyGitHubRepository(repository)) + } + return forks, nil +} + +// CreateFork creates a deterministic in-memory fork for mock Improve Kandev +// flows. Production clients still use the provider API and bounded polling. +func (m *MockClient) CreateFork(_ context.Context, owner, repo string) (*GitHubRepository, error) { + m.mu.Lock() + defer m.mu.Unlock() + if m.reposUnavailable { + return nil, ErrNoClient + } + parent, ok := m.repositoryDetails[repoKey{owner, repo}] + if !ok { + return nil, &GitHubAPIError{StatusCode: 404, Endpoint: "/repos/" + owner + "/" + repo} + } + login := m.user + fullName := login + "/" + repo + fork := &GitHubRepository{ + ID: parent.ID + 1, + FullName: fullName, + Owner: login, + Name: repo, + CloneURL: "https://github.com/" + fullName + ".git", + Fork: true, + ParentID: parent.ID, + ParentFullName: parent.FullName, + PushAccess: true, + AdminAccess: true, + } + m.repositoryDetails[repoKey{login, repo}] = fork + return copyGitHubRepository(fork), nil +} + func (m *MockClient) ListPRReviews(_ context.Context, owner, repo string, number int) ([]PRReview, error) { m.mu.RLock() defer m.mu.RUnlock() @@ -813,6 +893,15 @@ func (m *MockClient) AddRepos(org string, repos []GitHubRepo) { m.repos[org] = append(m.repos[org], repos...) } +// SetRepositoryDetails seeds the provider-authoritative repository response +// used by managed fork preparation tests. +func (m *MockClient) SetRepositoryDetails(repository GitHubRepository) { + m.mu.Lock() + defer m.mu.Unlock() + key := repositoryKeyFromFullName(repository.FullName) + m.repositoryDetails[key] = copyGitHubRepository(&repository) +} + // AddReviews appends reviews for a PR. func (m *MockClient) AddReviews(owner, repo string, number int, reviews []PRReview) { m.mu.Lock() diff --git a/apps/backend/internal/github/models.go b/apps/backend/internal/github/models.go index 83ebb61df3..f12637f40a 100644 --- a/apps/backend/internal/github/models.go +++ b/apps/backend/internal/github/models.go @@ -756,6 +756,26 @@ type GitHubRepo struct { PushedAt *time.Time `json:"pushed_at,omitempty"` } +// GitHubRepository is the provider-authoritative identity and permission +// projection needed when preparing a managed contribution destination. The +// derived parent and permission fields deliberately do not carry raw API +// response shapes into task metadata. +type GitHubRepository struct { + ID int64 + NodeID string + FullName string + Owner string + Name string + CloneURL string + HTMLURL string + DefaultBranch string + Fork bool + ParentID int64 + ParentFullName string + PushAccess bool + AdminAccess bool +} + // RepoBranch represents a branch in a GitHub repository. type RepoBranch struct { Name string `json:"name"` diff --git a/apps/backend/internal/github/noop_client.go b/apps/backend/internal/github/noop_client.go index a3c00848c8..b857aa30b9 100644 --- a/apps/backend/internal/github/noop_client.go +++ b/apps/backend/internal/github/noop_client.go @@ -33,6 +33,10 @@ func (c *NoopClient) FindPRByBranch(context.Context, string, string, string) (*P return nil, ErrNoClient } +func (c *NoopClient) FindPRByHead(context.Context, string, string, string, string, string) (*PR, error) { + return nil, ErrNoClient +} + func (c *NoopClient) ListAuthoredPRs(context.Context, string, string) ([]*PR, error) { return nil, ErrNoClient } @@ -61,6 +65,18 @@ func (c *NoopClient) HasRepositoryAccess(context.Context, string, string) (bool, return false, ErrNoClient } +func (c *NoopClient) GetRepository(context.Context, string, string) (*GitHubRepository, error) { + return nil, ErrNoClient +} + +func (c *NoopClient) ListRepositoryForks(context.Context, string, string) ([]*GitHubRepository, error) { + return nil, ErrNoClient +} + +func (c *NoopClient) CreateFork(context.Context, string, string) (*GitHubRepository, error) { + return nil, ErrNoClient +} + func (c *NoopClient) ListPRReviews(context.Context, string, string, int) ([]PRReview, error) { return nil, ErrNoClient } diff --git a/apps/backend/internal/github/pat_client.go b/apps/backend/internal/github/pat_client.go index cf1f937783..0bf45adfff 100644 --- a/apps/backend/internal/github/pat_client.go +++ b/apps/backend/internal/github/pat_client.go @@ -152,6 +152,48 @@ func (c *PATClient) HasRepositoryAccess(ctx context.Context, owner, repo string) return metadata.FullName != "", nil } +// GetRepository returns the provider-authoritative repository identity and +// permissions used by managed contribution-destination preparation. +func (c *PATClient) GetRepository(ctx context.Context, owner, repo string) (*GitHubRepository, error) { + var raw githubRepositoryResponse + endpoint := fmt.Sprintf("/repos/%s/%s", owner, repo) + if err := c.get(ctx, endpoint, &raw); err != nil { + return nil, fmt.Errorf("get repository %s/%s: %w", owner, repo, err) + } + return projectGitHubRepository(raw), nil +} + +// ListRepositoryForks lists every fork in the canonical repository's fork +// network, including forks whose names differ from the canonical name. +func (c *PATClient) ListRepositoryForks(ctx context.Context, owner, repo string) ([]*GitHubRepository, error) { + endpoint := fmt.Sprintf("/repos/%s/%s/forks?per_page=100", owner, repo) + forks := make([]*GitHubRepository, 0) + for endpoint != "" { + var page []githubRepositoryResponse + next, err := c.getPaginated(ctx, endpoint, &page) + if err != nil { + return nil, fmt.Errorf("list repository forks for %s/%s: %w", owner, repo, err) + } + for _, raw := range page { + forks = append(forks, projectGitHubRepository(raw)) + } + endpoint = next + } + return forks, nil +} + +// CreateFork creates a fork for the authenticated human automation identity. +// GitHub may return before the fork is fully readable; the service resolver +// performs the bounded follow-up verification. +func (c *PATClient) CreateFork(ctx context.Context, owner, repo string) (*GitHubRepository, error) { + var raw githubRepositoryResponse + endpoint := fmt.Sprintf("/repos/%s/%s/forks", owner, repo) + if err := c.postJSON(ctx, endpoint, []byte(`{}`), &raw); err != nil { + return nil, fmt.Errorf("create fork for %s/%s: %w", owner, repo, err) + } + return projectGitHubRepository(raw), nil +} + func (c *PATClient) GetPR(ctx context.Context, owner, repo string, number int) (*PR, error) { var raw patPR endpoint := fmt.Sprintf("/repos/%s/%s/pulls/%d", owner, repo, number) @@ -186,6 +228,22 @@ func (c *PATClient) FindPRByBranch(ctx context.Context, owner, repo, branch stri return status.PR, nil } +func (c *PATClient) FindPRByHead(ctx context.Context, owner, repo, headOwner, headRepo, branch string) (*PR, error) { + var raw []patPR + head := url.QueryEscape(headOwner + ":" + branch) + endpoint := fmt.Sprintf("/repos/%s/%s/pulls?state=open&head=%s&per_page=100", owner, repo, head) + if err := c.get(ctx, endpoint, &raw); err != nil { + return nil, fmt.Errorf("find PR by head %q:%q: %w", headOwner, branch, err) + } + for i := range raw { + pr := convertPatPR(&raw[i], owner, repo) + if sameRepositoryIdentity(pr.HeadRepoOwner, pr.HeadRepoName, headOwner, headRepo) { + return pr, nil + } + } + return nil, nil +} + func (c *PATClient) ListAuthoredPRs(ctx context.Context, owner, repo string) ([]*PR, error) { user, err := c.GetAuthenticatedUser(ctx) if err != nil { diff --git a/apps/backend/internal/github/pat_client_test.go b/apps/backend/internal/github/pat_client_test.go index dba015b76f..bb02720c7d 100644 --- a/apps/backend/internal/github/pat_client_test.go +++ b/apps/backend/internal/github/pat_client_test.go @@ -379,6 +379,35 @@ func TestPATClient_FindPRByBranch_UsesGraphQLHeadRefName(t *testing.T) { } } +func TestPATClient_FindPRByHead_UsesExactSourceRepository(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/repos/kdlbs/kandev/pulls" { + t.Errorf("path = %q, want /repos/kdlbs/kandev/pulls", r.URL.Path) + } + if got := r.URL.Query().Get("head"); got != "alice:feature" { + t.Errorf("head = %q, want alice:feature", got) + } + if got := r.URL.Query().Get("per_page"); got != "100" { + t.Errorf("per_page = %q, want 100", got) + } + _, _ = w.Write([]byte(`[{"number":12,"title":"fork PR","html_url":"https://x/12","state":"open", + "head":{"ref":"feature","sha":"abc123","repo":{"id":200,"name":"kandev","full_name":"alice/kandev","owner":{"login":"alice"}}}, + "base":{"ref":"main","repo":{"id":100,"name":"kandev","full_name":"kdlbs/kandev","owner":{"login":"kdlbs"}}}, + "user":{"login":"alice"}}]`)) + })) + t.Cleanup(srv.Close) + + pr, err := newPATClientPointingAt(t, srv.URL).FindPRByHead( + context.Background(), "kdlbs", "kandev", "alice", "kandev", "feature", + ) + if err != nil { + t.Fatalf("FindPRByHead: %v", err) + } + if pr == nil || pr.Number != 12 || pr.HeadRepoOwner != "alice" || pr.HeadRepoName != "kandev" { + t.Fatalf("PR = %#v, want source alice/kandev", pr) + } +} + func TestConvertPatRequestedReviewers(t *testing.T) { raw := &patPR{ RequestedReviewers: []struct { diff --git a/apps/backend/internal/github/poller.go b/apps/backend/internal/github/poller.go index be65b82128..001c528966 100644 --- a/apps/backend/internal/github/poller.go +++ b/apps/backend/internal/github/poller.go @@ -352,6 +352,14 @@ func (p *Poller) detectPRForWatch(ctx context.Context, watch *PRWatch) { return } + if rebindErr := p.service.rebindPRWatchRepository(ctx, watch, pr); rebindErr != nil { + p.logger.Error("failed to rebind PR watch to detected repository", + zap.String("watch_id", watch.ID), + zap.Int("pr_number", pr.Number), + zap.Error(rebindErr)) + return + } + // Found a PR — update the watch and create association if updateErr := p.service.store.UpdatePRWatchPRNumber(ctx, watch.ID, pr.Number); updateErr != nil { p.logger.Error("failed to update PR watch with detected PR", diff --git a/apps/backend/internal/github/repository_api.go b/apps/backend/internal/github/repository_api.go new file mode 100644 index 0000000000..59bdc5e2b3 --- /dev/null +++ b/apps/backend/internal/github/repository_api.go @@ -0,0 +1,65 @@ +package github + +import "strings" + +// githubRepositoryResponse is the shared subset returned by GET and POST +// repository endpoints. Keep the provider response private so only the +// credential-free projection crosses service boundaries. +type githubRepositoryResponse struct { + ID int64 `json:"id"` + NodeID string `json:"node_id"` + FullName string `json:"full_name"` + Name string `json:"name"` + CloneURL string `json:"clone_url"` + HTMLURL string `json:"html_url"` + DefaultBranch string `json:"default_branch"` + Fork bool `json:"fork"` + Owner struct { + Login string `json:"login"` + } `json:"owner"` + Parent *struct { + ID int64 `json:"id"` + FullName string `json:"full_name"` + } `json:"parent"` + Permissions struct { + Push bool `json:"push"` + Admin bool `json:"admin"` + } `json:"permissions"` +} + +func projectGitHubRepository(raw githubRepositoryResponse) *GitHubRepository { + repository := &GitHubRepository{ + ID: raw.ID, + NodeID: raw.NodeID, + FullName: raw.FullName, + Owner: raw.Owner.Login, + Name: raw.Name, + CloneURL: raw.CloneURL, + HTMLURL: raw.HTMLURL, + DefaultBranch: raw.DefaultBranch, + Fork: raw.Fork, + PushAccess: raw.Permissions.Push, + AdminAccess: raw.Permissions.Admin, + } + if raw.Parent != nil { + repository.ParentID = raw.Parent.ID + repository.ParentFullName = raw.Parent.FullName + } + return repository +} + +func copyGitHubRepository(repository *GitHubRepository) *GitHubRepository { + if repository == nil { + return nil + } + copy := *repository + return © +} + +func repositoryKeyFromFullName(fullName string) repoKey { + parts := strings.SplitN(strings.TrimSpace(fullName), "/", 2) + if len(parts) != 2 { + return repoKey{Owner: fullName} + } + return repoKey{Owner: parts[0], Repo: parts[1]} +} diff --git a/apps/backend/internal/github/service_automation_auth.go b/apps/backend/internal/github/service_automation_auth.go index 6da8daea31..45125ccd5d 100644 --- a/apps/backend/internal/github/service_automation_auth.go +++ b/apps/backend/internal/github/service_automation_auth.go @@ -1,6 +1,23 @@ package github -import "context" +import ( + "context" + "errors" + "fmt" + "strings" +) + +// exactHeadPRFinder is implemented by provider clients that can constrain a +// pull-request search to both the source repository and branch. A branch name +// is not unique across a fork network, so a parent-repository fallback must +// preserve the source repository identity. +type exactHeadPRFinder interface { + FindPRByHead(ctx context.Context, owner, repo, headOwner, headRepo, branch string) (*PR, error) +} + +type repositoryDetailsReader interface { + GetRepository(ctx context.Context, owner, repo string) (*GitHubRepository, error) +} // ResolveGitHubAutomationClient returns the workspace-owned automation client // for non-repository GitHub operations such as Gist-backed task sharing. @@ -23,7 +40,81 @@ func (s *Service) FindPRByBranchForWorkspace( if err != nil { return nil, err } - return resolved.Client.FindPRByBranch(ctx, owner, repo, branch) + return findPRByBranchInForkNetwork(ctx, resolved.Client, owner, repo, branch) +} + +// findPRByBranchInForkNetwork first searches the requested repository. When +// that repository is a fork and no PR is found, it searches the parent for an +// open PR whose head is the exact fork owner and branch. This handles PRs that +// target the canonical repository while keeping same-named branches from +// another fork out of the association path. +func findPRByBranchInForkNetwork( + ctx context.Context, client Client, owner, repo, branch string, +) (*PR, error) { + pr, err := client.FindPRByBranch(ctx, owner, repo, branch) + if err != nil || pr != nil { + return pr, err + } + + parentOwner, parentRepo, isFork, err := forkParentRepositoryForLookup(ctx, client, owner, repo) + if err != nil { + return nil, err + } + if !isFork { + return nil, nil + } + + finder, ok := client.(exactHeadPRFinder) + if !ok { + return nil, nil + } + pr, err = finder.FindPRByHead(ctx, parentOwner, parentRepo, owner, repo, branch) + if err != nil || pr == nil { + return pr, err + } + if !sameRepositoryIdentity(pr.HeadRepoOwner, pr.HeadRepoName, owner, repo) { + return nil, nil + } + return pr, nil +} + +func forkParentRepositoryForLookup( + ctx context.Context, client Client, owner, repo string, +) (parentOwner, parentRepo string, isFork bool, err error) { + repositoryReader, ok := client.(repositoryDetailsReader) + if !ok { + return "", "", false, nil + } + repository, err := repositoryReader.GetRepository(ctx, owner, repo) + if err != nil { + var apiErr *GitHubAPIError + if errors.As(err, &apiErr) && apiErr.StatusCode == 404 { + return "", "", false, nil + } + return "", "", false, fmt.Errorf("inspect repository %s/%s for parent PR lookup: %w", owner, repo, err) + } + if repository == nil || !repository.Fork { + return "", "", false, nil + } + parentOwner, parentRepo, ok = parseForkParentFullName(repository.ParentFullName) + if !ok || (strings.EqualFold(parentOwner, owner) && strings.EqualFold(parentRepo, repo)) { + return "", "", false, nil + } + return parentOwner, parentRepo, true, nil +} + +func parseForkParentFullName(fullName string) (owner, repo string, ok bool) { + parts := strings.SplitN(strings.TrimSpace(fullName), "/", 2) + if len(parts) != 2 { + return "", "", false + } + owner, repo = strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1]) + return owner, repo, owner != "" && repo != "" +} + +func sameRepositoryIdentity(ownerA, repoA, ownerB, repoB string) bool { + return strings.EqualFold(strings.TrimSpace(ownerA), strings.TrimSpace(ownerB)) && + strings.EqualFold(strings.TrimSpace(repoA), strings.TrimSpace(repoB)) } func (s *Service) GetPRFeedbackForAutomation( diff --git a/apps/backend/internal/github/service_fork.go b/apps/backend/internal/github/service_fork.go new file mode 100644 index 0000000000..653d699d71 --- /dev/null +++ b/apps/backend/internal/github/service_fork.go @@ -0,0 +1,475 @@ +package github + +import ( + "context" + "errors" + "fmt" + "strconv" + "strings" + "time" + + taskmodels "github.com/kandev/kandev/internal/task/models" +) + +const ( + contributionForkPreparationTimeout = 30 * time.Second +) + +var contributionForkPollInterval = 200 * time.Millisecond + +// ContributionForkStatus reports the workspace automation capability without +// exposing credentials or relying on an ambient executor identity. +type ContributionForkStatus string + +const ( + ContributionForkStatusDirectWrite ContributionForkStatus = "direct" + ContributionForkStatusReady ContributionForkStatus = "ready" + ContributionForkStatusCreatable ContributionForkStatus = "creatable" + ContributionForkStatusBlocked ContributionForkStatus = "blocked" +) + +// ContributionForkResolution contains transient provider information. Only +// Destination is suitable for persistence after Validate succeeds. +type ContributionForkResolution struct { + Status ContributionForkStatus + ActorLogin string + Repository *GitHubRepository + Destination *taskmodels.ContributionDestination +} + +var ( + ErrContributionForkConflict = errors.New("GitHub contribution fork has the wrong parent") + ErrContributionForkAppUnsupported = errors.New("GitHub App cannot prepare a contribution fork") + ErrContributionForkNotWritable = errors.New("GitHub contribution fork is not writable") + ErrContributionForkNotReady = errors.New("GitHub contribution fork is not ready") + ErrContributionForkUnsupported = errors.New("GitHub client cannot prepare a contribution fork") +) + +type contributionForkClient interface { + GetAuthenticatedUser(context.Context) (string, error) + GetRepository(context.Context, string, string) (*GitHubRepository, error) + ListRepositoryForks(context.Context, string, string) ([]*GitHubRepository, error) + CreateFork(context.Context, string, string) (*GitHubRepository, error) +} + +// ResolveContributionForkForWorkspace checks or creates the exact managed +// publication destination for one canonical repository. Set create to false +// for a read-only bootstrap capability probe. +func (s *Service) ResolveContributionForkForWorkspace( + ctx context.Context, + workspaceID, owner, repo string, + create bool, +) (ContributionForkResolution, error) { + if err := s.ensureRepositoryInWorkspaceScope(ctx, workspaceID, owner, repo); err != nil { + return ContributionForkResolution{}, err + } + resolved, err := s.resolveAutomationClient(ctx, workspaceID, owner, repo) + if err != nil { + return ContributionForkResolution{}, err + } + client, ok := resolved.Client.(contributionForkClient) + if !ok { + return ContributionForkResolution{}, ErrContributionForkUnsupported + } + prepareCtx, cancel := context.WithTimeout(ctx, contributionForkPreparationTimeout) + defer cancel() + result, err := resolveContributionFork(prepareCtx, client, resolved.Principal, owner, repo, create) + if err != nil || result.Destination == nil { + return result, err + } + if err := bindContributionDestinationCredential(result.Destination, resolved); err != nil { + return ContributionForkResolution{}, err + } + return result, nil +} + +// ProbeContributionForkCapabilityForWorkspace returns direct, ready, or +// creatable capability without mutating GitHub. Configuration failures are +// returned with a blocked status so callers can preserve the issue-only path. +func (s *Service) ProbeContributionForkCapabilityForWorkspace( + ctx context.Context, + workspaceID, owner, repo string, +) (ContributionForkResolution, error) { + result, err := s.ResolveContributionForkForWorkspace(ctx, workspaceID, owner, repo, false) + if err == nil { + return result, nil + } + result.Status = ContributionForkStatusBlocked + return result, err +} + +// ResolveContributionDestinationForWorkspace prepares and returns the +// server-authored destination. Direct target write returns a nil binding. +func (s *Service) ResolveContributionDestinationForWorkspace( + ctx context.Context, + workspaceID, owner, repo string, +) (*taskmodels.ContributionDestination, error) { + result, err := s.ResolveContributionForkForWorkspace(ctx, workspaceID, owner, repo, true) + if err != nil { + return nil, err + } + return result.Destination, nil +} + +// VerifyContributionDestinationForWorkspace re-reads the target repository +// through the current workspace automation connection. It is used at broker +// issuance and redemption so a deleted-and-recreated path cannot inherit an +// old destination lease. +func (s *Service) VerifyContributionDestinationForWorkspace( + ctx context.Context, + workspaceID, sourceOwner, sourceRepo, sourceProviderID, targetOwner, targetRepo, targetProviderID string, +) error { + if strings.TrimSpace(sourceProviderID) == "" || strings.TrimSpace(targetProviderID) == "" { + return errors.New("contribution destination provider IDs are required") + } + resolved, err := s.resolveAutomationClient(ctx, workspaceID, targetOwner, targetRepo) + if err != nil { + return err + } + client, ok := resolved.Client.(interface { + GetRepository(context.Context, string, string) (*GitHubRepository, error) + }) + if !ok { + return errors.New("GitHub automation client cannot verify contribution destinations") + } + target, err := client.GetRepository(ctx, targetOwner, targetRepo) + if err != nil { + return fmt.Errorf("verify contribution destination %s/%s: %w", targetOwner, targetRepo, err) + } + parsedTargetID, parseErr := strconv.ParseInt(strings.TrimSpace(targetProviderID), 10, 64) + if parseErr != nil || target == nil || target.ID != parsedTargetID || !target.Fork || target.ParentID <= 0 { + return errors.New("contribution destination target provider identity does not match") + } + parsedSourceID, parseErr := strconv.ParseInt(strings.TrimSpace(sourceProviderID), 10, 64) + if parseErr != nil || target.ParentID != parsedSourceID || + !strings.EqualFold(target.ParentFullName, strings.TrimSpace(sourceOwner)+"/"+strings.TrimSpace(sourceRepo)) { + return errors.New("contribution destination parent provider identity does not match") + } + return nil +} + +func resolveContributionFork( + ctx context.Context, + client contributionForkClient, + principal AuthPrincipal, + owner, repo string, + create bool, +) (ContributionForkResolution, error) { + if client == nil { + return ContributionForkResolution{}, ErrContributionForkUnsupported + } + canonical, err := client.GetRepository(ctx, owner, repo) + if err != nil { + return ContributionForkResolution{}, fmt.Errorf("verify canonical repository %s/%s: %w", owner, repo, err) + } + if err := validateCanonicalRepository(canonical, owner, repo); err != nil { + return ContributionForkResolution{}, err + } + fail := func(err error) (ContributionForkResolution, error) { + return ContributionForkResolution{Repository: canonical}, err + } + if repositoryWritable(canonical) { + login, err := contributionActorLogin(ctx, client, principal) + if err != nil { + return fail(err) + } + return ContributionForkResolution{ + Status: ContributionForkStatusDirectWrite, + ActorLogin: login, + Repository: canonical, + }, nil + } + if principal.Kind == AuthPrincipalApp { + return fail(fmt.Errorf("%w: direct write to %s/%s is required", ErrContributionForkAppUnsupported, owner, repo)) + } + login, err := contributionActorLogin(ctx, client, principal) + if err != nil { + return fail(err) + } + result, err := findContributionFork(ctx, client, canonical, login, owner, repo) + if err == nil { + return result, nil + } + if !errors.Is(err, errContributionForkMissing) { + return fail(err) + } + if !create { + return ContributionForkResolution{Status: ContributionForkStatusCreatable, ActorLogin: login, Repository: canonical}, nil + } + created, err := client.CreateFork(ctx, owner, repo) + if err != nil { + return fail(fmt.Errorf("create contribution fork for %s/%s: %w", owner, repo, err)) + } + forkOwner, forkName, err := repositoryOwnerAndName(created) + if err != nil { + return fail(fmt.Errorf("create contribution fork for %s/%s: %w", owner, repo, err)) + } + fork, err := waitForContributionFork(ctx, client, canonical, forkOwner, forkName) + if err != nil { + return fail(err) + } + result, err = buildContributionForkResolution(canonical, fork, login, forkOwner, forkName) + if err != nil { + return fail(err) + } + return result, nil +} + +var errContributionForkMissing = errors.New("contribution fork is missing") + +func findContributionFork( + ctx context.Context, + client contributionForkClient, + canonical *GitHubRepository, + login, owner, repo string, +) (ContributionForkResolution, error) { + fork, err := client.GetRepository(ctx, login, repo) + if err == nil { + return buildContributionForkResolution(canonical, fork, login, login, repo) + } + if !isContributionRepositoryNotFound(err) { + return ContributionForkResolution{}, fmt.Errorf("verify contribution fork %s/%s: %w", login, repo, err) + } + return findContributionForkInNetwork(ctx, client, canonical, login, owner, repo) +} + +func findContributionForkInNetwork( + ctx context.Context, + client contributionForkClient, + canonical *GitHubRepository, + login, owner, repo string, +) (ContributionForkResolution, error) { + forks, err := client.ListRepositoryForks(ctx, owner, repo) + if err != nil { + return ContributionForkResolution{}, fmt.Errorf("list contribution forks for %s/%s: %w", owner, repo, err) + } + var candidateErr error + for _, candidate := range forks { + candidateOwner, candidateName, nameErr := repositoryOwnerAndName(candidate) + if nameErr != nil || !strings.EqualFold(candidateOwner, login) { + continue + } + verified, verifyErr := client.GetRepository(ctx, candidateOwner, candidateName) + if verifyErr != nil { + if isContributionRepositoryNotFound(verifyErr) { + candidateErr = fmt.Errorf("%w: verify fork %s/%s", ErrContributionForkConflict, candidateOwner, candidateName) + continue + } + return ContributionForkResolution{}, fmt.Errorf("verify contribution fork %s/%s: %w", candidateOwner, candidateName, verifyErr) + } + result, validationErr := buildContributionForkResolution(canonical, verified, login, login, "") + if validationErr == nil { + return result, nil + } + candidateErr = validationErr + } + if candidateErr != nil { + return ContributionForkResolution{}, candidateErr + } + return ContributionForkResolution{}, errContributionForkMissing +} + +func buildContributionForkResolution( + canonical, fork *GitHubRepository, + actorLogin, expectedOwner, expectedRepo string, +) (ContributionForkResolution, error) { + if err := validateContributionFork(canonical, fork, expectedOwner, expectedRepo); err != nil { + return ContributionForkResolution{}, err + } + destination, err := makeContributionDestination(canonical, fork) + if err != nil { + return ContributionForkResolution{}, err + } + return ContributionForkResolution{ + Status: ContributionForkStatusReady, + ActorLogin: actorLogin, + Repository: fork, + Destination: destination, + }, nil +} + +func validateCanonicalRepository(repository *GitHubRepository, owner, repo string) error { + if repository == nil || !sameRepositoryName(repository, owner, repo) || repository.Fork { + return fmt.Errorf("verify canonical repository %s/%s: provider identity does not match", owner, repo) + } + if repository.ID <= 0 { + return fmt.Errorf("verify canonical repository %s/%s: provider ID is missing", owner, repo) + } + return nil +} + +func validateContributionFork(canonical, fork *GitHubRepository, login, repo string) error { + if fork == nil || !fork.Fork || + !strings.EqualFold(repositoryOwner(fork), login) || + (repo != "" && !sameRepositoryName(fork, login, repo)) || + fork.ParentID != canonical.ID || !strings.EqualFold(fork.ParentFullName, canonical.FullName) { + label := login + if repo != "" { + label += "/" + repo + } + return fmt.Errorf("%w: expected %s to fork %s", ErrContributionForkConflict, label, canonical.FullName) + } + if !repositoryWritable(fork) { + return fmt.Errorf("%w: %s/%s", ErrContributionForkNotWritable, login, repo) + } + return nil +} + +func repositoryOwner(repository *GitHubRepository) string { + if repository == nil { + return "" + } + if strings.TrimSpace(repository.Owner) != "" { + return strings.TrimSpace(repository.Owner) + } + owner, _, err := repositoryOwnerAndName(repository) + if err != nil { + return "" + } + return owner +} + +func bindContributionDestinationCredential( + destination *taskmodels.ContributionDestination, + resolved *resolvedServiceClient, +) error { + if destination == nil || resolved == nil || resolved.credential == nil { + return errors.New("managed contribution destination credential binding is unavailable") + } + credential := resolved.credential + binding := &taskmodels.ContributionDestinationCredentialBinding{ + Source: string(credential.Principal.Source), + Login: strings.TrimSpace(credential.Principal.Login), + CredentialGeneration: credential.CredentialGeneration, + AppRegistrationID: credential.AppRegistrationID, + AppCredentialGeneration: credential.AppCredentialGeneration, + } + if credential.Principal.InstallationID > 0 { + binding.InstallationID = credential.Principal.InstallationID + } + if err := binding.Validate(); err != nil { + return fmt.Errorf("validate managed contribution destination credential binding: %w", err) + } + destination.CredentialBinding = binding + return nil +} + +func makeContributionDestination(canonical, fork *GitHubRepository) (*taskmodels.ContributionDestination, error) { + source, err := repositoryBinding(canonical) + if err != nil { + return nil, fmt.Errorf("build contribution source: %w", err) + } + target, err := repositoryBinding(fork) + if err != nil { + return nil, fmt.Errorf("build contribution target: %w", err) + } + destination := &taskmodels.ContributionDestination{ + Version: taskmodels.ContributionDestinationVersion, + Provider: taskmodels.ContributionDestinationProviderGitHub, + SourceRepository: source, + TargetRepository: target, + } + if err := destination.Validate(); err != nil { + return nil, err + } + return destination, nil +} + +func repositoryBinding(repository *GitHubRepository) (taskmodels.RemoteContributionRepository, error) { + if repository == nil || repository.ID <= 0 { + return taskmodels.RemoteContributionRepository{}, errors.New("provider repository identity is missing") + } + fullName := repositoryFullName(repository) + parts := strings.Split(fullName, "/") + if len(parts) != 2 || parts[0] == "" || parts[1] == "" { + return taskmodels.RemoteContributionRepository{}, errors.New("provider repository full name is invalid") + } + remoteURL := strings.TrimSpace(repository.CloneURL) + if remoteURL == "" { + remoteURL = "https://github.com/" + fullName + ".git" + } + return taskmodels.RemoteContributionRepository{ + Host: "github.com", + Path: fullName, + ProviderID: strconv.FormatInt(repository.ID, 10), + RemoteURL: remoteURL, + }, nil +} + +func repositoryFullName(repository *GitHubRepository) string { + if repository == nil { + return "" + } + if strings.TrimSpace(repository.FullName) != "" { + return strings.TrimSpace(repository.FullName) + } + return strings.TrimSpace(repository.Owner) + "/" + strings.TrimSpace(repository.Name) +} + +func repositoryOwnerAndName(repository *GitHubRepository) (string, string, error) { + parts := strings.Split(repositoryFullName(repository), "/") + if len(parts) != 2 || strings.TrimSpace(parts[0]) == "" || strings.TrimSpace(parts[1]) == "" { + return "", "", errors.New("provider repository full name is invalid") + } + return strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1]), nil +} + +func sameRepositoryName(repository *GitHubRepository, owner, repo string) bool { + return strings.EqualFold(repositoryFullName(repository), strings.TrimSpace(owner)+"/"+strings.TrimSpace(repo)) +} + +func repositoryWritable(repository *GitHubRepository) bool { + return repository != nil && (repository.PushAccess || repository.AdminAccess) +} + +func contributionActorLogin(ctx context.Context, client contributionForkClient, principal AuthPrincipal) (string, error) { + if login := strings.TrimSpace(principal.Login); login != "" { + return login, nil + } + login, err := client.GetAuthenticatedUser(ctx) + if err != nil { + return "", fmt.Errorf("resolve GitHub automation login: %w", err) + } + if strings.TrimSpace(login) == "" { + return "", errors.New("GitHub automation connection did not report a login") + } + return strings.TrimSpace(login), nil +} + +func waitForContributionFork( + ctx context.Context, + client contributionForkClient, + canonical *GitHubRepository, + owner, repo string, +) (*GitHubRepository, error) { + for { + fork, err := client.GetRepository(ctx, owner, repo) + if err == nil { + if validationErr := validateContributionFork(canonical, fork, owner, repo); validationErr == nil { + return fork, nil + } else if !errors.Is(validationErr, ErrContributionForkNotWritable) { + return nil, validationErr + } + } else if !isContributionRepositoryNotFound(err) { + return nil, fmt.Errorf("verify created contribution fork %s/%s: %w", owner, repo, err) + } + timer := time.NewTimer(contributionForkPollInterval) + select { + case <-ctx.Done(): + timer.Stop() + return nil, fmt.Errorf("%w: %s/%s: %v", ErrContributionForkNotReady, owner, repo, ctx.Err()) + case <-timer.C: + } + } +} + +func isContributionRepositoryNotFound(err error) bool { + if err == nil { + return false + } + var apiErr *GitHubAPIError + if errors.As(err, &apiErr) { + return apiErr.StatusCode == 404 + } + return false +} diff --git a/apps/backend/internal/github/service_fork_test.go b/apps/backend/internal/github/service_fork_test.go new file mode 100644 index 0000000000..fed34becf3 --- /dev/null +++ b/apps/backend/internal/github/service_fork_test.go @@ -0,0 +1,417 @@ +package github + +import ( + "context" + "errors" + "fmt" + "testing" +) + +type forkResolverFakeClient struct { + user string + repos map[string]*GitHubRepository + forks []*GitHubRepository + getErrors map[string]error + getCalls map[string]int + readyAfter int + created []string + createRepo *GitHubRepository +} + +type contributionDestinationVerificationClient struct { + Client + repository *GitHubRepository +} + +func (c *contributionDestinationVerificationClient) GetRepository(context.Context, string, string) (*GitHubRepository, error) { + return copyGitHubRepository(c.repository), nil +} + +func (f *forkResolverFakeClient) GetAuthenticatedUser(context.Context) (string, error) { + return f.user, nil +} + +func (f *forkResolverFakeClient) GetRepository(_ context.Context, owner, repo string) (*GitHubRepository, error) { + key := owner + "/" + repo + if f.getCalls == nil { + f.getCalls = make(map[string]int) + } + f.getCalls[key]++ + if err := f.getErrors[key]; err != nil { + return nil, err + } + repository, ok := f.repos[key] + if !ok { + return nil, &GitHubAPIError{StatusCode: 404, Endpoint: "/repos/" + key} + } + if f.readyAfter > 0 && key != "kdlbs/kandev" && f.getCalls[key] >= f.readyAfter { + repository.PushAccess = true + repository.AdminAccess = true + } + return repository, nil +} + +func (f *forkResolverFakeClient) CreateFork(_ context.Context, owner, repo string) (*GitHubRepository, error) { + f.created = append(f.created, owner+"/"+repo) + if f.createRepo == nil { + return nil, errors.New("fake fork creation failed") + } + f.repos[f.createRepo.FullName] = f.createRepo + return f.createRepo, nil +} + +func (f *forkResolverFakeClient) ListRepositoryForks(context.Context, string, string) ([]*GitHubRepository, error) { + result := make([]*GitHubRepository, 0, len(f.forks)) + for _, fork := range f.forks { + result = append(result, copyGitHubRepository(fork)) + } + return result, nil +} + +func TestResolveContributionForkUsesDirectTargetWrite(t *testing.T) { + canonical := testGitHubRepository("kdlbs/kandev", "100", true) + client := &forkResolverFakeClient{ + user: "alice", + repos: map[string]*GitHubRepository{"kdlbs/kandev": canonical}, + } + + result, err := resolveContributionFork( + context.Background(), client, AuthPrincipal{Kind: AuthPrincipalHuman, Login: "alice"}, + "kdlbs", "kandev", true, + ) + if err != nil { + t.Fatalf("resolveContributionFork: %v", err) + } + if result.Status != ContributionForkStatusDirectWrite { + t.Fatalf("status = %q, want %q", result.Status, ContributionForkStatusDirectWrite) + } + if result.Destination != nil { + t.Fatalf("direct target returned destination: %#v", result.Destination) + } + if len(client.created) != 0 { + t.Fatalf("created forks = %v, want none", client.created) + } +} + +func TestResolveContributionForkAcceptsOnlyExactWritableFork(t *testing.T) { + canonical := testGitHubRepository("kdlbs/kandev", "100", false) + fork := testGitHubRepository("alice/kandev", "200", true) + fork.Fork = true + fork.ParentID = canonical.ID + fork.ParentFullName = canonical.FullName + client := &forkResolverFakeClient{ + user: "alice", + repos: map[string]*GitHubRepository{"kdlbs/kandev": canonical, "alice/kandev": fork}, + } + + result, err := resolveContributionFork( + context.Background(), client, AuthPrincipal{Kind: AuthPrincipalHuman, Login: "alice"}, + "kdlbs", "kandev", true, + ) + if err != nil { + t.Fatalf("resolveContributionFork: %v", err) + } + if result.Status != ContributionForkStatusReady { + t.Fatalf("status = %q, want %q", result.Status, ContributionForkStatusReady) + } + if result.Destination == nil { + t.Fatal("exact fork did not return a destination") + } + if got := result.Destination.TargetRepository.Path; got != "alice/kandev" { + t.Fatalf("destination target = %q, want alice/kandev", got) + } + if len(client.created) != 0 { + t.Fatalf("created forks = %v, want none", client.created) + } +} + +func TestResolveContributionForkReusesRenamedForkFromForkNetwork(t *testing.T) { + canonical := testGitHubRepository("kdlbs/kandev", "100", false) + fork := testGitHubRepository("alice/kandev-renamed", "200", true) + fork.Fork = true + fork.ParentID = canonical.ID + fork.ParentFullName = canonical.FullName + client := &forkResolverFakeClient{ + user: "alice", + repos: map[string]*GitHubRepository{"kdlbs/kandev": canonical, "alice/kandev-renamed": fork}, + getErrors: map[string]error{ + "alice/kandev": &GitHubAPIError{StatusCode: 404, Endpoint: "/repos/alice/kandev"}, + }, + forks: []*GitHubRepository{fork}, + } + + result, err := resolveContributionFork( + context.Background(), client, AuthPrincipal{Kind: AuthPrincipalHuman, Login: "alice"}, + "kdlbs", "kandev", true, + ) + if err != nil { + t.Fatalf("resolveContributionFork: %v", err) + } + if result.Destination == nil || result.Destination.TargetRepository.Path != "alice/kandev-renamed" { + t.Fatalf("destination = %#v, want the renamed fork", result.Destination) + } + if len(client.created) != 0 { + t.Fatalf("created forks = %v, want none", client.created) + } + second, err := resolveContributionFork( + context.Background(), client, AuthPrincipal{Kind: AuthPrincipalHuman, Login: "alice"}, + "kdlbs", "kandev", true, + ) + if err != nil { + t.Fatalf("second resolveContributionFork: %v", err) + } + if second.Destination == nil || second.Destination.TargetRepository.Path != "alice/kandev-renamed" { + t.Fatalf("second destination = %#v, want the same renamed fork", second.Destination) + } + if len(client.created) != 0 { + t.Fatalf("created forks after second resolution = %v, want none", client.created) + } +} + +func TestBindContributionDestinationCredentialCapturesConnectionGeneration(t *testing.T) { + canonical := testGitHubRepository("kdlbs/kandev", "100", false) + fork := testGitHubRepository("alice/kandev", "200", true) + fork.Fork = true + fork.ParentID = canonical.ID + fork.ParentFullName = canonical.FullName + destination, err := makeContributionDestination(canonical, fork) + if err != nil { + t.Fatalf("makeContributionDestination: %v", err) + } + resolved := &resolvedServiceClient{ + credential: &ResolvedCredential{ + Principal: AuthPrincipal{Source: ConnectionSourcePAT, Login: "alice"}, + CredentialGeneration: 9, + }, + } + + if err := bindContributionDestinationCredential(destination, resolved); err != nil { + t.Fatalf("bindContributionDestinationCredential: %v", err) + } + if destination.CredentialBinding == nil || destination.CredentialBinding.Source != string(ConnectionSourcePAT) || + destination.CredentialBinding.Login != "alice" || destination.CredentialBinding.CredentialGeneration != 9 { + t.Fatalf("credential binding = %#v", destination.CredentialBinding) + } +} + +func TestVerifyContributionDestinationForWorkspaceRejectsRecreatedTarget(t *testing.T) { + canonical := testGitHubRepository("kdlbs/kandev", "100", false) + fork := testGitHubRepository("automation/kandev", "200", true) + fork.Fork = true + fork.ParentID = canonical.ID + fork.ParentFullName = canonical.FullName + client := &contributionDestinationVerificationClient{Client: NewMockClient(), repository: fork} + connections := testConnectionReader{workspaces: map[string]*WorkspaceConnection{ + "workspace-1": { + WorkspaceID: "workspace-1", Source: ConnectionSourcePAT, Login: "automation", + Status: ConnectionStatusActive, CredentialGeneration: 1, + }, + }} + resolver := NewCredentialResolver(connections, fakeAuthSecrets{ + WorkspacePATSecretKey("workspace-1"): "workspace-token", + }) + resolver.SetAutomationProvider(testAutomationCredentialProvider{client: client}) + service := &Service{resolver: resolver} + + verify := func() error { + return service.VerifyContributionDestinationForWorkspace( + context.Background(), "workspace-1", "kdlbs", "kandev", "100", "automation", "kandev", "200", + ) + } + if err := verify(); err != nil { + t.Fatalf("VerifyContributionDestinationForWorkspace() error = %v", err) + } + + client.repository.ID = 201 + if err := verify(); err == nil { + t.Fatal("recreated target with a different provider ID was accepted") + } + client.repository.ID = 200 + client.repository.ParentID = 101 + if err := verify(); err == nil { + t.Fatal("target with a different parent provider ID was accepted") + } +} + +func TestResolveContributionForkRejectsExistingNonWritableFork(t *testing.T) { + canonical := testGitHubRepository("kdlbs/kandev", "100", false) + fork := testGitHubRepository("alice/kandev", "200", false) + fork.Fork = true + fork.ParentID = canonical.ID + fork.ParentFullName = canonical.FullName + client := &forkResolverFakeClient{ + user: "alice", + repos: map[string]*GitHubRepository{"kdlbs/kandev": canonical, "alice/kandev": fork}, + } + + _, err := resolveContributionFork( + context.Background(), client, AuthPrincipal{Kind: AuthPrincipalHuman, Login: "alice"}, + "kdlbs", "kandev", true, + ) + if !errors.Is(err, ErrContributionForkNotWritable) { + t.Fatalf("error = %v, want ErrContributionForkNotWritable", err) + } +} + +func TestResolveContributionForkRejectsWrongParent(t *testing.T) { + canonical := testGitHubRepository("kdlbs/kandev", "100", false) + wrongParent := testGitHubRepository("alice/kandev", "200", true) + wrongParent.Fork = true + wrongParent.ParentID = 999 + wrongParent.ParentFullName = "other/project" + client := &forkResolverFakeClient{ + user: "alice", + repos: map[string]*GitHubRepository{"kdlbs/kandev": canonical, "alice/kandev": wrongParent}, + } + + _, err := resolveContributionFork( + context.Background(), client, AuthPrincipal{Kind: AuthPrincipalHuman, Login: "alice"}, + "kdlbs", "kandev", true, + ) + if !errors.Is(err, ErrContributionForkConflict) { + t.Fatalf("error = %v, want ErrContributionForkConflict", err) + } + if len(client.created) != 0 { + t.Fatalf("created forks = %v, want none", client.created) + } +} + +func TestResolveContributionForkBlocksAppWithoutDirectWrite(t *testing.T) { + canonical := testGitHubRepository("kdlbs/kandev", "100", false) + client := &forkResolverFakeClient{ + user: "kdlbs-app[bot]", + repos: map[string]*GitHubRepository{"kdlbs/kandev": canonical}, + } + + _, err := resolveContributionFork( + context.Background(), client, AuthPrincipal{Kind: AuthPrincipalApp, Login: "kdlbs-app[bot]"}, + "kdlbs", "kandev", true, + ) + if !errors.Is(err, ErrContributionForkAppUnsupported) { + t.Fatalf("error = %v, want ErrContributionForkAppUnsupported", err) + } + if len(client.created) != 0 { + t.Fatalf("created forks = %v, want none", client.created) + } +} + +func TestResolveContributionForkReportsCreatableWithoutCreating(t *testing.T) { + canonical := testGitHubRepository("kdlbs/kandev", "100", false) + client := &forkResolverFakeClient{ + user: "alice", + repos: map[string]*GitHubRepository{"kdlbs/kandev": canonical}, + getErrors: map[string]error{ + "alice/kandev": &GitHubAPIError{StatusCode: 404, Endpoint: "/repos/alice/kandev"}, + }, + } + + result, err := resolveContributionFork( + context.Background(), client, AuthPrincipal{Kind: AuthPrincipalHuman, Source: ConnectionSourceGHCLI, Login: "alice"}, + "kdlbs", "kandev", false, + ) + if err != nil { + t.Fatalf("resolveContributionFork: %v", err) + } + if result.Status != ContributionForkStatusCreatable { + t.Fatalf("status = %q, want %q", result.Status, ContributionForkStatusCreatable) + } + if result.Destination != nil { + t.Fatalf("creatable probe returned destination: %#v", result.Destination) + } + if len(client.created) != 0 { + t.Fatalf("created forks = %v, want none", client.created) + } +} + +func TestResolveContributionForkCreatesAndVerifiesMissingHumanFork(t *testing.T) { + canonical := testGitHubRepository("kdlbs/kandev", "100", false) + fork := testGitHubRepository("alice/kandev", "200", true) + fork.Fork = true + fork.ParentID = canonical.ID + fork.ParentFullName = canonical.FullName + client := &forkResolverFakeClient{ + user: "alice", + repos: map[string]*GitHubRepository{"kdlbs/kandev": canonical}, + createRepo: fork, + } + + result, err := resolveContributionFork( + context.Background(), client, AuthPrincipal{Kind: AuthPrincipalHuman, Source: ConnectionSourcePAT, Login: "alice"}, + "kdlbs", "kandev", true, + ) + if err != nil { + t.Fatalf("resolveContributionFork: %v", err) + } + if result.Status != ContributionForkStatusReady || result.Destination == nil { + t.Fatalf("result = %#v, want ready destination", result) + } + if len(client.created) != 1 || client.created[0] != "kdlbs/kandev" { + t.Fatalf("created forks = %v, want [kdlbs/kandev]", client.created) + } +} + +func TestResolveContributionForkUsesCreatedRepositoryIdentityAndWaitsForReadiness(t *testing.T) { + canonical := testGitHubRepository("kdlbs/kandev", "100", false) + fork := testGitHubRepository("alice/kandev-renamed", "200", false) + fork.Fork = true + fork.ParentID = canonical.ID + fork.ParentFullName = canonical.FullName + client := &forkResolverFakeClient{ + user: "alice", + repos: map[string]*GitHubRepository{"kdlbs/kandev": canonical}, + createRepo: fork, + readyAfter: 11, + } + + result, err := resolveContributionFork( + context.Background(), client, AuthPrincipal{Kind: AuthPrincipalHuman, Source: ConnectionSourcePAT, Login: "alice"}, + "kdlbs", "kandev", true, + ) + if err != nil { + t.Fatalf("resolveContributionFork: %v", err) + } + if result.Destination == nil || result.Destination.TargetRepository.Path != "alice/kandev-renamed" { + t.Fatalf("destination = %#v, want created repository identity", result.Destination) + } + if client.getCalls["alice/kandev-renamed"] < client.readyAfter { + t.Fatalf("fork readiness checks = %d, want at least %d", client.getCalls["alice/kandev-renamed"], client.readyAfter) + } +} + +func TestIsContributionRepositoryNotFoundRequiresProviderError(t *testing.T) { + for _, err := range []error{ + errors.New("endpoint /repos/alice/404-user returned 500"), + errors.New("repository not found in a wrapped provider failure"), + } { + if isContributionRepositoryNotFound(err) { + t.Fatalf("isContributionRepositoryNotFound(%v) = true, want false", err) + } + } +} + +func testGitHubRepository(fullName, id string, push bool) *GitHubRepository { + var numericID int64 + if _, err := fmt.Sscan(id, &numericID); err != nil { + panic(err) + } + parts := splitRepositoryFullName(fullName) + return &GitHubRepository{ + ID: numericID, + FullName: fullName, + Owner: parts[0], + Name: parts[1], + CloneURL: "https://github.com/" + fullName + ".git", + Fork: false, + PushAccess: push, + AdminAccess: push, + } +} + +func splitRepositoryFullName(fullName string) []string { + for index, character := range fullName { + if character == '/' { + return []string{fullName[:index], fullName[index+1:]} + } + } + panic("invalid repository full name: " + fullName) +} diff --git a/apps/backend/internal/github/service_pr_branch_fallback_test.go b/apps/backend/internal/github/service_pr_branch_fallback_test.go new file mode 100644 index 0000000000..686359957a --- /dev/null +++ b/apps/backend/internal/github/service_pr_branch_fallback_test.go @@ -0,0 +1,159 @@ +package github + +import ( + "context" + "testing" +) + +const ( + forkOwner = "alice" + forkRepo = "kandev" + parentOwner = "kdlbs" + parentRepo = "kandev" + forkPRBranch = "feature/fork-pr-association" + forkPRNumber = 2652 + forkRepositoryID = 200 +) + +func seedForkNetwork(client *MockClient) { + client.SetRepositoryDetails(GitHubRepository{ + ID: 100, + FullName: parentOwner + "/" + parentRepo, + Owner: parentOwner, + Name: parentRepo, + }) + client.SetRepositoryDetails(GitHubRepository{ + ID: forkRepositoryID, + FullName: forkOwner + "/" + forkRepo, + Owner: forkOwner, + Name: forkRepo, + Fork: true, + ParentID: 100, + ParentFullName: parentOwner + "/" + parentRepo, + }) +} + +func forkNetworkPR() *PR { + return &PR{ + Number: forkPRNumber, + Title: "Contributor change", + State: "open", + URL: "https://github.com/kdlbs/kandev/pull/2652", + HeadBranch: forkPRBranch, + HeadRepoOwner: forkOwner, + HeadRepoName: forkRepo, + RepoOwner: parentOwner, + RepoName: parentRepo, + BaseRepoOwner: parentOwner, + BaseRepoName: parentRepo, + } +} + +func TestFindPRByBranchForWorkspace_ForkFallsBackToParentWithExactHead(t *testing.T) { + _, service, client, _ := setupPollerTest(t) + seedForkNetwork(client) + client.AddPR(forkNetworkPR()) + + pr, err := service.FindPRByBranchForWorkspace( + context.Background(), testWorkspaceID, forkOwner, forkRepo, forkPRBranch, + ) + if err != nil { + t.Fatalf("FindPRByBranchForWorkspace: %v", err) + } + if pr == nil { + t.Fatal("FindPRByBranchForWorkspace returned nil, want the parent PR") + } + if pr.Number != forkPRNumber || pr.RepoOwner != parentOwner || pr.RepoName != parentRepo { + t.Fatalf("PR = %#v, want #%d in %s/%s", pr, forkPRNumber, parentOwner, parentRepo) + } +} + +func TestFindPRByBranchForWorkspace_DoesNotUseAnotherForkWithSameBranch(t *testing.T) { + _, service, client, _ := setupPollerTest(t) + seedForkNetwork(client) + otherForkPR := forkNetworkPR() + otherForkPR.HeadRepoOwner = "another-user" + otherForkPR.HeadRepoName = forkRepo + client.AddPR(otherForkPR) + + pr, err := service.FindPRByBranchForWorkspace( + context.Background(), testWorkspaceID, forkOwner, forkRepo, forkPRBranch, + ) + if err != nil { + t.Fatalf("FindPRByBranchForWorkspace: %v", err) + } + if pr != nil { + t.Fatalf("PR = %#v, want nil because the parent PR has another head repository", pr) + } +} + +func TestPollerDetectPRForWatch_RebindsForkWatchToParentPRRepository(t *testing.T) { + poller, _, client, store := setupPollerTest(t) + seedTask(t, store, "task-1", false) + seedForkNetwork(client) + client.AddPR(forkNetworkPR()) + + watch := &PRWatch{ + WorkspaceID: testWorkspaceID, + SessionID: "session-1", + TaskID: "task-1", + RepositoryID: "repo-1", + Owner: forkOwner, + Repo: forkRepo, + PRNumber: 0, + Branch: forkPRBranch, + } + if err := store.CreatePRWatch(context.Background(), watch); err != nil { + t.Fatalf("CreatePRWatch: %v", err) + } + + poller.detectPRForWatch(context.Background(), watch) + + updated, err := store.GetPRWatch(context.Background(), watch.ID) + if err != nil { + t.Fatalf("GetPRWatch: %v", err) + } + if updated == nil { + t.Fatal("GetPRWatch returned nil") + } + if updated.Owner != parentOwner || updated.Repo != parentRepo { + t.Fatalf("watch repository = %s/%s, want %s/%s", updated.Owner, updated.Repo, parentOwner, parentRepo) + } + if updated.PRNumber != forkPRNumber { + t.Fatalf("watch PR number = %d, want %d", updated.PRNumber, forkPRNumber) + } +} + +func TestSyncWorkspaceWatchesBatched_ForkFallsBackToParent(t *testing.T) { + _, service, client, store := setupBatchedPollerTest(t) + seedTask(t, store, "task-1", false) + seedForkNetwork(client.MockClient) + client.AddPR(forkNetworkPR()) + client.branchResponses = []string{`{"data":{"b0":{"pullRequests":{"nodes":[]}}}}`} + + watch := &PRWatch{ + WorkspaceID: testWorkspaceID, + SessionID: "session-1", + TaskID: "task-1", + RepositoryID: "repo-1", + Owner: forkOwner, + Repo: forkRepo, + PRNumber: 0, + Branch: forkPRBranch, + } + if err := store.CreatePRWatch(context.Background(), watch); err != nil { + t.Fatalf("CreatePRWatch: %v", err) + } + + if _, err := service.SyncWorkspaceWatchesBatched(context.Background(), testWorkspaceID, []*PRWatch{watch}); err != nil { + t.Fatalf("SyncWorkspaceWatchesBatched: %v", err) + } + + updated, err := store.GetPRWatch(context.Background(), watch.ID) + if err != nil { + t.Fatalf("GetPRWatch: %v", err) + } + if updated.Owner != parentOwner || updated.Repo != parentRepo || updated.PRNumber != forkPRNumber { + t.Fatalf("watch = %#v, want parent repository and PR #%d", updated, forkPRNumber) + } +} diff --git a/apps/backend/internal/github/service_pr_watch.go b/apps/backend/internal/github/service_pr_watch.go index 09ffa5a8e5..fc52acff11 100644 --- a/apps/backend/internal/github/service_pr_watch.go +++ b/apps/backend/internal/github/service_pr_watch.go @@ -147,6 +147,28 @@ func (s *Service) UpdatePRWatchPRNumber(ctx context.Context, id string, prNumber return s.store.UpdatePRWatchPRNumber(ctx, id, prNumber) } +// UpdatePRWatchRepository updates a watch's provider repository identity after +// PR discovery. This keeps future status polling on the repository that owns +// the PR rather than the repository used to discover the branch. +func (s *Service) UpdatePRWatchRepository(ctx context.Context, id, owner, repo string) error { + return s.store.UpdatePRWatchRepository(ctx, id, owner, repo) +} + +func (s *Service) rebindPRWatchRepository(ctx context.Context, watch *PRWatch, pr *PR) error { + if watch == nil || pr == nil || strings.TrimSpace(pr.RepoOwner) == "" || strings.TrimSpace(pr.RepoName) == "" { + return nil + } + if sameRepositoryIdentity(watch.Owner, watch.Repo, pr.RepoOwner, pr.RepoName) { + return nil + } + if err := s.store.UpdatePRWatchRepository(ctx, watch.ID, pr.RepoOwner, pr.RepoName); err != nil { + return fmt.Errorf("update PR watch repository: %w", err) + } + watch.Owner = pr.RepoOwner + watch.Repo = pr.RepoName + return nil +} + // ResetPRWatch atomically resets a watch's branch and clears its pr_number so // the poller re-searches for a PR on the new branch. See Store.ResetPRWatch. func (s *Service) ResetPRWatch(ctx context.Context, id, branch string) error { @@ -1086,7 +1108,7 @@ func (s *Service) detectPRForWatchOnce( // fires WHILE FindPRByBranch is running wins over our post-fetch // classification — see Service.markRepoAsMissing for the rationale. repoErrGen := s.repoErrorGenSnapshot() - pr, err := resolved.Client.FindPRByBranch(ctx, watch.Owner, watch.Repo, watch.Branch) + pr, err := findPRByBranchInForkNetwork(ctx, resolved.Client, watch.Owner, watch.Repo, watch.Branch) if err != nil && isRepoNotResolvableErr(err) { s.markRepoAsMissingForScope(resolved.CacheScope, watch.Owner, watch.Repo, repoErrGen) // Wrap so wsSyncTaskPR can errors.Is(err, ErrRepoNotResolvable) @@ -1114,6 +1136,9 @@ func (s *Service) detectPRForWatchOnce( if err != nil || pr == nil { return nil, err } + if rebindErr := s.rebindPRWatchRepository(ctx, watch, pr); rebindErr != nil { + return nil, rebindErr + } if err := s.store.UpdatePRWatchPRNumber(ctx, watch.ID, pr.Number); err != nil { s.logger.Error("failed to update PR watch number during sync", zap.String("watch_id", watch.ID), zap.Int("pr_number", pr.Number), zap.Error(err)) diff --git a/apps/backend/internal/github/service_pr_watch_batched.go b/apps/backend/internal/github/service_pr_watch_batched.go index 198822f218..7b769bb205 100644 --- a/apps/backend/internal/github/service_pr_watch_batched.go +++ b/apps/backend/internal/github/service_pr_watch_batched.go @@ -84,7 +84,7 @@ func (s *Service) syncWatchesBatchedWithClient( results = append(results, s.applyBatchedNumberedWatch(ctx, cacheScope, w, statusByKey, now)) } for _, w := range searching { - results = append(results, s.applyBatchedSearchingWatch(ctx, cacheScope, w, statusByKey, now)) + results = append(results, s.applyBatchedSearchingWatch(ctx, client, cacheScope, w, statusByKey, now)) } return results, nil } @@ -326,7 +326,7 @@ func (s *Service) applyBatchedNumberedWatch( // side. A searching watch (pr_number=0) is promoted to a known PR when the // branch lookup returns one; otherwise we just bump last_checked_at. func (s *Service) applyBatchedSearchingWatch( - ctx context.Context, cacheScope string, w *PRWatch, statusByKey map[string]*PRStatus, now time.Time, + ctx context.Context, client Client, cacheScope string, w *PRWatch, statusByKey map[string]*PRStatus, now time.Time, ) PRWatchSyncResult { // Timestamps get bumped on both the "no PR found" and "PR detected" // paths, so hoist the single call above the branch to make that @@ -337,7 +337,21 @@ func (s *Service) applyBatchedSearchingWatch( } status, ok := statusByKey[graphqlBranchKey(w.Owner, w.Repo, w.Branch)] if !ok || status == nil || status.PR == nil { - return PRWatchSyncResult{Watch: w} + pr, err := findPRByBranchInForkNetwork(ctx, client, w.Owner, w.Repo, w.Branch) + if err != nil { + s.logger.Debug("failed to search parent repository for PR", + zap.String("watch_id", w.ID), zap.String("branch", w.Branch), zap.Error(err)) + return PRWatchSyncResult{Watch: w, SyncFailed: true} + } + if pr == nil { + return PRWatchSyncResult{Watch: w} + } + status = &PRStatus{PR: pr} + } + if err := s.rebindPRWatchRepository(ctx, w, status.PR); err != nil { + s.logger.Error("failed to rebind PR watch to detected repository", + zap.String("watch_id", w.ID), zap.Int("pr_number", status.PR.Number), zap.Error(err)) + return PRWatchSyncResult{Watch: w, Status: status, Found: true, SyncFailed: true} } if err := s.store.UpdatePRWatchPRNumber(ctx, w.ID, status.PR.Number); err != nil { s.logger.Error("failed to update PR watch with detected PR", diff --git a/apps/backend/internal/github/store.go b/apps/backend/internal/github/store.go index 38ea24227a..48e8c45c28 100644 --- a/apps/backend/internal/github/store.go +++ b/apps/backend/internal/github/store.go @@ -1489,6 +1489,16 @@ func (s *Store) UpdatePRWatchPRNumber(ctx context.Context, id string, prNumber i return err } +// UpdatePRWatchRepository repairs the provider repository identity after PR +// discovery. A watch can start on a contributor fork while the PR targets the +// canonical parent repository. +func (s *Store) UpdatePRWatchRepository(ctx context.Context, id, owner, repo string) error { + _, err := s.db.ExecContext(ctx, + `UPDATE github_pr_watches SET owner = ?, repo = ?, updated_at = ? WHERE id = ?`, + owner, repo, time.Now().UTC(), id) + return err +} + // ResetPRWatch atomically resets a watch to the searching state: updates the // tracked branch and clears pr_number in a single statement. Used when the // session's active branch changes (rename, checkout) so the poller re-searches diff --git a/apps/backend/internal/improvekandev/github.go b/apps/backend/internal/improvekandev/github.go index a07c55a1f5..875071ab6f 100644 --- a/apps/backend/internal/improvekandev/github.go +++ b/apps/backend/internal/improvekandev/github.go @@ -5,6 +5,7 @@ import ( "context" "fmt" "os/exec" + "strconv" "strings" "time" @@ -19,6 +20,7 @@ import ( // extra API call. type GitHubInfo interface { GetAuthenticatedLogin(ctx context.Context) (string, error) + GetRepositoryID(ctx context.Context, owner, name string) (string, error) HasRepoWriteAccess(ctx context.Context, owner, name string) (bool, error) UserHasFork(ctx context.Context, login, name string) (bool, error) } @@ -38,6 +40,18 @@ func (d *defaultGitHubInfo) GetAuthenticatedLogin(ctx context.Context) (string, return strings.TrimSpace(out), nil } +func (d *defaultGitHubInfo) GetRepositoryID(ctx context.Context, owner, name string) (string, error) { + out, err := runGH(ctx, "api", fmt.Sprintf("repos/%s/%s", owner, name), "--jq", ".id") + if err != nil { + return "", err + } + id, err := strconv.ParseInt(strings.TrimSpace(out), 10, 64) + if err != nil || id <= 0 { + return "", fmt.Errorf("GitHub repository %s/%s returned an invalid provider ID", owner, name) + } + return strconv.FormatInt(id, 10), nil +} + func (d *defaultGitHubInfo) HasRepoWriteAccess(ctx context.Context, owner, name string) (bool, error) { out, err := runGH(ctx, "api", fmt.Sprintf("repos/%s/%s", owner, name), "--jq", ".permissions.push") if err != nil { diff --git a/apps/backend/internal/improvekandev/handler.go b/apps/backend/internal/improvekandev/handler.go index 732f72acf2..f815423bde 100644 --- a/apps/backend/internal/improvekandev/handler.go +++ b/apps/backend/internal/improvekandev/handler.go @@ -3,10 +3,13 @@ package improvekandev import ( "context" "errors" + "fmt" "io" "net/http" "os" "path/filepath" + "strconv" + "strings" "time" "github.com/gin-gonic/gin" @@ -14,6 +17,7 @@ import ( "github.com/kandev/kandev/internal/auth/authn" "github.com/kandev/kandev/internal/common/logger" + "github.com/kandev/kandev/internal/github" "github.com/kandev/kandev/internal/system/logbundle" "github.com/kandev/kandev/internal/system/storage/tempartifacts" taskmodels "github.com/kandev/kandev/internal/task/models" @@ -65,6 +69,17 @@ type GitHubWorkspaceCopier interface { CopyWorkspaceConnectionToWorkspace(ctx context.Context, srcWorkspaceID, dstWorkspaceID string) error } +// ManagedGitHubForkProber is the workspace-scoped provider capability used +// when task credentials are managed. It must resolve the same automation +// identity that will receive the task's credential leases. +type ManagedGitHubForkProber interface { + DescribeTaskGitCredentialPolicy(ctx context.Context, workspaceID string) (github.TaskGitCredentialPolicy, error) + ProbeContributionForkCapabilityForWorkspace( + ctx context.Context, + workspaceID, owner, repo string, + ) (github.ContributionForkResolution, error) +} + // DefaultWorkspaceResolver resolves the workspace whose GitHub configuration // the dedicated workspace inherits on creation (active workspace in user // settings → first-created workspace → literal "default"). @@ -88,6 +103,7 @@ type Handler struct { // resolveRemote resolves a local repo path's origin remote. Defaults to // service.ResolveGitRemoteProvider; tests can substitute a fake. resolveRemote remoteResolver + managedGitHub ManagedGitHubForkProber tempArtifacts *tempartifacts.Registry } @@ -124,6 +140,10 @@ func (h *Handler) SetTemporaryArtifactRegistry(registry *tempartifacts.Registry) h.tempArtifacts = registry } +func (h *Handler) SetManagedGitHubForkProber(prober ManagedGitHubForkProber) { + h.managedGitHub = prober +} + // RegisterRoutes registers the bootstrap and diagnostic-bundle lease endpoints. func RegisterRoutes(router *gin.Engine, h *Handler) { if h == nil { @@ -157,13 +177,20 @@ const ( // ForkStatusWritable: user has push access on the upstream repo, no fork // is needed. ForkStatusWritable ForkStatus = "writable" - // ForkStatusReady: user already has a fork at github.com/{login}/kandev, - // so the PR step can push to it without forking again. + // ForkStatusReady: the workspace identity has a verified fork in the + // canonical repository's fork network, so the PR step can push to it + // without forking again. ForkStatusReady ForkStatus = "ready" + // ForkStatusCreatable: the managed human automation identity can create + // its exact fork during task creation. + ForkStatusCreatable ForkStatus = "creatable" // ForkStatusBlockedEMU: the authenticated user looks like an Enterprise // Managed User. EMU accounts cannot fork repositories outside their // owning enterprise, so the contribution flow will fail at the PR step. ForkStatusBlockedEMU ForkStatus = "blocked_emu" + // ForkStatusBlockedManaged means the selected workspace automation + // connection cannot prove or prepare a safe destination. + ForkStatusBlockedManaged ForkStatus = "blocked_managed" // ForkStatusUnknown: bootstrap could not determine fork eligibility // (e.g., gh CLI lookup failed). Frontend should proceed and rely on the // PR step to surface any errors. @@ -184,7 +211,7 @@ type BootstrapResponse struct { GitHubLogin string `json:"github_login"` HasWriteAccess bool `json:"has_write_access"` ForkStatus ForkStatus `json:"fork_status"` - ForkMessage string `json:"fork_message,omitempty"` + ForkReasonCode string `json:"fork_reason_code,omitempty"` } func (h *Handler) httpBootstrap(c *gin.Context) { @@ -208,7 +235,8 @@ func (h *Handler) httpBootstrap(c *gin.Context) { } workspaceID := workspace.ID - repo, err := h.resolveOrCloneRepo(ctx, workspaceID) + access := h.resolveGitHubAccessForWorkspace(ctx, workspaceID) + repo, err := h.resolveOrCloneRepo(ctx, workspaceID, access.providerRepoID) if err != nil { h.log.Error("improve-kandev: repository upsert failed", zap.Error(err)) c.JSON(http.StatusInternalServerError, gin.H{errorKey: "failed to register kandev repository"}) @@ -254,8 +282,6 @@ func (h *Handler) httpBootstrap(c *gin.Context) { c.JSON(http.StatusInternalServerError, gin.H{errorKey: "failed to create bundle dir"}) return } - access := h.resolveGitHubAccess(ctx) - c.JSON(http.StatusOK, BootstrapResponse{ WorkspaceID: workspaceID, RepositoryID: repo.ID, @@ -267,7 +293,7 @@ func (h *Handler) httpBootstrap(c *gin.Context) { GitHubLogin: access.login, HasWriteAccess: access.hasWrite, ForkStatus: access.forkStatus, - ForkMessage: access.forkMessage, + ForkReasonCode: access.forkReasonCode, }) } @@ -376,10 +402,13 @@ func (h *Handler) copyGitHubConnectionFromDefaultWorkspace(ctx context.Context, // 2. Match by scanning workspace repos whose origin remote resolves to // kdlbs/kandev; backfill provider info on the match. // 3. Fall back to cloning into the managed location and registering it. -func (h *Handler) resolveOrCloneRepo(ctx context.Context, workspaceID string) (*taskmodels.Repository, error) { +func (h *Handler) resolveOrCloneRepo(ctx context.Context, workspaceID, providerRepoID string) (*taskmodels.Repository, error) { if existing, err := h.taskSvc.GetRepositoryByProviderInfo(ctx, workspaceID, repoProvider, "https://github.com", repoOwner, repoName); err != nil { return nil, err } else if existing != nil { + if err := h.ensureKandevProviderRepoID(ctx, existing, providerRepoID); err != nil { + return nil, err + } return existing, nil } @@ -389,7 +418,9 @@ func (h *Handler) resolveOrCloneRepo(ctx context.Context, workspaceID string) (* // risk a duplicate than fail the bootstrap entirely. h.log.Warn("improve-kandev: list repositories failed; falling back to clone", zap.Error(err)) } else if match := findKandevRepoByLocalRemote(repos, h.resolveRemote); match != nil { - h.backfillKandevProviderInfo(ctx, match) + if err := h.backfillKandevProviderInfo(ctx, match, providerRepoID); err != nil { + return nil, err + } return match, nil } @@ -400,13 +431,14 @@ func (h *Handler) resolveOrCloneRepo(ctx context.Context, workspaceID string) (* return nil, err } repo, _, err := h.taskSvc.FindOrCreateRepository(ctx, &taskservice.FindOrCreateRepositoryRequest{ - WorkspaceID: workspaceID, - Provider: repoProvider, - ProviderHost: "https://github.com", - ProviderOwner: repoOwner, - ProviderName: repoName, - DefaultBranch: defaultBranch, - LocalPath: localPath, + WorkspaceID: workspaceID, + Provider: repoProvider, + ProviderHost: "https://github.com", + ProviderRepoID: providerRepoID, + ProviderOwner: repoOwner, + ProviderName: repoName, + DefaultBranch: defaultBranch, + LocalPath: localPath, }) return repo, err } @@ -436,10 +468,13 @@ func findKandevRepoByLocalRemote(repos []*taskmodels.Repository, resolve remoteR // backfillKandevProviderInfo fills missing provider/owner/name on an existing // repo so subsequent lookups by provider info hit the fast path. Failures are // logged but non-fatal — we still return the matched repo to the caller. -func (h *Handler) backfillKandevProviderInfo(ctx context.Context, repo *taskmodels.Repository) { +func (h *Handler) backfillKandevProviderInfo(ctx context.Context, repo *taskmodels.Repository, providerRepoID string) error { + if err := h.ensureKandevProviderRepoID(ctx, repo, providerRepoID); err != nil { + return err + } if repo.Provider == repoProvider && repo.ProviderHost == "https://github.com" && repo.ProviderOwner == repoOwner && repo.ProviderName == repoName { - return + return nil } provider, providerHost, owner, name := repoProvider, "https://github.com", repoOwner, repoName branch := repo.DefaultBranch @@ -455,29 +490,45 @@ func (h *Handler) backfillKandevProviderInfo(ctx context.Context, repo *taskmode }); err != nil { h.log.Warn("improve-kandev: backfill provider info failed", zap.String("repository_id", repo.ID), zap.Error(err)) - return + return nil } repo.Provider = provider repo.ProviderHost = providerHost repo.ProviderOwner = owner repo.ProviderName = name repo.DefaultBranch = branch + return nil } -// emuBlockedMessage explains the EMU-restriction case to the contributor in -// terms they can act on. Surfaced in the dialog when ForkStatusBlockedEMU is -// returned. -const emuBlockedMessage = "Your GitHub account appears to be an Enterprise Managed User (EMU) account, " + - "which typically cannot fork repositories outside your owning enterprise. " + - "The PR step would fail when forking kdlbs/kandev. Contact your GitHub admin " + - "if you'd like to enable this, or contribute via another account." +func (h *Handler) ensureKandevProviderRepoID( + ctx context.Context, + repo *taskmodels.Repository, + providerRepoID string, +) error { + providerRepoID = strings.TrimSpace(providerRepoID) + if repo == nil || providerRepoID == "" { + return nil + } + if repo.ProviderRepoID != "" && repo.ProviderRepoID != providerRepoID { + return errors.New("kdlbs/kandev provider identity changed; refusing to reuse the repository row") + } + if repo.ProviderRepoID == providerRepoID { + return nil + } + if _, err := h.taskSvc.UpdateRepository(ctx, repo.ID, &taskservice.UpdateRepositoryRequest{ProviderRepoID: &providerRepoID}); err != nil { + return fmt.Errorf("backfill kdlbs/kandev provider ID: %w", err) + } + repo.ProviderRepoID = providerRepoID + return nil +} // githubAccess is the resolved bootstrap GitHub state. type githubAccess struct { - login string - hasWrite bool - forkStatus ForkStatus - forkMessage string + login string + hasWrite bool + forkStatus ForkStatus + forkReasonCode string + providerRepoID string } // resolveGitHubAccess resolves the authenticated user's login, push access, @@ -489,6 +540,12 @@ func (h *Handler) resolveGitHubAccess(ctx context.Context) githubAccess { if h.gh == nil { return out } + providerRepoID, err := h.gh.GetRepositoryID(ctx, repoOwner, repoName) + if err != nil { + h.log.Debug("improve-kandev: canonical repository ID lookup failed", zap.Error(err)) + } else { + out.providerRepoID = providerRepoID + } login, err := h.gh.GetAuthenticatedLogin(ctx) if err != nil { h.log.Debug("improve-kandev: gh login lookup failed", zap.Error(err)) @@ -516,11 +573,84 @@ func (h *Handler) resolveGitHubAccess(ctx context.Context) githubAccess { } if isEMULogin(login) { out.forkStatus = ForkStatusBlockedEMU - out.forkMessage = emuBlockedMessage + out.forkReasonCode = string(ForkReasonAccountCannotFork) } return out } +func (h *Handler) resolveGitHubAccessForWorkspace(ctx context.Context, workspaceID string) githubAccess { + if h.managedGitHub == nil { + return h.resolveGitHubAccess(ctx) + } + policy, err := h.managedGitHub.DescribeTaskGitCredentialPolicy(ctx, workspaceID) + if err != nil { + return githubAccess{ + forkStatus: ForkStatusBlockedManaged, + forkReasonCode: string(managedForkErrorReasonCode(err)), + } + } + if policy.Mode != github.TaskGitCredentialsModeManaged { + return h.resolveGitHubAccess(ctx) + } + result, err := h.managedGitHub.ProbeContributionForkCapabilityForWorkspace( + ctx, workspaceID, repoOwner, repoName, + ) + out := githubAccess{ + login: result.ActorLogin, + forkStatus: ForkStatusBlockedManaged, + providerRepoID: providerRepositoryID(result.Repository), + } + if err != nil { + out.forkReasonCode = string(managedForkErrorReasonCode(err)) + return out + } + switch result.Status { + case github.ContributionForkStatusDirectWrite: + out.hasWrite = true + out.forkStatus = ForkStatusWritable + case github.ContributionForkStatusReady: + out.forkStatus = ForkStatusReady + case github.ContributionForkStatusCreatable: + out.forkStatus = ForkStatusCreatable + default: + out.forkReasonCode = string(managedForkErrorReasonCode(errors.New("managed GitHub fork capability is blocked"))) + } + return out +} + +type ForkReasonCode string + +const ( + ForkReasonAccountCannotFork ForkReasonCode = "account_cannot_fork" + ForkReasonAppUnsupported ForkReasonCode = "app_unsupported" + ForkReasonForkConflict ForkReasonCode = "fork_conflict" + ForkReasonForkNotWritable ForkReasonCode = "fork_not_writable" + ForkReasonForkNotReady ForkReasonCode = "fork_not_ready" + ForkReasonManagedUnavailable ForkReasonCode = "managed_unavailable" +) + +func managedForkErrorReasonCode(err error) ForkReasonCode { + switch { + case errors.Is(err, github.ErrContributionForkAppUnsupported): + return ForkReasonAppUnsupported + case errors.Is(err, github.ErrContributionForkConflict): + return ForkReasonForkConflict + case errors.Is(err, github.ErrContributionForkNotWritable): + return ForkReasonForkNotWritable + case errors.Is(err, github.ErrContributionForkNotReady): + return ForkReasonForkNotReady + default: + return ForkReasonManagedUnavailable + } +} + +func providerRepositoryID(repository *github.GitHubRepository) string { + if repository == nil || repository.ID <= 0 { + return "" + } + return strconv.FormatInt(repository.ID, 10) +} + // ensureWorkflow finds or creates a hidden Improve Kandev workflow in the // given workspace. Idempotent: matches by WorkflowTemplateID. func (h *Handler) ensureWorkflow( diff --git a/apps/backend/internal/improvekandev/handler_test.go b/apps/backend/internal/improvekandev/handler_test.go index e1d5d40c17..92b0f05578 100644 --- a/apps/backend/internal/improvekandev/handler_test.go +++ b/apps/backend/internal/improvekandev/handler_test.go @@ -14,6 +14,7 @@ import ( "github.com/gin-gonic/gin" "github.com/kandev/kandev/internal/auth/authn" "github.com/kandev/kandev/internal/common/logger" + "github.com/kandev/kandev/internal/github" "github.com/kandev/kandev/internal/system/logbundle" taskmodels "github.com/kandev/kandev/internal/task/models" ) @@ -22,19 +23,84 @@ import ( // returns the value of the corresponding field; if the err counterpart is // non-nil, the value is ignored and the error is returned instead. type fakeGitHubInfo struct { - login string - loginErr error - hasWrite bool - hasWriteErr error - hasFork bool - hasForkErr error - calledHasFork bool + login string + loginErr error + providerRepoID string + providerRepoIDErr error + hasWrite bool + hasWriteErr error + hasFork bool + hasForkErr error + calledHasFork bool +} + +type fakeManagedGitHub struct { + policy github.TaskGitCredentialPolicy + policyErr error + result github.ContributionForkResolution + resultErr error + probed bool +} + +func (f *fakeManagedGitHub) DescribeTaskGitCredentialPolicy(context.Context, string) (github.TaskGitCredentialPolicy, error) { + return f.policy, f.policyErr +} + +func (f *fakeManagedGitHub) ProbeContributionForkCapabilityForWorkspace(context.Context, string, string, string) (github.ContributionForkResolution, error) { + f.probed = true + return f.result, f.resultErr +} + +func TestResolveGitHubAccessForWorkspaceUsesManagedForkCapability(t *testing.T) { + managed := &fakeManagedGitHub{ + policy: github.TaskGitCredentialPolicy{Mode: github.TaskGitCredentialsModeManaged}, + result: github.ContributionForkResolution{ + Status: github.ContributionForkStatusCreatable, ActorLogin: "automation", + Repository: &github.GitHubRepository{ID: 100}, + }, + } + handler := newTestHandler(&fakeGitHubInfo{login: "ambient", hasWrite: true}) + handler.SetManagedGitHubForkProber(managed) + + access := handler.resolveGitHubAccessForWorkspace(context.Background(), "workspace-1") + if access.forkStatus != ForkStatusCreatable || access.login != "automation" || access.providerRepoID != "100" || !managed.probed { + t.Fatalf("managed access = %+v, probed=%v", access, managed.probed) + } +} + +func TestResolveGitHubAccessForWorkspaceBlocksManagedErrorsWithoutAmbientFallback(t *testing.T) { + managed := &fakeManagedGitHub{ + policy: github.TaskGitCredentialPolicy{Mode: github.TaskGitCredentialsModeManaged}, + resultErr: github.ErrContributionForkAppUnsupported, + } + handler := newTestHandler(&fakeGitHubInfo{login: "ambient", hasWrite: true}) + handler.SetManagedGitHubForkProber(managed) + + access := handler.resolveGitHubAccessForWorkspace(context.Background(), "workspace-1") + if access.forkStatus != ForkStatusBlockedManaged || access.login != "" || access.hasWrite { + t.Fatalf("managed error access = %+v", access) + } +} + +func TestResolveGitHubAccessFallsBackToCanonicalProviderID(t *testing.T) { + handler := newTestHandler(&fakeGitHubInfo{ + login: "alice", providerRepoID: "100", hasWrite: true, + }) + + access := handler.resolveGitHubAccess(context.Background()) + if access.providerRepoID != "100" { + t.Fatalf("providerRepoID = %q, want 100", access.providerRepoID) + } } func (f *fakeGitHubInfo) GetAuthenticatedLogin(_ context.Context) (string, error) { return f.login, f.loginErr } +func (f *fakeGitHubInfo) GetRepositoryID(_ context.Context, _, _ string) (string, error) { + return f.providerRepoID, f.providerRepoIDErr +} + func (f *fakeGitHubInfo) HasRepoWriteAccess(_ context.Context, _, _ string) (bool, error) { return f.hasWrite, f.hasWriteErr } @@ -127,8 +193,8 @@ func TestResolveGitHubAccess_ForkAlreadyExists(t *testing.T) { if access.forkStatus != ForkStatusReady { t.Errorf("fork status = %q, want %q", access.forkStatus, ForkStatusReady) } - if access.forkMessage != "" { - t.Errorf("ready status must not include a fork_message even for EMU-shaped logins: %q", access.forkMessage) + if access.forkReasonCode != "" { + t.Errorf("ready status must not include a fork reason even for EMU-shaped logins: %q", access.forkReasonCode) } } @@ -138,8 +204,8 @@ func TestResolveGitHubAccess_BlockedEMU(t *testing.T) { if access.forkStatus != ForkStatusBlockedEMU { t.Errorf("fork status = %q, want %q", access.forkStatus, ForkStatusBlockedEMU) } - if access.forkMessage == "" { - t.Errorf("blocked_emu must include a fork_message for the dialog") + if access.forkReasonCode == "" { + t.Errorf("blocked_emu must include a fork reason code for the dialog") } } @@ -160,8 +226,8 @@ func TestResolveGitHubAccess_UnknownOnForkLookupError(t *testing.T) { if access.forkStatus != ForkStatusUnknown { t.Errorf("fork status = %q, want %q", access.forkStatus, ForkStatusUnknown) } - if access.forkMessage != "" { - t.Errorf("fork lookup failures must not produce an EMU message even for underscore logins") + if access.forkReasonCode != "" { + t.Errorf("fork lookup failures must not produce an EMU reason even for underscore logins") } } @@ -171,8 +237,8 @@ func TestResolveGitHubAccess_NoForkNotEMU(t *testing.T) { if access.forkStatus != ForkStatusUnknown { t.Errorf("fork status = %q, want %q", access.forkStatus, ForkStatusUnknown) } - if access.forkMessage != "" { - t.Errorf("non-EMU users should not get a fork_message") + if access.forkReasonCode != "" { + t.Errorf("non-EMU users should not get a fork reason code") } } diff --git a/apps/backend/internal/mcp/handlers/clarification_pause_test.go b/apps/backend/internal/mcp/handlers/clarification_pause_test.go index 3788df6fc4..bef8a70f81 100644 --- a/apps/backend/internal/mcp/handlers/clarification_pause_test.go +++ b/apps/backend/internal/mcp/handlers/clarification_pause_test.go @@ -19,9 +19,13 @@ type recordingClarificationInputPauser struct { sessions []string count int err error + before func() } func (p *recordingClarificationInputPauser) PauseForClarificationInput(_ context.Context, sessionID string) (int, error) { + if p.before != nil { + p.before() + } p.sessions = append(p.sessions, sessionID) return p.count, p.err } diff --git a/apps/backend/internal/mcp/handlers/parent_question.go b/apps/backend/internal/mcp/handlers/parent_question.go index 92d2712e5a..bbf3187123 100644 --- a/apps/backend/internal/mcp/handlers/parent_question.go +++ b/apps/backend/internal/mcp/handlers/parent_question.go @@ -141,6 +141,21 @@ func (h *Handlers) handleAskParentQuestion(ctx context.Context, msg *ws.Message) return ws.NewError(msg.ID, msg.Action, ws.ErrorCodeInternalError, "failed to persist parent question: "+err.Error(), nil) } + // Establish the child's durable pause before notifying the parent. Parent + // replies resume the child immediately, so delivering the question first + // lets an answer race the cancellation of the turn that asked it. + h.setSessionWaitingForInput(ctx, target.childTask.ID, target.childSession.ID) + if h.inputPauser != nil { + if _, pauseErr := h.inputPauser.PauseForClarificationInput(context.WithoutCancel(ctx), target.childSession.ID); pauseErr != nil { + h.logger.Warn("failed to pause autopilot child before parent question delivery", + zap.String("question_id", questionID), zap.String("session_id", target.childSession.ID), zap.Error(pauseErr)) + } + } + + // The hard pause cancels the agent turn's request context. Keep delivery + // operations independent of that cancellation so the parent still receives + // the durable question after the child has been paused. + deliveryCtx := context.WithoutCancel(ctx) parentPrompt := formatParentQuestionPrompt(questionID, target.parentTask, target.childTask, req.Questions, req.Context) parentMetadata := map[string]interface{}{ models.MetaKeyParentQuestionID: questionID, @@ -151,23 +166,11 @@ func (h *Handlers) handleAskParentQuestion(ctx context.Context, msg *ws.Message) "sender_task_title": target.childTask.Title, senderSessionIDKey: target.childSession.ID, } - if _, dispatchErr := h.dispatchTaskMessage(ctx, target.parentTask.ID, target.parentSession, parentPrompt, parentMetadata, false, false); dispatchErr != nil { - _ = h.taskSvc.DeleteMessage(ctx, question.ID) + if _, dispatchErr := h.dispatchTaskMessage(deliveryCtx, target.parentTask.ID, target.parentSession, parentPrompt, parentMetadata, false, false); dispatchErr != nil { + _ = h.taskSvc.DeleteMessage(deliveryCtx, question.ID) return ws.NewError(msg.ID, msg.Action, ws.ErrorCodeInternalError, "failed to send question to direct parent: "+dispatchErr.Error(), nil) } - // Mark the child as waiting before asking the orchestrator to cancel the - // active turn. The pause is best effort because the durable message and - // parent delivery are already committed; a later lifecycle watchdog can - // still reconcile the session if the process exits during the race. - h.setSessionWaitingForInput(ctx, target.childTask.ID, target.childSession.ID) - if h.inputPauser != nil { - if _, pauseErr := h.inputPauser.PauseForClarificationInput(context.WithoutCancel(ctx), target.childSession.ID); pauseErr != nil { - h.logger.Warn("failed to pause autopilot child after parent question", - zap.String(parentQuestionIDKey, questionID), zap.String("session_id", target.childSession.ID), zap.Error(pauseErr)) - } - } - return ws.NewResponse(msg.ID, msg.Action, map[string]interface{}{ parentQuestionIDKey: questionID, stopTaskStatusKey: "waiting_for_parent", diff --git a/apps/backend/internal/mcp/handlers/parent_question_test.go b/apps/backend/internal/mcp/handlers/parent_question_test.go index 96188578a0..6b43a6df41 100644 --- a/apps/backend/internal/mcp/handlers/parent_question_test.go +++ b/apps/backend/internal/mcp/handlers/parent_question_test.go @@ -61,10 +61,23 @@ func TestHandleAskParentQuestion_PersistsRoutesAndPauses(t *testing.T) { svc, repo := newTestTaskService(t) parent, child, parentSession, childSession := seedParentQuestionScenario(t, svc, repo) h, orch := newMessageTaskHandler(t, svc, repo) - pauser := &recordingClarificationInputPauser{} + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + pauser := &recordingClarificationInputPauser{ + before: func() { + // The parent must not receive a reply route until the child's pause + // has completed. Otherwise message_task can resume a still-cancelling + // turn and lose the answer in a lifecycle race. + status := orch.queue.GetStatus(context.Background(), parentSession.ID) + require.Empty(t, status.Entries) + // The real pause cancels the MCP turn context. Parent delivery must + // survive that cancellation because it happens after the pause. + cancel() + }, + } h.inputPauser = pauser - resp, err := h.handleAskParentQuestion(context.Background(), parentQuestionMessage(t, child.ID, childSession.ID)) + resp, err := h.handleAskParentQuestion(ctx, parentQuestionMessage(t, child.ID, childSession.ID)) require.NoError(t, err) require.Equal(t, ws.MessageTypeResponse, resp.Type) diff --git a/apps/backend/internal/office/testharness/routes_office.go b/apps/backend/internal/office/testharness/routes_office.go index 690f4002a0..983e95968c 100644 --- a/apps/backend/internal/office/testharness/routes_office.go +++ b/apps/backend/internal/office/testharness/routes_office.go @@ -235,8 +235,11 @@ type seedRunRequest struct { ErrorMessage string `json:"error_message,omitempty"` IdempotencyKey string `json:"idempotency_key,omitempty"` RequestedAt *string `json:"requested_at,omitempty"` - ClaimedAt *string `json:"claimed_at,omitempty"` - FinishedAt *string `json:"finished_at,omitempty"` + // ScheduledRetryAt keeps a seeded queued run out of the live dispatcher + // until the E2E test explicitly changes its status. + ScheduledRetryAt *string `json:"scheduled_retry_at,omitempty"` + ClaimedAt *string `json:"claimed_at,omitempty"` + FinishedAt *string `json:"finished_at,omitempty"` } // seedRunHandler creates an office_runs row directly so the @@ -288,20 +291,31 @@ func createSeededRun( c *gin.Context, repo *officesqlite.Repository, log *logger.Logger, req seedRunRequest, ) (string, bool) { ctx := c.Request.Context() + var scheduledRetryAt *time.Time + if req.ScheduledRetryAt != nil && *req.ScheduledRetryAt != "" { + t, err := time.Parse(time.RFC3339, *req.ScheduledRetryAt) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "scheduled_retry_at must be RFC3339"}) + return "", false + } + t = t.UTC() + scheduledRetryAt = &t + } var idemPtr *string if req.IdempotencyKey != "" { v := req.IdempotencyKey idemPtr = &v } run := &officemodels.Run{ - ID: uuid.New().String(), - AgentProfileID: req.AgentProfileID, - Reason: req.Reason, - Payload: buildSeededRunPayload(req.TaskID, req.SessionID, req.CommentID, req.RoutineID), - Status: "queued", - CoalescedCount: 1, - IdempotencyKey: idemPtr, - ErrorMessage: req.ErrorMessage, + ID: uuid.New().String(), + AgentProfileID: req.AgentProfileID, + Reason: req.Reason, + Payload: buildSeededRunPayload(req.TaskID, req.SessionID, req.CommentID, req.RoutineID), + Status: "queued", + CoalescedCount: 1, + IdempotencyKey: idemPtr, + ScheduledRetryAt: scheduledRetryAt, + ErrorMessage: req.ErrorMessage, } if err := repo.CreateRun(ctx, run); err != nil { log.Error("test harness: create run failed", zap.Error(err)) diff --git a/apps/backend/internal/orchestrator/event_handlers_github.go b/apps/backend/internal/orchestrator/event_handlers_github.go index f5777b1f75..c6ebaff9d3 100644 --- a/apps/backend/internal/orchestrator/event_handlers_github.go +++ b/apps/backend/internal/orchestrator/event_handlers_github.go @@ -48,6 +48,7 @@ type GitHubService interface { ListPRWatchesBySession(ctx context.Context, sessionID string) ([]*github.PRWatch, error) UpdatePRWatchBranchIfSearching(ctx context.Context, id, branch string) error UpdatePRWatchPRNumber(ctx context.Context, id string, prNumber int) error + UpdatePRWatchRepository(ctx context.Context, id, owner, repo string) error ResetPRWatch(ctx context.Context, id, branch string) error AssociatePRWithTask(ctx context.Context, taskID, repositoryID string, pr *github.PR) (*github.TaskPR, error) AssociatePRWithTaskForWorkspace(ctx context.Context, workspaceID, taskID, repositoryID string, pr *github.PR) (*github.TaskPR, error) @@ -807,6 +808,19 @@ func (s *Service) searchPRForExistingWatch( ctx, workspaceID, watch.Owner, watch.Repo, branch, ) if findErr == nil && foundPR != nil { + if foundPR.RepoOwner != "" && foundPR.RepoName != "" && + (!strings.EqualFold(watch.Owner, foundPR.RepoOwner) || !strings.EqualFold(watch.Repo, foundPR.RepoName)) { + if err := s.githubService.UpdatePRWatchRepository(ctx, watch.ID, foundPR.RepoOwner, foundPR.RepoName); err != nil { + s.logger.Warn("failed to rebind PR watch repository", + zap.String("watch_id", watch.ID), + zap.String("owner", foundPR.RepoOwner), + zap.String("repo", foundPR.RepoName), + zap.Error(err)) + return + } + watch.Owner = foundPR.RepoOwner + watch.Repo = foundPR.RepoName + } if err := s.githubService.UpdatePRWatchPRNumber(ctx, watch.ID, foundPR.Number); err != nil { s.logger.Warn("failed to update PR watch number", zap.String("watch_id", watch.ID), @@ -1170,13 +1184,24 @@ func (s *Service) resolveTaskRepo(ctx context.Context, taskID string) (string, s func (s *Service) associatePRFromPushScoped( ctx context.Context, workspaceID, sessionID, taskID, owner, repoName, repositoryID, branch string, pr *github.PR, ) { - if _, watchErr := s.githubService.CreatePRWatchForWorkspace( - ctx, workspaceID, sessionID, taskID, repositoryID, owner, repoName, pr.Number, branch, - ); watchErr != nil { + watchOwner, watchRepo := owner, repoName + if pr != nil && pr.RepoOwner != "" && pr.RepoName != "" { + watchOwner, watchRepo = pr.RepoOwner, pr.RepoName + } + watch, watchErr := s.githubService.CreatePRWatchForWorkspace( + ctx, workspaceID, sessionID, taskID, repositoryID, watchOwner, watchRepo, pr.Number, branch, + ) + if watchErr != nil { s.logger.Error("failed to create PR watch on push detection", zap.String("session_id", sessionID), zap.String("repository_id", repositoryID), zap.Error(watchErr)) + } else if watch != nil && watch.ID != "" && + (!strings.EqualFold(watch.Owner, watchOwner) || !strings.EqualFold(watch.Repo, watchRepo)) { + if err := s.githubService.UpdatePRWatchRepository(ctx, watch.ID, watchOwner, watchRepo); err != nil { + s.logger.Warn("failed to rebind existing PR watch repository", + zap.String("watch_id", watch.ID), zap.Error(err)) + } } if _, assocErr := s.githubService.AssociatePRWithTaskForWorkspace( diff --git a/apps/backend/internal/orchestrator/event_handlers_github_test.go b/apps/backend/internal/orchestrator/event_handlers_github_test.go index a6a4a43856..cc3a2127cf 100644 --- a/apps/backend/internal/orchestrator/event_handlers_github_test.go +++ b/apps/backend/internal/orchestrator/event_handlers_github_test.go @@ -385,6 +385,9 @@ func (m *mockGitHubService) UpdatePRWatchPRNumber(_ context.Context, _ string, p m.updatedPRNumber = prNumber return nil } +func (m *mockGitHubService) UpdatePRWatchRepository(context.Context, string, string, string) error { + return nil +} func (m *mockGitHubService) ResetPRWatch(_ context.Context, _, branch string) error { m.resetWatchCalls++ m.resetWatchBranch = branch diff --git a/apps/backend/internal/orchestrator/executor/executor.go b/apps/backend/internal/orchestrator/executor/executor.go index 5266cd09f2..c609476c53 100644 --- a/apps/backend/internal/orchestrator/executor/executor.go +++ b/apps/backend/internal/orchestrator/executor/executor.go @@ -392,20 +392,21 @@ type LaunchAgentRequest struct { CopyFiles string // Worktree configuration for concurrent agent execution - UseWorktree bool // Whether to use a Git worktree for isolation - WorktreeID string // Existing worktree ID to reuse (skip creation if set) - RepositoryID string // Repository ID for worktree tracking - RepositoryPath string // Path to the main repository (for worktree creation) - BaseBranch string // Base branch for the worktree (e.g., "main") - DefaultBranch string // Repository's default_branch, used as a fallback when BaseBranch is missing - CheckoutBranch string // Branch to fetch and checkout after worktree creation (e.g., PR head branch) - PRNumber int // GitHub PR number when CheckoutBranch is a PR head; enables refs/pull//head fetch for fork PRs. - RemoteContribution *models.RemoteContribution - WorktreeBranchPrefix string // Branch prefix for worktree branches - WorktreeBranchTemplate string // Branch name template for worktree branches - WorktreeBranchTicket string // External ticket value for branch templates - PullBeforeWorktree bool // Whether to pull from remote before creating the worktree - RemoteSyncHandled bool // Provider-authenticated origin refresh already completed + UseWorktree bool // Whether to use a Git worktree for isolation + WorktreeID string // Existing worktree ID to reuse (skip creation if set) + RepositoryID string // Repository ID for worktree tracking + RepositoryPath string // Path to the main repository (for worktree creation) + BaseBranch string // Base branch for the worktree (e.g., "main") + DefaultBranch string // Repository's default_branch, used as a fallback when BaseBranch is missing + CheckoutBranch string // Branch to fetch and checkout after worktree creation (e.g., PR head branch) + PRNumber int // GitHub PR number when CheckoutBranch is a PR head; enables refs/pull//head fetch for fork PRs. + RemoteContribution *models.RemoteContribution + ContributionDestination *models.ContributionDestination + WorktreeBranchPrefix string // Branch prefix for worktree branches + WorktreeBranchTemplate string // Branch name template for worktree branches + WorktreeBranchTicket string // External ticket value for branch templates + PullBeforeWorktree bool // Whether to pull from remote before creating the worktree + RemoteSyncHandled bool // Provider-authenticated origin refresh already completed // Task directory mode: place worktree at ~/.kandev/tasks/{TaskDirName}/{RepoName}/ TaskDirName string // Semantic task directory name (e.g. "fix-bug_ab12") @@ -436,24 +437,25 @@ type WorkspaceFolderSpec struct{ Name, LocalPath string } // the orchestrator package does not need to import lifecycle types into its // public API. type RepoSpec struct { - RepositoryID string - RepositoryPath string - RepositoryURL string - RepoName string - BaseBranch string - DefaultBranch string // Repository's default_branch, used as fallback when BaseBranch is missing - CheckoutBranch string - PRNumber int // GitHub PR number when CheckoutBranch is a PR head; enables refs/pull//head fetch for fork PRs. - RemoteContribution *models.RemoteContribution - WorktreeID string - WorktreeBranchPrefix string - WorktreeBranchTemplate string - WorktreeBranchTicket string - PullBeforeWorktree bool - RemoteSyncHandled bool - RepoSetupScript string - RepoCleanupScript string - CopyFiles string + RepositoryID string + RepositoryPath string + RepositoryURL string + RepoName string + BaseBranch string + DefaultBranch string // Repository's default_branch, used as fallback when BaseBranch is missing + CheckoutBranch string + PRNumber int // GitHub PR number when CheckoutBranch is a PR head; enables refs/pull//head fetch for fork PRs. + RemoteContribution *models.RemoteContribution + ContributionDestination *models.ContributionDestination + WorktreeID string + WorktreeBranchPrefix string + WorktreeBranchTemplate string + WorktreeBranchTicket string + PullBeforeWorktree bool + RemoteSyncHandled bool + RepoSetupScript string + RepoCleanupScript string + CopyFiles string // BranchSlug, when non-empty, suffixes the repo dir so the same repo can // host multiple branch worktrees as siblings within one task. Set by the // orchestrator when buildRepoSpecs detects multiple rows sharing a diff --git a/apps/backend/internal/orchestrator/executor/executor_credentials.go b/apps/backend/internal/orchestrator/executor/executor_credentials.go index 9fa6c712a1..6a1cd28d83 100644 --- a/apps/backend/internal/orchestrator/executor/executor_credentials.go +++ b/apps/backend/internal/orchestrator/executor/executor_credentials.go @@ -45,14 +45,16 @@ const ( var ErrGitHubCredentialBrokerURL = errors.New("invalid Git credential broker URL") type githubCredentialScope struct { - Lease string `json:"lease"` - TaskID string `json:"task_id"` - SessionID string `json:"session_id"` - RepositoryID string `json:"repository_id"` - Owner string `json:"owner"` - Repo string `json:"repo"` - Host string `json:"host"` - Path string `json:"path"` + Lease string `json:"lease"` + TaskID string `json:"task_id"` + SessionID string `json:"session_id"` + RepositoryID string `json:"repository_id"` + Owner string `json:"owner"` + Repo string `json:"repo"` + Host string `json:"host"` + Path string `json:"path"` + ProviderID string `json:"provider_id,omitempty"` + ParentProviderID string `json:"parent_provider_id,omitempty"` } // GitCredentialLeaseIssuer creates opaque helper leases. *gitcredentials.Broker @@ -61,6 +63,10 @@ type GitCredentialLeaseIssuer interface { Issue(context.Context, gitcredentials.Scope) (gitcredentials.Lease, error) } +// GitHubCredentialLease preserves the historical executor-facing lease name +// while the broker uses the provider-neutral gitcredentials.Lease contract. +type GitHubCredentialLease = gitcredentials.Lease + // GitHubCredentialLeaseIssuer is retained as a source-compatible name for // callers which configure the same generic HTTPS helper broker. type GitHubCredentialLeaseIssuer = GitCredentialLeaseIssuer @@ -172,17 +178,34 @@ func (e *Executor) configureGitCredentialBrokerForRepositories( req *LaunchAgentRequest, infos []*repoInfo, ) error { - if e.gitCredentialIssuer == nil || len(infos) == 0 { + if len(infos) == 0 { return nil } - githubManaged, err := e.resolveManagedGitHubCredentials(ctx, req, infos) - if err != nil { - return err + 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 } if req.Env == nil { req.Env = make(map[string]string) } - scopes, helpers, err := e.issueGitCredentialScopes(ctx, req, infos, githubManaged) + if policy.Mode == taskGitCredentialsModeExecutor || req.Env[envGitHubToken] != "" || req.Env[envGHToken] != "" { + if err := removeManagedGitHubCredentials(req); err != nil { + return err + } + clearManagedContributionDestinations(req, infos) + return nil + } + 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) if err != nil { return err } @@ -238,6 +261,14 @@ func (e *Executor) issueGitCredentialScopes( 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 } @@ -285,6 +316,34 @@ func includesGitHubRepository(infos []*repoInfo) bool { return false } +func hasManagedContributionDestination(infos []*repoInfo) bool { + for _, info := range infos { + if info != nil && info.ContributionDestination != nil { + return true + } + } + return false +} + +func clearManagedContributionDestinations(req *LaunchAgentRequest, infos []*repoInfo) { + for _, info := range infos { + if info != nil { + info.ContributionDestination = nil + } + } + if req == nil { + return + } + req.ContributionDestination = nil + for index := range req.Repositories { + req.Repositories[index].ContributionDestination = nil + } +} + +func removeManagedGitHubCredentials(req *LaunchAgentRequest) error { + return removeManagedGitCredentials(req) +} + func removeManagedGitCredentials(req *LaunchAgentRequest) error { if req == nil || req.Env == nil { return nil @@ -353,6 +412,12 @@ func (e *Executor) issueGitCredentialScope( lease, err := e.gitCredentialIssuer.Issue(ctx, gitcredentials.Scope{ ProviderID: providerID, WorkspaceID: req.WorkspaceID, TaskID: req.TaskID, SessionID: req.SessionID, RepositoryID: info.RepositoryID, Host: host, Path: path, + IdentityProviderID: func() string { + if providerID == gitHubProviderID { + return strings.TrimSpace(repository.ProviderRepoID) + } + return "" + }(), }) if err != nil { return nil, fmt.Errorf("issue Git credential lease: %w", err) @@ -363,6 +428,12 @@ func (e *Executor) issueGitCredentialScope( return &githubCredentialScope{ Lease: lease.Token, TaskID: req.TaskID, SessionID: req.SessionID, RepositoryID: info.RepositoryID, Owner: owner, Repo: repo, Host: host, Path: path, + ProviderID: func() string { + if providerID == gitHubProviderID { + return strings.TrimSpace(repository.ProviderRepoID) + } + return "" + }(), }, nil } @@ -396,14 +467,78 @@ func (e *Executor) issueGitHubContributionCredentialScope( if !binding.CollaborationAllowed { return nil, fmt.Errorf("remote contribution does not permit collaboration") } + return e.issueGitHubCredentialScopeForIdentity( + ctx, req, info.RepositoryID, parts[0], parts[1], defaultGitHubHost, binding.SourceRepository.ProviderID, "", nil, + ) +} + +func (e *Executor) issueGitHubContributionDestinationCredentialScope( + ctx context.Context, + req *LaunchAgentRequest, + info *repoInfo, +) (*githubCredentialScope, error) { + destination := info.ContributionDestination + if destination == nil { + return nil, nil + } + if err := destination.Validate(); err != nil { + return nil, fmt.Errorf("validate contribution destination credential scope: %w", err) + } + if destination.Provider != models.ContributionDestinationProviderGitHub || info.Repository == nil { + return nil, nil + } + if !strings.EqualFold(destination.SourceRepository.Host, defaultGitHubHost) || + !strings.EqualFold(destination.TargetRepository.Host, defaultGitHubHost) { + return nil, fmt.Errorf("contribution destination is not a GitHub repository") + } + canonicalPath := strings.TrimSpace(info.Repository.ProviderOwner) + "/" + strings.TrimSpace(info.Repository.ProviderName) + if !strings.EqualFold(destination.SourceRepository.Path, canonicalPath) { + return nil, fmt.Errorf("contribution destination source does not match the canonical repository") + } + if strings.TrimSpace(info.Repository.ProviderRepoID) == "" || + !strings.EqualFold(destination.SourceRepository.ProviderID, info.Repository.ProviderRepoID) { + return nil, fmt.Errorf("contribution destination source provider identity does not match the canonical repository") + } + if destination.CredentialBinding == nil { + return nil, fmt.Errorf("contribution destination credential binding is missing") + } + parts := strings.Split(destination.TargetRepository.Path, "/") + if len(parts) != 2 || parts[0] == "" || parts[1] == "" { + return nil, fmt.Errorf("contribution destination GitHub target identity is invalid") + } + if strings.EqualFold(destination.TargetRepository.Path, canonicalPath) { + return nil, nil + } + return e.issueGitHubCredentialScopeForIdentity( + ctx, req, info.RepositoryID, parts[0], parts[1], defaultGitHubHost, + destination.TargetRepository.ProviderID, destination.SourceRepository.ProviderID, destination.CredentialBinding, + ) +} + +func (e *Executor) issueGitHubCredentialScopeForIdentity( + ctx context.Context, + req *LaunchAgentRequest, + repositoryID, owner, repo, host string, + providerID, parentProviderID string, + destinationBinding *models.ContributionDestinationCredentialBinding, +) (*githubCredentialScope, error) { if err := validateGitHubCredentialBrokerURL(e.gitCredentialBrokerURL, req.ExecutorType); err != nil { return nil, err } - path := "/" + parts[0] + "/" + parts[1] + ".git" - lease, err := e.gitCredentialIssuer.Issue(ctx, gitcredentials.Scope{ + path := "/" + owner + "/" + repo + ".git" + scope := gitcredentials.Scope{ ProviderID: gitHubProviderID, WorkspaceID: req.WorkspaceID, TaskID: req.TaskID, SessionID: req.SessionID, - RepositoryID: info.RepositoryID, Host: defaultGitHubHost, Path: path, - }) + RepositoryID: repositoryID, Host: host, Path: path, + IdentityProviderID: providerID, ParentProviderID: parentProviderID, + } + if destinationBinding != nil { + encodedBinding, err := json.Marshal(destinationBinding) + if err != nil { + return nil, fmt.Errorf("encode contribution destination credential binding: %w", err) + } + scope.CredentialBinding = string(encodedBinding) + } + lease, err := e.gitCredentialIssuer.Issue(ctx, scope) if err != nil { return nil, fmt.Errorf("issue Git credential lease: %w", err) } @@ -412,7 +547,8 @@ func (e *Executor) issueGitHubContributionCredentialScope( } return &githubCredentialScope{ Lease: lease.Token, TaskID: req.TaskID, SessionID: req.SessionID, - RepositoryID: info.RepositoryID, Owner: parts[0], Repo: parts[1], Host: defaultGitHubHost, Path: path, + RepositoryID: repositoryID, Owner: owner, Repo: repo, Host: host, Path: path, + ProviderID: providerID, ParentProviderID: parentProviderID, }, nil } diff --git a/apps/backend/internal/orchestrator/executor/executor_credentials_test.go b/apps/backend/internal/orchestrator/executor/executor_credentials_test.go index c73196b746..8756386e8e 100644 --- a/apps/backend/internal/orchestrator/executor/executor_credentials_test.go +++ b/apps/backend/internal/orchestrator/executor/executor_credentials_test.go @@ -303,7 +303,7 @@ func TestConfigureGitHubCredentialBrokerPublishesLocalHelperBeforePreparation(t } func TestConfigureGitHubCredentialBrokerIssuesOneLeasePerRepository(t *testing.T) { - issuer := &fakeGitHubCredentialLeaseIssuer{} + issuer := &fakeGitHubCredentialLeaseIssuer{lease: GitHubCredentialLease{Token: "opaque-lease"}} exec := newTestExecutor(t, &mockAgentManager{}, newMockRepository()) exec.SetGitHubCredentialBroker(issuer, "https://kandev.example/api/github/credentials/resolve") req := &LaunchAgentRequest{ @@ -534,6 +534,50 @@ func TestIssueGitHubContributionCredentialScopeIgnoresGitLabBinding(t *testing.T } } +func TestConfigureGitHubCredentialBrokerIncludesExactContributionDestination(t *testing.T) { + issuer := &fakeGitHubCredentialLeaseIssuer{lease: GitHubCredentialLease{Token: "opaque-lease"}} + exec := newTestExecutor(t, &mockAgentManager{}, newMockRepository()) + exec.SetGitHubCredentialBroker(issuer, "https://kandev.example/api/github/credentials/resolve") + 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{Provider: "github", ProviderRepoID: "100", ProviderOwner: "kdlbs", ProviderName: "kandev"}, + ContributionDestination: &models.ContributionDestination{ + Version: models.ContributionDestinationVersion, + Provider: models.ContributionDestinationProviderGitHub, + SourceRepository: models.ContributionDestinationRepository{ + Host: "github.com", Path: "kdlbs/kandev", ProviderID: "100", RemoteURL: "https://github.com/kdlbs/kandev.git", + }, + TargetRepository: models.ContributionDestinationRepository{ + Host: "github.com", Path: "agent/kandev", ProviderID: "200", RemoteURL: "https://github.com/agent/kandev.git", + }, + CredentialBinding: &models.ContributionDestinationCredentialBinding{ + Source: string("pat"), Login: "automation", CredentialGeneration: 7, + }, + }, + } + + if err := exec.configureGitHubCredentialBroker(context.Background(), req, info); err != nil { + t.Fatalf("configureGitHubCredentialBroker() error = %v", err) + } + if len(issuer.requests) != 2 { + t.Fatalf("lease requests = %d, want canonical and destination scopes: %+v", len(issuer.requests), issuer.requests) + } + if issuer.requests[1].Path != "/agent/kandev.git" { + t.Fatalf("destination lease scope = %+v", issuer.requests[1]) + } + var destinationBinding models.ContributionDestinationCredentialBinding + if err := json.Unmarshal([]byte(issuer.requests[1].CredentialBinding), &destinationBinding); err != nil { + t.Fatalf("decode destination lease binding: %v", err) + } + if issuer.requests[1].IdentityProviderID != "200" || issuer.requests[1].ParentProviderID != "100" || destinationBinding.Source != "pat" { + t.Fatalf("destination lease identity = %+v", issuer.requests[1]) + } +} + func TestConfigureGitHubCredentialBrokerPreservesExplicitProfileToken(t *testing.T) { issuer := &fakeGitHubCredentialLeaseIssuer{lease: gitcredentials.Lease{Token: "opaque-lease"}} exec := newTestExecutor(t, &mockAgentManager{}, newMockRepository()) @@ -561,6 +605,53 @@ func TestConfigureGitHubCredentialBrokerPreservesExplicitProfileToken(t *testing } } +func TestConfigureGitHubCredentialBrokerDisablesDestinationForExplicitProfileToken(t *testing.T) { + issuer := &fakeGitHubCredentialLeaseIssuer{lease: GitHubCredentialLease{Token: "opaque-lease"}} + exec := newTestExecutor(t, &mockAgentManager{}, newMockRepository()) + exec.SetGitHubCredentialBroker(issuer, "https://kandev.example/api/github/credentials/resolve") + destination := testCredentialDestination() + req := &LaunchAgentRequest{ + TaskID: "task-1", WorkspaceID: "workspace-1", SessionID: "session-1", + ExecutorType: string(models.ExecutorTypeRemoteDocker), + Env: map[string]string{envGHToken: "profile-token"}, + ContributionDestination: &destination, + } + info := &repoInfo{ + RepositoryID: "repo-1", + Repository: &models.Repository{Provider: "github", ProviderOwner: "kdlbs", ProviderName: "kandev"}, + ContributionDestination: &destination, + } + + if err := exec.configureGitHubCredentialBroker(context.Background(), req, info); err != nil { + t.Fatalf("configureGitHubCredentialBroker() error = %v", err) + } + if issuer.calls != 0 { + t.Fatalf("IssueGitHubCredentialLease calls = %d, want 0", issuer.calls) + } + if req.ContributionDestination != nil || info.ContributionDestination != nil { + t.Fatalf("managed destination remained active: request=%#v info=%#v", req.ContributionDestination, info.ContributionDestination) + } +} + +func TestConfigureGitHubCredentialBrokerRejectsUnboundDestination(t *testing.T) { + issuer := &fakeGitHubCredentialLeaseIssuer{lease: GitHubCredentialLease{Token: "opaque-lease"}} + exec := newTestExecutor(t, &mockAgentManager{}, newMockRepository()) + exec.SetGitHubCredentialBroker(issuer, "https://kandev.example/api/github/credentials/resolve") + destination := testCredentialDestination() + info := &repoInfo{ + RepositoryID: "repo-1", + Repository: &models.Repository{Provider: "github", ProviderRepoID: "100", ProviderOwner: "kdlbs", ProviderName: "kandev"}, + ContributionDestination: &destination, + } + err := exec.configureGitHubCredentialBroker(context.Background(), &LaunchAgentRequest{ + TaskID: "task-1", WorkspaceID: "workspace-1", SessionID: "session-1", + ExecutorType: string(models.ExecutorTypeRemoteDocker), Env: map[string]string{}, + }, info) + if err == nil || !strings.Contains(err.Error(), "credential binding is missing") { + t.Fatalf("configureGitHubCredentialBroker() error = %v, want missing binding error", err) + } +} + func TestConfigureGitHubCredentialBrokerSkipsExecutorInheritedPolicy(t *testing.T) { issuer := &fakeGitHubCredentialLeaseIssuer{lease: gitcredentials.Lease{Token: "opaque-lease"}} exec := newTestExecutor(t, &mockAgentManager{}, newMockRepository()) @@ -617,6 +708,46 @@ func TestConfigureGitHubCredentialBrokerSkipsExecutorInheritedPolicy(t *testing. } } +func TestConfigureGitHubCredentialBrokerDisablesDestinationForExecutorPolicy(t *testing.T) { + issuer := &fakeGitHubCredentialLeaseIssuer{lease: GitHubCredentialLease{Token: "opaque-lease"}} + exec := newTestExecutor(t, &mockAgentManager{}, newMockRepository()) + exec.SetGitHubCredentialBroker(issuer, "https://kandev.example/api/github/credentials/resolve") + exec.SetTaskGitCredentialPolicyResolver(fakeTaskGitCredentialPolicyResolver{ + policy: TaskGitCredentialPolicy{Mode: "executor"}, + }) + destination := testCredentialDestination() + req := &LaunchAgentRequest{ + TaskID: "task-1", WorkspaceID: "workspace-1", SessionID: "session-1", + ContributionDestination: &destination, + Env: map[string]string{}, + } + info := &repoInfo{ + RepositoryID: "repo-1", + Repository: &models.Repository{Provider: "github", ProviderOwner: "kdlbs", ProviderName: "kandev"}, + ContributionDestination: &destination, + } + + if err := exec.configureGitHubCredentialBroker(context.Background(), req, info); err != nil { + t.Fatalf("configureGitHubCredentialBroker() error = %v", err) + } + if req.ContributionDestination != nil || info.ContributionDestination != nil { + t.Fatalf("managed destination remained active: request=%#v info=%#v", req.ContributionDestination, info.ContributionDestination) + } +} + +func testCredentialDestination() models.ContributionDestination { + return models.ContributionDestination{ + Version: models.ContributionDestinationVersion, + Provider: models.ContributionDestinationProviderGitHub, + SourceRepository: models.ContributionDestinationRepository{ + Host: "github.com", Path: "kdlbs/kandev", ProviderID: "100", RemoteURL: "https://github.com/kdlbs/kandev.git", + }, + TargetRepository: models.ContributionDestinationRepository{ + Host: "github.com", Path: "agent/kandev", ProviderID: "200", RemoteURL: "https://github.com/agent/kandev.git", + }, + } +} + func TestConfigureGitHubCredentialBrokerRejectsRemoteLoopbackURL(t *testing.T) { issuer := &fakeGitHubCredentialLeaseIssuer{lease: gitcredentials.Lease{Token: "opaque-lease"}} exec := newTestExecutor(t, &mockAgentManager{}, newMockRepository()) diff --git a/apps/backend/internal/orchestrator/executor/executor_execute.go b/apps/backend/internal/orchestrator/executor/executor_execute.go index b5ab058084..5b641a5e65 100644 --- a/apps/backend/internal/orchestrator/executor/executor_execute.go +++ b/apps/backend/internal/orchestrator/executor/executor_execute.go @@ -1458,16 +1458,17 @@ func buildRepoSpecs(allRepos []*repoInfo) []RepoSpec { out := make([]RepoSpec, 0, len(allRepos)) for _, info := range allRepos { spec := RepoSpec{ - RepositoryID: info.RepositoryID, - RepositoryPath: info.RepositoryPath, - BaseBranch: info.BaseBranch, - CheckoutBranch: info.CheckoutBranch, - PRNumber: info.PRNumber, - RemoteContribution: info.RemoteContribution, - WorktreeBranchPrefix: info.WorktreeBranchPrefix, - WorktreeBranchTemplate: info.WorktreeBranchTemplate, - PullBeforeWorktree: info.PullBeforeWorktree, - RemoteSyncHandled: info.RemoteSyncHandled, + RepositoryID: info.RepositoryID, + RepositoryPath: info.RepositoryPath, + BaseBranch: info.BaseBranch, + CheckoutBranch: info.CheckoutBranch, + PRNumber: info.PRNumber, + RemoteContribution: info.RemoteContribution, + ContributionDestination: info.ContributionDestination, + WorktreeBranchPrefix: info.WorktreeBranchPrefix, + WorktreeBranchTemplate: info.WorktreeBranchTemplate, + PullBeforeWorktree: info.PullBeforeWorktree, + RemoteSyncHandled: info.RemoteSyncHandled, } if info.Repository != nil { spec.RepoName = info.Repository.Name @@ -1531,6 +1532,7 @@ func (e *Executor) applyRepositoryConfig(req *LaunchAgentRequest, task *v1.Task, req.CheckoutBranch = repoInfo.CheckoutBranch req.PRNumber = repoInfo.PRNumber req.RemoteContribution = repoInfo.RemoteContribution + req.ContributionDestination = repoInfo.ContributionDestination req.WorktreeBranchPrefix = repoInfo.WorktreeBranchPrefix req.WorktreeBranchTemplate = repoInfo.WorktreeBranchTemplate req.PullBeforeWorktree = repoInfo.PullBeforeWorktree diff --git a/apps/backend/internal/orchestrator/executor/executor_resume.go b/apps/backend/internal/orchestrator/executor/executor_resume.go index cbfa915252..7ca7b6ecd8 100644 --- a/apps/backend/internal/orchestrator/executor/executor_resume.go +++ b/apps/backend/internal/orchestrator/executor/executor_resume.go @@ -61,18 +61,19 @@ func isTerminalSessionState(state models.TaskSessionState) bool { // repoInfo holds resolved repository details for agent launch. type repoInfo struct { - RepositoryID string - RepositoryPath string - BaseBranch string - CheckoutBranch string - PRNumber int // GitHub PR number when CheckoutBranch is a PR head; sourced from task_repositories.metadata["pr_number"]. - RemoteContribution *models.RemoteContribution - Position int - WorktreeBranchPrefix string - WorktreeBranchTemplate string - PullBeforeWorktree bool - RemoteSyncHandled bool - Repository *models.Repository + RepositoryID string + RepositoryPath string + BaseBranch string + CheckoutBranch string + PRNumber int // GitHub PR number when CheckoutBranch is a PR head; sourced from task_repositories.metadata["pr_number"]. + RemoteContribution *models.RemoteContribution + ContributionDestination *models.ContributionDestination + Position int + WorktreeBranchPrefix string + WorktreeBranchTemplate string + PullBeforeWorktree bool + RemoteSyncHandled bool + Repository *models.Repository } // resumeCredentialSnapshotBackup holds the non-secret routing metadata that @@ -177,6 +178,11 @@ func (e *Executor) resolveTaskRepoInfoForSession( info.BaseBranch = binding.BaseBranch info.CheckoutBranch = binding.HeadBranch } + if destination, found, err := models.LoadContributionDestination(tr.Metadata); err != nil { + return nil, fmt.Errorf("load contribution destination for task repository %q: %w", tr.ID, err) + } else if found { + info.ContributionDestination = &destination + } if info.RepositoryID == "" { return info, nil } @@ -1165,6 +1171,12 @@ func (e *Executor) applyResumeRepoConfig( repositoryPath := repository.LocalPath applyResumeRepoBasics(req, repository, repositoryPath) + for _, info := range allRepos { + if info != nil && info.RepositoryID == repositoryID { + req.ContributionDestination = info.ContributionDestination + break + } + } if err := e.applyResumeCloneURL(req, repository, baseBranch); err != nil { return "", err @@ -1288,6 +1300,11 @@ func (e *Executor) applyResumeWorktreeConfig( req.CheckoutBranch = primaryTaskRepo.CheckoutBranch req.PRNumber = prNumberFromMetadata(primaryTaskRepo.Metadata) } + if primaryTaskRepo != nil && primaryTaskRepo.RepositoryID == repositoryID && req.ContributionDestination == nil { + if destination, found, err := models.LoadContributionDestination(primaryTaskRepo.Metadata); err == nil && found { + req.ContributionDestination = &destination + } + } req.WorktreeBranchPrefix = repository.WorktreeBranchPrefix req.WorktreeBranchTemplate = repository.WorktreeBranchTemplate req.PullBeforeWorktree = repository.PullBeforeWorktree diff --git a/apps/backend/internal/orchestrator/executor/executor_resume_test.go b/apps/backend/internal/orchestrator/executor/executor_resume_test.go index 6a453dbfc3..0a43795645 100644 --- a/apps/backend/internal/orchestrator/executor/executor_resume_test.go +++ b/apps/backend/internal/orchestrator/executor/executor_resume_test.go @@ -1152,6 +1152,43 @@ func TestApplyResumeRepoConfig_BaseBranchByExecutorType(t *testing.T) { } } +func TestApplyResumeWorktreeConfigPreservesSelectedRepositoryDestination(t *testing.T) { + primaryDestination := resumeTestContributionDestination("200") + selectedDestination := resumeTestContributionDestination("201") + primaryMetadata := map[string]interface{}{} + if err := models.PutContributionDestination(primaryMetadata, &primaryDestination); err != nil { + t.Fatalf("PutContributionDestination() = %v", err) + } + + repo := newMockRepository() + repo.taskRepositories["primary-link"] = &models.TaskRepository{ + ID: "primary-link", TaskID: "task-1", RepositoryID: "repo-primary", Metadata: primaryMetadata, + } + exec := newTestExecutor(t, &mockAgentManager{}, repo) + req := &LaunchAgentRequest{ContributionDestination: &selectedDestination} + exec.applyResumeWorktreeConfig( + context.Background(), &v1.Task{ID: "task-1"}, req, + &models.Repository{ID: "repo-selected"}, "repo-selected", "/tmp/repo", "main", nil, + ) + + if req.ContributionDestination == nil || req.ContributionDestination.TargetRepository.ProviderID != "201" { + t.Fatalf("resume destination = %#v, want selected repository destination", req.ContributionDestination) + } +} + +func resumeTestContributionDestination(providerID string) models.ContributionDestination { + return models.ContributionDestination{ + Version: models.ContributionDestinationVersion, + Provider: models.ContributionDestinationProviderGitHub, + SourceRepository: models.ContributionDestinationRepository{ + Host: "github.com", Path: "kdlbs/kandev", ProviderID: "100", RemoteURL: "https://github.com/kdlbs/kandev.git", + }, + TargetRepository: models.ContributionDestinationRepository{ + Host: "github.com", Path: "alice/kandev", ProviderID: providerID, RemoteURL: "https://github.com/alice/kandev.git", + }, + } +} + // TestApplyResumeRepoConfig_WorktreeStampsTaskDir locks in the fix for // resumes of single-repo worktree tasks: the lifecycle preparer hands the // request to worktree.Manager.Create, which rejects requests missing diff --git a/apps/backend/internal/scriptengine/providers.go b/apps/backend/internal/scriptengine/providers.go index b92341da77..a9633f7b76 100644 --- a/apps/backend/internal/scriptengine/providers.go +++ b/apps/backend/internal/scriptengine/providers.go @@ -78,6 +78,62 @@ func RemoteContributionSetupScript(binding *models.RemoteContribution) (string, branchSuffix, branchSuffix), nil } +// ContributionDestinationSetupScript configures a server-bound fork as the +// push remote for the current branch. It never changes origin or the branch's +// upstream remote, so fetch and pull continue to use the canonical checkout. +func ContributionDestinationSetupScript(destination *models.ContributionDestination) (string, error) { + return ContributionDestinationSetupScriptAt(destination, "/workspace") +} + +// ContributionDestinationSetupScriptAt is the workspace-path variant used by +// host and SSH preparers whose checkout root is not /workspace. +func ContributionDestinationSetupScriptAt(destination *models.ContributionDestination, workspacePath string) (string, error) { + if destination == nil { + return "", nil + } + if err := destination.Validate(); err != nil { + return "", fmt.Errorf("validate contribution destination: %w", err) + } + remoteName := destination.ContributionRemoteName() + return fmt.Sprintf(` + +# ---- kandev-managed: configure contribution destination ---- +# origin and the branch upstream remain canonical; only ordinary pushes use +# this exact, server-verified destination repository. +( + set -eu + cd %s + destination_remote=%s + destination_url=%s + if configured_url=$(git config --get "remote.$destination_remote.url" 2>/dev/null); then + if [ "$configured_url" != "$destination_url" ]; then + echo 'kandev: contribution destination identity conflict' >&2 + exit 1 + fi + else + git remote add "$destination_remote" "$destination_url" + fi + configured_push_urls=$(git config --get-all "remote.$destination_remote.pushurl" 2>/dev/null || true) + if [ -n "$configured_push_urls" ]; then + while IFS= read -r configured_push_url; do + if [ "$configured_push_url" != "$destination_url" ]; then + echo 'kandev: contribution destination push identity conflict' >&2 + exit 1 + fi + done < 0 { + request.Repositories[0].ContributionDestination = f.destination + } + return nil +} + +func TestCreateTaskPreparesManagedDestinationBeforeTaskInsert(t *testing.T) { + svc, _, repo := createTestService(t) + ctx := context.Background() + workflowID := seedWorkspaceAndWorkflowForCreate(t, ctx, repo, "ws-improve") + workflow, err := repo.GetWorkflow(ctx, workflowID) + if err != nil { + t.Fatalf("get workflow: %v", err) + } + templateID := "improve-kandev" + workflow.WorkflowTemplateID = &templateID + if err := repo.UpdateWorkflow(ctx, workflow); err != nil { + t.Fatalf("update workflow template: %v", err) + } + if err := repo.CreateRepository(ctx, &models.Repository{ + ID: "repo-kandev", + WorkspaceID: "ws-improve", + Name: "kandev", + Provider: "github", + ProviderHost: "https://github.com", + ProviderOwner: "kdlbs", + ProviderName: "kandev", + ProviderRepoID: "100", + RemoteURL: "https://github.com/kdlbs/kandev.git", + DefaultBranch: "main", + }); err != nil { + t.Fatalf("create repository: %v", err) + } + + preparer := &fakeContributionDestinationPreparer{err: errors.New("fork preparation failed")} + svc.SetContributionDestinationPreparer(preparer) + _, err = svc.CreateTask(ctx, &CreateTaskRequest{ + WorkspaceID: "ws-improve", + WorkflowID: workflowID, + ExternalID: "improve-1", + Title: "Improve", + Repositories: []TaskRepositoryInput{{ + RepositoryID: "repo-kandev", + BaseBranch: "main", + }}, + }) + if err == nil || err.Error() != "fork preparation failed" { + t.Fatalf("CreateTask error = %v, want fork preparation failure", err) + } + if preparer.calls != 1 { + t.Fatalf("preparer calls = %d, want 1", preparer.calls) + } + if preparer.gotWorkflow == nil || preparer.gotWorkflow.WorkflowTemplateID == nil || + *preparer.gotWorkflow.WorkflowTemplateID != "improve-kandev" { + t.Fatalf("preparer workflow = %#v, want improve-kandev template", preparer.gotWorkflow) + } + if len(preparer.gotRepos) != 1 || preparer.gotRepos[0].ID != "repo-kandev" { + t.Fatalf("preparer repositories = %#v, want canonical repository", preparer.gotRepos) + } + if _, lookupErr := repo.GetTaskByExternalID(ctx, "ws-improve", "improve-1"); !errors.Is(lookupErr, repository.ErrTaskNotFound) && + !errors.Is(lookupErr, repoerrors.ErrTaskNotFound) { + t.Fatalf("task lookup error = %v, want task not inserted", lookupErr) + } +} + +func TestCreateTaskSkipsDestinationPreparationForOrdinaryWorkflow(t *testing.T) { + svc, _, repo := createTestService(t) + ctx := context.Background() + workflowID := seedWorkspaceAndWorkflowForCreate(t, ctx, repo, "ws-ordinary") + preparer := &fakeContributionDestinationPreparer{} + svc.SetContributionDestinationPreparer(preparer) + + if _, err := svc.CreateTask(ctx, &CreateTaskRequest{ + WorkspaceID: "ws-ordinary", + WorkflowID: workflowID, + Title: "Ordinary", + }); err != nil { + t.Fatalf("CreateTask: %v", err) + } + if preparer.calls != 0 { + t.Fatalf("preparer calls = %d, want 0", preparer.calls) + } +} + +func TestCreateTaskPersistsServerAuthoredDestinationOnCanonicalAttachment(t *testing.T) { + svc, _, repo := createTestService(t) + ctx := context.Background() + workflowID := seedWorkspaceAndWorkflowForCreate(t, ctx, repo, "ws-persist-destination") + workflow, err := repo.GetWorkflow(ctx, workflowID) + if err != nil { + t.Fatalf("get workflow: %v", err) + } + templateID := "improve-kandev" + workflow.WorkflowTemplateID = &templateID + if err := repo.UpdateWorkflow(ctx, workflow); err != nil { + t.Fatalf("update workflow template: %v", err) + } + if err := repo.CreateRepository(ctx, &models.Repository{ + ID: "repo-persist-destination", + WorkspaceID: "ws-persist-destination", + Name: "kandev", + Provider: "github", + ProviderHost: "https://github.com", + ProviderOwner: "kdlbs", + ProviderName: "kandev", + ProviderRepoID: "100", + RemoteURL: "https://github.com/kdlbs/kandev.git", + DefaultBranch: "main", + }); err != nil { + t.Fatalf("create repository: %v", err) + } + destination := testContributionDestinationForService() + preparer := &fakeContributionDestinationPreparer{destination: &destination} + svc.SetContributionDestinationPreparer(preparer) + result, err := svc.CreateTask(ctx, &CreateTaskRequest{ + WorkspaceID: "ws-persist-destination", + WorkflowID: workflowID, + Title: "Persist destination", + Repositories: []TaskRepositoryInput{{RepositoryID: "repo-persist-destination", BaseBranch: "main"}}, + }) + if err != nil { + t.Fatalf("CreateTask: %v", err) + } + attachments, err := repo.ListTaskRepositories(ctx, result.Task.ID) + if err != nil { + t.Fatalf("ListTaskRepositories: %v", err) + } + if len(attachments) != 1 { + t.Fatalf("attachments = %d, want 1", len(attachments)) + } + got, ok, err := models.LoadContributionDestination(attachments[0].Metadata) + if err != nil { + t.Fatalf("LoadContributionDestination: %v", err) + } + if !ok || got != destination { + t.Fatalf("stored destination = %#v, %v; want %#v, true", got, ok, destination) + } +} + +func testContributionDestinationForService() models.ContributionDestination { + return models.ContributionDestination{ + Version: models.ContributionDestinationVersion, + Provider: models.ContributionDestinationProviderGitHub, + SourceRepository: models.RemoteContributionRepository{ + Host: "github.com", Path: "kdlbs/kandev", ProviderID: "100", RemoteURL: "https://github.com/kdlbs/kandev.git", + }, + TargetRepository: models.RemoteContributionRepository{ + Host: "github.com", Path: "alice/kandev", ProviderID: "200", RemoteURL: "https://github.com/alice/kandev.git", + }, + } +} diff --git a/apps/backend/internal/task/service/service_requests.go b/apps/backend/internal/task/service/service_requests.go index 04ea8244b0..8caad13c91 100644 --- a/apps/backend/internal/task/service/service_requests.go +++ b/apps/backend/internal/task/service/service_requests.go @@ -30,7 +30,11 @@ type TaskRepositoryInput struct { // intentionally excluded from JSON request surfaces; callers must not be // able to forge a writable source binding. RemoteContribution *models.RemoteContribution `json:"-"` - TrustedRemote bool `json:"-"` + // ContributionDestination is server-authored during managed Improve + // Kandev task preparation. It is never accepted from REST, WebSocket, or + // MCP JSON request bodies. + ContributionDestination *models.ContributionDestination `json:"-"` + TrustedRemote bool `json:"-"` // ResolveProviderDefaults opts the GitHub-URL resolution path into a // synchronous default-branch probe (git ls-remote --symref) when neither diff --git a/apps/backend/internal/task/service/service_tasks.go b/apps/backend/internal/task/service/service_tasks.go index 1ae055d72f..1c6151b1b2 100644 --- a/apps/backend/internal/task/service/service_tasks.go +++ b/apps/backend/internal/task/service/service_tasks.go @@ -250,6 +250,9 @@ func (s *Service) prepareTaskForCreation(ctx context.Context, req *CreateTaskReq if err := s.validateTaskWorkflow(ctx, req); err != nil { return nil, err } + if err := s.prepareContributionDestination(ctx, req); err != nil { + return nil, err + } workflowStepID := s.resolveWorkflowStep(ctx, req) task := s.buildTask(req, workflowStepID) @@ -264,6 +267,48 @@ func (s *Service) prepareTaskForCreation(ctx context.Context, req *CreateTaskReq return task, nil } +func (s *Service) prepareContributionDestination(ctx context.Context, req *CreateTaskRequest) error { + if s.contributionDestinationPreparer == nil || req.WorkflowID == "" { + return nil + } + workflow, err := s.workflows.GetWorkflow(ctx, req.WorkflowID) + if err != nil { + return fmt.Errorf("load workflow for contribution destination: %w", err) + } + if workflow == nil || workflow.WorkflowTemplateID == nil || *workflow.WorkflowTemplateID == "" { + return nil + } + repositories, err := s.loadContributionDestinationRepositories(ctx, req) + if err != nil { + return err + } + return s.contributionDestinationPreparer.PrepareContributionDestination(ctx, req, workflow, repositories) +} + +func (s *Service) loadContributionDestinationRepositories( + ctx context.Context, + req *CreateTaskRequest, +) ([]*models.Repository, error) { + if len(req.Repositories) == 0 || s.repoEntities == nil { + return nil, nil + } + repositories := make([]*models.Repository, len(req.Repositories)) + for index, input := range req.Repositories { + if input.RepositoryID == "" { + continue + } + repository, err := s.repoEntities.GetRepository(ctx, input.RepositoryID) + if err != nil { + return nil, fmt.Errorf("load repository %s for contribution destination: %w", input.RepositoryID, err) + } + if repository == nil || repository.WorkspaceID != req.WorkspaceID { + return nil, repoerrors.ErrRepositoryNotFound + } + repositories[index] = repository + } + return repositories, nil +} + // finalizeCreatedTask runs create-sequence step 6, the required synchronous // post-create work, after task has been inserted: blocker relationships, // task repositories, the created-event publish, and the feeder-pull refresh. @@ -706,6 +751,11 @@ func (s *Service) createTaskRepositories(ctx context.Context, taskID, workspaceI return fmt.Errorf("remote contribution branches do not match the resolved binding") } } + if repoInput.ContributionDestination != nil { + if err := repoInput.ContributionDestination.Validate(); err != nil { + return fmt.Errorf("invalid contribution destination: %w", err) + } + } repositoryID, baseBranch, _, err := s.resolveRepoInput(ctx, workspaceID, repoInput, repoByPath) if err != nil { return err @@ -743,6 +793,11 @@ func (s *Service) createTaskRepositories(ctx context.Context, taskID, workspaceI return fmt.Errorf("persist remote contribution: %w", err) } } + if repoInput.ContributionDestination != nil { + if err := models.PutContributionDestination(metadata, repoInput.ContributionDestination); err != nil { + return fmt.Errorf("persist contribution destination: %w", err) + } + } taskRepo := &models.TaskRepository{ TaskID: taskID, RepositoryID: repositoryID, diff --git a/apps/backend/internal/task/statussummary/projector.go b/apps/backend/internal/task/statussummary/projector.go index 9e59768657..d723e322c1 100644 --- a/apps/backend/internal/task/statussummary/projector.go +++ b/apps/backend/internal/task/statussummary/projector.go @@ -417,4 +417,3 @@ func (p *Projector) persistAndPublishLocked(ctx context.Context, taskID string, } return true, nil } - diff --git a/apps/backend/internal/worktree/manager_contribution.go b/apps/backend/internal/worktree/manager_contribution.go index 132bdb1a01..4a381badcc 100644 --- a/apps/backend/internal/worktree/manager_contribution.go +++ b/apps/backend/internal/worktree/manager_contribution.go @@ -80,6 +80,55 @@ func (m *Manager) materializeRemoteContribution(ctx context.Context, repoPath st return remoteName, remoteRef, nil } +func (m *Manager) configureContributionDestination( + ctx context.Context, + repoPath, worktreePath, branch string, + destination *models.ContributionDestination, +) error { + if destination == nil { + return nil + } + if err := destination.Validate(); err != nil { + return fmt.Errorf("validate contribution destination: %w", err) + } + remoteName := destination.ContributionRemoteName() + if err := m.ensureContributionRemote(ctx, repoPath, remoteName, destination.TargetRepository.RemoteURL); err != nil { + return fmt.Errorf("configure contribution destination remote: %w", err) + } + pushURL := m.newNonInteractiveGitCmd(ctx, repoPath, "config", "--get-all", "remote."+remoteName+".pushurl") + configured, readErr := runGitCmdOutput(ctx, pushURL) + if readErr == nil && !contributionDestinationPushURLsMatch(string(configured), destination.TargetRepository.RemoteURL) { + return fmt.Errorf("contribution destination push URL does not match the validated target") + } + if readErr != nil { + setPushURL := m.newNonInteractiveGitCmd(ctx, repoPath, "config", "--add", "remote."+remoteName+".pushurl", destination.TargetRepository.RemoteURL) + if err := runGitCmd(ctx, setPushURL); err != nil { + return fmt.Errorf("set contribution destination push URL: %w", err) + } + } + if branch == "" { + return nil + } + setPushRemote := m.newNonInteractiveGitCmd(ctx, worktreePath, "config", "branch."+branch+".pushRemote", remoteName) + if err := runGitCmd(ctx, setPushRemote); err != nil { + return fmt.Errorf("set contribution destination push remote: %w", err) + } + return nil +} + +func contributionDestinationPushURLsMatch(configured, target string) bool { + urls := strings.Split(strings.TrimSpace(configured), "\n") + if len(urls) == 0 || (len(urls) == 1 && urls[0] == "") { + return false + } + for _, configuredURL := range urls { + if strings.TrimSpace(configuredURL) != target { + return false + } + } + return true +} + func (m *Manager) validateContributionAncestor(ctx context.Context, repoPath, expectedSHA, descendantRef string) error { cmd := m.newNonInteractiveGitCmd(ctx, repoPath, "merge-base", "--is-ancestor", expectedSHA, descendantRef) if err := runGitCmd(ctx, cmd); err != nil { diff --git a/apps/backend/internal/worktree/manager_lifecycle.go b/apps/backend/internal/worktree/manager_lifecycle.go index 6873f9ad54..df0f30257b 100644 --- a/apps/backend/internal/worktree/manager_lifecycle.go +++ b/apps/backend/internal/worktree/manager_lifecycle.go @@ -37,6 +37,9 @@ func (m *Manager) Create(ctx context.Context, req CreateRequest) (*Worktree, err } if wt, handled, err := m.tryReuseExisting(ctx, req); handled { + if err == nil && wt != nil { + err = m.configureContributionDestination(ctx, req.RepositoryPath, wt.Path, wt.Branch, req.ContributionDestination) + } return wt, err } @@ -72,6 +75,9 @@ func (m *Manager) Create(ctx context.Context, req CreateRequest) (*Worktree, err if err != nil { return nil, err } + if err := m.configureContributionDestination(ctx, req.RepositoryPath, wt.Path, wt.Branch, req.ContributionDestination); err != nil { + return nil, err + } return wt, nil } diff --git a/apps/backend/internal/worktree/manager_remote_contribution_test.go b/apps/backend/internal/worktree/manager_remote_contribution_test.go index 2cf6320e83..b397afb7fd 100644 --- a/apps/backend/internal/worktree/manager_remote_contribution_test.go +++ b/apps/backend/internal/worktree/manager_remote_contribution_test.go @@ -10,6 +10,16 @@ import ( "github.com/kandev/kandev/internal/task/models" ) +func TestContributionDestinationPushURLsMatchAcceptsRepeatedTarget(t *testing.T) { + targetURL := "https://github.com/agent/kandev.git" + if !contributionDestinationPushURLsMatch(targetURL+"\n"+targetURL, targetURL) { + t.Fatal("repeated identical contribution push URLs were rejected") + } + if contributionDestinationPushURLsMatch(targetURL+"\nhttps://github.com/other/kandev.git", targetURL) { + t.Fatal("mixed contribution push URLs were accepted") + } +} + func TestCreateWorktree_RemoteContributionUsesSourceRemoteAndExactHead(t *testing.T) { contributionURL := "https://github.com/contributor/widget.git" sourceBare, sourceSHA := initContributionSource(t) diff --git a/apps/backend/internal/worktree/worktree.go b/apps/backend/internal/worktree/worktree.go index 2db3406a6a..04df97393e 100644 --- a/apps/backend/internal/worktree/worktree.go +++ b/apps/backend/internal/worktree/worktree.go @@ -175,7 +175,8 @@ type CreateRequest struct { // RemoteContribution identifies an existing provider contribution whose // source branch must be fetched from its own remote and verified by SHA. - RemoteContribution *models.RemoteContribution + RemoteContribution *models.RemoteContribution + ContributionDestination *models.ContributionDestination // WorktreeBranchPrefix is the prefix to use for the worktree branch name. // If empty, the default prefix is used. @@ -259,6 +260,11 @@ func (r *CreateRequest) Validate() error { if err := r.validateRemoteContribution(); err != nil { return err } + if r.ContributionDestination != nil { + if err := r.ContributionDestination.Validate(); err != nil { + return fmt.Errorf("invalid contribution destination: %w", err) + } + } if r.BaseBranch == "" { // Defence-in-depth: prefer the explicit FallbackBaseBranch (typically // the repository's default_branch carried by the caller) over an diff --git a/apps/web/app/page.tsx b/apps/web/app/page.tsx index a1d9119398..82302abce1 100644 --- a/apps/web/app/page.tsx +++ b/apps/web/app/page.tsx @@ -130,9 +130,9 @@ async function loadWorkspaceState({ const [workflowList, repositoriesResponse, quickChatResponse, quickTerminalResponse] = await Promise.all([ listWorkflows(activeWorkspaceId, { cache: "no-store", includeHidden: true }), - listRepositories(activeWorkspaceId, undefined, { cache: "no-store" }).catch(() => ({ - repositories: [], - })), + listRepositories(activeWorkspaceId, undefined, { cache: "no-store" }) + .then((response) => ({ ...response, loaded: true })) + .catch(() => ({ repositories: [], loaded: false })), listQuickChatSessions(activeWorkspaceId, { cache: "no-store" }).catch(() => ({ sessions: [], task_sessions: [], @@ -168,7 +168,10 @@ async function loadWorkspaceState({ repositories: { itemsByWorkspaceId: { [activeWorkspaceId]: repositoriesResponse.repositories }, loadingByWorkspaceId: { [activeWorkspaceId]: false }, - loadedByWorkspaceId: { [activeWorkspaceId]: true }, + // A failed SSR fetch must remain retryable on the client. Marking an + // empty fallback as loaded leaves task creation permanently disabled + // because the dialog never gets a repository selection. + loadedByWorkspaceId: { [activeWorkspaceId]: repositoriesResponse.loaded }, }, quickChat: { isOpen: false, diff --git a/apps/web/components/improve-kandev-dialog-create.tsx b/apps/web/components/improve-kandev-dialog-create.tsx index 0f62704d69..b821d43711 100644 --- a/apps/web/components/improve-kandev-dialog-create.tsx +++ b/apps/web/components/improve-kandev-dialog-create.tsx @@ -23,6 +23,7 @@ import { contributorAccessMessage, getImproveKandevForkBlockedReason, getImproveKandevStepDescription, + isImproveKandevForkBlockedStatus, resolveImproveKandevWorkflow, type ImproveKandevKind, } from "./improve-kandev-dialog-model"; @@ -55,6 +56,20 @@ function bootstrapBlockedReason(t: TFunction, bootstrap: BootstrapState): string return null; } +function getForkBlockedReason( + t: TFunction, + kind: ImproveKind, + ready: Extract | null, +): string | null { + if (!ready) return null; + const reason = getImproveKandevForkBlockedReason( + kind, + ready.data.fork_status, + ready.data.fork_reason_code, + ); + return reason ? t(reason) : null; +} + // Keys, not resolved copy: a `t()` at module scope would freeze at the boot // locale and the pseudo-locale could not see it. const PLACEHOLDER_KEYS: Record = { @@ -81,9 +96,7 @@ export function CreateModeView(props: CreateModeViewProps) { const [kind, setKind] = useState("bug"); const ready = bootstrap.kind === "ready" ? bootstrap : null; const workflow = resolveImproveKandevWorkflow(ready, kind); - const forkBlockedReason = ready - ? getImproveKandevForkBlockedReason(kind, ready.data.fork_status, ready.data.fork_message) - : null; + const forkBlockedReason = getForkBlockedReason(t, kind, ready); // Tasks land in the dedicated workspace the bootstrap returned, not the // user's active one. While bootstrap is in flight the active workspace is // only a fallback — submit stays blocked until `ready`. @@ -349,15 +362,22 @@ function ContributorBanner({ }) { const { t } = useTranslation(); const { github_login: login, has_write_access: hasWrite } = data; - if (data.fork_status === "blocked_emu" && kind !== "issue") { + if (isImproveKandevForkBlockedStatus(data.fork_status) && kind !== "issue") { return (
- {data.fork_message ?? t("common:thisGithubAccountCannotForkKdlbs")} + + {t( + getImproveKandevForkBlockedReason(kind, data.fork_status, data.fork_reason_code) ?? + (data.fork_status === "blocked_managed" + ? "common:managedGithubForkUnavailable" + : "common:thisGithubAccountCannotForkKdlbs"), + )} +
); } - const accessMessage = contributorAccessMessage(kind, hasWrite); + const accessMessage = t(contributorAccessMessage(kind, hasWrite, data.fork_status)); return (
{hasWrite ? ( diff --git a/apps/web/components/improve-kandev-dialog-model.test.ts b/apps/web/components/improve-kandev-dialog-model.test.ts index fbc571f84e..90c020de21 100644 --- a/apps/web/components/improve-kandev-dialog-model.test.ts +++ b/apps/web/components/improve-kandev-dialog-model.test.ts @@ -115,18 +115,17 @@ describe("improve kandev dialog model", () => { }); }); - it("blocks only implementation kinds for EMU fork restrictions", () => { - const message = "Forks are unavailable"; - expect(getImproveKandevForkBlockedReason("bug", "blocked_emu", message)).toBe(message); - expect(getImproveKandevForkBlockedReason("feature", "blocked_emu", message)).toBe(message); - expect(getImproveKandevForkBlockedReason("issue", "blocked_emu", message)).toBeNull(); - expect(getImproveKandevForkBlockedReason("bug", "unknown", null)).toBeNull(); - }); - - it("describes contributor access for each workflow kind", () => { - expect(contributorAccessMessage("issue", false)).toContain("does not require"); - expect(contributorAccessMessage("bug", true)).toContain("write access"); - expect(contributorAccessMessage("feature", false)).toContain("fork"); + it("blocks only implementation kinds for managed fork restrictions", () => { + expect(getImproveKandevForkBlockedReason("bug", "blocked_emu", "account_cannot_fork")).toBe( + "common:thisGithubAccountCannotForkKdlbs", + ); + expect(getImproveKandevForkBlockedReason("feature", "blocked_managed", "fork_conflict")).toBe( + "common:managedGithubForkUnavailable", + ); + expect( + getImproveKandevForkBlockedReason("issue", "blocked_emu", "account_cannot_fork"), + ).toBeNull(); + expect(getImproveKandevForkBlockedReason("bug", "unknown", undefined)).toBeNull(); }); it("prefers stable workflow step ids for descriptions", () => { @@ -135,3 +134,16 @@ describe("improve kandev dialog model", () => { ).toContain("implements the change"); }); }); + +describe("improve kandev contributor access", () => { + it("describes each workflow kind with a translation key", () => { + expect(contributorAccessMessage("issue", false)).toBe("common:improveKandevIssueNoForkAccess"); + expect(contributorAccessMessage("bug", true)).toBe("common:improveKandevDirectPushAccess"); + expect(contributorAccessMessage("feature", false, "creatable")).toBe( + "common:improveKandevManagedForkPrepared", + ); + expect(contributorAccessMessage("feature", false, "ready")).toBe( + "common:improveKandevExistingForkPrepared", + ); + }); +}); diff --git a/apps/web/components/improve-kandev-dialog-model.ts b/apps/web/components/improve-kandev-dialog-model.ts index 28f8ad5255..847190e99c 100644 --- a/apps/web/components/improve-kandev-dialog-model.ts +++ b/apps/web/components/improve-kandev-dialog-model.ts @@ -13,6 +13,13 @@ export const IMPROVE_KANDEV_WORKSPACE_NAME = "Improve Kandev"; export type ImproveKandevKind = "bug" | "feature" | "issue"; export type ImproveKandevMode = "intro" | "create"; +export type ContributorAccessMessageKey = + | "common:improveKandevIssueNoForkAccess" + | "common:improveKandevDirectPushAccess" + | "common:improveKandevManagedForkPrepared" + | "common:improveKandevExistingForkPrepared" + | "common:improveKandevExecutorForkAtPr"; + type StorageLike = Pick; const STEP_DESCRIPTIONS: Record = { @@ -29,7 +36,7 @@ const STEP_DESCRIPTIONS: Record = { export type ImproveKandevReadyState = { data: Pick< ImproveKandevBootstrapResponse, - "workflow_id" | "issue_workflow_id" | "fork_status" | "fork_message" + "workflow_id" | "issue_workflow_id" | "fork_status" | "fork_reason_code" >; steps: WorkflowStep[]; issueSteps: WorkflowStep[]; @@ -51,12 +58,16 @@ export function getImproveKandevBrowserStorage(): StorageLike | undefined { } } -export function contributorAccessMessage(kind: ImproveKandevKind, hasWrite: boolean): string { - if (kind === "issue") return "Opening an issue does not require a fork or push access."; - if (hasWrite) { - return "You have write access to kdlbs/kandev, so the agent will push directly to a branch on the upstream repo."; - } - return "The agent will fork kdlbs/kandev to your account during the PR step and open a pull request from your fork."; +export function contributorAccessMessage( + kind: ImproveKandevKind, + hasWrite: boolean, + forkStatus?: ImproveKandevBootstrapResponse["fork_status"], +): ContributorAccessMessageKey { + if (kind === "issue") return "common:improveKandevIssueNoForkAccess"; + if (hasWrite) return "common:improveKandevDirectPushAccess"; + if (forkStatus === "creatable") return "common:improveKandevManagedForkPrepared"; + if (forkStatus === "ready") return "common:improveKandevExistingForkPrepared"; + return "common:improveKandevExecutorForkAtPr"; } export function getImproveKandevStepDescription(step: Pick): string { @@ -109,8 +120,20 @@ export function resolveImproveKandevWorkflow( export function getImproveKandevForkBlockedReason( kind: ImproveKandevKind, forkStatus: ImproveKandevBootstrapResponse["fork_status"] | undefined, - forkMessage: string | null | undefined, + forkReasonCode: ImproveKandevBootstrapResponse["fork_reason_code"] | undefined, ): string | null { - if (kind === "issue" || forkStatus !== "blocked_emu") return null; - return forkMessage ?? "This GitHub account cannot fork kdlbs/kandev."; + if (kind === "issue" || !isImproveKandevForkBlockedStatus(forkStatus)) return null; + if (forkReasonCode === "account_cannot_fork") { + return "common:thisGithubAccountCannotForkKdlbs"; + } + if (forkStatus === "blocked_emu" && forkReasonCode === undefined) { + return "common:thisGithubAccountCannotForkKdlbs"; + } + return "common:managedGithubForkUnavailable"; +} + +export function isImproveKandevForkBlockedStatus( + forkStatus: ImproveKandevBootstrapResponse["fork_status"] | undefined, +): boolean { + return forkStatus === "blocked_emu" || forkStatus === "blocked_managed"; } diff --git a/apps/web/components/review/review-repository-identity.ts b/apps/web/components/review/review-repository-identity.ts index 43be582717..8be380454d 100644 --- a/apps/web/components/review/review-repository-identity.ts +++ b/apps/web/components/review/review-repository-identity.ts @@ -37,6 +37,10 @@ function worktreeDirectoryName(path: string | undefined): string | undefined { return directory ? sanitizeReviewRepositoryName(directory) || undefined : undefined; } +function isRepositoryWorktreeName(worktreeName: string, canonicalName: string): boolean { + return worktreeName === canonicalName || worktreeName.startsWith(`${canonicalName}-`); +} + function findPRTaskRepository( pr: ReviewPRIdentity, taskRepositories: ReviewTaskRepositoryIdentity[], @@ -78,6 +82,27 @@ function findPRWorktree( return slugMatches.length === 1 ? slugMatches[0] : undefined; } +function resolveSiblingRepositoryName( + canonicalName: string, + pr: ReviewPRIdentity, + taskRepository: ReviewTaskRepositoryIdentity | undefined, + siblingRepositories: ReviewTaskRepositoryIdentity[], + worktrees: ReviewWorktreeIdentity[], +): string { + if (taskRepository && siblingRepositories.length >= 2) { + if (taskRepository === siblingRepositories[0]) return canonicalName; + } + + const worktreeName = worktreeDirectoryName(findPRWorktree(pr, taskRepository, worktrees)?.path); + if (worktreeName && isRepositoryWorktreeName(worktreeName, canonicalName)) return worktreeName; + + if (!taskRepository || siblingRepositories.length < 2) return canonicalName; + + const branch = taskRepository.checkout_branch || taskRepository.base_branch || pr.head_branch; + const branchSlug = sanitizeReviewRepositoryName(branch ?? ""); + return branchSlug ? `${canonicalName}-${branchSlug}` : canonicalName; +} + /** * Resolves the repository_name stamped by agentctl for a selected PR. Same-repo * multi-branch tasks use sibling worktree directories (`repo-branch-slug`), so @@ -100,16 +125,11 @@ export function resolvePRReviewRepositoryIdentity({ // root. Its worktree basename is therefore the task slug, not a repository // identity, and may arrive after initial render. Keep the canonical name so // hydration cannot rename the selected PR's file group. - if (taskRepository && siblingRepositories.length >= 2) { - if (taskRepository === siblingRepositories[0]) return canonicalName; - } - - const worktreeName = worktreeDirectoryName(findPRWorktree(pr, taskRepository, worktrees)?.path); - if (worktreeName) return worktreeName; - - if (!taskRepository || siblingRepositories.length < 2) return canonicalName; - - const branch = taskRepository.checkout_branch || taskRepository.base_branch || pr.head_branch; - const branchSlug = sanitizeReviewRepositoryName(branch ?? ""); - return branchSlug ? `${canonicalName}-${branchSlug}` : canonicalName; + return resolveSiblingRepositoryName( + canonicalName, + pr, + taskRepository, + siblingRepositories, + worktrees, + ); } diff --git a/apps/web/components/sentry/sentry-settings.tsx b/apps/web/components/sentry/sentry-settings.tsx index c0c756bc67..e74fc96426 100644 --- a/apps/web/components/sentry/sentry-settings.tsx +++ b/apps/web/components/sentry/sentry-settings.tsx @@ -197,7 +197,10 @@ export function SentryConnectionSection({ workspaceId }: { workspaceId: string } {t("sentry:instances")} {loading && instances.length === 0 ? ( -

+

{t("sentry:loadingEllipsis")}

) : ( diff --git a/apps/web/e2e/fixtures/docker-probe.ts b/apps/web/e2e/fixtures/docker-probe.ts index 82a0f5357c..564f375a35 100644 --- a/apps/web/e2e/fixtures/docker-probe.ts +++ b/apps/web/e2e/fixtures/docker-probe.ts @@ -15,6 +15,9 @@ export const E2E_ALPINE_IMAGE_TAG = "kandev-agent:e2e-alpine"; /** Label value shared by resources created in this Playwright process. */ export const E2E_DOCKER_SCOPE = `e2e-${process.pid}-${randomUUID().slice(0, 8)}`; const FAKE_LSP_SERVER = path.resolve(__dirname, "fake-lsp-server.mjs"); +const SCOPED_CONTAINER_CLEANUP_TIMEOUT_MS = 30_000; +const SCOPED_CONTAINER_CLEANUP_POLL_MS = 250; +const SCOPED_CONTAINER_CLEANUP_EMPTY_POLLS = 8; const E2E_DOCKERFILE = `FROM node:22-slim RUN apt-get update \\ @@ -69,6 +72,70 @@ function buildImage(tag: string, dockerfile: string): void { } } +function listScopedKandevContainers(scope: string, scanAll = false): string[] | null { + const list = spawnSync( + "docker", + ["ps", "-aq", "--filter", `label=kandev.e2e.run=${scope}`, "--no-trunc"], + { encoding: "utf8", timeout: 5_000 }, + ); + if (list.status !== 0) return null; + const ids = list.stdout + .split("\n") + .map((s) => s.trim()) + .filter(Boolean); + if (ids.length > 0 || !scanAll) return ids; + + // Docker can briefly return an empty label-filtered list while a just + // stopped container is becoming visible to the daemon's index. On the + // second and later empty polls, inspect the daemon's IDs and filter labels ourselves. + // This remains safe for concurrent shards because the fallback below only + // returns exact managed/run label matches to the scoped remover. + const all = spawnSync("docker", ["ps", "-aq", "--no-trunc"], { + encoding: "utf8", + timeout: 5_000, + }); + if (all.status !== 0) return null; + const allIDs = all.stdout + .split("\n") + .map((s) => s.trim()) + .filter(Boolean); + if (allIDs.length === 0) return []; + + const matchingIDs: string[] = []; + for (const id of allIDs) { + const inspected = spawnSync( + "docker", + [ + "inspect", + "--format", + '{{.Id}}\t{{index .Config.Labels "kandev.managed"}}\t{{index .Config.Labels "kandev.e2e.run"}}', + id, + ], + { encoding: "utf8", timeout: 1_000 }, + ); + if (inspected.status !== 0) continue; + const [fullID, managed, run] = inspected.stdout.trim().split("\t"); + if (managed === "true" && run === scope) matchingIDs.push(fullID || id); + } + return matchingIDs; +} + +function removeContainerIDs(ids: string[]): void { + for (const id of ids) { + spawnSync("docker", ["rm", "-f", id], { stdio: "ignore", timeout: 5_000 }); + } +} + +function discardMissingContainerIDs(pendingIDs: Set): void { + for (const id of pendingIDs) { + const inspected = spawnSync("docker", ["inspect", id], { + stdio: "ignore", + timeout: 5_000, + }); + if (inspected.status !== 0) pendingIDs.delete(id); + } +} + /** * Remove only containers owned by this E2E process. Never use a daemon-wide * kandev.managed=true sweep: another shard can share the Docker daemon. @@ -93,10 +160,49 @@ function scopedKandevContainerIDs(scope: string): string[] { .filter(Boolean); } -export function removeScopedKandevContainers(scope = E2E_DOCKER_SCOPE): void { - const ids = scopedKandevContainerIDs(scope); - if (ids.length === 0) return; - spawnSync("docker", ["rm", "-f", ...ids], { stdio: "ignore" }); +export async function removeScopedKandevContainers(scope = E2E_DOCKER_SCOPE): Promise { + const deadline = Date.now() + SCOPED_CONTAINER_CLEANUP_TIMEOUT_MS; + let emptyPolls = 0; + let lastIDs: string[] = []; + const pendingIDs = new Set(); + + while (Date.now() < deadline) { + let currentIDs = listScopedKandevContainers(scope); + if (currentIDs?.length === 0 && emptyPolls >= 1) { + currentIDs = listScopedKandevContainers(scope, true); + } + if (currentIDs) { + lastIDs = currentIDs; + for (const id of lastIDs) pendingIDs.add(id); + removeContainerIDs(lastIDs); + + // `docker ps` can stop reporting a container while the daemon is still + // completing `rm -f`. Verify every ID we observed with inspect before + // declaring the scope empty, otherwise the next test can see the old + // container during that removal window. + discardMissingContainerIDs(pendingIDs); + + if (lastIDs.length === 0 && pendingIDs.size === 0) { + emptyPolls += 1; + // Docker's label index can lag behind a stopped container by more than + // one poll under CI load. Require a stable empty window before the + // next test starts so a newly visible scoped container is not leaked. + if (emptyPolls >= SCOPED_CONTAINER_CLEANUP_EMPTY_POLLS) return; + } else { + emptyPolls = 0; + } + } + + await dwell( + SCOPED_CONTAINER_CLEANUP_POLL_MS, + "poll-interval", + "Docker label visibility has no event notification", + ); + } + + throw new Error( + `timed out removing process-scoped Kandev containers: ${[...pendingIDs, ...lastIDs].join(", ") || "docker unavailable"}`, + ); } /** @@ -110,7 +216,7 @@ export async function waitForScopedKandevContainersRemoved( ): Promise { const deadline = Date.now() + timeoutMs; while (Date.now() < deadline) { - removeScopedKandevContainers(scope); + await removeScopedKandevContainers(scope); if (scopedKandevContainerIDs(scope).length === 0) return; await dwell( 100, @@ -118,7 +224,7 @@ export async function waitForScopedKandevContainersRemoved( "sampling interval for the container-teardown loop above; docker rm is asynchronous and the CLI reports completion only by the container list emptying", ); } - removeScopedKandevContainers(scope); + await removeScopedKandevContainers(scope); const remaining = scopedKandevContainerIDs(scope); if (remaining.length > 0) { throw new Error( diff --git a/apps/web/e2e/fixtures/docker-test-base.ts b/apps/web/e2e/fixtures/docker-test-base.ts index d1bfa189a0..7ed98f2177 100644 --- a/apps/web/e2e/fixtures/docker-test-base.ts +++ b/apps/web/e2e/fixtures/docker-test-base.ts @@ -7,6 +7,7 @@ import { buildE2EImage, E2E_IMAGE_TAG, hasDocker, + removeScopedKandevContainers, waitForScopedKandevContainersRemoved, } from "./docker-probe"; import { ApiClient } from "../helpers/api-client"; @@ -31,7 +32,7 @@ export type DockerSeedData = { * and pre-seeds a local_docker executor profile pointing at it. */ export const dockerTest = backendFixture.extend< - { testPage: Page }, + { testPage: Page; dockerCleanup: void }, { apiClient: ApiClient; seedData: DockerSeedData } >({ apiClient: [ @@ -42,6 +43,29 @@ export const dockerTest = backendFixture.extend< { scope: "worker" }, ], + // Keep reset and Docker cleanup in an automatic test-scoped fixture. A + // fixture teardown is ordered around every test, including API-only tests + // and serial suites, so a stopped container cannot leak into the next test. + dockerCleanup: [ + async ({ apiClient, seedData }, use) => { + const reset = async () => { + try { + await apiClient.e2eReset(seedData.workspaceId, [seedData.workflowId]); + } finally { + await removeScopedKandevContainers(); + } + }; + + await reset(); + try { + await use(); + } finally { + await reset(); + } + }, + { auto: true }, + ], + seedData: [ async ({ apiClient, backend }, use, _workerInfo) => { if (!hasDocker()) { @@ -147,25 +171,6 @@ export const dockerTest = backendFixture.extend< }, }); -// Container lifecycle cleanup must run for API-only tests too. `testPage` is a -// lazy fixture, so keeping reset there allowed tests that only request -// apiClient/seedData to leave tasks and containers behind for the next test. -dockerTest.beforeEach(async ({ apiClient, seedData }) => { - try { - await apiClient.e2eReset(seedData.workspaceId, [seedData.workflowId]); - } finally { - await waitForScopedKandevContainersRemoved(); - } -}); - -dockerTest.afterEach(async ({ apiClient, seedData }) => { - try { - await apiClient.e2eReset(seedData.workspaceId, [seedData.workflowId]); - } finally { - await waitForScopedKandevContainersRemoved(); - } -}); - export { expect } from "@playwright/test"; export const test = dockerTest; // Re-export base for convenience to keep import paths consistent across specs. diff --git a/apps/web/e2e/fixtures/office-fixture.ts b/apps/web/e2e/fixtures/office-fixture.ts index ca1dd68b7e..051da639ec 100644 --- a/apps/web/e2e/fixtures/office-fixture.ts +++ b/apps/web/e2e/fixtures/office-fixture.ts @@ -77,4 +77,11 @@ export const test = base.extend<{ testPage: Page }, OfficeFixtures>({ }, }); +// Tests in this suite deliberately exercise status transitions. Reset the +// worker-shared CEO before every test so a previous paused/stopped/working +// state cannot make the scheduler silently reject the next assignment. +test.beforeEach(async ({ officeApi, officeSeed }) => { + await officeApi.updateAgentStatus(officeSeed.agentId, "idle"); +}); + export { expect } from "@playwright/test"; diff --git a/apps/web/e2e/fixtures/test-base.ts b/apps/web/e2e/fixtures/test-base.ts index aedffa5f70..5bc5db957d 100644 --- a/apps/web/e2e/fixtures/test-base.ts +++ b/apps/web/e2e/fixtures/test-base.ts @@ -4,10 +4,10 @@ import fs from "node:fs"; import path from "node:path"; import { backendFixture, type BackendContext } from "./backend"; import { ApiClient } from "../helpers/api-client"; +import { dwell } from "../helpers/causal-waits"; import { PrAssetCapture } from "../helpers/pr-asset-capture"; import { makeGitEnv } from "../helpers/git-helper"; import type { WorkflowStep } from "../../lib/types/http"; -import { dwell } from "../helpers/causal-waits"; const DEFAULT_SIDEBAR_VIEW = { id: "view-all-tasks", @@ -18,6 +18,9 @@ const DEFAULT_SIDEBAR_VIEW = { collapsed_groups: [], }; +const AGENT_PROFILE_READY_TIMEOUT_MS = 30_000; +const AGENT_PROFILE_READY_POLL_MS = 250; + export type SeedData = { workspaceId: string; workflowId: string; @@ -33,6 +36,61 @@ export type SeedData = { worktreeExecutorProfileId: string; }; +async function waitForSeedAgentProfile( + apiClient: ApiClient, + backend: BackendContext, + profileId: string, +): Promise { + const deadline = Date.now() + AGENT_PROFILE_READY_TIMEOUT_MS; + let lastObservation = "no response"; + + while (Date.now() < deadline) { + try { + await backend.ensureReady(); + const { agents } = await apiClient.listAgents(); + const profiles = agents.flatMap((agent) => agent.profiles ?? []); + const profile = profiles.find((candidate) => candidate.id === profileId); + + if (profile && profile.enabled !== false) return profile.id; + + if (profile) { + await apiClient.updateAgentProfile(profileId, { enabled: true }); + return profile.id; + } + + // Profile-mode tests intentionally restart the same database with the + // production registry. Its orphan reconciler soft-deletes the E2E mock + // profile because mock-agent is disabled there. When the E2E profile is + // restored, create a fresh disposable profile instead of waiting for a + // user-deleted row that the backend correctly will not resurrect. + const agent = agents.find((candidate) => candidate.name === "mock-agent") ?? agents[0]; + if (agent) { + const replacement = await apiClient.createAgentProfile(agent.id, "mock-fast", { + model: "mock-fast", + }); + return replacement.id; + } + + lastObservation = + `seed profile ${profileId} was not returned by /api/v1/agents ` + + `(available profiles: ${profiles.map((candidate) => candidate.id).join(", ") || "none"})`; + } catch (error) { + lastObservation = error instanceof Error ? error.message : String(error); + } + + await dwell( + AGENT_PROFILE_READY_POLL_MS, + "poll-interval", + "agent profile readiness has no event notification", + ); + } + + throw new Error( + `E2E backend did not expose enabled seed agent profile ${profileId} within ` + + `${AGENT_PROFILE_READY_TIMEOUT_MS}ms (${lastObservation})`, + ); +} + export const test = backendFixture.extend< { testPage: Page; @@ -189,6 +247,15 @@ export const test = backendFixture.extend< // may have written during previous tests. testPage: async ({ browser, backend, apiClient, seedData }, use) => { await backend.ensureReady(); + // A suite-level test may restart the worker backend after the worker-scoped + // seed fixture ran. Health only proves that the listener is serving; it does + // not prove that the persisted seed profile is present and enabled for the + // task-create dialog. Re-establish that invariant before opening a page. + seedData.agentProfileId = await waitForSeedAgentProfile( + apiClient, + backend, + seedData.agentProfileId, + ); // Clean up tasks, test-created workflows, and extra agent profiles from // previous tests in this worker. Keep the seeded workflow and the seed // agent profile so the worker-scoped seedData fixture remains valid. diff --git a/apps/web/e2e/helpers/api-client.ts b/apps/web/e2e/helpers/api-client.ts index 465805c8b0..c26851bfbc 100644 --- a/apps/web/e2e/helpers/api-client.ts +++ b/apps/web/e2e/helpers/api-client.ts @@ -14,6 +14,7 @@ import type { PRCommitDetail, TaskCIAutomationOptions, TaskCIAutomationPatch, + TaskPR, } from "../../lib/types/github"; import type { TaskStatusSummary } from "../../lib/types/task-status-summary"; import type { SecretListItem, SecretScope } from "../../lib/types/http-secrets"; @@ -1608,6 +1609,14 @@ export class ApiClient { return res.json(); } + async listTaskPRs(taskId: string): Promise { + const response = await this.request<{ task_prs?: Record }>( + "GET", + `/api/v1/github/task-prs?task_ids=${encodeURIComponent(taskId)}`, + ); + return response.task_prs?.[taskId] ?? []; + } + async mockGitHubSeedPRFeedback(data: { owner: string; repo: string; @@ -2048,6 +2057,17 @@ export class ApiClient { return this.request("DELETE", `/api/v1/tasks/${taskId}/dependencies/${dependsOnTaskId}`); } + async ensureTaskSession(taskId: string): Promise<{ + success: boolean; + task_id: string; + session_id?: string; + state: string; + source: string; + newly_created: boolean; + }> { + return this.request("POST", `/api/v1/tasks/${taskId}/sessions/ensure`); + } + async setPrimarySession(sessionId: string): Promise { await this.wsRequest("session.set_primary", { session_id: sessionId }); } @@ -2341,6 +2361,10 @@ export class ApiClient { await this.wsRequest("message.queue.cancel", { session_id: sessionId }); } + async getQueueStatus(sessionId: string): Promise<{ count: number }> { + return this.wsRequest("message.queue.get", { session_id: sessionId }); + } + // --- Integration config seeding (real API, not mock) --- async setJiraConfig(payload: { @@ -2399,9 +2423,9 @@ export class ApiClient { */ async waitForIntegrationAuthHealthy( integration: "jira" | "linear" | "sentry", - options: number | { timeoutMs?: number; workspaceId?: string } = 30_000, + options: number | { timeoutMs?: number; workspaceId?: string } = 60_000, ): Promise { - const timeoutMs = typeof options === "number" ? options : (options.timeoutMs ?? 30_000); + const timeoutMs = typeof options === "number" ? options : (options.timeoutMs ?? 60_000); const workspaceId = typeof options === "number" ? undefined : options.workspaceId; const deadline = Date.now() + timeoutMs; while (Date.now() < deadline) { @@ -2422,20 +2446,27 @@ export class ApiClient { integration: "jira" | "linear" | "sentry", workspaceId?: string, ): Promise { - if (integration === "sentry") { - const path = await this.withActiveWorkspace("/api/v1/sentry/instances", workspaceId); + try { + if (integration === "sentry") { + const path = await this.withActiveWorkspace("/api/v1/sentry/instances", workspaceId); + const res = await this.rawRequest("GET", path); + if (!res.ok || res.status !== 200) return false; + const body = (await res.json()) as { + instances?: Array<{ hasSecret?: boolean; lastOk?: boolean }>; + }; + return (body.instances ?? []).some((i) => Boolean(i.hasSecret) && Boolean(i.lastOk)); + } + const path = await this.withActiveWorkspace(`/api/v1/${integration}/config`, workspaceId); const res = await this.rawRequest("GET", path); if (!res.ok || res.status !== 200) return false; - const body = (await res.json()) as { - instances?: Array<{ hasSecret?: boolean; lastOk?: boolean }>; - }; - return (body.instances ?? []).some((i) => Boolean(i.hasSecret) && Boolean(i.lastOk)); + const cfg = (await res.json()) as { hasSecret?: boolean; lastOk?: boolean }; + return Boolean(cfg.hasSecret) && Boolean(cfg.lastOk); + } catch { + // The auth-health probe runs while a worker backend can be restarting. + // Treat a refused connection like any other not-yet-healthy response so + // the bounded poll can observe the recovered backend. + return false; } - const path = await this.withActiveWorkspace(`/api/v1/${integration}/config`, workspaceId); - const res = await this.rawRequest("GET", path); - if (!res.ok || res.status !== 200) return false; - const cfg = (await res.json()) as { hasSecret?: boolean; lastOk?: boolean }; - return Boolean(cfg.hasSecret) && Boolean(cfg.lastOk); } // --- Azure DevOps Mock Control --- @@ -2798,6 +2829,7 @@ export class ApiClient { idempotencyKey?: string; errorMessage?: string; requestedAt?: string; + scheduledRetryAt?: string; claimedAt?: string; finishedAt?: string; }): Promise<{ run_id: string }> { @@ -2813,6 +2845,7 @@ export class ApiClient { if (opts.idempotencyKey !== undefined) payload.idempotency_key = opts.idempotencyKey; if (opts.errorMessage !== undefined) payload.error_message = opts.errorMessage; if (opts.requestedAt !== undefined) payload.requested_at = opts.requestedAt; + if (opts.scheduledRetryAt !== undefined) payload.scheduled_retry_at = opts.scheduledRetryAt; if (opts.claimedAt !== undefined) payload.claimed_at = opts.claimedAt; if (opts.finishedAt !== undefined) payload.finished_at = opts.finishedAt; return this.request("POST", "/api/v1/_test/runs", payload); diff --git a/apps/web/e2e/helpers/multi-pr-review.ts b/apps/web/e2e/helpers/multi-pr-review.ts index 7ccfa354e1..ed53168f07 100644 --- a/apps/web/e2e/helpers/multi-pr-review.ts +++ b/apps/web/e2e/helpers/multi-pr-review.ts @@ -1,4 +1,5 @@ import type { SeedData } from "../fixtures/test-base"; +import { expect } from "@playwright/test"; import type { ApiClient } from "./api-client"; export const REVIEW_OWNER = "testorg"; @@ -97,5 +98,21 @@ export async function seedMultiPRReviewTask( }); } + // Association requests are synchronous, but this read barrier makes the + // seed contract explicit before the browser's first task-PR sync. Without + // it, a cold worker could open the changes panel after only the first row + // was visible to the frontend. + const expectedNumbers = REVIEW_PRS.map((pr) => pr.number).sort((a, b) => a - b); + await expect + .poll( + async () => + (await apiClient.listTaskPRs(task.id)).map((pr) => pr.pr_number).sort((a, b) => a - b), + { + timeout: 15_000, + message: "waiting for both seeded review PR associations", + }, + ) + .toEqual(expectedNumbers); + return task; } diff --git a/apps/web/e2e/helpers/plugin-fixture.ts b/apps/web/e2e/helpers/plugin-fixture.ts index b985181dd9..a72a470c28 100644 --- a/apps/web/e2e/helpers/plugin-fixture.ts +++ b/apps/web/e2e/helpers/plugin-fixture.ts @@ -14,5 +14,10 @@ export async function installFixturePlugin(page: Page): Promise { await page.getByTestId("install-plugin-tab-upload").click(); await page.getByTestId("install-plugin-file-input").setInputFiles(PACKAGE_PATH); await page.getByTestId("install-plugin-upload-submit").click(); + // The plugin row is inserted before the UI bundle finishes loading. The + // controlled dialog closes only after loadPlugins resolves, so use that + // transition as the fixture's readiness boundary before navigating to a + // surface that consumes the plugin's registrations. + await expect(page.getByTestId("install-plugin-dialog")).toBeHidden({ timeout: 30_000 }); await expect(page.getByTestId(`plugin-row-${PLUGIN_ID}`)).toBeVisible({ timeout: 30_000 }); } diff --git a/apps/web/e2e/helpers/session-store.ts b/apps/web/e2e/helpers/session-store.ts index 0ec447c159..1ca1c8db50 100644 --- a/apps/web/e2e/helpers/session-store.ts +++ b/apps/web/e2e/helpers/session-store.ts @@ -5,6 +5,7 @@ type E2EStoreWindow = Window & { getState: () => { taskSessions: { items: Record> }; tasks: { activeSessionId: string | null }; + sessionAgentctl: { itemsBySessionId: Record }; setAvailableCommands: (sessionId: string, commands: AvailableCommand[]) => void; }; setState: ( @@ -21,6 +22,22 @@ type AvailableCommand = { input_hint?: string; }; +/** Wait until the session agentctl is ready for controls that require it. */ +export async function waitForSessionAgentctlReady( + page: Page, + sessionId: string, + timeout = 60_000, +): Promise { + await page.waitForFunction( + (sid) => { + const store = (window as E2EStoreWindow).__KANDEV_E2E_STORE__; + return store?.getState().sessionAgentctl.itemsBySessionId[sid]?.status === "ready"; + }, + sessionId, + { timeout, message: `agentctl did not become ready for session ${sessionId}` }, + ); +} + export async function waitForActiveSessionForegroundActivity( page: Page, activity: "generating" | "background" | null, diff --git a/apps/web/e2e/pages/jira-settings-page.ts b/apps/web/e2e/pages/jira-settings-page.ts index 8a9740f0ab..4a0749ea39 100644 --- a/apps/web/e2e/pages/jira-settings-page.ts +++ b/apps/web/e2e/pages/jira-settings-page.ts @@ -32,6 +32,11 @@ export class JiraSettingsPage { await this.siteInput.waitFor({ state: "visible" }); } + async gotoWorkspace(workspaceId: string) { + await this.page.goto(`/settings/workspaces/${workspaceId}/integrations/jira`); + await this.siteInput.waitFor({ state: "visible" }); + } + async fillForm(args: { siteUrl: string; email?: string; secret: string; projectKey?: string }) { await this.siteInput.fill(args.siteUrl); if (args.email !== undefined) await this.emailInput.fill(args.email); diff --git a/apps/web/e2e/pages/sentry-settings-page.ts b/apps/web/e2e/pages/sentry-settings-page.ts index 68e18817ce..12f8989bd1 100644 --- a/apps/web/e2e/pages/sentry-settings-page.ts +++ b/apps/web/e2e/pages/sentry-settings-page.ts @@ -6,6 +6,7 @@ export class SentrySettingsPage { readonly addInstanceButton: Locator; readonly cards: Locator; readonly noInstances: Locator; + readonly instanceListLoading: Locator; readonly addNameInput: Locator; readonly addUrlInput: Locator; readonly addSecretInput: Locator; @@ -16,6 +17,7 @@ export class SentrySettingsPage { this.addInstanceButton = page.getByTestId("sentry-add-instance-button"); this.cards = page.getByTestId("sentry-instance-card"); this.noInstances = page.getByTestId("sentry-no-instances"); + this.instanceListLoading = page.getByTestId("sentry-instance-list-loading"); this.addNameInput = page.getByTestId("sentry-add-name-input"); this.addUrlInput = page.getByTestId("sentry-add-url-input"); this.addSecretInput = page.getByTestId("sentry-add-secret-input"); @@ -23,11 +25,16 @@ export class SentrySettingsPage { this.addSaveButton = page.getByRole("button", { name: "Save changes" }); } - async goto(workspaceId: string) { - await this.page.goto(`/settings/workspaces/${workspaceId}/integrations/sentry`); + async goto(workspaceId?: string) { + const path = workspaceId + ? `/settings/workspaces/${encodeURIComponent(workspaceId)}/integrations/sentry` + : "/settings/integrations/sentry"; + await this.page.goto(path); // The "Add instance" button renders whenever no form is open — present in - // both the empty and populated states, so it is a reliable ready signal. + // both the empty and populated states. It is not sufficient on its own: + // it also renders while the async instance list is loading. await this.addInstanceButton.waitFor({ state: "visible" }); + await this.instanceListLoading.waitFor({ state: "hidden", timeout: 15_000 }); } async addInstance(name: string, secret: string, url?: string) { diff --git a/apps/web/e2e/pages/session-page.ts b/apps/web/e2e/pages/session-page.ts index 2436b708d0..20a718b524 100644 --- a/apps/web/e2e/pages/session-page.ts +++ b/apps/web/e2e/pages/session-page.ts @@ -13,6 +13,8 @@ function sectionLabelToStateTestId(label: string): string { return "task-state-backlog"; } +const TERMINAL_READY_TIMEOUT = 30_000; + export class SessionPage { readonly chat: Locator; readonly sidebar: Locator; @@ -705,7 +707,7 @@ export class SessionPage { } stepperStep(name: string): Locator { - return this.page.getByTestId(`workflow-step-${name}`); + return this.page.locator(`[data-testid="workflow-step-${name}"][aria-current="step"]`); } /** PR button in the topbar (visible only when a PR is associated). */ @@ -1257,8 +1259,12 @@ export class SessionPage { * disappears — i.e. the WebSocket actually opened for that env terminal. * Use this to detect the "terminal hangs forever on Connecting" bug. */ - async expectTerminalConnected(timeout = 15_000): Promise { - await this.terminal.getByTestId("passthrough-loading").waitFor({ state: "hidden", timeout }); + async expectTerminalConnected(timeout = TERMINAL_READY_TIMEOUT): Promise { + await this.page + .locator("[data-testid='terminal-panel']:visible") + .first() + .getByTestId("passthrough-loading") + .waitFor({ state: "hidden", timeout }); } /** @@ -1266,9 +1272,10 @@ export class SessionPage { * the prompt), then type a command and press Enter. */ async typeInTerminal(command: string): Promise { + await this.expectTerminalConnected(); await expect .poll(async () => (await this.readXtermBuffer("terminal-panel")).length > 0, { - timeout: 15_000, + timeout: TERMINAL_READY_TIMEOUT, message: "Waiting for terminal shell to connect", }) .toBe(true); diff --git a/apps/web/e2e/tests/chat/busy-signal.spec.ts b/apps/web/e2e/tests/chat/busy-signal.spec.ts index 091fed608d..025ed1ed20 100644 --- a/apps/web/e2e/tests/chat/busy-signal.spec.ts +++ b/apps/web/e2e/tests/chat/busy-signal.spec.ts @@ -69,7 +69,9 @@ test.describe("Coarse RUNNING busy signal", () => { // contract remains coarse for the entire RUNNING turn. await waitForActiveSessionForegroundActivity(testPage, "generating"); await expect(session.idleInput()).not.toBeVisible(); - await expect(testPage.locator('[data-placeholder^="Queue"]')).toBeVisible(); + await expect(testPage.locator('[data-placeholder^="Queue"]')).toBeVisible({ + timeout: 20_000, + }); const editor = session.activeChat().locator(".tiptap.ProseMirror:visible"); await typeWhileBusy(testPage, editor, "queue this follow-up"); @@ -81,7 +83,12 @@ test.describe("Coarse RUNNING busy signal", () => { await expect(session.agentStatus()).toBeVisible(); await waitForActiveSessionForegroundActivity(testPage, "generating"); await expect(session.idleInput()).not.toBeVisible(); - await expect(testPage.locator('[data-placeholder^="Queue"]')).toBeVisible(); + // The queued draft is restored into the editor after reload, so the + // contenteditable no longer renders its empty-state placeholder. The + // queue chip is the stable post-reload contract for the queued state. + await expect(testPage.getByTestId("queue-chip")).toBeVisible({ + timeout: 20_000, + }); }); test("foreground generation continues to queue input", async ({ diff --git a/apps/web/e2e/tests/chat/mobile-auto-scroll-toggle.spec.ts b/apps/web/e2e/tests/chat/mobile-auto-scroll-toggle.spec.ts index c87b4890e6..ca1aa6e034 100644 --- a/apps/web/e2e/tests/chat/mobile-auto-scroll-toggle.spec.ts +++ b/apps/web/e2e/tests/chat/mobile-auto-scroll-toggle.spec.ts @@ -52,6 +52,8 @@ async function seedOverflowingTask( } test.describe("Mobile transcript auto-scroll toggle", () => { + test.describe.configure({ timeout: 120_000 }); + test("can be hidden without changing the mobile transcript auto-scroll default", async ({ testPage, apiClient, diff --git a/apps/web/e2e/tests/chat/mobile-subagent.spec.ts b/apps/web/e2e/tests/chat/mobile-subagent.spec.ts index 18b0dc5303..2f254191a3 100644 --- a/apps/web/e2e/tests/chat/mobile-subagent.spec.ts +++ b/apps/web/e2e/tests/chat/mobile-subagent.spec.ts @@ -43,9 +43,9 @@ test.describe("Mobile subagent card", () => { "Explore the codebase", ); - await expect(card.locator('[data-testid="subagent-meta"]')).toBeVisible({ - timeout: 15_000, - }); + // Chat idle can arrive before the nested subagent completion event is + // hydrated into the card. Wait for the metadata state boundary explicitly. + await expect(card.locator('[data-testid="subagent-meta"]')).toBeVisible({ timeout: 15_000 }); await expect(card.locator('[data-testid="subagent-meta-duration"]')).toContainText("2.2s"); await expect(card.locator('[data-testid="subagent-meta-tokens"]')).toContainText("9,987"); await expect(card.locator('[data-testid="subagent-meta-tools"]')).toContainText("3 tools"); diff --git a/apps/web/e2e/tests/chat/model-selector-error.spec.ts b/apps/web/e2e/tests/chat/model-selector-error.spec.ts index a54560b32a..38f82ac966 100644 --- a/apps/web/e2e/tests/chat/model-selector-error.spec.ts +++ b/apps/web/e2e/tests/chat/model-selector-error.spec.ts @@ -430,6 +430,7 @@ test.describe("Chat model selector — persistence", () => { // effort default to High, so wait for the complete runtime snapshot // instead of asserting the transient model-only label. await testPage.getByRole("option", { name: /Mock Smart/ }).click(); + await expect(trigger).toContainText("Mock Smart", { timeout: 5_000 }); await expect .poll(async () => { const { sessions } = await apiClient.listTaskSessions(task.id); diff --git a/apps/web/e2e/tests/chat/shell-output-echo.spec.ts b/apps/web/e2e/tests/chat/shell-output-echo.spec.ts index 9229f5d2d5..74822b516b 100644 --- a/apps/web/e2e/tests/chat/shell-output-echo.spec.ts +++ b/apps/web/e2e/tests/chat/shell-output-echo.spec.ts @@ -15,6 +15,7 @@ test.describe("shell command output echo stripping", () => { apiClient, seedData, }) => { + test.setTimeout(120_000); const command = "cat file.txt"; const echoedOutput = `$ ${command}=== marker ===\n`; @@ -46,6 +47,7 @@ test.describe("shell command output echo stripping", () => { await expect(commandRow).toHaveText(command); const disclosure = chat.getByRole("button", { name: "Show command output" }); + await expect(disclosure).toBeVisible({ timeout: 45_000 }); const responsePromise = testPage.waitForResponse( (response) => response.url().endsWith("/shell-output") && response.status() === 200, ); @@ -64,6 +66,7 @@ test.describe("shell command output echo stripping", () => { apiClient, seedData, }) => { + test.setTimeout(120_000); // Some providers resolve a relative file-path argument in the command // to an absolute (cwd-joined) one before actually invoking the real // terminal, while the reported "command" - the same text the chat @@ -104,6 +107,7 @@ test.describe("shell command output echo stripping", () => { await expect(commandRow).toHaveText(command); const disclosure = chat.getByRole("button", { name: "Show command output" }); + await expect(disclosure).toBeVisible({ timeout: 45_000 }); const responsePromise = testPage.waitForResponse( (response) => response.url().endsWith("/shell-output") && response.status() === 200, ); @@ -163,6 +167,7 @@ test.describe("shell command output echo stripping", () => { await expect(commandRow).toHaveText(command); const disclosure = chat.getByRole("button", { name: "Show command output" }); + await expect(disclosure).toBeVisible({ timeout: 45_000 }); const responsePromise = testPage.waitForResponse( (response) => response.url().endsWith("/shell-output") && response.status() === 200, ); diff --git a/apps/web/e2e/tests/chat/subagent.spec.ts b/apps/web/e2e/tests/chat/subagent.spec.ts index 73d7194c74..511ab31e90 100644 --- a/apps/web/e2e/tests/chat/subagent.spec.ts +++ b/apps/web/e2e/tests/chat/subagent.spec.ts @@ -1,6 +1,43 @@ import { test, expect } from "../../fixtures/test-base"; import { SessionPage } from "../../pages/session-page"; +async function waitForPersistedSubagentMetrics( + apiClient: { + listSessionMessages: (sessionId: string) => Promise<{ + messages: Array<{ metadata?: Record }>; + }>; + }, + sessionId: string, +) { + await expect + .poll( + async () => { + const { messages } = await apiClient.listSessionMessages(sessionId); + return messages.some((message) => { + const normalized = message.metadata?.normalized; + if (!normalized || typeof normalized !== "object") return false; + const subagentTask = (normalized as { subagent_task?: unknown }).subagent_task; + if (!subagentTask || typeof subagentTask !== "object") return false; + const metrics = subagentTask as { + duration_ms?: unknown; + total_tokens?: unknown; + tool_use_count?: unknown; + }; + return ( + metrics.duration_ms === 2200 && + metrics.total_tokens === 9987 && + metrics.tool_use_count === 3 + ); + }); + }, + { + timeout: 30_000, + message: "subagent completion metrics were not persisted in the session", + }, + ) + .toBe(true); +} + // Drives the mock-agent's `subagent` scenario (triggered by the `/e2e:subagent` // directive). The scenario emits a claude-style subagent (Task) tool call with // full result metadata, which the kandev adapter normalizes to a subagent_task @@ -50,10 +87,25 @@ test.describe("Subagent card", () => { "Explore the codebase", ); - // Metadata row of chips surfaces the completed subagent's metrics. - await expect(card.locator('[data-testid="subagent-meta"]')).toBeVisible({ - timeout: 15_000, - }); + // Chat idle means the outer turn is no longer streaming. The nested + // subagent completion event can still be hydrating into the card. Wait for + // the persisted message that causes the metadata row before checking the + // rendered consequence. + const sessionId = task.session_id ?? task.primary_session_id; + expect(sessionId).toBeTruthy(); + if (!sessionId) throw new Error("agent task did not return a session id"); + await waitForPersistedSubagentMetrics(apiClient, sessionId); + + const metadata = card.locator('[data-testid="subagent-meta"]'); + // A live update may have been missed while the page was hydrating. Once + // the backend state is present, one reload rehydrates the authoritative + // message list without turning this into an unbounded retry loop. + if (!(await metadata.isVisible())) { + await testPage.reload(); + await session.waitForLoad(); + await session.waitForChatIdle({ timeout: 15_000 }); + } + await expect(metadata).toBeVisible(); await expect(card.locator('[data-testid="subagent-meta-duration"]')).toContainText("2.2s"); await expect(card.locator('[data-testid="subagent-meta-tokens"]')).toContainText("9,987"); await expect(card.locator('[data-testid="subagent-meta-tools"]')).toContainText("3 tools"); diff --git a/apps/web/e2e/tests/docker/storage-maintenance.spec.ts b/apps/web/e2e/tests/docker/storage-maintenance.spec.ts index c933f947d3..4bb0b48847 100644 --- a/apps/web/e2e/tests/docker/storage-maintenance.spec.ts +++ b/apps/web/e2e/tests/docker/storage-maintenance.spec.ts @@ -6,7 +6,11 @@ import { E2E_IMAGE_TAG, waitForScopedKandevContainersRemoved, } from "../../fixtures/docker-probe"; -import { dockerInspectExists, dockerRemove } from "../../helpers/docker"; +import { + dockerInspectExists, + dockerRemove, + waitForDockerContainerRemoved, +} from "../../helpers/docker"; function createStoppedContainer(labels: string[]): string { const args = ["create"]; @@ -82,12 +86,10 @@ test.describe.serial("process-scoped container cleanup", () => { }); test("starts the next test without the previous process-owned container", async () => { - await expect - .poll(() => dockerInspectExists(previousTestContainer), { - timeout: 30_000, - message: "previous process-owned container should be removed before the next test", - }) - .toBe(false); + await waitForDockerContainerRemoved( + previousTestContainer, + "previous process-owned container was not removed", + ); }); }); diff --git a/apps/web/e2e/tests/git/git-changes-panel.spec.ts b/apps/web/e2e/tests/git/git-changes-panel.spec.ts index 11a04e8ae2..6ddd78a092 100644 --- a/apps/web/e2e/tests/git/git-changes-panel.spec.ts +++ b/apps/web/e2e/tests/git/git-changes-panel.spec.ts @@ -1,6 +1,7 @@ import { test, expect } from "../../fixtures/test-base"; import { dwell } from "../../helpers/causal-waits"; import type { ApiClient } from "../../helpers/api-client"; +import { waitForSessionState } from "../../helpers/session"; import { KanbanPage } from "../../pages/kanban-page"; import { SessionPage } from "../../pages/session-page"; import type { Page } from "@playwright/test"; @@ -673,14 +674,27 @@ test.describe("Git Changes Panel", () => { test.setTimeout(120_000); const profile = await createStandardProfile(apiClient, "Git Reset Profile"); - await apiClient.createTaskWithAgent(seedData.workspaceId, "Git Reset Test", profile.id, { - description: "Testing reset to commit", - workflow_id: seedData.workflowId, - workflow_step_id: seedData.startStepId, - repository_ids: [seedData.repositoryId], - }); + const task = await apiClient.createTaskWithAgent( + seedData.workspaceId, + "Git Reset Test", + profile.id, + { + description: "Testing reset to commit", + workflow_id: seedData.workflowId, + workflow_step_id: seedData.startStepId, + repository_ids: [seedData.repositoryId], + }, + ); const session = await openTaskSession(testPage, "Git Reset Test"); + expect(task.session_id, "task must have a session to await").toBeTruthy(); + await waitForSessionState(apiClient, { + taskId: task.id, + sessionId: task.session_id as string, + expectedState: "WAITING_FOR_INPUT", + message: "the initial reset-test turn did not settle before git actions", + timeout: 30_000, + }); // Set up git helper const repoDir = path.join(backend.tmpDir, "repos", "e2e-repo"); @@ -743,8 +757,8 @@ test.describe("Git Changes Panel", () => { const resetButton = firstCommitRow.getByRole("button", { name: "Reset to this commit" }); await expect(resetButton).toBeVisible({ timeout: 5_000 }); // The action is rendered inside a group-hover span. Keep the row hovered - // for the user-facing check above, then bypass the CSS interception race - // if the hover slot disappears during React's status refresh. + // for the user-facing check above, then bypass CSS interception while the + // stable idle session keeps the row from being replaced by a status push. await resetButton.click({ force: true }); // Confirm the reset in the dialog diff --git a/apps/web/e2e/tests/improve-kandev.spec.ts b/apps/web/e2e/tests/improve-kandev.spec.ts index e69263ce85..932602475b 100644 --- a/apps/web/e2e/tests/improve-kandev.spec.ts +++ b/apps/web/e2e/tests/improve-kandev.spec.ts @@ -11,7 +11,13 @@ import { test, expect } from "../fixtures/test-base"; const BOOTSTRAP_URL = "**/api/v1/system/improve-kandev/bootstrap"; const HEALTH_URL = "**/api/v1/system/health"; -type ForkStatus = "writable" | "ready" | "blocked_emu" | "unknown"; +type ForkStatus = + | "writable" + | "ready" + | "creatable" + | "blocked_emu" + | "blocked_managed" + | "unknown"; type BootstrapOverrides = { /** Override the dedicated workspace the response points at. */ @@ -23,7 +29,13 @@ type BootstrapOverrides = { github_login?: string; has_write_access?: boolean; fork_status?: ForkStatus; - fork_message?: string; + fork_reason_code?: + | "account_cannot_fork" + | "app_unsupported" + | "fork_conflict" + | "fork_not_writable" + | "fork_not_ready" + | "managed_unavailable"; issueWorkflowId?: string; /** * When provided, the bootstrap route handler awaits this promise before @@ -92,7 +104,7 @@ async function mockImproveKandevApis( github_login: overrides.github_login ?? "octocat", has_write_access: hasWrite, fork_status: forkStatus, - ...(overrides.fork_message ? { fork_message: overrides.fork_message } : {}), + ...(overrides.fork_reason_code ? { fork_reason_code: overrides.fork_reason_code } : {}), }), }); }); @@ -359,6 +371,7 @@ test.describe("Improve Kandev dialog", () => { await mockImproveKandevApis(testPage, seedData, { github_login: "octocat", has_write_access: false, + fork_status: "creatable", }); await testPage.goto("/"); @@ -387,7 +400,7 @@ test.describe("Improve Kandev dialog", () => { // Contributor banner (fork mode) await expect(createDialog.getByText("@octocat")).toBeVisible(); await expect( - createDialog.getByText(/agent will fork kdlbs\/kandev to your account/i), + createDialog.getByText(/will prepare a writable fork for this task/i), ).toBeVisible(); // Workflow preview header @@ -439,16 +452,11 @@ test.describe("Improve Kandev dialog", () => { }) => { await apiClient.createWorkspace("Improve Kandev"); await apiClient.saveUserSettings({ agent_generated_task_titles: false }); - const blockedMessage = - "Your GitHub account appears to be an Enterprise Managed User (EMU) account, " + - "which typically cannot fork repositories outside your owning enterprise. " + - "The PR step would fail when forking kdlbs/kandev. Contact your GitHub admin " + - "if you'd like to enable this, or contribute via another account."; await mockImproveKandevApis(testPage, seedData, { github_login: "alice_corp", has_write_access: false, fork_status: "blocked_emu", - fork_message: blockedMessage, + fork_reason_code: "account_cannot_fork", }); await testPage.goto("/"); @@ -461,7 +469,9 @@ test.describe("Improve Kandev dialog", () => { const createDialog = testPage.getByTestId("create-task-dialog"); await expect(createDialog).toBeVisible({ timeout: 10_000 }); - await expect(createDialog.getByText(blockedMessage)).toBeVisible(); + await expect( + createDialog.getByText("This GitHub account cannot fork kdlbs/kandev."), + ).toBeVisible(); await createDialog.getByTestId("task-title-input").fill("EMU contribution"); await createDialog.getByTestId("task-description-input").fill("Describe the problem"); await expect(createDialog.getByTestId("submit-start-agent")).toBeDisabled(); @@ -470,6 +480,39 @@ test.describe("Improve Kandev dialog", () => { await expect(createDialog.getByTestId("submit-start-agent")).toBeEnabled({ timeout: 10_000 }); }); + test("blocks implementation but keeps issue reporting available when managed fork setup is blocked", async ({ + testPage, + apiClient, + seedData, + }) => { + await apiClient.createWorkspace("Improve Kandev"); + await apiClient.saveUserSettings({ agent_generated_task_titles: false }); + await mockImproveKandevApis(testPage, seedData, { + github_login: "automation", + has_write_access: false, + fork_status: "blocked_managed", + fork_reason_code: "managed_unavailable", + }); + + await testPage.goto("/"); + await testPage.getByTestId("sidebar-improve-kandev-button").click(); + await testPage.getByTestId("improve-kandev-proceed").click(); + + const createDialog = testPage.getByTestId("create-task-dialog"); + await expect(createDialog).toBeVisible({ timeout: 10_000 }); + await expect( + createDialog.getByText( + "Managed GitHub access could not prepare a verified fork for this task. Check the workspace GitHub connection or open an issue instead.", + ), + ).toBeVisible(); + await createDialog.getByTestId("task-title-input").fill("Managed fork recovery"); + await createDialog.getByTestId("task-description-input").fill("Describe the problem"); + await expect(createDialog.getByTestId("submit-start-agent")).toBeDisabled(); + + await createDialog.getByRole("tab", { name: "Open issue" }).click(); + await expect(createDialog.getByTestId("submit-start-agent")).toBeEnabled({ timeout: 10_000 }); + }); + test("locked mode hides workflow picker and source-mode switch (URL / None)", async ({ testPage, apiClient, diff --git a/apps/web/e2e/tests/integrations/jira-settings.spec.ts b/apps/web/e2e/tests/integrations/jira-settings.spec.ts index ad28d5d888..62d992c7d7 100644 --- a/apps/web/e2e/tests/integrations/jira-settings.spec.ts +++ b/apps/web/e2e/tests/integrations/jira-settings.spec.ts @@ -4,9 +4,10 @@ import { JiraSettingsPage } from "../../pages/jira-settings-page"; test.describe("Jira settings", () => { test("empty workspace shows form with disabled save/test until secret is filled", async ({ testPage, + seedData, }) => { const settings = new JiraSettingsPage(testPage); - await settings.goto(); + await settings.gotoWorkspace(seedData.workspaceId); await expect(settings.siteInput).toHaveValue(""); await expect(settings.secretInput).toHaveValue(""); @@ -26,9 +27,10 @@ test.describe("Jira settings", () => { test("saving the config persists across reload and shows the auth banner", async ({ testPage, apiClient, + seedData, }) => { const settings = new JiraSettingsPage(testPage); - await settings.goto(); + await settings.gotoWorkspace(seedData.workspaceId); await settings.fillForm({ siteUrl: "https://acme.atlassian.net", @@ -41,7 +43,10 @@ test.describe("Jira settings", () => { await expect(settings.saveButton).toHaveCount(0); // The post-save probe runs async; await it before reloading so the new // banner state is in the DB by the time the page re-fetches the config. - await apiClient.waitForIntegrationAuthHealthy("jira", { timeoutMs: 60_000 }); + await apiClient.waitForIntegrationAuthHealthy("jira", { + workspaceId: seedData.workspaceId, + timeoutMs: 60_000, + }); await testPage.reload(); await settings.siteInput.waitFor(); @@ -51,9 +56,13 @@ test.describe("Jira settings", () => { await expect(settings.statusBanner).toHaveAttribute("data-state", "ok"); }); - test("test connection surfaces inline success and failure", async ({ testPage, apiClient }) => { + test("test connection surfaces inline success and failure", async ({ + testPage, + apiClient, + seedData, + }) => { const settings = new JiraSettingsPage(testPage); - await settings.goto(); + await settings.gotoWorkspace(seedData.workspaceId); await apiClient.mockJiraSetAuthResult({ ok: true, @@ -76,11 +85,12 @@ test.describe("Jira settings", () => { test("seeded auth-health failure renders the failed banner on load", async ({ testPage, apiClient, + seedData, }) => { const settings = new JiraSettingsPage(testPage); // Save first so a config row exists, then simulate the poller writing // a failure status onto it. - await settings.goto(); + await settings.gotoWorkspace(seedData.workspaceId); await settings.fillForm({ siteUrl: "https://acme.atlassian.net", email: "alice@example.com", @@ -91,9 +101,10 @@ test.describe("Jira settings", () => { // probe goroutine could otherwise overwrite our forced lastOk=false back // to true a few ms after the mockJiraSetAuthHealth call, flipping the // banner to "ok" right when the assertion expects "failed". - await apiClient.waitForIntegrationAuthHealthy("jira"); + await apiClient.waitForIntegrationAuthHealthy("jira", { workspaceId: seedData.workspaceId }); await apiClient.mockJiraSetAuthHealth({ + workspaceId: seedData.workspaceId, ok: false, error: "session expired", }); @@ -103,9 +114,9 @@ test.describe("Jira settings", () => { await expect(settings.statusBanner).toContainText(/session expired/i); }); - test("delete clears the saved configuration", async ({ testPage }) => { + test("delete clears the saved configuration", async ({ testPage, seedData }) => { const settings = new JiraSettingsPage(testPage); - await settings.goto(); + await settings.gotoWorkspace(seedData.workspaceId); await settings.fillForm({ siteUrl: "https://acme.atlassian.net", email: "alice@example.com", @@ -126,9 +137,10 @@ test.describe("Jira settings", () => { test("server / data center save flow with PAT persists across reload", async ({ testPage, apiClient, + seedData, }) => { const settings = new JiraSettingsPage(testPage); - await settings.goto(); + await settings.gotoWorkspace(seedData.workspaceId); await settings.selectInstance("server"); // Switching to Server auto-selects PAT — the dropdown should now show it @@ -141,7 +153,7 @@ test.describe("Jira settings", () => { await expect(settings.saveButton).toBeEnabled(); await settings.saveButton.click(); await expect(settings.saveButton).toHaveCount(0); - await apiClient.waitForIntegrationAuthHealthy("jira"); + await apiClient.waitForIntegrationAuthHealthy("jira", { workspaceId: seedData.workspaceId }); await testPage.reload(); await settings.siteInput.waitFor(); @@ -152,9 +164,9 @@ test.describe("Jira settings", () => { await expect(settings.statusBanner).toHaveAttribute("data-state", "ok"); }); - test("switching instance type swaps the auth-method options", async ({ testPage }) => { + test("switching instance type swaps the auth-method options", async ({ testPage, seedData }) => { const settings = new JiraSettingsPage(testPage); - await settings.goto(); + await settings.gotoWorkspace(seedData.workspaceId); // Cloud default: api_token + session_cookie options, no PAT. await expect(settings.authSelect).toContainText(/API token/i); @@ -180,9 +192,10 @@ test.describe("Jira settings", () => { test("email field hides for session cookie and PAT, returns for cloud + api_token", async ({ testPage, + seedData, }) => { const settings = new JiraSettingsPage(testPage); - await settings.goto(); + await settings.gotoWorkspace(seedData.workspaceId); // Cloud + api_token (default) shows email. await expect(settings.emailInput).toBeVisible(); @@ -204,9 +217,10 @@ test.describe("Jira settings", () => { test("saved secret is not reused after switching identity fields", async ({ testPage, apiClient, + seedData, }) => { const settings = new JiraSettingsPage(testPage); - await settings.goto(); + await settings.gotoWorkspace(seedData.workspaceId); await settings.fillForm({ siteUrl: "https://acme.atlassian.net", email: "alice@example.com", @@ -214,7 +228,7 @@ test.describe("Jira settings", () => { }); await settings.saveButton.click(); await expect(settings.saveButton).toHaveCount(0); - await apiClient.waitForIntegrationAuthHealthy("jira"); + await apiClient.waitForIntegrationAuthHealthy("jira", { workspaceId: seedData.workspaceId }); await testPage.reload(); await settings.siteInput.waitFor(); @@ -239,9 +253,9 @@ test.describe("Jira settings", () => { await expect(settings.saveButton).toBeDisabled(); }); - test("PAT help link targets the configured site", async ({ testPage }) => { + test("PAT help link targets the configured site", async ({ testPage, seedData }) => { const settings = new JiraSettingsPage(testPage); - await settings.goto(); + await settings.gotoWorkspace(seedData.workspaceId); await settings.selectInstance("server"); await settings.siteInput.fill("https://jira.acme.com/"); diff --git a/apps/web/e2e/tests/integrations/linear-settings.spec.ts b/apps/web/e2e/tests/integrations/linear-settings.spec.ts index 4ff66ace6d..bb4417f26c 100644 --- a/apps/web/e2e/tests/integrations/linear-settings.spec.ts +++ b/apps/web/e2e/tests/integrations/linear-settings.spec.ts @@ -182,6 +182,7 @@ test.describe("Linear settings", () => { }); await apiClient.mockLinearSetAuthHealth({ + workspaceId: seedData.workspaceId, ok: false, error: "rate limited", }); diff --git a/apps/web/e2e/tests/integrations/sentry-settings.spec.ts b/apps/web/e2e/tests/integrations/sentry-settings.spec.ts index fc433c112e..924ff1e2b6 100644 --- a/apps/web/e2e/tests/integrations/sentry-settings.spec.ts +++ b/apps/web/e2e/tests/integrations/sentry-settings.spec.ts @@ -20,6 +20,7 @@ test.describe("Sentry settings — instances", () => { const settings = new SentrySettingsPage(testPage); await settings.goto(seedData.workspaceId); const card = settings.cardByName("Original Sentry"); + await expect(card).toBeVisible({ timeout: 15_000 }); await card.getByTestId("sentry-instance-edit-button").click(); await testPage.getByTestId("sentry-edit-name-input").fill("Renamed Sentry"); diff --git a/apps/web/e2e/tests/layout/preview-tab-session-switch.spec.ts b/apps/web/e2e/tests/layout/preview-tab-session-switch.spec.ts index 50c493bc70..5156bbfff8 100644 --- a/apps/web/e2e/tests/layout/preview-tab-session-switch.spec.ts +++ b/apps/web/e2e/tests/layout/preview-tab-session-switch.spec.ts @@ -35,9 +35,12 @@ async function waitForWorkspaceFile( async function openFileInPreview(page: Page, session: SessionPage, filename: string) { await session.clickTab("Files"); - await expect(session.files).toBeVisible({ timeout: 10_000 }); + await expect(session.files).toBeVisible({ timeout: 20_000 }); const fileRow = session.files.getByText(filename); - await expect(fileRow).toBeVisible({ timeout: 30_000 }); + // The file browser retries its agentctl tree load for up to 18s while a + // freshly prepared workspace becomes reachable. Let that bounded retry + // schedule finish before opening the file. + await expect(fileRow).toBeVisible({ timeout: 45_000 }); await fileRow.click(); // Retry click if preview tab didn't appear (executor may need a moment) const previewTab = page.getByTestId("preview-tab-file-editor"); @@ -81,6 +84,19 @@ function tabWrapperByText(page: Page, text: string) { return page.locator(".dv-tab", { has: page.locator(".dv-default-tab", { hasText: text }) }); } +function publishSeedCommit(git: GitHelper, remoteURL: string) { + // Task workspaces are created from the repository's configured origin. A + // commit that only exists in the fixture's checkout can therefore be + // absent from the task file tree under CI load. + const remotes = git.exec("git remote").split(/\r?\n/); + if (remotes.includes("origin")) { + git.exec(`git remote set-url origin "${remoteURL}"`); + } else { + git.exec(`git remote add origin "${remoteURL}"`); + } + git.exec("git push origin HEAD:main"); +} + test.describe("Preview tab survives session switch", () => { test("preview tab persists after switching tasks and back", async ({ testPage, @@ -104,6 +120,7 @@ test.describe("Preview tab survives session switch", () => { git.createFile(FILE_A, "// alpha content"); git.stageAll(); git.commit("seed alpha"); + publishSeedCommit(git, seedData.repositoryRemoteURL); // Create Task A and wait for it to complete const taskA = await apiClient.createTaskWithAgent( @@ -212,6 +229,7 @@ test.describe("Preview tab survives session switch", () => { git.createFile(FILE_PROMOTED, "// promoted content"); git.stageAll(); git.commit("seed promoted"); + publishSeedCommit(git, seedData.repositoryRemoteURL); const taskA = await seedFinishedTask(apiClient, seedData, "Promote Round-Trip A"); const taskB = await seedFinishedTask(apiClient, seedData, "Promote Round-Trip B"); @@ -266,6 +284,7 @@ test.describe("Preview tab survives session switch", () => { git.createFile(FILE_ACTIVE, "// active content"); git.stageAll(); git.commit("seed active"); + publishSeedCommit(git, seedData.repositoryRemoteURL); const taskA = await seedFinishedTask(apiClient, seedData, "Active Tab Round-Trip A"); const taskB = await seedFinishedTask(apiClient, seedData, "Active Tab Round-Trip B"); @@ -318,6 +337,7 @@ test.describe("Preview tab survives session switch", () => { git.createFile(FILE_REFRESH, "// refresh content"); git.stageAll(); git.commit("seed refresh"); + publishSeedCommit(git, seedData.repositoryRemoteURL); await seedFinishedTask(apiClient, seedData, "Refresh Active Tab Task"); diff --git a/apps/web/e2e/tests/mobile-improve-kandev.spec.ts b/apps/web/e2e/tests/mobile-improve-kandev.spec.ts index 367ab1404a..27eede633f 100644 --- a/apps/web/e2e/tests/mobile-improve-kandev.spec.ts +++ b/apps/web/e2e/tests/mobile-improve-kandev.spec.ts @@ -6,8 +6,10 @@ const HEALTH_URL = "**/api/v1/system/health"; test.describe("Improve Kandev on mobile", () => { test("dismisses the intro and reaches the issue-only task option without overflow", async ({ testPage, + apiClient, seedData, }) => { + await apiClient.saveUserSettings({ agent_generated_task_titles: false }); await testPage.route(HEALTH_URL, (route) => route.fulfill({ status: 200, @@ -29,7 +31,8 @@ test.describe("Improve Kandev on mobile", () => { bundle_file: "/tmp/kandev-improve-mobile-e2e/diagnostic-bundle.zip", github_login: "octocat", has_write_access: false, - fork_status: "unknown", + fork_status: "blocked_managed", + fork_reason_code: "managed_unavailable", }), }), ); @@ -49,10 +52,19 @@ test.describe("Improve Kandev on mobile", () => { const createDialog = testPage.getByTestId("create-task-dialog"); await expect(createDialog).toBeVisible({ timeout: 10_000 }); + await expect( + createDialog.getByText( + "Managed GitHub access could not prepare a verified fork for this task. Check the workspace GitHub connection or open an issue instead.", + ), + ).toBeVisible(); + await createDialog.getByTestId("task-title-input").fill("Mobile issue fallback"); + await createDialog.getByTestId("task-description-input").fill("Describe the problem"); + await expect(createDialog.getByTestId("submit-start-agent")).toBeDisabled(); await createDialog.getByRole("tab", { name: "Open issue" }).tap(); await expect( createDialog.getByText(/only create a GitHub issue.*will not implement/i), ).toBeVisible(); + await expect(createDialog.getByTestId("submit-start-agent")).toBeEnabled({ timeout: 10_000 }); expect( await testPage.evaluate( () => document.documentElement.scrollWidth <= document.documentElement.clientWidth, diff --git a/apps/web/e2e/tests/office/comment-run-status.spec.ts b/apps/web/e2e/tests/office/comment-run-status.spec.ts index 76fdac17ba..6329b843d3 100644 --- a/apps/web/e2e/tests/office/comment-run-status.spec.ts +++ b/apps/web/e2e/tests/office/comment-run-status.spec.ts @@ -2,6 +2,11 @@ import type { Response } from "@playwright/test"; import { test, expect } from "../../fixtures/office-fixture"; import { waitForHttp } from "../../helpers/causal-waits"; +// Keep queued seed rows out of the live scheduler. Each test drives its own +// status transitions, so an automatic claim would make the initial UI state +// depend on scheduler timing. +const holdQueuedRun = () => new Date(Date.now() + 60 * 60 * 1000).toISOString(); + /** * E2E coverage for the per-comment run status badge on the office task * chat page. A user comment that triggers a `task_comment` wakeup @@ -61,6 +66,7 @@ test.describe("User comment run status badge", () => { taskId: task.id, commentId: comment.comment_id, idempotencyKey: `task_comment:${comment.comment_id}`, + scheduledRetryAt: holdQueuedRun(), }); const commentsLoaded = waitForHttp(testPage, "GET", COMMENTS_PATH); @@ -104,6 +110,7 @@ test.describe("User comment run status badge", () => { taskId: task.id, commentId: comment.comment_id, idempotencyKey: `task_comment:${comment.comment_id}`, + scheduledRetryAt: holdQueuedRun(), }); const commentsLoaded = waitForHttp(testPage, "GET", COMMENTS_PATH); @@ -169,6 +176,7 @@ test.describe("User comment run status badge", () => { taskId: task.id, commentId: comment.comment_id, idempotencyKey: `task_comment:${comment.comment_id}`, + scheduledRetryAt: holdQueuedRun(), }); await apiClient.updateRunStatus(seeded.run_id, { status: "failed", @@ -225,6 +233,7 @@ test.describe("User comment run status badge", () => { taskId: task.id, commentId: userComment.comment_id, idempotencyKey: `task_comment:${userComment.comment_id}`, + scheduledRetryAt: holdQueuedRun(), }); await apiClient.seedComment({ diff --git a/apps/web/e2e/tests/office/scheduler-events.spec.ts b/apps/web/e2e/tests/office/scheduler-events.spec.ts index 6f75731e55..59b380a8d3 100644 --- a/apps/web/e2e/tests/office/scheduler-events.spec.ts +++ b/apps/web/e2e/tests/office/scheduler-events.spec.ts @@ -18,14 +18,46 @@ import { test, expect } from "../../fixtures/office-fixture"; type RunRow = { id: string; reason: string; task_id?: string; comment_id?: string }; +type RunPage = { + runs?: RunRow[]; + next_cursor?: string; + next_id?: string; +}; + async function listAgentRuns( apiClient: { rawRequest: (m: string, u: string) => Promise }, agentId: string, ): Promise { - const res = await apiClient.rawRequest("GET", `/api/v1/office/agents/${agentId}/runs`); - if (!res.ok) return []; - const body = (await res.json()) as { runs?: RunRow[] }; - return body.runs ?? []; + const runs: RunRow[] = []; + let cursor = ""; + let cursorId = ""; + + for (;;) { + const query = new URLSearchParams({ limit: "100" }); + if (cursor) { + query.set("cursor", cursor); + if (cursorId) query.set("cursor_id", cursorId); + } + + const res = await apiClient.rawRequest( + "GET", + `/api/v1/office/agents/${agentId}/runs?${query.toString()}`, + ); + if (!res.ok) return []; + + const body = (await res.json()) as RunPage; + runs.push(...(body.runs ?? [])); + if (!body.next_cursor) return runs; + + // The API returns a stable (requested_at, id) cursor pair. Guard against + // a malformed response looping forever while still allowing old servers + // that omit the tie-breaker ID to make progress by timestamp. + if (body.next_cursor === cursor && (body.next_id ?? "") === cursorId) { + return runs; + } + cursor = body.next_cursor; + cursorId = body.next_id ?? ""; + } } test.describe("Office reactive scheduler", () => { diff --git a/apps/web/e2e/tests/pr/pr-push-autolink.spec.ts b/apps/web/e2e/tests/pr/pr-push-autolink.spec.ts index ed27903020..bd9edd4a37 100644 --- a/apps/web/e2e/tests/pr/pr-push-autolink.spec.ts +++ b/apps/web/e2e/tests/pr/pr-push-autolink.spec.ts @@ -35,6 +35,7 @@ test.describe("GitHub PR push-detection auto-link", () => { ); await apiClient.updateRepository(seedData.repositoryId, { provider: "github", + provider_host: "https://github.com", provider_owner: "testorg", provider_name: "testrepo", }); diff --git a/apps/web/e2e/tests/review/mobile-review-multi-pr.spec.ts b/apps/web/e2e/tests/review/mobile-review-multi-pr.spec.ts index c45b5b2e47..65afdf6116 100644 --- a/apps/web/e2e/tests/review/mobile-review-multi-pr.spec.ts +++ b/apps/web/e2e/tests/review/mobile-review-multi-pr.spec.ts @@ -69,13 +69,26 @@ async function openMobileReview( await expect(changesPanel).toBeVisible({ timeout: 15_000 }); const prFiles = changesPanel.getByTestId("pr-files-section"); await expect(prFiles).toBeVisible({ timeout: 20_000 }); - for (const pr of REVIEW_PRS) { - await expect( - prFiles.locator( - `[data-changes-file=${JSON.stringify(REVIEW_SHARED_FILE)}][data-pr-key="${REVIEW_OWNER}/${repositoryName}/${pr.number}"]`, - ), - ).toBeVisible({ timeout: 30_000 }); - } + const expectedFileLocators = REVIEW_PRS.map((pr) => + prFiles.locator( + `[data-changes-file=${JSON.stringify(REVIEW_SHARED_FILE)}][data-pr-key="${REVIEW_OWNER}/${repositoryName}/${pr.number}"]`, + ), + ); + await expect + .poll( + async () => { + const visible = await Promise.all( + expectedFileLocators.map((locator) => locator.isVisible().catch(() => false)), + ); + return visible.filter(Boolean).length; + }, + { + timeout: 30_000, + intervals: [250, 500, 1_000], + message: "waiting for all seeded PR files to hydrate in the mobile changes panel", + }, + ) + .toBe(REVIEW_PRS.length); await changesPanel.getByRole("button", { name: "Review", exact: true }).tap(); await expect(session.reviewDialog()).toBeVisible({ timeout: 15_000 }); return; diff --git a/apps/web/e2e/tests/search/task-workspace-content-search.spec.ts b/apps/web/e2e/tests/search/task-workspace-content-search.spec.ts index 9c6c615698..5f20abdc07 100644 --- a/apps/web/e2e/tests/search/task-workspace-content-search.spec.ts +++ b/apps/web/e2e/tests/search/task-workspace-content-search.spec.ts @@ -1,12 +1,10 @@ import fs from "node:fs"; import path from "node:path"; import { execSync } from "node:child_process"; -import type { Locator, Page } from "@playwright/test"; import { expect, test } from "../../fixtures/test-base"; import { GitHelper, makeGitEnv } from "../../helpers/git-helper"; import { SessionPage } from "../../pages/session-page"; import { MODIFIER } from "./shared"; -import { dwell } from "../../helpers/causal-waits"; const SEARCH_TERM = "TaskWorkspaceNeedle"; const EXTRA_REPOSITORY_NAME = "content-search-extra"; @@ -27,52 +25,6 @@ function initializeRepository(directory: string, gitEnv: NodeJS.ProcessEnv): Git return new GitHelper(directory, gitEnv); } -/** - * Content search is intentionally a one-shot request. Re-drive it while the - * task's repositories finish registering with the workspace process manager. - */ -async function waitForRepositoryGroups({ - page, - input, - groups, - query, - expectedCount, - timeout, -}: { - page: Page; - input: Locator; - groups: Locator; - query: string; - expectedCount: number; - timeout: number; -}): Promise { - const deadline = Date.now() + timeout; - let lastError: Error | undefined; - - while (Date.now() < deadline) { - const attemptTimeout = Math.min(5_000, deadline - Date.now()); - try { - await expect(groups).toHaveCount(expectedCount, { timeout: attemptTimeout }); - return; - } catch (error) { - lastError = error instanceof Error ? error : new Error(String(error)); - } - - if (Date.now() >= deadline) break; - await input.fill(""); - await dwell( - page, - 50, - "poll-interval", - "spacing between retype attempts in the retry loop above; the search input is debounced and the retry exists to re-trigger it, so there is no completion event to await here", - ); - await input.fill(query); - } - - if (lastError) throw lastError; - throw new Error(`Expected ${expectedCount} content-search repository groups`); -} - test("@search keeps workspace modes out of non-task routes", async ({ testPage }) => { await testPage.goto("/settings/preferences/appearance"); @@ -148,14 +100,7 @@ test("@search searches all task repositories and opens the selected match", asyn await input.fill(SEARCH_TERM); const groups = dialog.getByTestId("content-search-repo-group"); - await waitForRepositoryGroups({ - page: testPage, - input, - groups, - query: SEARCH_TERM, - expectedCount: 2, - timeout: 30_000, - }); + await expect(groups).toHaveCount(2, { timeout: 30_000 }); await expect( dialog.locator( `[data-testid="content-search-repo-group"][data-repository="${EXTRA_REPOSITORY_NAME}"]`, diff --git a/apps/web/e2e/tests/session/port-forward-dialog.spec.ts b/apps/web/e2e/tests/session/port-forward-dialog.spec.ts index 77d3795e11..f952dbee49 100644 --- a/apps/web/e2e/tests/session/port-forward-dialog.spec.ts +++ b/apps/web/e2e/tests/session/port-forward-dialog.spec.ts @@ -2,6 +2,7 @@ import { type Page } from "@playwright/test"; import { test, expect } from "../../fixtures/test-base"; import type { SeedData } from "../../fixtures/test-base"; import type { ApiClient } from "../../helpers/api-client"; +import { waitForSessionAgentctlReady } from "../../helpers/session-store"; import { SessionPage } from "../../pages/session-page"; /** @@ -43,6 +44,7 @@ async function seedRemoteSession( // registers, leaving isAgentBusy=true and the idle input never rendering. The // helper reloads once to re-derive state from SSR. The plain wait flaked here. await session.waitForChatIdle({ timeout: 30_000 }); + await waitForSessionAgentctlReady(testPage, task.session_id); // Reset workspace default executor so other tests aren't affected await apiClient.updateWorkspace(seedData.workspaceId, { diff --git a/apps/web/e2e/tests/session/session-layout.spec.ts b/apps/web/e2e/tests/session/session-layout.spec.ts index 0ef9105019..28600d5055 100644 --- a/apps/web/e2e/tests/session/session-layout.spec.ts +++ b/apps/web/e2e/tests/session/session-layout.spec.ts @@ -43,10 +43,6 @@ const TERMINAL_MARKER = "KANDEV_E2E_MARKER_12345"; const DONE_STATES = ["COMPLETED", "WAITING_FOR_INPUT"]; test.describe("Session layout", () => { - // The standalone executor can fail to allocate a port on cold start; - // retry once so a transient backend hiccup doesn't fail the suite. - test.describe.configure({ retries: 1 }); - test("maximize terminal hides other panels", async ({ testPage, apiClient, seedData }) => { const session = await seedTaskWithSession(testPage, apiClient, seedData, "Maximize Test"); @@ -89,7 +85,7 @@ test.describe("Session layout", () => { apiClient, seedData, }) => { - test.setTimeout(60_000); + test.setTimeout(120_000); // Create Task A, type a command, and maximize terminal const session = await seedTaskWithSession(testPage, apiClient, seedData, "Task A Maximize"); diff --git a/apps/web/e2e/tests/settings/secrets-copy-move.spec.ts b/apps/web/e2e/tests/settings/secrets-copy-move.spec.ts index a05dce6a93..d2146755e3 100644 --- a/apps/web/e2e/tests/settings/secrets-copy-move.spec.ts +++ b/apps/web/e2e/tests/settings/secrets-copy-move.spec.ts @@ -1,6 +1,8 @@ import { randomUUID } from "node:crypto"; import { test, expect } from "../../fixtures/test-base"; +import { waitForHttp } from "../../helpers/causal-waits"; +import type { SecretListItem } from "../../../lib/types/http-secrets"; const GLOBAL_VALUE = "e2e-copy-move-global-value"; @@ -29,6 +31,18 @@ async function createSecretFromSettings( await expect(page.locator("body")).not.toContainText(value); } +/** Selects a workspace by its exact name in the open destination picker. */ +async function selectWorkspaceDestination( + page: import("@playwright/test").Page, + dialog: import("@playwright/test").Locator, + workspaceName: string, +) { + const destination = dialog.getByRole("combobox", { name: "Destination" }); + await expect(destination).toBeVisible(); + await destination.click(); + await page.getByRole("listbox").getByRole("option", { name: workspaceName, exact: true }).click(); +} + /** * Opens the copy/move dialog for a named secret and submits through the * dialog's OWN primary button (never the settings floating Save control). @@ -37,15 +51,48 @@ async function submitCopyMove( page: import("@playwright/test").Page, secretName: string, mode: "Copy" | "Move", -) { + destinationWorkspaceName?: string, +): Promise { await page.getByRole("button", { name: `Copy or move ${secretName}` }).click(); const dialog = page.getByRole("dialog"); await expect(dialog).toBeVisible(); if (mode === "Move") { await dialog.getByRole("radio", { name: /Move/ }).click(); } + if (destinationWorkspaceName) { + await selectWorkspaceDestination(page, dialog, destinationWorkspaceName); + } + const transferResponse = waitForHttp(page, "POST", /\/api\/v1\/secrets\/[^/]+\/(?:copy|move)$/, { + predicate: (response) => response.status() === 201, + }); await dialog.getByRole("button", { name: mode, exact: true }).click(); + const transferred = (await (await transferResponse).json()) as SecretListItem; await expect(dialog).toBeHidden(); + return transferred; +} + +async function waitForWorkspaceSecret( + apiClient: { + listSecrets: (options: { + scope: "workspace"; + workspaceId: string; + }) => Promise>; + }, + workspaceId: string, + name: string, +) { + // The transfer request and the settings list are separate reads. Wait for + // the persisted row before navigating so the destination page cannot fetch + // the workspace list during the short write-to-read window. + await expect + .poll( + async () => { + const secrets = await apiClient.listSecrets({ scope: "workspace", workspaceId }); + return secrets.some((secret) => secret.name === name); + }, + { timeout: 30_000 }, + ) + .toBe(true); } test.describe("secrets-copy-move", () => { @@ -56,14 +103,25 @@ test.describe("secrets-copy-move", () => { }) => { const sourceName = `E2E Copy Move Global Copy ${runToken()}`; const copiedName = `${sourceName} (from Global)`; + const workspaces = await apiClient.listWorkspaces(); + const workspaceName = workspaces.workspaces.find( + (workspace) => workspace.id === seedData.workspaceId, + )?.name; + expect(workspaceName).toBeTruthy(); await createSecretFromSettings(testPage, "/settings/general/secrets", sourceName, GLOBAL_VALUE); const global = (await apiClient.listSecrets()).find((secret) => secret.name === sourceName); expect(global?.id).toBeTruthy(); - await submitCopyMove(testPage, sourceName, "Copy"); + const transferred = await submitCopyMove(testPage, sourceName, "Copy", workspaceName!); + expect(transferred).toMatchObject({ + name: copiedName, + scope: "workspace", + workspace_id: seedData.workspaceId, + }); // The source stays on the Global page; the copy appears in the workspace. await expect(testPage.getByText(sourceName, { exact: true })).toBeVisible(); + await waitForWorkspaceSecret(apiClient, seedData.workspaceId, copiedName); await testPage.goto(`/settings/workspace/${seedData.workspaceId}/secrets`); await expect(testPage.getByText(copiedName, { exact: true })).toBeVisible(); await expect(testPage.locator("body")).not.toContainText(GLOBAL_VALUE); @@ -116,9 +174,14 @@ test.describe("secrets-copy-move", () => { }) => { const sourceName = `E2E Copy Move Global Move ${runToken()}`; const movedName = `${sourceName} (from Global)`; + const workspaces = await apiClient.listWorkspaces(); + const workspaceName = workspaces.workspaces.find( + (workspace) => workspace.id === seedData.workspaceId, + )?.name; + expect(workspaceName).toBeTruthy(); await createSecretFromSettings(testPage, "/settings/general/secrets", sourceName, GLOBAL_VALUE); - await submitCopyMove(testPage, sourceName, "Move"); + await submitCopyMove(testPage, sourceName, "Move", workspaceName!); await expect(testPage.getByText(sourceName, { exact: true })).toHaveCount(0); await testPage.goto(`/settings/workspace/${seedData.workspaceId}/secrets`); @@ -135,6 +198,7 @@ test.describe("secrets-copy-move", () => { test("blocks a duplicate target name and marks the name field invalid", async ({ testPage, + apiClient, seedData, }) => { const sourceName = `E2E Copy Move Conflict Source ${runToken()}`; @@ -146,11 +210,17 @@ test.describe("secrets-copy-move", () => { conflictingName, WORKSPACE_VALUE, ); + const workspaces = await apiClient.listWorkspaces(); + const workspaceName = workspaces.workspaces.find( + (workspace) => workspace.id === seedData.workspaceId, + )?.name; + expect(workspaceName).toBeTruthy(); await testPage.goto("/settings/general/secrets"); await testPage.getByRole("button", { name: `Copy or move ${sourceName}` }).click(); const dialog = testPage.getByRole("dialog"); await expect(dialog).toBeVisible(); + await selectWorkspaceDestination(testPage, dialog, workspaceName!); const nameInput = dialog.getByLabel("Name"); await expect(nameInput).toHaveValue(conflictingName); diff --git a/apps/web/e2e/tests/system/storage-maintenance.spec.ts b/apps/web/e2e/tests/system/storage-maintenance.spec.ts index ecd7c90b71..a89c12f534 100644 --- a/apps/web/e2e/tests/system/storage-maintenance.spec.ts +++ b/apps/web/e2e/tests/system/storage-maintenance.spec.ts @@ -73,6 +73,7 @@ test.describe("System storage maintenance", () => { await expect(testPage.getByTestId("storage-run-now")).toHaveAttribute( "data-job-state", "succeeded", + { timeout: 30_000 }, ); }); diff --git a/apps/web/e2e/tests/task/add-workspace-sources.spec.ts b/apps/web/e2e/tests/task/add-workspace-sources.spec.ts index e1f0428b91..94ef6278b0 100644 --- a/apps/web/e2e/tests/task/add-workspace-sources.spec.ts +++ b/apps/web/e2e/tests/task/add-workspace-sources.spec.ts @@ -3,6 +3,7 @@ import type { Locator, Page } from "@playwright/test"; import { execFileSync } from "node:child_process"; import fs from "node:fs"; import path from "node:path"; +import type { ApiClient } from "../../helpers/api-client"; import { GitHelper, makeGitEnv } from "../../helpers/git-helper"; import { SessionPage } from "../../pages/session-page"; @@ -47,6 +48,25 @@ function activeFileTab(page: Page, filename: string) { return page.locator(".dv-tab.dv-active-tab", { hasText: filename }).first(); } +async function waitForWorkspaceReady( + apiClient: ApiClient, + taskId: string, + sessionId: string, +): Promise { + await expect + .poll( + async () => { + const { sessions } = await apiClient.listTaskSessions(taskId); + return sessions.find((session) => session.id === sessionId)?.workspace_path ?? ""; + }, + { + timeout: 60_000, + message: "task workspace should be ready before opening the session", + }, + ) + .toMatch(/\S/); +} + test.describe("Attach local workspace sources", () => { test("adds a local repository and folder successively, scopes Changes to Git, and persists", async ({ testPage, @@ -70,6 +90,8 @@ test.describe("Attach local workspace sources", () => { }, ); + if (!task.session_id) throw new Error("task creation did not return a session id"); + await waitForWorkspaceReady(apiClient, task.id, task.session_id); await testPage.goto(`/t/${task.id}`); const session = new SessionPage(testPage); await session.waitForLoad(); @@ -89,7 +111,6 @@ test.describe("Attach local workspace sources", () => { const openFolder = testPage.getByRole("menuitem", { name: "Open workspace folder" }); await expect(addSources).toBeEnabled(); await expect(openFolder).toBeEnabled(); - if (!task.session_id) throw new Error("task creation did not return a session id"); await Promise.all([ testPage.waitForRequest( (request) => diff --git a/apps/web/e2e/tests/task/archive-freezes-task-state.spec.ts b/apps/web/e2e/tests/task/archive-freezes-task-state.spec.ts index 83d279527c..0cdefb6186 100644 --- a/apps/web/e2e/tests/task/archive-freezes-task-state.spec.ts +++ b/apps/web/e2e/tests/task/archive-freezes-task-state.spec.ts @@ -19,14 +19,22 @@ test.describe("Archiving a task freezes its runtime state", () => { apiClient, seedData, }) => { - test.setTimeout(60_000); + test.setTimeout(90_000); const taskTitle = "Archive Freezes State"; - const created = await apiClient.createTask(seedData.workspaceId, taskTitle, { - workflow_id: seedData.workflowId, - workflow_step_id: seedData.startStepId, - repository_ids: [seedData.repositoryId], - }); + const created = await apiClient.createTaskWithAgent( + seedData.workspaceId, + taskTitle, + seedData.agentProfileId, + { + // Keep the turn alive long enough for a loaded CI shard to observe + // IN_PROGRESS and archive it. The cancellation-aware delay exits as + // soon as the test sends agent.cancel. + description: 'e2e:message("still going")\ne2e:delay(60000)', + workflow_id: seedData.workflowId, + workflow_step_id: seedData.startStepId, + }, + ); const taskId = created.id; const { session_id: sessionId } = await apiClient.seedTaskSession(taskId, { state: "RUNNING", @@ -39,12 +47,12 @@ test.describe("Archiving a task freezes its runtime state", () => { // stays frozen for the rest of the test. await expect .poll(async () => (await apiClient.getTask(taskId)).state, { - timeout: 20_000, + timeout: 30_000, message: "waiting for task to reach IN_PROGRESS before archiving", }) .toBe("IN_PROGRESS"); - // Archive while the agent turn is still in flight (15s delay keeps the + // Archive while the agent turn is still in flight (60s delay keeps the // mock agent from finishing on its own during this test). await apiClient.archiveTask(taskId); @@ -61,6 +69,10 @@ test.describe("Archiving a task freezes its runtime state", () => { // User-facing check: the Tasks list (with archived tasks shown) must // reflect the same frozen state — the card must not have been // silently moved into a "needs review" bucket. + // The task intentionally has no repository, so clear any persisted + // repository filter before opening the list. The shared E2E user can have + // inherited settings from a preceding test in the same worker. + await apiClient.saveUserSettings({ repository_ids: [] }); await testPage.goto("/tasks"); await testPage.waitForLoadState("networkidle"); await testPage.getByRole("checkbox", { name: "Show archived" }).click(); diff --git a/apps/web/e2e/tests/task/create-task-branch-selector.spec.ts b/apps/web/e2e/tests/task/create-task-branch-selector.spec.ts index 991df9f1da..125479d30e 100644 --- a/apps/web/e2e/tests/task/create-task-branch-selector.spec.ts +++ b/apps/web/e2e/tests/task/create-task-branch-selector.spec.ts @@ -368,10 +368,12 @@ test.describe("Fresh-branch flow", () => { // Submit and assert the discard modal never appears (clean tree). // Wait for the create-task request to fire so we know the submit path // really executed and didn't short-circuit before the modal would render. + const submit = testPage.getByTestId("submit-start-agent"); + await expect(submit).toBeEnabled({ timeout: 10_000 }); const createTaskRequest = testPage.waitForRequest( (req) => req.url().endsWith("/api/v1/tasks") && req.method() === "POST", ); - await testPage.getByTestId("submit-start-agent").click(); + await submit.click(); await createTaskRequest; await expect(testPage.getByTestId("discard-local-changes-dialog")).toHaveCount(0); } finally { diff --git a/apps/web/e2e/tests/task/mobile-create-task-new-local-repository.spec.ts b/apps/web/e2e/tests/task/mobile-create-task-new-local-repository.spec.ts index 0197b967c0..f2de17afb5 100644 --- a/apps/web/e2e/tests/task/mobile-create-task-new-local-repository.spec.ts +++ b/apps/web/e2e/tests/task/mobile-create-task-new-local-repository.spec.ts @@ -42,6 +42,28 @@ async function openCreationDrawer(page: Page): Promise { await expect(refresh).toBeVisible(); await expect(action).toBeVisible(); const searchControl = search.locator(".."); + await expect + .poll( + async () => { + const [searchControlBox, refreshBox, actionBox] = await Promise.all([ + searchControl.boundingBox(), + refresh.boundingBox(), + action.boundingBox(), + ]); + if (!searchControlBox || !refreshBox || !actionBox) return false; + const refreshGap = refreshBox.x - (searchControlBox.x + searchControlBox.width); + return ( + searchControlBox.width > refreshBox.width + actionBox.width && + refreshGap >= 0 && + refreshGap <= 8 + ); + }, + { + timeout: 15_000, + message: "repository picker toolbar did not settle into its mobile layout", + }, + ) + .toBe(true); const [searchControlBox, refreshBox, actionBox] = await Promise.all([ searchControl.boundingBox(), refresh.boundingBox(), diff --git a/apps/web/e2e/tests/task/mobile-plan-toolbar-implement.spec.ts b/apps/web/e2e/tests/task/mobile-plan-toolbar-implement.spec.ts index 93e53dfa96..7dba82e8fe 100644 --- a/apps/web/e2e/tests/task/mobile-plan-toolbar-implement.spec.ts +++ b/apps/web/e2e/tests/task/mobile-plan-toolbar-implement.spec.ts @@ -5,6 +5,7 @@ import type { ApiClient } from "../../helpers/api-client"; import type { SeedData } from "../../fixtures/test-base"; import { watchWs } from "../../helpers/causal-waits"; import { planScript } from "../../helpers/seed-session-messages"; +import { waitForSessionState } from "../../helpers/session"; import { SessionPage } from "../../pages/session-page"; const PLAN_STEP = "Build the mobile toolbar implement action"; @@ -28,6 +29,14 @@ async function seedMobileTaskWithPlan(testPage: Page, apiClient: ApiClient, seed await expect.poll(() => apiClient.getTaskPlan(task.id), { timeout: 30_000 }).not.toBeNull(); await session.waitForChatIdle({ timeout: 45_000 }); await session.composerReady(); + expect(task.session_id, "task must have a session to await").toBeTruthy(); + await waitForSessionState(apiClient, { + taskId: task.id, + sessionId: task.session_id as string, + expectedState: "WAITING_FOR_INPUT", + message: "the plan session did not settle before enabling the toolbar", + timeout: 30_000, + }); await openMobilePlanPanel(testPage, session); // The implement button is disabled until the panel actually holds the plan. // Two transports can deliver it -- the `task.plan.created` notification @@ -52,7 +61,7 @@ test.describe("mobile: Plan toolbar implement", () => { apiClient, seedData, }) => { - test.setTimeout(120_000); + test.setTimeout(180_000); // Must be armed before the first navigation: Playwright only reports // sockets opened after the listener is attached. const ws = watchWs(testPage); @@ -60,7 +69,7 @@ test.describe("mobile: Plan toolbar implement", () => { const toolbarButton = testPage.getByTestId("plan-toolbar-implement-button"); await expect(toolbarButton).toBeVisible({ timeout: 10_000 }); - await expect(toolbarButton).toBeEnabled(); + await expect(toolbarButton).toBeEnabled({ timeout: 60_000 }); await expect(toolbarButton).toBeInViewport(); const toolbarSpacing = await testPage diff --git a/apps/web/e2e/tests/task/mobile-task-listing-display.spec.ts b/apps/web/e2e/tests/task/mobile-task-listing-display.spec.ts index a8aa3ee288..eec9182d0d 100644 --- a/apps/web/e2e/tests/task/mobile-task-listing-display.spec.ts +++ b/apps/web/e2e/tests/task/mobile-task-listing-display.spec.ts @@ -1,5 +1,6 @@ import { test, expect } from "../../fixtures/test-base"; import { MobileKanbanPage } from "../../pages/mobile-kanban-page"; +import { waitForHttp } from "../../helpers/causal-waits"; const VIEW_STORAGE_KEY = "kandev.taskListing.view.v1"; const TASK_TITLE = "Mobile rich task"; @@ -18,6 +19,20 @@ test.describe("Mobile task listing display preferences", () => { workflow_step_id: seedData.startStepId, repository_ids: [seedData.repositoryId], }); + await expect + .poll( + async () => { + const persistedTask = await apiClient.getTask(task.id); + return persistedTask.repositories?.some( + (repository) => repository.repository_id === seedData.repositoryId, + ); + }, + { + message: "task repository association was not persisted before listing", + timeout: 15_000, + }, + ) + .toBe(true); await apiClient.mockGitHubAssociateTaskPR({ task_id: task.id, owner: "kandev", @@ -41,7 +56,28 @@ test.describe("Mobile task listing display preferences", () => { await expect(testPage).toHaveURL(/\/tasks/); await expect(testPage.getByTestId("tasks-list")).toBeVisible(); + const taskListLoaded = waitForHttp(testPage, "GET", /\/api\/v1\/workspaces\/[^/]+\/tasks$/, { + predicate: async (response) => { + if (response.status() !== 200) return false; + const body = (await response.json()) as { + tasks?: Array<{ + id?: string; + repositories?: Array<{ repository_id?: string }>; + }>; + }; + return ( + body.tasks?.some( + (candidate) => + candidate.id === task.id && + candidate.repositories?.some( + (repository) => repository.repository_id === seedData.repositoryId, + ), + ) ?? false + ); + }, + }); await testPage.goto("/"); + await taskListLoaded; await expect(testPage).toHaveURL(/\/tasks/); await testPage.getByRole("button", { name: "Open menu" }).click(); const tasksMenu = testPage.getByRole("dialog", { name: "Menu" }); diff --git a/apps/web/e2e/tests/task/plan-toolbar-implement.spec.ts b/apps/web/e2e/tests/task/plan-toolbar-implement.spec.ts index 1882ad704b..160dcf998b 100644 --- a/apps/web/e2e/tests/task/plan-toolbar-implement.spec.ts +++ b/apps/web/e2e/tests/task/plan-toolbar-implement.spec.ts @@ -26,7 +26,11 @@ async function seedTaskWithPlan( await testPage.goto(`/t/${task.id}`); const session = new SessionPage(testPage); await session.waitForLoad(); - await expect.poll(() => apiClient.getTaskPlan(task.id), { timeout: 30_000 }).not.toBeNull(); + await expect + .poll(() => apiClient.getTaskPlan(task.id).then((plan) => plan?.content.trim() ?? ""), { + timeout: 30_000, + }) + .not.toBe(""); await session.waitForChatIdle({ timeout: 45_000 }); await session.composerReady(); await openPlanPanel(session); @@ -55,7 +59,7 @@ test.describe("Plan toolbar implement", () => { const toolbarButton = testPage.getByTestId("plan-toolbar-implement-button"); await expect(toolbarButton).toBeVisible({ timeout: 10_000 }); - await expect(toolbarButton).toBeEnabled(); + await expect(toolbarButton).toBeEnabled({ timeout: 30_000 }); const toolbarSpacing = await testPage .getByTestId("plan-toolbar-implement-control") diff --git a/apps/web/e2e/tests/task/sidebar-settled-spinner.spec.ts b/apps/web/e2e/tests/task/sidebar-settled-spinner.spec.ts index 106a8aa4c6..e013f40d8d 100644 --- a/apps/web/e2e/tests/task/sidebar-settled-spinner.spec.ts +++ b/apps/web/e2e/tests/task/sidebar-settled-spinner.spec.ts @@ -63,21 +63,23 @@ test.describe("Sidebar spinner clears when a session settles", () => { await session.waitForLoad(); await expect(session.sidebar).toBeVisible({ timeout: 10_000 }); - const settledTask = await apiClient.createTaskWithAgent( - seedData.workspaceId, - "Settled Turn", - seedData.agentProfileId, - { - description: SETTLE_SCRIPT, - repository_ids: [seedData.repositoryId], - workflow_id: seedData.workflowId, - workflow_step_id: seedData.startStepId, - }, - ); + const settledTask = await apiClient.createTask(seedData.workspaceId, "Settled Turn", { + description: SETTLE_SCRIPT, + agent_profile_id: seedData.agentProfileId, + repository_ids: [seedData.repositoryId], + workflow_id: seedData.workflowId, + workflow_step_id: seedData.startStepId, + }); const settledRow = session.sidebarTaskItem("Settled Turn"); await expect(settledRow).toBeVisible({ timeout: 15_000 }); + // Start only after the live sidebar has observed the task row. Creating + // and starting in one request can settle the short mock turn before the + // client receives the generating task.updated event under CI load. + const ensured = await apiClient.ensureTaskSession(settledTask.id); + expect(ensured.session_id).toBeTruthy(); + // Client must observe generating while the session is still RUNNING. await expect(settledRow.getByTestId("task-state-running")).toBeVisible({ timeout: 30_000, diff --git a/apps/web/e2e/tests/task/task-create-prompt-autocomplete.spec.ts b/apps/web/e2e/tests/task/task-create-prompt-autocomplete.spec.ts index 70a979e4f3..335fa48201 100644 --- a/apps/web/e2e/tests/task/task-create-prompt-autocomplete.spec.ts +++ b/apps/web/e2e/tests/task/task-create-prompt-autocomplete.spec.ts @@ -51,7 +51,9 @@ test.describe("Task creation: custom prompt autocomplete", () => { const menu = testPage.getByText(MENU_TITLE); await expect(menu).toBeVisible({ timeout: 5_000 }); - await expect(testPage.getByRole("option", { name: new RegExp(PROMPT_NAME) })).toBeVisible(); + const promptOption = testPage.getByRole("option").filter({ hasText: PROMPT_NAME }); + await expect(promptOption).toBeVisible(); + await expect(promptOption).toHaveAttribute("aria-selected", "true"); await textarea.press("Enter"); @@ -104,9 +106,14 @@ test.describe("Task creation: custom prompt autocomplete", () => { // pressing Enter against an empty/half-open menu lets the key fall through // to the form submit, which closes the dialog and fails the assertions // below. Gating on the option (not just the title) is the condition that - // makes the selection deterministic. + // makes the selection deterministic. Also wait for the filtered result to + // become the active item: the menu opens on the bare `@` trigger before + // the async frame that applies the typed query, so an early Enter could + // still select the first built-in prompt. await expect(testPage.getByText(MENU_TITLE)).toBeVisible(); - await expect(testPage.getByRole("option", { name: new RegExp(PROMPT_NAME) })).toBeVisible(); + const promptOption = testPage.getByRole("option").filter({ hasText: PROMPT_NAME }); + await expect(promptOption).toBeVisible(); + await expect(promptOption).toHaveAttribute("aria-selected", "true"); await textarea.press("Enter"); // Dialog must still be open — Enter selected the menu item, not the form submit. diff --git a/apps/web/e2e/tests/task/task-switcher-status.spec.ts b/apps/web/e2e/tests/task/task-switcher-status.spec.ts index 119a358ef1..d65694e2ab 100644 --- a/apps/web/e2e/tests/task/task-switcher-status.spec.ts +++ b/apps/web/e2e/tests/task/task-switcher-status.spec.ts @@ -78,6 +78,12 @@ test.describe("Task switcher sidebar status", () => { }); // --- Complete Alpha first, then navigate to its session --- + // Prepare the session while Alpha is still in Inbox. Moving a task into an + // auto-start step can otherwise race session creation with the first + // kanban navigation under CI load, leaving the task in Working without an + // agent turn to complete it. + const preparedAlpha = await apiClient.ensureTaskSession(taskAlpha.id); + expect(preparedAlpha.session_id).toBeTruthy(); await apiClient.moveTask(taskAlpha.id, workflow.id, workingStep.id); const kanban = new KanbanPage(testPage); diff --git a/apps/web/e2e/tests/terminal/terminal-agent.spec.ts b/apps/web/e2e/tests/terminal/terminal-agent.spec.ts index a8d0625131..c4a7c645fa 100644 --- a/apps/web/e2e/tests/terminal/terminal-agent.spec.ts +++ b/apps/web/e2e/tests/terminal/terminal-agent.spec.ts @@ -1,6 +1,7 @@ import { test, expect } from "../../fixtures/test-base"; import type { SeedData } from "../../fixtures/test-base"; import type { ApiClient } from "../../helpers/api-client"; +import { waitForSessionState } from "../../helpers/session"; import { KanbanPage } from "../../pages/kanban-page"; import { SessionPage } from "../../pages/session-page"; import { errors, type Page } from "@playwright/test"; @@ -268,6 +269,18 @@ test.describe("Terminal agent (TUI passthrough)", () => { const session = await openTaskSession(testPage, "TUI Reset Task"); await session.expectPassthroughHasText("Mock Agent"); + // The terminal header can render before the auto-started boot turn ends. + // Wait for the backend lifecycle state before moving the task. Otherwise + // the workflow reset can race with that first PTY turn under CI load. + expect(task.session_id, "task must have a session to await").toBeTruthy(); + await waitForSessionState(apiClient, { + taskId: task.id, + sessionId: task.session_id as string, + expectedState: "WAITING_FOR_INPUT", + message: "the initial passthrough turn did not settle before the cascade", + timeout: 30_000, + }); + // Trigger cascade: Analyze → (turn complete) → Implement (reset + auto_start) → Review await apiClient.moveTask(task.id, workflow.id, analyzeStep.id); diff --git a/apps/web/e2e/tests/terminal/terminal-new-tab.spec.ts b/apps/web/e2e/tests/terminal/terminal-new-tab.spec.ts index d6fc323329..3e0cacadb2 100644 --- a/apps/web/e2e/tests/terminal/terminal-new-tab.spec.ts +++ b/apps/web/e2e/tests/terminal/terminal-new-tab.spec.ts @@ -25,13 +25,15 @@ async function createTaskAndWaitForDone(apiClient: ApiClient, seedData: SeedData }, ); + if (!task.session_id) throw new Error(`${title} task did not return a session id`); await expect .poll( async () => { const { sessions } = await apiClient.listTaskSessions(task.id); - return DONE_STATES.includes(sessions[0]?.state ?? ""); + const session = sessions.find((candidate) => candidate.id === task.session_id); + return DONE_STATES.includes(session?.state ?? ""); }, - { timeout: 30_000, message: `Waiting for ${title} session to settle` }, + { timeout: 60_000, message: `Waiting for ${title} session to settle` }, ) .toBe(true); diff --git a/apps/web/e2e/tests/workflow/workflow-automation.spec.ts b/apps/web/e2e/tests/workflow/workflow-automation.spec.ts index 13d2b4afdd..65dc1cf172 100644 --- a/apps/web/e2e/tests/workflow/workflow-automation.spec.ts +++ b/apps/web/e2e/tests/workflow/workflow-automation.spec.ts @@ -231,7 +231,7 @@ test.describe("Workflow automation", () => { // explicit budget as the assertions above — on the default expect timeout // it loses the race under a loaded 14-shard run while passing locally. await expect(session.taskInSection("Plan Mode Workflow Task", "Turn Finished")).toBeVisible({ - timeout: 15_000, + timeout: 60_000, }); }); diff --git a/apps/web/e2e/tests/workflow/workflow-cycle-guardrails.spec.ts b/apps/web/e2e/tests/workflow/workflow-cycle-guardrails.spec.ts index 2e44d5eacc..787ad20dba 100644 --- a/apps/web/e2e/tests/workflow/workflow-cycle-guardrails.spec.ts +++ b/apps/web/e2e/tests/workflow/workflow-cycle-guardrails.spec.ts @@ -63,7 +63,10 @@ test.describe("Workflow cycle guardrails", () => { persistedSteps.steps.find((step) => step.id === review.id)?.events?.on_turn_complete, ).toBeFalsy(); - await settings.saveChanges(); + // The backend poll below is the authoritative save boundary. The + // floating indicator can retain a dirty contributor while another + // settings contributor finishes its own render update. + await settings.submitSaveChanges(); await expect .poll(async () => { persistedSteps = await apiClient.listWorkflowSteps(workflow.id); diff --git a/apps/web/e2e/tests/workflow/workflow-queue-helpers.ts b/apps/web/e2e/tests/workflow/workflow-queue-helpers.ts index 50f863eb26..3fb4d72a81 100644 --- a/apps/web/e2e/tests/workflow/workflow-queue-helpers.ts +++ b/apps/web/e2e/tests/workflow/workflow-queue-helpers.ts @@ -30,7 +30,7 @@ export async function seedQueuedWorkflowMessageScenario( `${name} Task`, seedData.agentProfileId, { - description: 'e2e:delay(8000)\ne2e:message("initial response")', + description: 'e2e:delay(20000)\ne2e:message("initial response")', workflow_id: workflow.id, workflow_step_id: sourceStep.id, repository_ids: [seedData.repositoryId], @@ -46,7 +46,7 @@ export async function seedQueuedWorkflowMessageScenario( // on the client receiving the state over WS, and under the WS-subscribe race the // client can miss it when its subscription registers after the transition fans // out; the backend session state is the source of truth and avoids that race. - // The e2e:delay(8000) keeps the first turn busy long enough to observe a STARTING + // The e2e:delay(20000) keeps the first turn busy long enough to observe a STARTING // or RUNNING state (the mock agent can jump STARTING->WAITING_FOR_INPUT without // ever surfacing RUNNING, so accept either busy state). await expect @@ -85,15 +85,22 @@ export async function expectDeliveredWorkflowMessage( await expect .poll( async () => { - const { messages } = await apiClient.listSessionMessages(sessionId); - return messages.some( + const [queue, { messages }] = await Promise.all([ + apiClient.getQueueStatus(sessionId), + apiClient.listSessionMessages(sessionId), + ]); + const workflowMessageExists = messages.some( (message) => message.author_type === "user" && message.metadata?.workflow_message === true && message.metadata?.workflow_step_name === WORKFLOW_REVIEW_STEP, ); + const workflowResponseExists = messages.some((message) => + message.content.includes("workflow queued response"), + ); + return workflowMessageExists && workflowResponseExists && queue.count === 0; }, - { timeout: 30_000 }, + { timeout: 30_000, message: "Waiting for the queued workflow message to be delivered" }, ) .toBe(true); diff --git a/apps/web/e2e/tests/workflow/workflow-settings.spec.ts b/apps/web/e2e/tests/workflow/workflow-settings.spec.ts index 094f6ffcca..28b67b0179 100644 --- a/apps/web/e2e/tests/workflow/workflow-settings.spec.ts +++ b/apps/web/e2e/tests/workflow/workflow-settings.spec.ts @@ -38,7 +38,7 @@ test.describe("Workflow settings", () => { // Template data can contain the same step name more than once. The // workflow card is the scope under test; any matching rendered step is // sufficient and avoids a strict-mode race while the card hydrates. - await expect(card.getByText(step.name).first()).toBeVisible(); + await expect(card.getByText(step.name, { exact: true }).first()).toBeVisible(); } }); diff --git a/apps/web/hooks/domains/kanban/use-all-workflow-snapshots-inflight.test.ts b/apps/web/hooks/domains/kanban/use-all-workflow-snapshots-inflight.test.ts index 1ef666ca01..8f37741aa3 100644 --- a/apps/web/hooks/domains/kanban/use-all-workflow-snapshots-inflight.test.ts +++ b/apps/web/hooks/domains/kanban/use-all-workflow-snapshots-inflight.test.ts @@ -9,6 +9,11 @@ type SnapshotTask = { position: number; state: "IN_PROGRESS"; parentTaskId?: string; + statusSummary?: { + revision: number; + updated_at: string; + primary_session?: { id: string; state: "RUNNING" | "WAITING_FOR_INPUT" }; + }; }; type MockState = { connection: { status: string }; @@ -36,7 +41,9 @@ type MockState = { const WORKFLOW_ID = "wf-A"; const WORKSPACE_ID = "ws-A"; const STEP_ID = "step-1"; +const LIVE_STEP_ID = "step-2"; const PARENT_TASK_ID = "parent-task"; +const LIVE_PLACEMENT_TASK_ID = "task-with-live-placement"; const mocks = vi.hoisted(() => ({ clearKanbanMulti: vi.fn(), @@ -174,4 +181,124 @@ describe("useAllWorkflowSnapshots in-flight websocket tasks", () => { ), ); }); + + it("keeps a newer live status summary when an older snapshot finishes later", async () => { + resetState(); + let resolveFetch: (value: unknown) => void = () => {}; + mocks.fetchWorkflowSnapshot.mockReturnValueOnce( + new Promise((resolve) => { + resolveFetch = resolve; + }), + ); + + renderHook(() => useAllWorkflowSnapshots(WORKSPACE_ID)); + await waitFor(() => + expect(mocks.fetchWorkflowSnapshot).toHaveBeenCalledWith(WORKFLOW_ID, expect.anything()), + ); + + setLightweightSnapshot({ + id: "task-with-live-status", + workflowStepId: STEP_ID, + title: "Live status", + position: 0, + state: "IN_PROGRESS", + statusSummary: { + revision: 4, + updated_at: "2026-08-13T00:00:04Z", + primary_session: { id: "session-1", state: "WAITING_FOR_INPUT" }, + }, + }); + resolveFetch({ + steps: [{ id: STEP_ID, name: "Doing", color: null, position: 0 }], + tasks: [ + { + id: "task-with-live-status", + workflow_step_id: STEP_ID, + title: "Live status", + position: 0, + state: "IN_PROGRESS", + status_summary: { + revision: 3, + updated_at: "2026-08-13T00:00:03Z", + primary_session: { id: "session-1", state: "RUNNING" }, + }, + }, + ], + }); + + await waitFor(() => expect(mocks.setWorkflowSnapshot).toHaveBeenCalled()); + expect(mocks.setWorkflowSnapshot).toHaveBeenCalledWith( + WORKFLOW_ID, + expect.objectContaining({ + tasks: [ + expect.objectContaining({ + id: "task-with-live-status", + statusSummary: expect.objectContaining({ revision: 4 }), + }), + ], + }), + ); + }); +}); + +describe("useAllWorkflowSnapshots live task placement", () => { + it("keeps a newer live task placement when an older snapshot finishes later", async () => { + resetState(); + let resolveFetch: (value: unknown) => void = () => {}; + mocks.fetchWorkflowSnapshot.mockReturnValueOnce( + new Promise((resolve) => { + resolveFetch = resolve; + }), + ); + + setLightweightSnapshot({ + id: LIVE_PLACEMENT_TASK_ID, + workflowStepId: STEP_ID, + title: "Live placement", + position: 0, + state: "IN_PROGRESS", + }); + + renderHook(() => useAllWorkflowSnapshots(WORKSPACE_ID)); + await waitFor(() => + expect(mocks.fetchWorkflowSnapshot).toHaveBeenCalledWith(WORKFLOW_ID, expect.anything()), + ); + + setLightweightSnapshot({ + id: LIVE_PLACEMENT_TASK_ID, + workflowStepId: LIVE_STEP_ID, + title: "Live placement", + position: 2, + state: "IN_PROGRESS", + }); + resolveFetch({ + steps: [ + { id: STEP_ID, name: "Doing", color: null, position: 0 }, + { id: LIVE_STEP_ID, name: "Done", color: null, position: 1 }, + ], + tasks: [ + { + id: LIVE_PLACEMENT_TASK_ID, + workflow_step_id: STEP_ID, + title: "Live placement", + position: 0, + state: "IN_PROGRESS", + }, + ], + }); + + await waitFor(() => expect(mocks.setWorkflowSnapshot).toHaveBeenCalled()); + expect(mocks.setWorkflowSnapshot).toHaveBeenCalledWith( + WORKFLOW_ID, + expect.objectContaining({ + tasks: [ + expect.objectContaining({ + id: LIVE_PLACEMENT_TASK_ID, + workflowStepId: LIVE_STEP_ID, + position: 2, + }), + ], + }), + ); + }); }); diff --git a/apps/web/hooks/domains/kanban/use-all-workflow-snapshots.ts b/apps/web/hooks/domains/kanban/use-all-workflow-snapshots.ts index 8db40d4f21..30a583552a 100644 --- a/apps/web/hooks/domains/kanban/use-all-workflow-snapshots.ts +++ b/apps/web/hooks/domains/kanban/use-all-workflow-snapshots.ts @@ -18,6 +18,82 @@ function isBootHydratedSnapshot(snapshot: WorkflowSnapshotData | undefined): boo return !!snapshot && snapshot.isPlaceholder !== true; } +function hasNewerLivePlacement(existing: KanbanTask, fetchStart: KanbanTask | undefined): boolean { + return ( + fetchStart !== undefined && + (existing.workflowId !== fetchStart.workflowId || + existing.workflowStepId !== fetchStart.workflowStepId || + existing.position !== fetchStart.position) + ); +} + +function mergeFetchedTask( + mapped: KanbanTask, + existing: KanbanTask, + fetchStart: KanbanTask | undefined, +): KanbanTask { + if (hasNewerLivePlacement(existing, fetchStart)) { + // A live task.updated/task.moved event arrived after this request + // started. Keep the newer placement instead of moving the card + // back to the location captured by the older snapshot response. + mapped.workflowId = existing.workflowId; + mapped.workflowStepId = existing.workflowStepId; + mapped.position = existing.position; + } + + // Multiple mounted surfaces can refresh the same workflow at once. + // A response that started before a live WS status update may finish + // afterwards, so do not let an older snapshot roll the status + // projection back to a lower revision. + const existingRevision = existing.statusSummary?.revision; + const mappedRevision = mapped.statusSummary?.revision; + if (existingRevision !== undefined && existingRevision > (mappedRevision ?? -1)) { + mapped.statusSummary = existing.statusSummary; + } + // An equal revision can still carry a newer queued-prompt count. Preserve + // the freshest status projection while keeping the revision guard above. + mapped.statusSummary = pickFreshestStatusSummary(mapped.statusSummary, existing.statusSummary); + mapped.primarySessionId = mapped.primarySessionId || existing.primarySessionId; + mapped.primarySessionState = mapped.primarySessionState || existing.primarySessionState; + // Autopilot is immutable after creation. Keep the cached value when + // an older or partial snapshot does not include the field. + mapped.autopilot = mapped.autopilot ?? existing.autopilot; + return mapped; +} + +function mergeSnapshotTasks( + snapshotTasks: Task[], + stepIds: Set, + snapshotAtFetchStart: WorkflowSnapshotData | undefined, + existingSnapshot: WorkflowSnapshotData | undefined, +): KanbanTask[] { + const taskIdsAtFetchStart = new Set((snapshotAtFetchStart?.tasks ?? []).map((t) => t.id)); + const existingById = new Map((existingSnapshot?.tasks ?? []).map((t) => [t.id, t])); + const fetchStartById = new Map( + (snapshotAtFetchStart?.tasks ?? []).map((task) => [task.id, task]), + ); + + const tasks = snapshotTasks + .filter((task) => !task.is_ephemeral) + .map((task) => { + const mapped = mapSnapshotTask(task, stepIds); + if (!mapped) return null; + const existing = existingById.get(mapped.id); + return existing ? mergeFetchedTask(mapped, existing, fetchStartById.get(mapped.id)) : mapped; + }) + .filter((t): t is KanbanTask => t !== null); + const snapshotTaskIds = new Set(tasks.map((t) => t.id)); + const preserveExistingPlaceholderTasks = snapshotAtFetchStart?.isPlaceholder === true; + const inFlightCreatedTasks = (existingSnapshot?.tasks ?? []).filter( + (task) => + (preserveExistingPlaceholderTasks || !taskIdsAtFetchStart.has(task.id)) && + !snapshotTaskIds.has(task.id) && + stepIds.has(task.workflowStepId), + ); + + return [...tasks, ...inFlightCreatedTasks]; +} + async function fetchAndWriteSnapshot( wf: Workflow, store: StoreApi, @@ -27,7 +103,6 @@ async function fetchAndWriteSnapshot( ): Promise { try { const snapshotAtFetchStart = store.getState().kanbanMulti.snapshots[wf.id]; - const taskIdsAtFetchStart = new Set((snapshotAtFetchStart?.tasks ?? []).map((t) => t.id)); const snapshot = await fetchWorkflowSnapshot(wf.id, { cache: "no-store" }); if ( fetchGenRef.current !== myGen || @@ -53,51 +128,19 @@ async function fetchAndWriteSnapshot( })); const stepIds = new Set(steps.map((s) => s.id)); - // Preserve runtime fields (e.g., primarySessionId) from existing snapshot - // tasks when the fresh API response omits them (backend uses omitempty). const existingSnapshot = store.getState().kanbanMulti.snapshots[wf.id]; - const existingById = new Map((existingSnapshot?.tasks ?? []).map((t) => [t.id, t])); - - const tasks: KanbanTask[] = snapshot.tasks - .filter((task) => !task.is_ephemeral) - .map((task) => { - const mapped = mapSnapshotTask(task, stepIds); - if (!mapped) return null; - const existing = existingById.get(mapped.id); - if (existing) { - mapped.primarySessionId = mapped.primarySessionId || existing.primarySessionId; - mapped.primarySessionState = mapped.primarySessionState || existing.primarySessionState; - // Autopilot is immutable after creation. Keep the cached value when - // an older or partial snapshot does not include the field. - mapped.autopilot = mapped.autopilot ?? existing.autopilot; - // This response was issued before it landed, so its status summary can - // be older than a `task.status_summary.updated` delta already applied - // to the cache. Taking it unconditionally regresses the row, and a - // settled task emits no further deltas to repair it. An equal revision - // still wins: the response re-stamps `queued_prompt_count` outside the - // revision (see pickFreshestStatusSummary). - mapped.statusSummary = pickFreshestStatusSummary( - mapped.statusSummary, - existing.statusSummary, - ); - } - return mapped; - }) - .filter((t): t is KanbanTask => t !== null); - const snapshotTaskIds = new Set(tasks.map((t) => t.id)); - const preserveExistingPlaceholderTasks = snapshotAtFetchStart?.isPlaceholder === true; - const inFlightCreatedTasks = (existingSnapshot?.tasks ?? []).filter( - (task) => - (preserveExistingPlaceholderTasks || !taskIdsAtFetchStart.has(task.id)) && - !snapshotTaskIds.has(task.id) && - stepIds.has(task.workflowStepId), + const tasks = mergeSnapshotTasks( + snapshot.tasks, + stepIds, + snapshotAtFetchStart, + existingSnapshot, ); const workflowSnapshot = { workflowId: wf.id, workflowName: wf.name, steps, - tasks: [...tasks, ...inFlightCreatedTasks], + tasks, }; store.getState().setWorkflowSnapshot(wf.id, workflowSnapshot); const activeKanban = store.getState().kanban; diff --git a/apps/web/hooks/domains/session/use-workspace-content-search.test.ts b/apps/web/hooks/domains/session/use-workspace-content-search.test.ts index 38faa47677..92bed8a030 100644 --- a/apps/web/hooks/domains/session/use-workspace-content-search.test.ts +++ b/apps/web/hooks/domains/session/use-workspace-content-search.test.ts @@ -49,9 +49,10 @@ describe("useWorkspaceContentSearch request lifecycle", () => { await act(async () => vi.advanceTimersByTimeAsync(249)); expect(mockSearchWorkspaceContent).not.toHaveBeenCalled(); - await act(async () => vi.advanceTimersByTimeAsync(1)); + await act(async () => vi.runAllTimersAsync()); expect(mockSearchWorkspaceContent).toHaveBeenCalledWith(mockClient, "session-1", "needle", 50); + expect(mockSearchWorkspaceContent).toHaveBeenCalledTimes(8); expect(result.current.results).toEqual(results); expect(result.current.isSearching).toBe(false); }); @@ -67,14 +68,12 @@ describe("useWorkspaceContentSearch request lifecycle", () => { match_ranges: [], }, ]; - mockSearchWorkspaceContent - .mockResolvedValueOnce({ results: previousResults }) - .mockResolvedValueOnce({ results: [] }); + mockSearchWorkspaceContent.mockResolvedValue({ results: previousResults }); const { result, rerender } = renderHook( ({ query }) => useWorkspaceContentSearch({ enabled: true, query, sessionId: "session-1" }), { initialProps: { query: "previous" } }, ); - await act(async () => vi.advanceTimersByTimeAsync(250)); + await act(async () => vi.runAllTimersAsync()); expect(result.current.results).toEqual(previousResults); rerender({ query: "next" }); @@ -138,12 +137,46 @@ describe("useWorkspaceContentSearch validation and failures", () => { }), ); - await act(async () => vi.advanceTimersByTimeAsync(250)); + await act(async () => vi.runAllTimersAsync()); expect(result.current.results).toEqual([]); expect(result.current.error).toBe("transport-error"); expect(result.current.isSearching).toBe(false); }); + + it("merges repository results that arrive on later retry attempts", async () => { + const primary = { + repository_name: "primary", + path: "src/primary.ts", + line: 1, + column: 1, + preview: "primary needle", + match_ranges: [], + }; + const extra = { + repository_name: "extra", + path: "src/extra.ts", + line: 2, + column: 1, + preview: "extra needle", + match_ranges: [], + }; + mockSearchWorkspaceContent + .mockResolvedValueOnce({ results: [primary] }) + .mockResolvedValueOnce({ results: [primary, extra] }) + .mockResolvedValue({ results: [primary, extra] }); + const { result } = renderHook(() => + useWorkspaceContentSearch({ + enabled: true, + query: "needle", + sessionId: "session-1", + }), + ); + + await act(async () => vi.runAllTimersAsync()); + + expect(result.current.results).toEqual([primary, extra]); + }); }); describe("useWorkspaceContentSearch stale responses", () => { @@ -186,8 +219,9 @@ describe("useWorkspaceContentSearch stale responses", () => { rerender({ query: "second" }); await act(async () => vi.advanceTimersByTimeAsync(250)); await act(async () => resolveFirst?.({ results: firstResults })); + await act(async () => vi.runAllTimersAsync()); - expect(mockSearchWorkspaceContent).toHaveBeenCalledTimes(2); + expect(mockSearchWorkspaceContent).toHaveBeenCalledTimes(9); expect(result.current.results).toEqual(secondResults); expect(result.current.isSearching).toBe(false); }); diff --git a/apps/web/hooks/domains/session/use-workspace-content-search.ts b/apps/web/hooks/domains/session/use-workspace-content-search.ts index 08007ea79d..c570b01792 100644 --- a/apps/web/hooks/domains/session/use-workspace-content-search.ts +++ b/apps/web/hooks/domains/session/use-workspace-content-search.ts @@ -6,6 +6,8 @@ import { getWebSocketClient } from "@/lib/ws/connection"; import { searchWorkspaceContent } from "@/lib/ws/workspace-files"; const CONTENT_SEARCH_DEBOUNCE_MS = 250; +const CONTENT_SEARCH_RETRY_ATTEMPTS = 8; +const CONTENT_SEARCH_RETRY_DELAY_MS = 500; const CONTENT_SEARCH_LIMIT_PER_REPO = 50; const MAX_CONTENT_SEARCH_CODE_POINTS = 200; @@ -20,6 +22,45 @@ type WorkspaceContentSearchOptions = { sessionId: string | null; }; +function contentSearchResultKey(result: WorkspaceContentSearchResult): string { + return [result.repository_name, result.path, result.line, result.column].join("\u0000"); +} + +async function searchWorkspaceContentWithRetries( + sessionId: string, + query: string, + isCancelled: () => boolean, +): Promise { + const client = getWebSocketClient(); + if (!client) throw new Error("websocket transport unavailable"); + + const results = new Map(); + let lastError: unknown; + for (let attempt = 0; attempt < CONTENT_SEARCH_RETRY_ATTEMPTS; attempt += 1) { + if (isCancelled()) return []; + if (attempt > 0) { + await new Promise((resolve) => setTimeout(resolve, CONTENT_SEARCH_RETRY_DELAY_MS)); + if (isCancelled()) return []; + } + try { + const response = await searchWorkspaceContent( + client, + sessionId, + query, + CONTENT_SEARCH_LIMIT_PER_REPO, + ); + for (const result of response.results ?? []) { + results.set(contentSearchResultKey(result), result); + } + } catch (error) { + lastError = error; + } + } + + if (results.size === 0 && lastError) throw lastError; + return [...results.values()]; +} + export function useWorkspaceContentSearch({ enabled, query, @@ -61,25 +102,14 @@ export function useWorkspaceContentSearch({ setIsSearching(true); setError(null); const timer = setTimeout(async () => { - const client = getWebSocketClient(); - if (!client) { - if (!cancelled) { - setResults([]); - setError("transport-error"); - setIsSearching(false); - } - return; - } - try { - const response = await searchWorkspaceContent( - client, + const nextResults = await searchWorkspaceContentWithRetries( sessionId, normalizedQuery, - CONTENT_SEARCH_LIMIT_PER_REPO, + () => cancelled, ); if (!cancelled) { - setResults(response.results ?? []); + setResults(nextResults); setError(null); } } catch { diff --git a/apps/web/hooks/domains/settings/use-settings-data.test.tsx b/apps/web/hooks/domains/settings/use-settings-data.test.tsx new file mode 100644 index 0000000000..0241720d23 --- /dev/null +++ b/apps/web/hooks/domains/settings/use-settings-data.test.tsx @@ -0,0 +1,82 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { createElement, type ReactNode } from "react"; +import { act, cleanup, renderHook } from "@testing-library/react"; +import { StateProvider, useAppStore } from "@/components/state-provider"; + +const mocks = vi.hoisted(() => ({ + listAgents: vi.fn(), + listAvailableAgents: vi.fn(), + listExecutors: vi.fn(), +})); + +vi.mock("@/lib/api", () => ({ + listAgents: mocks.listAgents, + listAvailableAgents: mocks.listAvailableAgents, + listExecutors: mocks.listExecutors, +})); + +import { useSettingsData } from "./use-settings-data"; + +function wrapper({ children }: { children: ReactNode }) { + return createElement(StateProvider, null, children); +} + +function useSettingsSnapshot() { + useSettingsData(); + const agentsLoaded = useAppStore((state) => state.settingsData.agentsLoaded); + const agentProfiles = useAppStore((state) => state.agentProfiles.items); + return { agentsLoaded, agentProfiles }; +} + +beforeEach(() => { + vi.useFakeTimers(); + mocks.listAgents.mockReset(); + mocks.listAvailableAgents.mockReset().mockReturnValue(new Promise(() => {})); + mocks.listExecutors.mockReset().mockResolvedValue({ executors: [] }); +}); + +afterEach(() => { + cleanup(); + vi.useRealTimers(); +}); + +describe("useSettingsData", () => { + it("retries an empty agent list before declaring that no profiles exist", async () => { + const profile = { + id: "profile-1", + agentDisplayName: "Mock Agent", + name: "Default", + cliPassthrough: false, + }; + mocks.listAgents.mockResolvedValueOnce({ agents: [], total: 0 }).mockResolvedValueOnce({ + agents: [{ id: "agent-1", name: "Mock Agent", profiles: [profile] }], + total: 1, + }); + + const { result } = renderHook(() => useSettingsSnapshot(), { wrapper }); + + await act(async () => { + await Promise.resolve(); + await vi.advanceTimersByTimeAsync(100); + await Promise.resolve(); + }); + + expect(mocks.listAgents).toHaveBeenCalledTimes(2); + expect(result.current.agentsLoaded).toBe(true); + expect(result.current.agentProfiles[0]?.id).toBe("profile-1"); + }); + + it("uses the final attempt after exhausting every retry delay", async () => { + mocks.listAgents.mockResolvedValue({ agents: [], total: 0 }); + + const { result } = renderHook(() => useSettingsSnapshot(), { wrapper }); + + await act(async () => { + await vi.runAllTimersAsync(); + }); + + expect(mocks.listAgents).toHaveBeenCalledTimes(5); + expect(result.current.agentsLoaded).toBe(true); + expect(result.current.agentProfiles).toEqual([]); + }); +}); diff --git a/apps/web/hooks/domains/settings/use-settings-data.ts b/apps/web/hooks/domains/settings/use-settings-data.ts index 0d0e4dcbd9..528d8cc431 100644 --- a/apps/web/hooks/domains/settings/use-settings-data.ts +++ b/apps/web/hooks/domains/settings/use-settings-data.ts @@ -3,6 +3,53 @@ import { useAppStore } from "@/components/state-provider"; import { listAgents, listAvailableAgents, listExecutors } from "@/lib/api"; import { toAgentProfileOption } from "@/lib/state/slices/settings/types"; +const AGENT_LIST_RETRY_DELAYS_MS = [100, 250, 500, 1_000] as const; + +type AgentListResponse = Awaited>; + +function hasAgentProfiles(response: AgentListResponse): boolean { + return response.agents.some((agent) => (agent.profiles?.length ?? 0) > 0); +} + +function waitForAgentListRetry(delayMs: number): Promise { + return new Promise((resolve) => setTimeout(resolve, delayMs)); +} + +/** + * Agent discovery and profile persistence complete before the backend health + * endpoint becomes ready, but a freshly started client can still observe an + * empty list while that state is settling. Keep the settings surface loading + * briefly instead of treating that transient response as the authoritative + * "no agents" state. + */ +export async function listAgentsUntilSettled(): Promise { + for (const retryDelayMs of AGENT_LIST_RETRY_DELAYS_MS) { + try { + const response = await listAgents({ cache: "no-store" }); + if (hasAgentProfiles(response)) { + return response; + } + } catch {} + + await waitForAgentListRetry(retryDelayMs); + } + + return listAgents({ cache: "no-store" }); +} + +function applyAgentList( + response: AgentListResponse, + setSettingsAgents: (agents: AgentListResponse["agents"]) => void, + setAgentProfiles: (profiles: ReturnType[]) => void, +): void { + setSettingsAgents(response.agents); + setAgentProfiles( + response.agents.flatMap((agent) => + agent.profiles.map((profile) => toAgentProfileOption(agent, profile)), + ), + ); +} + export function useSettingsData(enabled = true) { const executors = useAppStore((state) => state.executors.items); const settingsAgents = useAppStore((state) => state.settingsAgents.items); @@ -32,15 +79,8 @@ export function useSettingsData(enabled = true) { if (!enabled) return; if (settingsData.agentsLoaded) return; if (settingsAgents.length === 0) { - listAgents({ cache: "no-store" }) - .then((response) => { - setSettingsAgents(response.agents); - setAgentProfiles( - response.agents.flatMap((agent) => - agent.profiles.map((profile) => toAgentProfileOption(agent, profile)), - ), - ); - }) + listAgentsUntilSettled() + .then((response) => applyAgentList(response, setSettingsAgents, setAgentProfiles)) .catch(() => { setSettingsAgents([]); setAgentProfiles([]); @@ -90,15 +130,8 @@ export function useSettingsData(enabled = true) { if (!settingsData.agentsLoaded) return; // Wait for the initial agents fetch first. if (reconciledRef.current) return; reconciledRef.current = true; - listAgents({ cache: "no-store" }) - .then((response) => { - setSettingsAgents(response.agents); - setAgentProfiles( - response.agents.flatMap((agent) => - agent.profiles.map((profile) => toAgentProfileOption(agent, profile)), - ), - ); - }) + listAgentsUntilSettled() + .then((response) => applyAgentList(response, setSettingsAgents, setAgentProfiles)) .catch(() => { // Best-effort reconcile; keep prior (possibly stale) profiles rather // than wiping the dialog state on a transient error. diff --git a/apps/web/hooks/domains/workspace/use-repositories.test.ts b/apps/web/hooks/domains/workspace/use-repositories.test.ts index dba316b38f..8e1f0e85f7 100644 --- a/apps/web/hooks/domains/workspace/use-repositories.test.ts +++ b/apps/web/hooks/domains/workspace/use-repositories.test.ts @@ -68,6 +68,20 @@ describe("useRepositories", () => { expect(mockListRepositories).toHaveBeenCalledWith("ws-1", undefined, { cache: "no-store" }); }); + it("retries a transient initial fetch failure", async () => { + setup(/* loaded */ false); + mockListRepositories + .mockRejectedValueOnce(new Error("temporary failure")) + .mockResolvedValueOnce({ repositories: [{ id: "r2", name: "Retry Repo" }] }); + + renderHook(() => useRepositories("ws-1", true, false)); + + await waitFor(() => + expect(mockSetRepositories).toHaveBeenCalledWith("ws-1", [{ id: "r2", name: "Retry Repo" }]), + ); + expect(mockListRepositories).toHaveBeenCalledTimes(2); + }); + it("does nothing when disabled or workspace is null", async () => { setup(/* loaded */ false); renderHook(() => useRepositories(null, true, true)); @@ -88,7 +102,7 @@ describe("useRepositories", () => { it("keeps cached repositories when a manual refresh fails", async () => { setup(/* loaded */ true); - mockListRepositories.mockRejectedValueOnce(new Error("Network unavailable")); + mockListRepositories.mockRejectedValue(new Error("Network unavailable")); const { result } = renderHook(() => useRepositories("ws-1")); await expect(result.current.refresh()).resolves.toBeUndefined(); diff --git a/apps/web/hooks/domains/workspace/use-repositories.ts b/apps/web/hooks/domains/workspace/use-repositories.ts index 38cb524d7d..b112f64ade 100644 --- a/apps/web/hooks/domains/workspace/use-repositories.ts +++ b/apps/web/hooks/domains/workspace/use-repositories.ts @@ -4,6 +4,18 @@ import type { Repository } from "@/lib/types/http"; import { listRepositories } from "@/lib/api"; const EMPTY_REPOSITORIES: Repository[] = []; +const REPOSITORY_LIST_RETRY_DELAYS_MS = [100, 250, 500, 1_000] as const; + +async function listRepositoriesUntilSettled(workspaceId: string) { + for (const retryDelayMs of REPOSITORY_LIST_RETRY_DELAYS_MS) { + try { + return await listRepositories(workspaceId, undefined, { cache: "no-store" }); + } catch { + await new Promise((resolve) => setTimeout(resolve, retryDelayMs)); + } + } + return listRepositories(workspaceId, undefined, { cache: "no-store" }); +} /** * Loads a workspace's repositories from the store, fetching once when not yet @@ -37,7 +49,7 @@ export function useRepositories(workspaceId: string | null, enabled = true, forc if (!enabled || !workspaceId) return; setRepositoriesLoading(workspaceId, true); try { - const response = await listRepositories(workspaceId, undefined, { cache: "no-store" }); + const response = await listRepositoriesUntilSettled(workspaceId); setRepositories(workspaceId, response.repositories); } catch { // Keep the existing cached repositories when a manual refresh fails. @@ -60,7 +72,7 @@ export function useRepositories(workspaceId: string | null, enabled = true, forc if (forcedRef.current === workspaceId) return; let cancelled = false; setRepositoriesLoading(workspaceId, true); - listRepositories(workspaceId, undefined, { cache: "no-store" }) + listRepositoriesUntilSettled(workspaceId) .then((response) => { if (cancelled) return; forcedRef.current = workspaceId; @@ -83,14 +95,14 @@ export function useRepositories(workspaceId: string | null, enabled = true, forc if (isLoaded) return; let cancelled = false; setRepositoriesLoading(workspaceId, true); - listRepositories(workspaceId, undefined, { cache: "no-store" }) + listRepositoriesUntilSettled(workspaceId) .then((response) => { if (cancelled) return; setRepositories(workspaceId, response.repositories); }) .catch(() => { - if (cancelled) return; - setRepositories(workspaceId, []); + // Keep the cache unloaded after a failed request. The next dialog + // mount can retry instead of treating an empty fallback as real. }) .finally(() => { if (cancelled) return; diff --git a/apps/web/hooks/domains/workspace/use-repository-branches.test.ts b/apps/web/hooks/domains/workspace/use-repository-branches.test.ts index 4407c87ab0..2c8217be9e 100644 --- a/apps/web/hooks/domains/workspace/use-repository-branches.test.ts +++ b/apps/web/hooks/domains/workspace/use-repository-branches.test.ts @@ -34,6 +34,26 @@ afterEach(() => { }); describe("useBranches", () => { + it("retries a transient branch-list failure before caching the result", async () => { + listBranchesMock + .mockRejectedValueOnce(new Error("temporary failure")) + .mockResolvedValueOnce({ branches: [{ name: "main", type: "remote" }] }); + + const source: BranchSource = { + kind: "id", + workspaceId: WORKSPACE_ID, + repositoryId: "repo-retry", + }; + renderHook(() => useBranches(source)); + + await waitFor(() => + expect(setRepositoryBranchesMock).toHaveBeenCalledWith("repo-retry", [ + { name: "main", type: "remote" }, + ]), + ); + expect(listBranchesMock).toHaveBeenCalledTimes(2); + }); + it("loads a new repository while the prior repository request is still pending", async () => { listBranchesMock .mockImplementationOnce(() => new Promise(() => undefined)) diff --git a/apps/web/hooks/domains/workspace/use-repository-branches.ts b/apps/web/hooks/domains/workspace/use-repository-branches.ts index f37681584d..861f3abcf9 100644 --- a/apps/web/hooks/domains/workspace/use-repository-branches.ts +++ b/apps/web/hooks/domains/workspace/use-repository-branches.ts @@ -6,6 +6,7 @@ import { listBranches, listRepositoryBranches } from "@/lib/api"; import type { Branch } from "@/lib/types/http"; const EMPTY_BRANCHES: Branch[] = []; +const BRANCH_LIST_RETRY_DELAYS_MS = [100, 250, 500, 1_000] as const; /** * Source of branches for a row: either a workspace repo (by id) or an @@ -24,6 +25,25 @@ function cacheKeyFor(source: BranchSource | null): string { return source.kind === "id" ? source.repositoryId : `path::${source.workspaceId}::${source.path}`; } +async function listBranchesUntilSettled(source: BranchSource): Promise { + for (const retryDelayMs of BRANCH_LIST_RETRY_DELAYS_MS) { + try { + const response = + source.kind === "id" + ? await listBranches(source.workspaceId, { repositoryId: source.repositoryId }) + : await listBranches(source.workspaceId, { path: source.path }); + return response.branches; + } catch { + await new Promise((resolve) => setTimeout(resolve, retryDelayMs)); + } + } + const response = + source.kind === "id" + ? await listBranches(source.workspaceId, { repositoryId: source.repositoryId }) + : await listBranches(source.workspaceId, { path: source.path }); + return response.branches; +} + /** * Loads git branches for a workspace repo or an on-machine path. One hook, * one cache, one backend endpoint — the source shape decides which query @@ -62,14 +82,12 @@ export function useBranches(source: BranchSource | null, enabled = true): UseBra inFlightKeysRef.current.add(key); setRepositoryBranchesLoading(key, true); - const promise = - source.kind === "id" - ? listBranches(source.workspaceId, { repositoryId: source.repositoryId }) - : listBranches(source.workspaceId, { path: source.path }); - - promise - .then((response) => setRepositoryBranches(key, response.branches)) - .catch(() => setRepositoryBranches(key, [])) + listBranchesUntilSettled(source) + .then((branches) => setRepositoryBranches(key, branches)) + .catch(() => { + // Keep the cache unloaded after a failed request. A transient branch + // endpoint failure must not permanently disable the branch pill. + }) .finally(() => { inFlightKeysRef.current.delete(key); setRepositoryBranchesLoading(key, false); diff --git a/apps/web/lib/api/domains/improve-kandev-api.ts b/apps/web/lib/api/domains/improve-kandev-api.ts index 80ae3c28fe..e10f8837b5 100644 --- a/apps/web/lib/api/domains/improve-kandev-api.ts +++ b/apps/web/lib/api/domains/improve-kandev-api.ts @@ -2,12 +2,31 @@ import { fetchJson, type ApiRequestOptions } from "../client"; // Result of the bootstrap fork-capability probe; mirrors backend ForkStatus. // "writable": user has push access to upstream, no fork needed. -// "ready": a fork already exists at github.com/{login}/kandev. +// "ready": a verified fork already exists in the canonical repository's +// fork network, including forks whose names were changed. +// "creatable": managed workspace automation can create the exact fork during +// task creation. // "blocked_emu": user looks like an Enterprise Managed User and likely // cannot fork repositories outside their owning enterprise. +// "blocked_managed": managed workspace automation cannot prepare a verified +// fork; implementation reports must stay blocked. // "unknown": bootstrap could not determine fork eligibility; proceed and // rely on the PR step to surface any errors. -export type ForkStatus = "writable" | "ready" | "blocked_emu" | "unknown"; +export type ForkStatus = + | "writable" + | "ready" + | "creatable" + | "blocked_emu" + | "blocked_managed" + | "unknown"; + +export type ImproveKandevForkReasonCode = + | "account_cannot_fork" + | "app_unsupported" + | "fork_conflict" + | "fork_not_writable" + | "fork_not_ready" + | "managed_unavailable"; export type ImproveKandevBootstrapResponse = { /** Dedicated Improve Kandev workspace the task must be created in. */ @@ -21,7 +40,7 @@ export type ImproveKandevBootstrapResponse = { github_login: string; has_write_access: boolean; fork_status: ForkStatus; - fork_message?: string; + fork_reason_code?: ImproveKandevForkReasonCode; }; export async function bootstrapImproveKandev( diff --git a/apps/web/lib/ssr/mapper.test.ts b/apps/web/lib/ssr/mapper.test.ts index d84eb002e0..817e141127 100644 --- a/apps/web/lib/ssr/mapper.test.ts +++ b/apps/web/lib/ssr/mapper.test.ts @@ -84,6 +84,19 @@ describe("snapshotToState", () => { expect(state.kanban?.tasks[0]?.activeSubagentCount).toBe(expected); }); + it("hydrates the task status summary into the initial kanban state", () => { + const snapshot = snapshotWithPendingAction(undefined); + snapshot.tasks[0].status_summary = { + revision: 4, + updated_at: now, + primary_session: { id: "session-1", state: "WAITING_FOR_INPUT" }, + }; + + const state = snapshotToState(snapshot); + + expect(state.kanban?.tasks[0]?.statusSummary).toEqual(snapshot.tasks[0].status_summary); + }); + it("preserves workflow step WIP fields", () => { const state = snapshotToState({ workflow: { diff --git a/apps/web/lib/ssr/mapper.ts b/apps/web/lib/ssr/mapper.ts index 9a5bf3fc52..e1cb9da6f2 100644 --- a/apps/web/lib/ssr/mapper.ts +++ b/apps/web/lib/ssr/mapper.ts @@ -54,6 +54,7 @@ export function snapshotToState(snapshot: WorkflowSnapshot): Partial { activeSubagentCount: task.active_subagent_count ?? undefined, sessionCount: task.session_count ?? undefined, reviewStatus: task.review_status ?? undefined, + statusSummary: task.status_summary, parentTaskId: task.parent_id ?? undefined, metadata: task.metadata, workspaceMode: workspaceModeFromMetadata(task.metadata), diff --git a/apps/web/src/locales/en/common.json b/apps/web/src/locales/en/common.json index e39d2c6bf5..39dfcb5800 100644 --- a/apps/web/src/locales/en/common.json +++ b/apps/web/src/locales/en/common.json @@ -210,7 +210,12 @@ "continue": "Continue", "improveKandevBugPlaceholder": "E.g. When I open the kanban board, the column header text overlaps the task count badge on screens narrower than 1200px...", "improveKandevFeaturePlaceholder": "E.g. Add a keyboard shortcut to mark a task as Done from the task detail view, and show the shortcut in the task action menu...", + "improveKandevDirectPushAccess": "You have write access to kdlbs/kandev, so the agent will push directly to a branch on the upstream repo.", + "improveKandevExistingForkPrepared": "Kandev found a writable fork for this task. The agent will push the branch there and open the pull request against kdlbs/kandev.", + "improveKandevExecutorForkAtPr": "The agent will prepare a fork through the executor's credentials during the PR step and open the pull request from it.", + "improveKandevIssueNoForkAccess": "Opening an issue does not require a fork or push access.", "improveKandevIssuePlaceholder": "Describe the problem or request you want published. The agent will ask for any details required by the matching GitHub issue template...", + "improveKandevManagedForkPrepared": "Kandev will prepare a writable fork for this task before the agent starts. The agent will push the branch there and open the pull request against kdlbs/kandev.", "includeRecentBackendAndBrowserLogs": "Include recent backend & browser logs as context for the agent", "installed": "Installed", "integrations": "Integrations", @@ -281,6 +286,7 @@ "openingLastTask": "Opening last task…", "openIssue": "Open issue", "optionalTheRepositoryTheAgentWorks": "Optional; the repository the agent works in.", + "managedGithubForkUnavailable": "Managed GitHub access could not prepare a verified fork for this task. Check the workspace GitHub connection or open an issue instead.", "panels": "Panels", "parentDirectory": "Parent directory", "pending": "Pending", diff --git a/apps/web/src/locales/pseudo/common.json b/apps/web/src/locales/pseudo/common.json index 6e22bfd059..beb2de7344 100644 --- a/apps/web/src/locales/pseudo/common.json +++ b/apps/web/src/locales/pseudo/common.json @@ -210,7 +210,12 @@ "continue": "Ćōńţĩńũē", "improveKandevBugPlaceholder": "Ē.ĝ. Ŵĥēń Ĩ ōƥēń ţĥē ķàńƀàń ƀōàŕď, ţĥē ćōĺũḿń ĥēàďēŕ ţēxţ ōvēŕĺàƥś ţĥē ţàśķ ćōũńţ ƀàďĝē ōń śćŕēēńś ńàŕŕōŵēŕ ţĥàń 1200ƥx...", "improveKandevFeaturePlaceholder": "Ē.ĝ. Àďď à ķēŷƀōàŕď śĥōŕţćũţ ţō ḿàŕķ à ţàśķ àś Ďōńē ƒŕōḿ ţĥē ţàśķ ďēţàĩĺ vĩēŵ, àńď śĥōŵ ţĥē śĥōŕţćũţ ĩń ţĥē ţàśķ àćţĩōń ḿēńũ...", + "improveKandevDirectPushAccess": "Ŷōũ ĥàvē ŵŕĩţē àććēśś ţō ķďĺƀś/ķàńďēv, śō ţĥē àĝēńţ ŵĩĺĺ ƥũśĥ ďĩŕēćţĺŷ ţō à ƀŕàńćĥ ōń ţĥē ũƥśţŕēàḿ ŕēƥō.", + "improveKandevExistingForkPrepared": "Ķàńďēv ƒōũńď à ŵŕĩţàƀĺē ƒōŕķ ƒōŕ ţĥĩś ţàśķ. Ţĥē àĝēńţ ŵĩĺĺ ƥũśĥ ţĥē ƀŕàńćĥ ţĥēŕē àńď ōƥēń ţĥē ƥũĺĺ ŕēqũēśţ àĝàĩńśţ ķďĺƀś/ķàńďēv.", + "improveKandevExecutorForkAtPr": "Ţĥē àĝēńţ ŵĩĺĺ ƥŕēƥàŕē à ƒōŕķ ţĥŕōũĝĥ ţĥē ēxēćũţōŕ'ś ćŕēďēńţĩàĺś ďũŕĩńĝ ţĥē ƤŔ śţēƥ àńď ōƥēń ţĥē ƥũĺĺ ŕēqũēśţ ƒŕōḿ ĩţ.", + "improveKandevIssueNoForkAccess": "Ōƥēńĩńĝ àń ĩśśũē ďōēś ńōţ ŕēqũĩŕē à ƒōŕķ ōŕ ƥũśĥ àććēśś.", "improveKandevIssuePlaceholder": "Ďēśćŕĩƀē ţĥē ƥŕōƀĺēḿ ōŕ ŕēqũēśţ ŷōũ ŵàńţ ƥũƀĺĩśĥēď. Ţĥē àĝēńţ ŵĩĺĺ àśķ ƒōŕ àńŷ ďēţàĩĺś ŕēqũĩŕēď ƀŷ ţĥē ḿàţćĥĩńĝ ĜĩţĤũƀ ĩśśũē ţēḿƥĺàţē...", + "improveKandevManagedForkPrepared": "Ķàńďēv ŵĩĺĺ ƥŕēƥàŕē à ŵŕĩţàƀĺē ƒōŕķ ƒōŕ ţĥĩś ţàśķ ƀēƒōŕē ţĥē àĝēńţ śţàŕţś. Ţĥē àĝēńţ ŵĩĺĺ ƥũśĥ ţĥē ƀŕàńćĥ ţĥēŕē àńď ōƥēń ţĥē ƥũĺĺ ŕēqũēśţ àĝàĩńśţ ķďĺƀś/ķàńďēv.", "includeRecentBackendAndBrowserLogs": "Ĩńćĺũďē ŕēćēńţ ƀàćķēńď & ƀŕōŵśēŕ ĺōĝś àś ćōńţēxţ ƒōŕ ţĥē àĝēńţ", "installed": "Ĩńśţàĺĺēď", "integrations": "Ĩńţēĝŕàţĩōńś", @@ -281,6 +286,7 @@ "openingLastTask": "Ōƥēńĩńĝ ĺàśţ ţàśķ…", "openIssue": "Ōƥēń ĩśśũē", "optionalTheRepositoryTheAgentWorks": "Ōƥţĩōńàĺ; ţĥē ŕēƥōśĩţōŕŷ ţĥē àĝēńţ ŵōŕķś ĩń.", + "managedGithubForkUnavailable": "Ḿàńàĝēď ĜĩţĤũƀ àććēśś ćōũĺď ńōţ ƥŕēƥàŕē à vēŕĩƒĩēď ƒōŕķ ƒōŕ ţĥĩś ţàśķ. Ćĥēćķ ţĥē ŵōŕķśƥàćē ĜĩţĤũƀ ćōńńēćţĩōń ōŕ ōƥēń àń ĩśśũē ĩńśţēàď.", "panels": "Ƥàńēĺś", "parentDirectory": "Ƥàŕēńţ ďĩŕēćţōŕŷ", "pending": "Ƥēńďĩńĝ", diff --git a/apps/web/src/locales/pt-pt/common.json b/apps/web/src/locales/pt-pt/common.json index a5f169e58c..52e318e543 100644 --- a/apps/web/src/locales/pt-pt/common.json +++ b/apps/web/src/locales/pt-pt/common.json @@ -207,7 +207,12 @@ "continue": "Continuar", "improveKandevBugPlaceholder": "Ex.: Quando abro o quadro kanban, o texto do cabeçalho da coluna sobrepõe-se ao emblema com a contagem de tarefas em ecrãs com menos de 1200 px de largura...", "improveKandevFeaturePlaceholder": "Ex.: Adicionar um atalho de teclado para marcar uma tarefa como Concluída a partir da vista de detalhe da tarefa, e mostrar o atalho no menu de ações da tarefa...", + "improveKandevDirectPushAccess": "Tem acesso de escrita a kdlbs/kandev, por isso o agente enviará diretamente para um branch no repositório upstream.", + "improveKandevExistingForkPrepared": "O Kandev encontrou um fork com permissões de escrita para esta tarefa. O agente enviará o branch para esse fork e abrirá o pull request contra kdlbs/kandev.", + "improveKandevExecutorForkAtPr": "O agente preparará um fork através das credenciais do executor durante o passo do PR e abrirá o pull request a partir dele.", + "improveKandevIssueNoForkAccess": "A abertura de uma issue não requer um fork nem acesso de escrita.", "improveKandevIssuePlaceholder": "Descreva o problema ou o pedido que quer publicar. O agente pedirá os detalhes exigidos pelo modelo de issue do GitHub correspondente...", + "improveKandevManagedForkPrepared": "O Kandev preparará um fork com permissões de escrita para esta tarefa antes de o agente iniciar. O agente enviará o branch para esse fork e abrirá o pull request contra kdlbs/kandev.", "includeRecentBackendAndBrowserLogs": "Incluir os registos recentes do backend e do navegador como contexto para o agente", "installed": "Instalado", "integrations": "Integrações", @@ -275,6 +280,7 @@ "openingLastTask": "A abrir a última tarefa…", "openIssue": "Abrir issue", "optionalTheRepositoryTheAgentWorks": "Opcional; o repositório em que o agente trabalha.", + "managedGithubForkUnavailable": "O acesso gerido ao GitHub não conseguiu preparar um fork verificado para esta tarefa. Verifique a ligação do GitHub do workspace ou abra uma issue.", "panels": "Painéis", "parentDirectory": "Diretório superior", "pending": "Pendente", diff --git a/docs/decisions/2026-08-12-task-bound-fork-destinations.md b/docs/decisions/2026-08-12-task-bound-fork-destinations.md new file mode 100644 index 0000000000..898f01655e --- /dev/null +++ b/docs/decisions/2026-08-12-task-bound-fork-destinations.md @@ -0,0 +1,132 @@ +# ADR-2026-08-12-task-bound-fork-destinations: Bind Fork Push Destinations to Tasks + +**Status:** accepted (amended 2026-08-13) +**Date:** 2026-08-12 +**Area:** backend, frontend, workflow, security, GitHub + +## Context + +A contribution task created against a canonical GitHub repository has two repository identities before +a pull request exists. The canonical repository owns issues, the future pull request, the base branch, +and Kandev's repository row. A contributor-owned fork is only the destination for publishing the task +branch. + +Replacing the repository row or `origin` with the fork makes issue lookup and pull-request detection use +the wrong repository. Keeping the canonical identity without recording the fork makes managed task Git +credentials reject the push, because their helper and `gh` shim accept only repository identities covered +by task-bound leases. Switching the workspace to executor-owned credentials would make the flow work, but +it can expose a host credential with access to unrelated repositories. + +Kandev already solves the equivalent post-PR problem with a server-authored `remote_contribution` +binding. That binding keeps the target repository as `origin`, adds a dedicated source remote, and permits +one exact additional managed credential scope. A pre-PR contribution needs the same trust shape without +claiming that a pull request, source branch, or head SHA already exists. + +Managed credential leases are fixed when the task runtime starts. Preparing a fork only when the task +enters its PR workflow step is therefore too late for a long-lived managed session unless the whole +environment is recreated. Managed fork resolution must complete before the first task launch. + +Path-only metadata does not prove identity when the workspace automation connection changes, GitHub +deletes and recreates a repository at the same owner/name, or a fork is renamed. The destination contract +must bind both the credential identity and the provider-owned repository identities. + +## Decision + +Kandev will represent a pre-PR fork as a versioned, server-authored +`contribution_destination` binding on the canonical task-repository attachment. The binding contains only +the provider, the exact credential-free source repository identity and URL, provider-owned stable IDs, and +a non-secret binding to the workspace automation source and credential generation. The attachment's normal +`repository_id` remains the target identity. No token, lease, provider title, or caller-supplied Git remote +is trusted or persisted. + +When the workspace selects managed task credentials, the Improve Kandev task-creation path resolves the +destination through the selected workspace automation connection before the task is settled or its first +agent starts. Direct target write access requires no second binding. Otherwise Kandev reuses or creates the +automation actor's fork, verifies that its parent provider ID and full name are exactly `kdlbs/kandev`, +verifies write access, and persists the binding. A same-name repository with a different parent fails closed. + +Exact-name lookup is only the fast path. If it returns a provider 404, Kandev searches the canonical +repository's fork network and re-reads each same-owner candidate through the provider before accepting it. +This reuses renamed forks without trusting list payloads or creating a duplicate. + +Fork ownership must match the identity that supplies managed task credentials. A human PAT or named GitHub +CLI automation connection may own and create a fork. A GitHub App may contribute directly when its +installation can write the target; otherwise automatic fork preparation fails with configuration guidance. +Kandev does not create a fork with a personal connection and then attempt to push it with an unrelated App +installation. + +Runtime materialization preserves canonical `origin` and adds a collision-resistant dedicated remote for +the bound fork. The task branch tracks that remote for ordinary `git push`; pull, base comparison, issue +lookup, and pull-request creation continue to target the canonical repository explicitly. Managed GitHub +credentials add a second lease only when its owner/repository exactly matches a valid destination binding +on the same task attachment. The broker authorizer compares canonical, target, and parent provider IDs and +re-reads the target through the current workspace automation connection at both lease issuance and +redemption. A path reused by a deleted-and-recreated repository cannot inherit an old lease. + +If task policy becomes executor-owned or an explicit executor `GH_TOKEN`/`GITHUB_TOKEN` is present, the +runtime clears any managed destination from the launch request and uses the separate unmanaged compatibility +path. A destination bound to another workspace connection generation, login, App installation, or App +credential generation is rejected or revoked. + +The managed Improve Kandev PR path does not run `gh repo fork` or rename `origin`. It pushes `HEAD` through +the configured contribution remote and creates the pull request with explicit canonical `--repo`, base, +and fork-owner/head arguments. Launch, resume, and managed-origin reconciliation reconstruct this +arrangement idempotently. + +Executor-owned task credentials remain an explicit unmanaged compatibility path. Kandev does not infer or +persist a fork from an opaque local, SSH, container, or cloud executor identity. The PR workflow may retain +its existing agent-managed fork setup only when no managed destination is present and the workspace policy +is executor-owned; it must never use that fallback after a managed preparation failure. + +## Consequences + +- GitHub issue and pull-request integration continue to use `kdlbs/kandev`. +- Managed mode exposes at most the canonical repository plus one server-verified fork to the task, instead + of the executor's ambient GitHub authority. +- Fork resolution or creation becomes required synchronous task-creation work for Improve Kandev + implementation tasks that use managed credentials. Failure leaves no partly launched task. +- The binding and remote setup must be projected through every supported executor and reconstructed on + resume. +- Bootstrap capability reporting must use the workspace automation connection, not ambient host `gh`, so + the displayed actor and the eventual managed credential source cannot disagree in managed mode. The + executor-owned compatibility probe remains explicitly separate. +- Bootstrap persists the canonical provider repository ID when it can resolve it. New blocked states cross + the API as stable `fork_reason_code` values; the frontend translates those codes instead of displaying + backend-authored English text. +- Existing `remote_contribution` bindings remain the authority for tasks attached to an already-open pull + request. The new binding covers only the period before a pull request exists. +- User-managed local repository rows retain their existing remote exemption. Kandev-managed provider rows + continue to reconcile `origin` to canonical HTTPS. + +## Alternatives considered + +### Replace `origin` with the contributor fork + +Rejected. It makes the fork look canonical and breaks issue lookup, pull-request detection, base fetches, +and managed-checkout reconciliation. + +### Allow any fork of the canonical repository at credential time + +Rejected. Discovering a relationship at lease redemption is less deterministic, makes authorization depend +on a live provider query, and can silently broaden a task after creation. The exact destination must be +server-verified and task-bound before launch. + +### Prepare the fork when entering the PR step + +Rejected. Existing sessions keep their launch-time credential snapshot. A context reset does not reliably +rebuild the task environment or issue new leases. + +### Attach the fork as a second task repository + +Rejected. The fork is not a second workspace source. Materializing it independently would duplicate the +checkout and confuse repository ownership, diffs, and change-request association. + +### Require executor-owned credentials for fork contributors + +Rejected. It restores functionality by abandoning the managed trust boundary and may expose credentials +for unrelated personal or work repositories. + +### Pre-bind an executor-owned fork from workspace automation + +Rejected. Executor credentials can belong to a different host or account and are intentionally opaque to +Kandev. A workspace-authored fork would not prove that the executor can write it. diff --git a/docs/decisions/INDEX.md b/docs/decisions/INDEX.md index 1088e19432..22cfbb63ff 100644 --- a/docs/decisions/INDEX.md +++ b/docs/decisions/INDEX.md @@ -166,4 +166,5 @@ Read individual ADRs for full context. Create new ones via `/record decision` or | 2026-08-12-empty-utility-bindings-inherit-default | [Empty Built-in Utility Bindings Inherit the Default](2026-08-12-empty-utility-bindings-inherit-default.md) | accepted | backend, frontend | 2026-08-12 | | 2026-08-12-setup-timeout-owns-launch-budget | [One Setup Timeout Owns Launch Budgets](2026-08-12-setup-timeout-owns-launch-budget.md) | accepted | backend | 2026-08-12 | | 2026-08-10-no-em-dash-public-copy | [Keep Em Dashes Out of Public Copy](2026-08-10-no-em-dash-public-copy.md) | accepted | frontend, infra | 2026-08-10 | +| 2026-08-12-task-bound-fork-destinations | [Bind Fork Push Destinations to Tasks](2026-08-12-task-bound-fork-destinations.md) | accepted (amended 2026-08-13) | backend, frontend, workflow, security, GitHub | 2026-08-12 | | 2026-08-13-hard-delete-task-contribution-links | [Hard delete owns task contribution links](2026-08-13-hard-delete-task-contribution-links.md) | accepted | backend | 2026-08-13 | diff --git a/docs/plans/improve-kandev-managed-fork/plan.md b/docs/plans/improve-kandev-managed-fork/plan.md new file mode 100644 index 0000000000..d87f74c02c --- /dev/null +++ b/docs/plans/improve-kandev-managed-fork/plan.md @@ -0,0 +1,227 @@ +--- +spec: docs/specs/improve-kandev/spec.md +created: 2026-08-12 +status: complete +--- + +# Implementation Plan: Managed Fork Contributions + +## Overview + +Keep the Improve Kandev task attached to the canonical `kdlbs/kandev` repository while giving a +managed-credential task one exact, server-verified fork as its publication destination. Resolve that +destination through the workspace automation connection before the first agent launch, persist it on the +canonical task-repository attachment, and reuse the existing remote-contribution patterns for a second +broker lease, a dedicated Git remote, and restart-safe push routing. Preserve executor-owned credentials as +a separate unmanaged path because Kandev cannot prove the identity behind an opaque executor credential. + +Review remediation on 2026-08-13 closes the remaining trust-boundary cases: destination credential +bindings carry the selected connection generation, provider IDs are checked against live fork-parent +identity, renamed forks are discovered through the fork network, and bootstrap errors cross the API as +stable reason codes for frontend translation. + +The implementation follows +[ADR-2026-08-12-task-bound-fork-destinations](../../decisions/2026-08-12-task-bound-fork-destinations.md), +[ADR-2026-08-04-remote-contribution-bindings](../../decisions/2026-08-04-remote-contribution-bindings.md), +and the [task Git credential policy](../../decisions/2026-07-27-task-git-credential-policy.md). + +## Implementation status + +- [x] Task 01: contribution-destination model and workspace GitHub resolution. +- [x] Task 02: task-creation preparation, persistence, and bootstrap capability alignment. +- [x] Task 03: managed leases, runtime remotes, push routing, and resume reconstruction. +- [x] Task 04: Improve Kandev workflow, desktop/mobile recovery states, integration coverage, and docs. + +## Invariants + +- The repository row, task attachment, provider identity, and managed `origin` remain the canonical + `kdlbs/kandev` repository. +- A fork is one task-bound push destination, not a second workspace source and not an alternate canonical + repository. +- Only the backend may author `contribution_destination`; REST, WebSocket, MCP, workflow prompts, and Git + remotes cannot supply a trusted fork identity. +- Fork preparation uses the same workspace automation source that supplies managed task credentials. A + personal identity is not silently combined with unrelated App credentials. +- A persisted destination records the non-secret credential source/login/generation (and App installation + identity where applicable). Policy changes, explicit executor tokens, and connection changes clear or + revoke the managed destination. +- Canonical and target provider IDs are required for managed destination scopes. Lease issuance and + redemption re-read the target and verify its fork parent through the current automation connection. +- Existing renamed forks are found through the canonical repository's fork network and individually + re-read before reuse; path-only list results are never trusted. +- All persisted URLs are canonical and credential-free. Tokens, leases, credential helpers, provider + content, and ambient remote state never enter the binding. +- Managed authorization proves task, session, canonical attachment, binding version, provider, host, and + exact fork owner/repository, provider IDs, parent identity, and credential binding before issuing the + second scope. +- Direct target write access needs no fork binding or second lease. +- Executor-owned task access does not receive a workspace-authored destination binding and retains its + explicit agent-managed publication path. +- Existing `remote_contribution` behavior for already-open pull requests remains unchanged. +- Kandev-managed provider rows continue to reconcile canonical `origin`; user-managed local rows retain + their current exemption. +- Fork preparation failure occurs before task settlement or agent launch. Runtime validation and resume + also fail closed on malformed or inconsistent persisted state. + +## Backend + +### Contribution-destination domain and provider resolution + +- Add a versioned `ContributionDestination` beside task repository models. Reuse the existing validated, + credential-free repository identity shape and deterministic contribution-remote naming where practical, + while keeping pre-PR bindings distinct from `RemoteContribution`, which requires an existing change + number, branch, and head SHA. +- Add strict put/load/validate helpers for `task_repositories.metadata["contribution_destination"]`. + Validation accepts only supported providers and canonical repository URLs and rejects unknown versions, + credentials, queries, fragments, malformed paths, and target/source identity aliasing. +- Add a narrow GitHub contribution resolver to the workspace GitHub service. It checks direct target write + access first. Otherwise it requires a human automation principal, checks the exact path and then the + canonical fork network for a renamed actor-owned fork, proves its parent provider ID/path is exactly + `kdlbs/kandev`, creates the fork when absent, polls within a bounded deadline, and confirms source write + access before returning the bound destination. +- Implement the resolver for PAT and named CLI automation clients through their existing authenticated API + paths. Treat an App installation without direct target write access as unsupported for automatic fork + ownership and return actionable, typed failure. Never fall back to ambient host `gh`. + +### Creation-time preparation and persistence + +- Add an optional server-side contribution-destination resolver seam to task creation. It runs after + external-ID deduplication and workflow/repository validation but before the task insert, so retries reuse a + settled task and provider failures leave no partly created task. +- The injected Improve Kandev resolver acts only when task Git access is managed, the selected workflow's + immutable template ID is `improve-kandev`, and the resolved attachment is canonical `kdlbs/kandev`. + Ordinary workflows, executor-owned access, the issue-only template, local repositories, and already-open + `remote_contribution` tasks are unchanged. +- Carry the resulting binding only through an internal, JSON-excluded field on + `service.TaskRepositoryInput`. Validate it again and persist it while creating the canonical + `TaskRepository`, preserving unrelated metadata and existing PR-number behavior. +- In managed mode, replace Improve Kandev's ambient `gh` bootstrap probe with the same workspace-scoped + resolver in read-only capability mode. Report direct write, exact ready fork, creatable fork, and blocked + automation configuration from the identity that will supply task credentials. Keep executor-owned + capability probing explicitly separate and labeled. +- Persist the canonical provider repository ID during bootstrap for managed and CLI-compatible fallback + paths whenever the provider returns it. Return stable fork reason codes rather than backend-authored + English messages. + +### Runtime projection and Git routing + +- Load the binding into `repoInfo` and thread it through `RepoSpec`, lifecycle prepare/create requests, + executor metadata, agentctl workspace configuration, and multi-repository projections as one optional + typed object. +- Generalize the existing contribution-remote setup so a pre-PR destination adds the same + collision-resistant remote without fetching or checking out a remote head. Preserve the task's current + feature branch and set its upstream/push target to the same branch name on the fork. +- Reconstruct and validate the fork remote and upstream on fresh launch, warm resume, reset/recreation, + Worktree, Local, Docker, SSH, and Sprites paths. A conflicting remote identity fails rather than being + overwritten. Canonical `origin` reconciliation runs as today and does not remove the contribution remote. +- Project explicit push remote/branch data to agentctl so Changes-panel pushes and raw `git push` agree. + Pull, rebase, merge, base fetch, issue lookup, and change-request target selection remain canonical. + +### Credential authorization + +- Extend executor managed-scope construction to add the destination source owner/repository after the + canonical scope. Deduplicate a same-identity result defensively, although valid fork bindings must differ + from the target. +- Extend the broker authorizer to accept the requested source only when it exactly matches a valid + `contribution_destination` on the authorized task-repository attachment, including source/target + provider IDs and a live provider-authoritative parent check. Keep the existing `remote_contribution` + proof as a separate accepted case. +- Revalidate the destination's credential binding against the current workspace connection and credential + generation at lease issuance and redemption. Executor-owned policy and explicit profile tokens must + remove managed destination state before launch. +- Preserve the current credential helper and `gh` shim exact-match behavior. Git push redeems the fork + lease through the dedicated remote; `gh pr create --repo kdlbs/kandev` redeems the canonical lease. No SSH + or executor-owned fallback is added to managed mode. + +## Workflow and frontend + +- Rewrite the Improve Kandev PR-step prompt so managed publication treats routing as already prepared. It + verifies canonical `origin`, pushes with ordinary `git push` through the configured upstream, and invokes + `gh pr create` with explicit `--repo kdlbs/kandev`, `--base main`, and `:` head + identity. The executor-owned branch retains the current agent-managed fork setup and is permitted only + when no managed destination is expected; a managed preparation failure never falls back to it. +- Extend the bootstrap API's fork-status union with a general blocked-managed-credentials state and a safe + recovery message. Keep issue-only submission available. +- Reuse the existing Improve Kandev dialog and contributor banner on desktop and mobile. This is a + content/state change inside the current responsive dialog, not a new composition: the same shared model + blocks Bug fix/Feature request, while Open issue remains the primary recovery path. The existing mobile + Improve Kandev dialog is the closest exemplar; it retains its single dialog scroll owner, current touch + targets, safe-area behavior, and absence of horizontal document overflow. + +## Tests + +- **Domain/provider:** table-driven model tests cover round trip, unknown version, credential-bearing URLs, + malformed identities, and deterministic remote names. PAT/CLI resolver tests cover direct write, existing + exact and renamed-network forks, fork creation and bounded polling, same-name wrong-parent conflict, + source write denial, EMU, App-without-target-write rejection, typed CLI 404 handling, and provider error + redaction. +- **Creation:** task service tests prove resolution precedes insert/launch, external-ID retries do not repeat + fork creation, only the Improve implementation template is enriched, internal fields cannot be forged, + metadata preserves unrelated keys, executor-owned access is bypassed, and resolution failure leaves no + task or session. +- **Credentials:** executor and broker tests prove canonical plus exact fork scopes for the same task/session, + reject unrelated forks, wrong workspaces, wrong attachments, malformed bindings, unknown versions, + changed workspace connections, explicit profile tokens, executor-owned policy, and provider-ID reuse. +- **Runtime:** temporary canonical/fork repositories cover stable remote naming, current-branch upstream, + direct and agentctl pushes, conflicting remote rejection, canonical-origin reconciliation, local-row + exemption, restart reconstruction, and unchanged ordinary/remote-contribution behavior on each runtime + projection seam. +- **Workflow/UI:** loader regression tests pin the managed prepared route, the guarded executor-owned route, + and the absence of origin rewriting in managed instructions. Component/model tests cover every fork + status, reason-code translation, task-access mode, and issue-only bypass. Existing desktop and mobile + Improve Kandev Playwright specs cover the blocked managed-credential message, touch-reachable issue + fallback, viewport containment, and no horizontal overflow. +- **Integration:** a focused backend test creates a managed Improve task with fake GitHub APIs and temporary + canonical/fork remotes, launches it, commits, pushes, resumes, and proves only the fork branch advances + while canonical `origin`, issue identity, broker scope, and PR target stay `kdlbs/kandev`. + +## Public documentation + +- Update the **Choose task Git credentials** reference section in `docs/public/integrations.md` to explain + task-bound contribution destinations, canonical `origin`, the exact second lease, and the App limitation. +- Update the Git operations reference only where it describes task-bound contribution push routing. Keep + the existing Changes-panel `origin` contract for ordinary tasks explicit rather than implying every task + receives a fork. +- Treat `integrations.md` as reference and `git-operations.md` as reference. No new page or navigation entry + is required. + +## Execution order + +1. [Task 01: Destination model and GitHub resolution](task-01-destination-model-and-github-resolution.md) +2. [Task 02: Creation-time preparation and bootstrap](task-02-creation-preparation-and-bootstrap.md) +3. [Task 03: Runtime credentials and fork routing](task-03-runtime-credentials-and-fork-routing.md) +4. [Task 04: Workflow, UI, integration, and docs](task-04-workflow-ui-integration-and-docs.md) + +Tasks are sequential. Each task establishes a persisted or security-sensitive contract consumed by the +next, and Tasks 02-03 touch shared creation and launch paths where parallel implementation would create +avoidable merge and trust-boundary risk. + +## Completion + +Completed 2026-08-12. The implementation keeps the canonical `kdlbs/kandev` attachment and `origin`, +binds one server-verified fork for managed Improve Kandev tasks, carries that binding through launch, +resume, materialization, credentials, broker authorization, and Git/PR operations, and keeps the +executor-owned path separate. The workflow prompt, translated desktop/mobile recovery state, public +references, and targeted browser coverage now describe the same behavior. + +The backend integration is covered across the creation, broker, lifecycle, remoting, Git push, and PR +provider seams. The new PR test verifies that only the prepared destination advances and that `gh pr +create` receives the canonical repository and explicit fork-owner head. + +## Final verification + +```bash +rtk make -C apps/backend test # passed +rtk make -C apps/backend lint # passed +cd apps && rtk pnpm --filter @kandev/web typecheck # passed +cd apps && rtk pnpm --filter @kandev/web lint # passed +cd apps/web && rtk pnpm e2e:run --host --no-build --project chromium -- e2e/tests/improve-kandev.spec.ts # 15 passed +cd apps/web && rtk pnpm e2e:run --host --no-build --project mobile-chrome -- e2e/tests/mobile-improve-kandev.spec.ts # 1 passed +rtk node --test scripts/validate-public-docs.test.mjs +rtk node scripts/validate-public-docs.mjs # 41 pages validated +rtk git diff --check # passed +``` + +Install `apps/node_modules` with `cd apps && rtk pnpm install --frozen-lockfile` first when the worktree is +fresh. Implementation follows TDD: each task adds its focused failing tests before production changes and +records the red and green commands in its completion section. diff --git a/docs/plans/improve-kandev-managed-fork/task-01-destination-model-and-github-resolution.md b/docs/plans/improve-kandev-managed-fork/task-01-destination-model-and-github-resolution.md new file mode 100644 index 0000000000..69b372af2b --- /dev/null +++ b/docs/plans/improve-kandev-managed-fork/task-01-destination-model-and-github-resolution.md @@ -0,0 +1,93 @@ +--- +id: "01-destination-model-and-github-resolution" +title: "Destination model and GitHub resolution" +status: complete +wave: 1 +depends_on: [] +plan: "plan.md" +spec: "../../specs/improve-kandev/spec.md" +--- + +# Task 01: Destination Model and GitHub Resolution + +## Acceptance + +- A strict versioned `contribution_destination` model round-trips through task-repository metadata without + accepting secrets, malformed URLs, unsupported providers, target aliases, or unknown versions. +- The workspace GitHub service reports direct target write or returns one exact writable fork whose parent + is the requested canonical repository; it creates and bounded-polls a missing human-owned fork. A CLI + 404 follows the same missing-fork path as PAT clients, and a renamed existing fork is reused only after + fork-network discovery and authoritative parent/actor/writability checks. +- The persisted destination records the canonical source and target provider IDs plus the non-secret + credential connection and generation that authorized it. +- PAT and named CLI automation identities are supported. A GitHub App without direct target write fails + with a typed configuration error and never borrows a personal or ambient host credential. + +## Verification + +```bash +cd apps/backend +rtk go test ./internal/task/models ./internal/github -run 'Test.*(ContributionDestination|ContributionFork|ForkDestination)' +``` + +## Files likely touched + +- `apps/backend/internal/task/models/contribution_destination.go` +- `apps/backend/internal/task/models/contribution_destination_test.go` +- `apps/backend/internal/github/models.go` +- `apps/backend/internal/github/client.go` +- `apps/backend/internal/github/pat_client.go` +- `apps/backend/internal/github/gh_client.go` +- `apps/backend/internal/github/service.go` +- focused GitHub PAT, CLI, service, and fake-client tests + +## Dependencies + +None. Reuse credential-free repository validation and remote-name hashing from +`internal/task/models/remote_contribution.go` without weakening that existing binding. + +## Parallelism + +Sequential foundation. Tasks 02-03 consume the exact persisted and provider-authoritative shape. + +## Inputs + +- Spec: Improve Kandev creation-time publication route and fork failure modes. +- ADR: server-authored task binding, same automation identity, canonical target ownership. +- Existing patterns: `RemoteContributionRepository`, workspace auth resolution, PAT/CLI client adapters, + and provider error sanitization. + +## Risks + +- A repository named `/kandev` is not sufficient proof; compare parent provider identity and path. +- A matching path is not sufficient proof after delete/recreate; compare source and target provider IDs and + the live parent relationship. +- GitHub fork creation is asynchronous. Poll with a bounded context and retry only provider-defined + not-ready states, not authorization or conflict failures. +- Do not widen the main GitHub client interface if a narrow capability interface keeps mocks and providers + simpler. + +## TDD sequence + +1. Add failing model validation/round-trip and resolver behavior tests. +2. Implement the smallest domain helpers and provider capability required to pass. +3. Refactor shared credential-free repository validation without changing remote-contribution behavior. +4. Run both focused suites again with retries/caches disabled where applicable. + +## Output contract + +Report the binding schema, provider authority checks, actor limitations, files changed, red/green commands, +remaining risks, divergence, and task/plan status updates. + +## Completion + +Completed 2026-08-13. Added the versioned credential-free destination model and metadata helpers, stable +destination remote naming, GitHub repository/fork API capabilities for PAT, CLI, mock, and no-op clients, +and the workspace resolver. Direct target write is preferred; otherwise only a human automation identity +may create or adopt a verified fork-network member whose parent identity matches `kdlbs/kandev`, including +renamed forks. CLI missing-fork responses are typed consistently with PAT responses, and the destination +stores stable provider IDs and a non-secret credential binding. App installations without direct target +write fail closed with typed, sanitized errors. + +Verification: `rtk go test ./internal/task/models ./internal/github -count=1` passed, including round-trip, +malformed-binding, fork-creation, parent-identity, polling, conflict, and actor-capability cases. diff --git a/docs/plans/improve-kandev-managed-fork/task-02-creation-preparation-and-bootstrap.md b/docs/plans/improve-kandev-managed-fork/task-02-creation-preparation-and-bootstrap.md new file mode 100644 index 0000000000..65a02cf09e --- /dev/null +++ b/docs/plans/improve-kandev-managed-fork/task-02-creation-preparation-and-bootstrap.md @@ -0,0 +1,91 @@ +--- +id: "02-creation-preparation-and-bootstrap" +title: "Creation preparation and bootstrap" +status: complete +wave: 2 +depends_on: ["01-destination-model-and-github-resolution"] +plan: "plan.md" +spec: "../../specs/improve-kandev/spec.md" +--- + +# Task 02: Creation Preparation and Bootstrap + +## Acceptance + +- Improve Kandev implementation task creation under managed task access resolves direct write or an exact + fork destination after deduplication and validation but before inserting the task or launching a session. +- The binding travels only through a server-owned, JSON-excluded service field and is persisted on the + canonical task-repository attachment; ordinary and issue-only tasks remain unchanged. +- Managed bootstrap uses the same workspace automation resolver and reports direct, ready, creatable, or + blocked capability for the identity that will supply managed task credentials, including the canonical + provider repository ID. Executor-owned access retains a separate capability path and receives no + server-authored binding. Blocked responses expose a stable reason code for localized clients. + +## Verification + +```bash +cd apps/backend +rtk go test ./internal/task/service ./internal/improvekandev ./internal/backendapp -run 'Test.*(ContributionDestination|ImproveKandev.*Fork|ForkCapability|CreateTask.*Contribution)' +``` + +## Files likely touched + +- `apps/backend/internal/task/service/service.go` +- `apps/backend/internal/task/service/service_requests.go` +- `apps/backend/internal/task/service/service_tasks.go` +- focused task creation and metadata tests +- `apps/backend/internal/improvekandev/github.go` +- `apps/backend/internal/improvekandev/handler.go` +- `apps/backend/internal/improvekandev/handler_test.go` +- `apps/backend/internal/backendapp/helpers.go` +- focused backend wiring tests + +## Dependencies + +Task 01's validated binding and workspace GitHub resolver. + +## Parallelism + +Sequential. Creation ordering is the authority boundary for every later runtime lease. + +## Inputs + +- Spec: managed preparation before first launch, bootstrap identity alignment, executor-owned separation, + and issue-only bypass. +- ADR: no caller-authored destination and no partly launched task on provider failure. +- Existing patterns: external-ID create settlement, `RemoteContribution`'s internal service field, hidden + workflow template IDs, and Improve Kandev bootstrap connection copying. + +## Risks + +- Run provider preparation after external-ID lookup so retries cannot create or poll the fork twice. +- Match the immutable workflow template ID, not a mutable workflow name. +- Do not make generic task creation depend on GitHub when the optional resolver is absent or the workflow is + unrelated. +- Resolve task Git policy before enrichment and never infer an executor identity from workspace automation. +- Preserve all required create compensation/settlement behavior and attachment ordering. + +## TDD sequence + +1. Add failing ordering, deduplication, enrichment-scope, persistence, and bootstrap actor tests. +2. Add the optional task service seam and server-only input field. +3. Wire the Improve resolver and replace ambient-host bootstrap probing. +4. Refactor duplicated capability mapping, then rerun focused suites. + +## Output contract + +Report create ordering, trusted/untrusted boundaries, bootstrap response changes, files changed, red/green +commands, remaining risks, divergence, and task/plan status updates. + +## Completion + +Completed 2026-08-13. Task creation now prepares the destination after deduplication and repository/workflow +validation but before insertion, carries it through a JSON-excluded service field, and persists it only as +server-authored task-repository metadata. The Improve Kandev adapter is limited to the immutable template +and managed credential mode. Bootstrap uses the same workspace-scoped capability resolver, while +executor-owned access remains explicitly separate and managed failures cannot fall back to ambient `gh`. +Bootstrap also records the canonical provider ID and returns stable fork reason codes, which the UI +localizes instead of rendering backend prose. + +Verification: `rtk go test ./internal/task/service ./internal/improvekandev ./internal/backendapp -count=1` +passed, including ordering, persistence, failure, capability, and wiring cases. diff --git a/docs/plans/improve-kandev-managed-fork/task-03-runtime-credentials-and-fork-routing.md b/docs/plans/improve-kandev-managed-fork/task-03-runtime-credentials-and-fork-routing.md new file mode 100644 index 0000000000..1dfbf1827e --- /dev/null +++ b/docs/plans/improve-kandev-managed-fork/task-03-runtime-credentials-and-fork-routing.md @@ -0,0 +1,95 @@ +--- +id: "03-runtime-credentials-and-fork-routing" +title: "Runtime credentials and fork routing" +status: complete +wave: 3 +depends_on: ["02-creation-preparation-and-bootstrap"] +plan: "plan.md" +spec: "../../specs/improve-kandev/spec.md" +--- + +# Task 03: Runtime Credentials and Fork Routing + +## Acceptance + +- Every launch/resume path loads the validated destination, preserves canonical `origin`, adds the stable + fork remote, and routes the task's current branch to the same branch name on that fork. +- Managed sessions receive the canonical lease plus one exact destination lease, and the broker rejects all + unrelated, malformed, cross-task, cross-session, and cross-workspace requests. The destination lease + proves source, target, and parent provider IDs and the bound credential connection/generation against + live provider state. +- A policy change to executor-owned access, or an explicit executor `GH_TOKEN`/`GITHUB_TOKEN`, clears the + server-managed destination and does not issue a managed destination lease. Changing the workspace + automation connection or its generation invalidates the old binding. +- Raw `git push` and agentctl push use the fork destination; base fetches and change-request targeting remain + canonical. Existing ordinary and already-open remote-contribution tasks do not regress. + +## Verification + +```bash +cd apps/backend +rtk go test ./internal/backendapp ./internal/orchestrator/executor ./internal/agent/runtime/lifecycle ./internal/agentctl/server/process -run 'Test.*(ContributionDestination|ForkRouting|BrokerScope|ReconcileGitHubOrigin)' +``` + +## Files likely touched + +- `apps/backend/internal/orchestrator/executor/executor_resume.go` +- `apps/backend/internal/orchestrator/executor/executor_credentials.go` +- `apps/backend/internal/orchestrator/executor/executor_execute.go` +- focused executor resolution and credential tests +- `apps/backend/internal/backendapp/services.go` +- `apps/backend/internal/backendapp/services_github_broker_test.go` +- lifecycle request/config/materialization files and tests for Worktree, Local, Docker, SSH, and Sprites +- `apps/backend/internal/agentctl/server/process/git.go` +- `apps/backend/internal/agentctl/server/process/git_test.go` +- agentctl workspace configuration and tracker tests + +## Dependencies + +Task 02's persisted, server-authored destination on the canonical attachment. + +## Parallelism + +Sequential security and runtime slice. Credential authorization must land with routing so no accepted scope +is unused and no enabled push path lacks authorization. + +## Inputs + +- Spec: canonical origin, exact managed scope, restart reconstruction, and local-row exemption. +- ADRs: task-bound destination plus existing remote-contribution remote/lease pattern. +- Existing patterns: `issueGitHubCredentialScopes`, `githubBrokerScopeAuthorizer`, contribution setup + scripts, `GitOperator` explicit remote/branch fields, and managed-origin reconciliation. + +## Risks + +- A valid metadata map alone does not prove task/session ownership; retain every existing broker ownership + check before matching the destination. +- Shared Git repositories/worktrees need collision-resistant remotes and identity checks before reuse. +- Preserve the local checkout exemption. Only Kandev-managed provider clones are reconciled. +- Do not alter `gh` shim fallback order or add ambient authentication after a managed mismatch. + +## TDD sequence + +1. Add failing scope-authorization and temporary-Git routing/resume tests. +2. Thread the typed binding through launch configuration without primitive field sprawl. +3. Generalize remote setup and push routing, then enable the second scope. +4. Refactor common contribution routing and rerun ordinary plus remote-contribution regressions. + +## Output contract + +Report authorization proofs, runtime/executor coverage, origin/upstream behavior, files changed, red/green +commands, remaining risks, divergence, and task/plan status updates. + +## Completion + +Completed 2026-08-13. The destination is projected through executor, lifecycle, materialization, Worktree, +Local, Docker, SSH, Sprites, and agentctl paths. Runtime setup adds one exact push remote and branch +pushRemote while preserving canonical `origin` and pull/upstream behavior. Managed credential scopes and +the broker authorizer prove the exact task-bound destination. Raw pushes, agentctl pushes, and GitHub PR +creation use the destination and canonical target respectively; malformed or conflicting state fails closed. +Lease issuance and redemption also revalidate the credential binding and live source/target/parent IDs, and +executor-owned policy or explicit tokens remove the managed route. + +Verification: `rtk make -C apps/backend test` and `rtk make -C apps/backend lint` both passed. Focused +runtime, credential, lifecycle, Git, PR, and broker tests passed before the full suite; the new process test +also verifies the explicit `--repo kdlbs/kandev --head owner:branch` invocation. diff --git a/docs/plans/improve-kandev-managed-fork/task-04-workflow-ui-integration-and-docs.md b/docs/plans/improve-kandev-managed-fork/task-04-workflow-ui-integration-and-docs.md new file mode 100644 index 0000000000..763d500585 --- /dev/null +++ b/docs/plans/improve-kandev-managed-fork/task-04-workflow-ui-integration-and-docs.md @@ -0,0 +1,99 @@ +--- +id: "04-workflow-ui-integration-and-docs" +title: "Workflow UI integration and docs" +status: complete +wave: 4 +depends_on: ["03-runtime-credentials-and-fork-routing"] +plan: "plan.md" +spec: "../../specs/improve-kandev/spec.md" +--- + +# Task 04: Workflow, UI, Integration, and Docs + +## Acceptance + +- The managed PR workflow never runs `gh repo fork` or rewrites remotes. It pushes through the prepared + upstream and creates the PR with explicit canonical repository, base, fork owner, and head branch. The + executor-owned compatibility branch remains explicit and cannot be selected after managed failure. +- Desktop and mobile Improve Kandev dialogs block implementation reports for an unsupported managed + automation configuration, translate stable backend reason codes into actionable copy, and keep Open + issue usable. +- Focused integration proves creation, launch, push, resume, credential authorization, canonical origin, + and PR targeting end to end. Public reference docs describe the behavior and limitation. + +## Verification + +```bash +cd apps/backend +rtk go test ./config/workflows ./internal/improvekandev ./internal/orchestrator/executor ./internal/backendapp -run 'Test.*(ImproveKandev|ContributionDestination|ManagedFork)' +cd apps && rtk pnpm --filter @kandev/web test -- improve-kandev +cd apps/web && rtk pnpm e2e:run improve-kandev.spec.ts +cd apps/web && rtk pnpm e2e:run --project mobile-chrome mobile-improve-kandev.spec.ts +rtk node --test scripts/validate-public-docs.test.mjs +rtk node scripts/validate-public-docs.mjs +``` + +## Files likely touched + +- `apps/backend/config/workflows/improve-kandev.yml` +- `apps/backend/config/workflows/loader_test.go` +- focused backend integration test files +- `apps/web/lib/api/domains/improve-kandev-api.ts` +- `apps/web/components/improve-kandev-dialog-model.ts` +- `apps/web/components/improve-kandev-dialog-create.tsx` +- associated component/model tests and English/translated locale catalogs +- `apps/web/e2e/tests/improve-kandev.spec.ts` +- `apps/web/e2e/tests/mobile-improve-kandev.spec.ts` +- `docs/public/integrations.md` +- `docs/public/git-operations.md` if its ordinary-task push reference needs a destination exception + +## Dependencies + +Task 03's working destination remote, push target, and managed credential scopes. + +## Parallelism + +Sequential final integration. The prompt and UI must describe behavior already enforced by the backend. + +## Inputs + +- Spec: explicit managed PR target/head, guarded executor-owned compatibility, blocked managed + configuration, issue-only bypass, and desktop/mobile parity. +- Mobile exemplar: the existing Improve Kandev dialog and mobile Improve Kandev Playwright flow. No new + drawer, route, scroll owner, or touch interaction is introduced. +- Docs guidance: `integrations.md` and `git-operations.md` remain reference pages. + +## Risks + +- `gh pr create` must receive explicit target and head identity; canonical `origin` alone cannot identify + an unpublished cross-fork head reliably. +- New user-facing copy must use i18n and must not contain an em dash. +- E2E must assert the user outcome through the dialog, not only the bootstrap JSON, and mobile must retain + viewport containment and no document horizontal overflow. +- Run Playwright against a fresh production build through the managed runner and confirm both desktop and + mobile projects discover tests. + +## TDD sequence + +1. Add failing loader, component/model, integration, desktop, and mobile assertions. +2. Rewrite the workflow prompt and expose the typed blocked state through shared UI logic. +3. Update public references and refactor repeated status rendering. +4. Run focused desktop/mobile suites, public-doc validation, and the plan's final audit. + +## Output contract + +Report workflow commands, desktop/mobile behavior, integration evidence, public docs classification, files +changed, red/green commands, remaining risks, divergence, and task/plan status updates. + +## Completion + +Completed 2026-08-13. The Improve Kandev prompt now uses the prepared managed route, preserves canonical +`origin`, blocks executor-owned recovery after managed preparation failure, and creates the PR with an +explicit canonical repository and fork-owner head. The UI exposes translated ready/creatable/blocked +states using a stable fork reason code and keeps issue reporting available. Desktop Playwright passed +15/15 tests, including the blocked managed state; mobile Playwright passed 1/1 with the existing 44px +touch target and overflow assertions. + +Public documentation classification: reference updates only in `docs/public/integrations.md` and +`docs/public/git-operations.md`; no new page or navigation entry was required. The public-doc test suite +passed (60 tests), and the published-doc validator passed for 41 pages. diff --git a/docs/public/git-operations.md b/docs/public/git-operations.md index 2f9af538e6..54f8800a71 100644 --- a/docs/public/git-operations.md +++ b/docs/public/git-operations.md @@ -20,6 +20,13 @@ Use the task's **Changes** panel to inspect, stage, discard, commit, push, reset The repository must be a valid Git checkout in the executor workspace and the session's `agentctl` must be reachable. Remote commands use the remote named `origin`; configure its URL and credentials before relying on Pull, Push, Rebase, Merge, or change-request creation. +Managed Improve Kandev tasks are the exception to the ordinary single-remote push description. The +canonical `origin` still identifies `kdlbs/kandev` for pulls, base comparisons, issue lookup, and +the pull-request target. Before launch, Kandev prepares one exact fork remote for the task branch +and configures Git to use it for pushes. Run ordinary **Push** or `git push`; do not rename `origin` +or replace it with a fork. The PR workflow uses the canonical repository and an explicit +`:` head. Other tasks keep the normal `origin` push behavior. + These UI operations enter through Kandev's `/ws` endpoint, which currently has no backend authentication. Anyone who can reach an unprotected backend can invoke destructive Git actions with the executor's permissions. Keep Kandev on loopback or behind an authenticated, origin-protected reverse proxy; see [WebSocket API](websocket-api.md). Credentials are resolved where `agentctl` runs. A host SSH agent, credential helper, `gh` login, or `az` login is not automatically available inside every Docker, SSH, or remote executor. Give the executor only the repository access it needs and test with a disposable branch. See [Executors](executors.md) for executor-specific credential handling. @@ -66,7 +73,7 @@ All operations below run in the selected repository workspace. | UI operation | Effective Git behavior | Important consequence | |--------------|------------------------|-----------------------| | Pull | `git pull origin BRANCH`, optionally with `--rebase`. | Uses the current branch when any upstream exists. With no upstream it falls back to `origin/main`, then `origin/master`, then the current branch. It does not parse an upstream that points to a differently named remote branch. | -| Push | `git push origin CURRENT_BRANCH`; adds `--set-upstream` when requested or no upstream exists. | Force Push uses `--force-with-lease`, not unconditional `--force`. It still rewrites remote history when the lease is valid. | +| Push | `git push origin CURRENT_BRANCH`; adds `--set-upstream` when requested or no upstream exists. | Force Push uses `--force-with-lease`, not unconditional `--force`. It still rewrites remote history when the lease is valid. Managed Improve Kandev tasks use the branch's prepared fork push remote instead of `origin`. | | Rebase | Fetches `origin BASE`, then rebases onto `origin/BASE`. | Rewrites local commits. If conflict files are detected from Git output, Kandev attempts `git rebase --abort` automatically and returns the file list. | | Merge | Fetches `origin BASE`, then merges `origin/BASE`. | Conflicts are deliberately left in the worktree. Resolve and commit them, or use Abort Merge. | | Abort | Runs `git merge --abort` or `git rebase --abort`. | Fails when that operation is not in progress or the repository cannot be restored. | @@ -106,17 +113,21 @@ Do not reset or amend commits already consumed by other users unless you intend ## Create a pull request or merge request -**Create PR** first runs: +For an ordinary task, **Create PR** first runs: ```bash git push --set-upstream origin HEAD ``` +For a managed Improve Kandev task, the prepared branch push remote replaces +`origin` for this push. Keep `origin` canonical and create the GitHub pull +request with the explicit fork head shown below. + It then selects a provider from the `origin` hostname: | Provider | Required runtime tools | Creation behavior | |----------|------------------------|-------------------| -| GitHub | Authenticated `gh` CLI | `gh pr create` with title, body, current head, optional base, and optional `--draft`. | +| GitHub | Authenticated `gh` CLI | `gh pr create` with title, body, current head, optional base, and optional `--draft`. Managed Improve Kandev uses `--repo kdlbs/kandev` and an explicit `:` head. | | GitLab | Authenticated `glab` CLI or the active workspace's matching GitLab PAT | Reuses an existing open MR for the same source/target or creates one with title, description, current source, explicit or project-default target, and optional draft state. The configured GitLab origin must match the HTTPS or SSH `origin`. | | Azure Repos | Authenticated `az` CLI plus the `azure-devops` extension | `az repos pr create` with parsed organization, project, repository, source, optional target, and optional draft. | diff --git a/docs/public/integrations.md b/docs/public/integrations.md index a4406a6bc8..53d65c5a46 100644 --- a/docs/public/integrations.md +++ b/docs/public/integrations.md @@ -122,6 +122,16 @@ path on desktop hover or focus and in a touch-accessible drawer on mobile. configuration. A separate broker-aware shim handles `gh`. The task receives neither the stored PAT nor an App private key. An executor-profile `GH_TOKEN` or `GITHUB_TOKEN` deliberately takes precedence for that task. + +For a managed **Improve Kandev** task, Kandev keeps the task attached to the canonical +`kdlbs/kandev` repository. Before the first launch, the workspace automation connection resolves +one exact writable fork as the task's contribution destination. Kandev stores that destination +without credentials, adds a dedicated push remote, and issues one additional broker lease for the +exact fork. The canonical `origin` remains the pull, issue, and pull-request target. A direct write +connection does not need a fork. An App connection without direct write access cannot own an +automatic personal fork, so managed fork preparation fails closed; the Improve Kandev issue-only +option remains available. + - **Inherit executor Git credentials** is the default for newly created workspaces and does not install Kandev's broker helper or `gh` shim. Local and Worktree tasks use credentials already visible to the host Git process (including SSH). diff --git a/docs/specs/improve-kandev/spec.md b/docs/specs/improve-kandev/spec.md index 5770814dce..8c6ead7d86 100644 --- a/docs/specs/improve-kandev/spec.md +++ b/docs/specs/improve-kandev/spec.md @@ -1,11 +1,14 @@ --- status: building created: 2026-04-29 +amended: 2026-08-13 owner: Carlos Florencio --- # Improve Kandev +Decision: [ADR-2026-08-12-task-bound-fork-destinations](../../decisions/2026-08-12-task-bound-fork-destinations.md). + ## Why Users who hit a bug or have a feature idea today have no in-app way to report it, @@ -53,6 +56,35 @@ the user's own agent picks up immediately — turning every report into a contri the URLs so the user can verify the change in a second kandev instance. - **PR** — agent invokes the `pr` skill to commit, push, and open a pull request against `main` in `kdlbs/kandev`. +- Creating an Improve Kandev implementation task with managed task credentials + prepares its publication route before the first agent launches. Kandev uses + the dedicated workspace's automation connection to check direct write + access. Without direct access, it reuses or creates that automation actor's + fork, verifies that the fork's parent is exactly `kdlbs/kandev`, verifies + write access, and stores a versioned, credential-free + `contribution_destination` binding on the task's canonical repository + attachment. +- The repository row and managed checkout `origin` remain + `https://github.com/kdlbs/kandev`. A bound fork is a dedicated push remote, + not another repository attachment and not the repository identity used for + issues or pull requests. Managed task credentials authorize the canonical + repository plus that exact fork only; they never fall back to executor or + host credentials. +- In managed mode, the **PR** step pushes the current branch through the + prepared destination remote and creates the pull request with explicit target + repository, base, fork owner, and head branch. It does not run `gh repo + fork`, rename `origin`, or depend on `gh` inferring a cross-fork topology. + Executor-owned mode retains an explicitly separate agent-managed fork path + because Kandev cannot prove the identity behind an opaque executor credential. +- A managed destination is bound to the exact workspace automation source, + credential generation, and, for an App installation, registration, + installation, and App credential generation. Kandev checks this binding at + lease issuance and redemption. Policy changes, explicit executor tokens, + and connection changes cannot reuse the old destination. +- Fork discovery uses the canonical repository's fork network after the + canonical-name fast path. A renamed fork is reusable only after an + authoritative provider read confirms its owner, provider ID, writable + permissions, and exact parent identity. - The `improve-kandev` workflow is hidden from the workflow management page in workspace settings and from the workflow picker in the standard task-create dialog, except in the dedicated `Improve Kandev` workspace itself where the @@ -71,8 +103,10 @@ the user's own agent picks up immediately — turning every report into a contri checks for sensitive data and likely duplicates, then publishes the issue to `kdlbs/kandev` with the matching template and reports the issue URL. The agent must ask follow-up questions instead of inventing missing required details. -- A pre-flight check surfaces `gh auth` status from `/api/v1/system/health` and - prevents submission with a clear error when GitHub auth is missing. +- For executor-owned credentials, a pre-flight check surfaces `gh auth` status + from `/api/v1/system/health` and prevents submission with a clear error when + GitHub CLI auth is missing. Managed credentials use the workspace automation + capability and bound destination result instead of executor `gh` auth. - An account that cannot fork `kdlbs/kandev` is blocked from the implementation workflows but may still use the issue-only workflow. @@ -92,6 +126,18 @@ the user's own agent picks up immediately — turning every report into a contri - `workflow_id: string` — the workspace instance of `improve-kandev`. - `issue_workflow_id: string` — the workspace instance of `report-kandev-issue`. +- Under managed task credentials, the bootstrap GitHub identity and + fork-capability probe use the selected Improve Kandev workspace automation + connection, which is also the source for task leases. Fork status + distinguishes direct write, an exact ready fork, a fork that can be created + during task creation, and a blocked automation configuration. Executor-owned + mode keeps its separately labeled executor capability probe. Blocked + responses include user-facing recovery guidance and still allow the + issue-only workflow. +- Bootstrap records the canonical provider repository ID whenever the + provider identity can be resolved. Fork failures cross the API as stable + `fork_reason_code` values; the frontend translates those codes and does not + render backend error text. - Both workflow IDs refer to hidden, workspace-scoped workflow instances in the returned workspace and are safe to request repeatedly. @@ -106,6 +152,11 @@ the user's own agent picks up immediately — turning every report into a contri - The two hidden workflow instances live in the dedicated workspace and remain idempotent: opening the dialog again reuses the existing workflow for each template. +- `contribution_destination` is stored only on the canonical + `task_repositories` attachment. It contains a version, provider, and exact + credential-free fork identity/URL plus a non-secret automation connection + binding. Tokens, leases, credential helpers, and ambient Git remotes are + never persisted. ## Failure modes @@ -124,6 +175,26 @@ the user's own agent picks up immediately — turning every report into a contri - A fork restriction blocks only **Bug fix** and **Feature request** submission. Switching to **Open issue** clears that restriction because publishing an issue requires neither a fork nor push access. +- In managed mode, if direct write is unavailable and the workspace automation + connection cannot own a fork, task creation is blocked before persistence or + launch. This includes a GitHub App installation without target write access; + Kandev does not create a fork with a personal identity and push it with + unrelated App credentials. +- If `/kandev` exists but is not a fork of `kdlbs/kandev`, or + if GitHub does not make a newly created fork readable and writable within the + bounded preparation window, creation fails without starting an agent. +- If the recorded canonical or target provider ID changes, or the provider no + longer reports the target as a fork of the recorded canonical repository, + managed lease issuance and redemption fail closed. A deleted-and-recreated + repository at the same path cannot inherit an old lease. +- If an existing fork was renamed, the next resolution searches the canonical + fork network and reuses it only after an authoritative repository read. It + does not create a second same-owner fork. +- Bootstrap returns stable fork reason codes. Desktop and mobile translate + those codes, while **Open issue** remains available for blocked states. +- A malformed, unknown-version, or target-mismatched destination binding fails + closed during launch or resume. An unrelated fork can never be redeemed as a + managed credential scope. ## Scenarios @@ -159,9 +230,51 @@ the user's own agent picks up immediately — turning every report into a contri auto-starts with the test step prompt, runs `make install` and `make dev`, and reports the assigned URLs back to the user. +- **GIVEN** the Improve Kandev workspace uses managed credentials and its human + automation actor cannot push to `kdlbs/kandev`, **WHEN** an implementation + task is created, **THEN** Kandev resolves or creates the actor's exact fork, + persists it as the task-bound contribution destination, and launches the + task with credential scopes for only `kdlbs/kandev` and that fork. + - **GIVEN** the user has verified the change works, **WHEN** they move the task to **PR**, **THEN** the agent invokes the `pr` skill and opens a pull request - against `main` in `kdlbs/kandev`. + against `main` in `kdlbs/kandev`, while canonical `origin` remains unchanged + and the branch is pushed through the prepared fork remote. + +- **GIVEN** `/kandev` exists but its parent is not + `kdlbs/kandev`, **WHEN** an implementation task is created, **THEN** Kandev + blocks creation with a destination-conflict error and does not authorize or + overwrite that repository. + +- **GIVEN** the automation actor's existing fork was renamed after an earlier + Improve Kandev task, **WHEN** another implementation task is created, + **THEN** Kandev reuses that verified fork from the canonical fork network + without calling fork creation again. + +- **GIVEN** a managed destination was bound to one workspace connection, + **WHEN** policy changes to executor-owned, an explicit `GH_TOKEN` or + `GITHUB_TOKEN` is supplied, or the connection generation/login changes, + **THEN** Kandev does not activate the old destination lease. + +- **GIVEN** the destination path is deleted and recreated with another + provider ID or parent, **WHEN** the task lease is issued or redeemed, + **THEN** Kandev rejects it even when the owner/name path is unchanged. + +- **GIVEN** the workspace automation connection is a GitHub App that lacks + write access to `kdlbs/kandev` and task access is managed, **WHEN** the user + selects an implementation report kind, **THEN** the dialog shows a blocking + managed-credential recovery message on desktop and mobile, while **Open + issue** remains available. + +- **GIVEN** task access inherits executor credentials, **WHEN** an Improve + Kandev implementation task is created, **THEN** Kandev does not claim a + server-authored fork identity for the opaque executor account and the PR step + retains its executor-owned fork preparation path. + +- **GIVEN** a managed Improve Kandev task is resumed, **WHEN** Kandev + reconciles its provider-backed checkout, **THEN** `origin` is restored to the + canonical HTTPS repository and the same validated contribution remote and + fork lease are reconstructed without consulting ambient Git configuration. - **GIVEN** the standard task-create dialog or the workspace workflows settings page is open, **WHEN** the page lists workflows, **THEN** neither @@ -195,9 +308,11 @@ the user's own agent picks up immediately — turning every report into a contri task uses the workspace's visible workflow rather than inheriting the hidden task-detail workflow. -- **GIVEN** the user has not configured `gh auth`, **WHEN** they open the - Improve Kandev dialog, **THEN** the dialog shows a blocking error referencing - the health-check result and disables the submit button. +- **GIVEN** the task uses executor-owned credentials and the user has not + configured `gh auth`, **WHEN** they open the Improve Kandev dialog, **THEN** + the dialog shows a blocking error referencing the health-check result and + disables the submit button. Managed credentials use the workspace + automation capability instead. - **GIVEN** the user opens the dedicated workspace's workflows settings page, **WHEN** the page loads, **THEN** it lists `improve-kandev` with steps @@ -292,10 +407,10 @@ the user's own agent picks up immediately — turning every report into a contri - Rate limiting, quotas, or one-task-at-a-time guards. - Log redaction or sensitive-value scrubbing. - Manual upstream-URL configuration. The user is expected to have `gh` - authenticated; during the PR step, the agent automatically forks - `kdlbs/kandev` to the user's account when they lack write access on the - upstream repo, and pushes directly otherwise. Manual fork/remote setup - remains an optional advanced workflow but is not part of this feature. + authenticated only when executor-owned credentials are selected. In managed + mode Kandev prepares the exact fork destination before launch; manual + fork/remote setup remains an optional advanced workflow but is not part of + this feature. - A generic feedback inbox or report archive; this feature produces tasks, not stored reports. - Cleanup of the temporary log bundle directory; left to OS/temp policy. diff --git a/docs/specs/integrations/github-authentication.md b/docs/specs/integrations/github-authentication.md index 6b14486439..abb6dddda2 100644 --- a/docs/specs/integrations/github-authentication.md +++ b/docs/specs/integrations/github-authentication.md @@ -1,7 +1,7 @@ --- status: building created: 2026-07-19 -amended: 2026-08-07 +amended: 2026-08-12 owner: Kandev --- @@ -52,6 +52,12 @@ automation under different GitHub Apps without operating separate Kandev deploym workspace automation connection. **Inherit executor Git credentials** injects no GitHub broker helper or `gh` shim: Local and Worktree tasks use host-visible Git/SSH credentials, while remote tasks use credentials configured in that executor. +- A task that is explicitly created for a pre-PR fork contribution under managed task credentials + may carry one server-authored, versioned `contribution_destination` on its canonical repository + attachment. Managed routing may issue one additional lease for that exact fork. The fork is a + push destination only: canonical repository identity, `origin`, issue lookup, and pull-request + targeting remain unchanged. Executor-owned identities are opaque and do not receive a + workspace-authored destination binding. - Every newly created workspace attempts to persist **Inherit executor Git credentials** as its initial task policy. After a successful settings write, if creation is performed by an internal trusted caller, an auth-disabled synthetic administrator, or a real administrator while host @@ -83,6 +89,9 @@ automation under different GitHub Apps without operating separate Kandev deploym that result for primary-repository configuration, multi-repository configuration, and credential routing. Origin reconciliation is serialized per managed checkout, compares the current and desired canonical URLs, and performs no write when they already match. +- Repository preparation validates any contribution destination before issuing credentials, adds a + collision-resistant dedicated fork remote, and reconstructs it on launch and resume. It never + accepts a fork inferred from the checkout's current remotes or a caller-provided repository name. - Git failures while inspecting or reconciling a managed checkout preserve a bounded, credential-redacted diagnostic. Git's dubious-ownership failure is classified as a service/data ownership mismatch with guidance to restore the intended Kandev service account or reconcile the @@ -92,6 +101,10 @@ automation under different GitHub Apps without operating separate Kandev deploym only in memory. PAT/CLI tokens retain their provider-granted scope once delivered to a trusted agent subprocess. GitHub HTTPS and the broker-aware `gh` shim fail closed rather than consulting another ambient helper after a managed-helper failure. +- A managed pre-PR fork destination must be writable by the same workspace automation source that + supplies the task leases. Human PAT and named CLI automation actors may own or create their fork. + An App installation without direct target write access is not silently paired with a user's + personal fork or personal token. - Managed Git helper execution does not depend on the post-startup `PATH`: Git resolves an absolute Kandev-owned `agentctl` executable published before the first managed Git operation. Local and Worktree preparation binds the helper to the standalone launcher's absolute executable @@ -429,6 +442,10 @@ post-signature processing failures produce `failing`; a later valid successful d - The effective task Git environment is runtime-only. It is copied only after the existing task/session or task-environment ownership check, is never persisted in task metadata or terminal records, and is never written to logs, errors, browser payloads, or process arguments. +- The broker accepts a fork owner/repository only when it exactly matches a valid + `contribution_destination` or `remote_contribution` on the same authorized task-repository + attachment. Unknown binding versions, malformed URLs, cross-workspace rows, unrelated forks, and + target mismatches fail closed. - Indexed Git configuration is validated and composed as a single ordered block at environment merge boundaries. Kandev never replaces a complete inherited block merely by assigning its own `GIT_CONFIG_COUNT`; managed helper reset semantics are expressed as later Git config entries. @@ -448,6 +465,9 @@ post-signature processing failures produce `failing`; a later valid successful d account; selecting another stored login fails with guidance to activate it or upgrade the CLI. - If the managed `agentctl` helper, broker, or `gh` shim is unavailable, the command fails with a managed-credential error and does not fall through to another HTTPS helper or interactive prompt. +- If a contribution task cannot prove direct target write access or prepare an exact fork writable + by the workspace automation connection, task creation fails before launch. A same-name repository + that is not a fork of the canonical target is reported as a conflict and is never overwritten. - If an authorized task terminal, passthrough PTY, or task-scoped command cannot receive its effective managed Git environment, Kandev fails that process start before it runs the requested command. It does not silently fall back to an ambient credential helper or host `gh` login.