Files
kandev/apps/backend/internal/agent/runtime/lifecycle/executor_backend.go
T
João Salavisa d83c1dd982 feat(diff): use stored base_branch for diff stats + per-task compare picker (#1273)
* feat(diff): use stored base_branch for diff stats + per-task compare picker

Task-card additions/deletions and the Changes panel were computed against a
hardcoded origin/main → master priority list inside agentctl. For tasks
branched off a release/staging branch this inflated the counts by every
commit since the actual fork point, and the Changes panel grouped unrelated
files alongside the task's real work.

This wires the per-repo task_repositories.base_branch end-to-end into the
agentctl WorkspaceTracker so each repo's diff stats resolve against the
recorded base instead of the integration-branch fallback. A picker on the
Changes panel branch hover card lets the user change the base mid-task; the
new value is persisted via PATCH /tasks/:id/repositories/:repo_id, pushed
live to the running agentctl instance, and the session's cached
base_commit_sha is cleared so the commits panel + cumulative diff refetch
against the new base without a session restart.

Falls back to the existing origin/main → master list for legacy tasks
(empty base_branch) and to the branch tip when no merge-base exists with
HEAD (unrelated histories — local rebase backups, etc.) so the stats and
commits panels stay anchored on the same SHA in every failure mode.

* fix(agentctl): sanitize user-controlled base_branch refs before git exec

CodeQL flagged the picker's user-controlled base_branch payload reaching
exec.Command("git", ..., baseBranch) inside workspace_git_cmd.go as a
command-built-from-user-controlled-source vulnerability. A malicious caller
could smuggle a leading-"-" argument like "-upload-pack=..." that git
itself reinterprets as a flag, even though exec.Command keeps args
separate from the shell.

Adds an IsSafeGitRef sanitizer at every boundary the value crosses on its
way to git: the service write path, the agentctl HTTP handlers
(/workspace/base-branches, /git/log, /git/cumulative-diff), and
WorkspaceTracker.SetBaseBranch itself. Unsafe values are rejected at the
service tier and silently downgraded to the no-override fallback inside
agentctl so a misbehaving caller can't half-break the tracker.

* fix(diff): address PR review blockers and CI lint

- Split UpdateRepositoryBaseBranch into validate/load/apply helpers to drop
  cyclomatic complexity below the cyclop limit that gated Run Backend Tests.
- Replace direct m.cfg.BaseBranches reads/writes with getBaseBranches /
  setBaseBranches under a dedicated RWMutex. UpdateBaseBranches used to
  write under repoTrackersMu while rescan + lazy-subpath callers read under
  workspaceTrackersMu, leaving an unsynchronized data race on the map.
- Drop the false 'PR target is auto-wired' claim from the MCP tool
  description — GitOperator.CreatePR takes baseBranch as an explicit arg
  and never reads the tracker's value.
- Picker no-op guard now compares against currentBase instead of storedBase
  so selecting the displayed fallback (legacy task before any picker
  change) is treated as no-op instead of triggering a spurious save.
- HTTP handler now maps non-validation service errors to 500 instead of
  400, classifying via the existing isValidationError helper plus the new
  unsafe-git-ref message.

* fix(diff): regex-based ref sanitiser at every git subprocess sink

CodeQL kept flagging workspace_git_cmd.go:43 even after SetBaseBranch
rejected unsafe values — the taint tracker doesn't follow sanitised stores
through struct fields, so it still saw user input flowing into
exec.Command("git", ..., baseBranch).

Switches IsSafeGitRef to a single regex check (a pattern CodeQL recognises
as a sanitiser barrier) and adds SanitizeGitRef calls inline at every
agentctl call site that splices a user-controlled ref into a git arg:

  - workspace_git_status.resolveStoredRef (before the rev-parse probes)
  - workspace_git_status.computeBaseCommit (before merge-base/rev-parse)
  - workspace_git_status.getAheadBehindCounts (before rev-list)
  - agentctl/server/api/git.go runGitLogForRepo + handleGitCumulativeDiff
    (before computeMergeBase/GetRevParse on the HTTP TargetBranch)

Behaviour is unchanged for valid refs — only narrows the allowlist (now
[A-Za-z0-9_./-]) so the sanitiser barrier is recognisable to static
analysis. SetBaseBranch's field-store sanitiser stays as defence in depth.

* fix(diff): transform sanitiser at every map read for CodeQL taint flow

CodeQL still flagged workspace_git_cmd.go:43 because:
- handleSetBaseBranches used an IsSafeGitRef bool guard before a verbatim
  map[k] = v copy. CodeQL recognises regex-transforming sanitisers but not
  bool-guarded passthroughs.
- lookupBaseBranch returned m.cfg.BaseBranches[k] verbatim. Even though
  the map was sanitised on write, taint flow across map stores + field
  stores loses the sanitised state in the analyser.

Switches both sites to the transforming SanitizeGitRef helper so every
read produces a freshly-sanitised value before it flows to a downstream
git argument. Behaviour is unchanged for valid refs.

* fix(diff): regex transformer sanitiser + guard live push from empty map

CodeQL still flagged the previous SanitizeGitRef because it returned the
input string unchanged on success — an identity passthrough that the taint
tracker doesn't recognise as a sanitiser barrier.

Switches to safeGitRefPattern.FindString so the output comes from the
regex match (not the parameter). IsSafeGitRef now wraps SanitizeGitRef so
the predicate and the transformer share one allowlist.

Also addresses two Claude review blockers:

- collectTaskBaseBranches returns a nil/empty map when the task has no
  recorded base_branches; pushing that to agentctl wipes every tracker's
  override. Now skipped — the DB row is the source of truth for the next
  session launch.

- applyBaseBranchSideEffects now runs on context.WithoutCancel so an HTTP
  / WS request cancel after the task_repositories UPDATE commits can't
  leave session.base_commit_sha stale + the live agentctl tracker map
  un-pushed, drifting from the persisted row.

* fix(diff): share securityutil.IsValidBranchName + harden error classification

CodeQL still flagged the picker's regex sanitiser as taint passthrough.
Refactors SanitizeGitRef / isSafeBaseBranchRef to delegate to the existing
securityutil.IsValidBranchName helper that agentctl's Rebase / Merge /
RenameBranch already use without being flagged. The shared allowlist also
fixes a Claude review blocker: 'feature/@2024' used to pass the service
validator but get dropped by the agentctl-side check at push time,
silently wiping every tracker's override.

Plus the two error-classification blockers Claude raised on the previous
HEAD:

- WS handler (task_ws_handlers.go wsUpdateTaskRepository) now distinguishes
  validation errors (surfaced verbatim) from internal faults (opaque
  500-style message). HTTP path already did this via the existing
  isValidationError helper.
- MCP handler (handlers.handleUpdateRepositoryBaseBranch) gets the same
  treatment via a new isValidationError local to the mcp/handlers
  package so raw service error strings stop leaking across the MCP
  boundary.

* fix(diff): inline securityutil.IsValidBranchName at every git subprocess sink

CodeQL's interprocedural taint tracking wasn't recognising SanitizeGitRef
as a sanitiser even after it delegated to securityutil.IsValidBranchName
— the wrapper helper + map/field hops between source and sink kept the
flow tainted in the analyser.

Inlines the IsValidBranchName allowlist check (already used inline by
Rebase/Merge in git.go without being flagged) at every call site that
splices a user-controlled ref into a git subprocess argument:

- workspace_git_status.resolveStoredRef (before rev-parse --verify)
- workspace_git_status.computeBaseCommit (before merge-base / rev-parse)
- workspace_git_status.getAheadBehindCounts (before rev-list)
- agentctl/server/api/git.go runGitLogForRepo + handleGitCumulativeDiff
- agentctl/server/api/workspace_base_branches.go handleSetBaseBranches

Behaviour unchanged: same allowlist as Rebase / Merge / RenameBranch
already use. SanitizeGitRef / IsSafeGitRef survive as defence-in-depth
helpers for callers that want a one-line check; they delegate to the same
securityutil helper so the rules stay aligned.

* fix(diff): inline regexp.MatchString at every git subprocess sink

CodeQL still flagged workspace_git_cmd.go:43 even with the inline
securityutil.IsValidBranchName helper — the taint tracker apparently does
not follow into helper functions for the go/command-injection rule on
this path.

Hoists the regex to package-scope vars (safeBranchRefPattern in both
process and api packages) and calls regexp.MatchString directly at each
sink site. Behaviour is identical to securityutil.IsValidBranchName; only
the call form changes.

* fix(diff): SHA-shape allowlist on BaseCommit before downstream git diff

CodeQL still flagged workspace_git_cmd.go:43 because update.BaseCommit
is the OUTPUT of an earlier merge-base / rev-parse call whose argument
list contained a user-controlled branch ref — the analyser treats that
command's output as still tainted even when the input ref was sanitised.

Adds an inline 40-hex SHA-shape allowlist check in enrichWithBranchDiff
right before `git diff --numstat <update.BaseCommit>` so the sanitiser
barrier sits in the same function as the next subprocess call. Behaviour
is unchanged: merge-base / rev-parse already produce SHA-shaped output.

* fix(diff): async tracker refresh + tighten init spread order

Two CodeRabbit/Claude review fixes:

- UpdateBaseBranches stamps each tracker's new baseBranch synchronously
  then runs RefreshGitStatus on a background goroutine instead of inside
  the picker-save HTTP handler. Per-tracker git subprocesses can each
  take ~3-5 calls; serialising them on the request goroutine made the
  PATCH response wait for every refresh to finish. The picker UI re-fetches
  via the existing WS stream once Refresh emits, so the handler doesn't
  need to block.

- updateTaskRepositoryBaseBranch now spreads options.init BEFORE the
  required method/body fields so an upstream caller's init overrides
  can't replace the PATCH method or the base_branch payload. Matches
  the convention deleteTask / moveTask already follow.

* fix(diff): narrow mcp isValidationError + opaque 500 + post-sanitize log count

Three final Claude review threads on the latest commit:

- mcp/handlers isValidationError drops the broad 'invalid' substring so
  DB / IO errors whose messages happen to contain 'invalid' don't
  misclassify as ErrorCodeValidation.

- task_http_handlers httpUpdateTaskRepository now logs the raw service
  error server-side and returns an opaque 'failed to update task
  repository' on the 500 path. Stops potential DB / IO detail leakage
  through the HTTP boundary.

- workspace_base_branches debug log reports accepted + rejected counts
  (post-sanitisation) instead of the pre-filter total so the metric
  actually reflects what hit UpdateBaseBranches.

---------

Co-authored-by: Kandev Agent <agent@kandev.dev>
2026-06-05 15:53:39 +01:00

359 lines
14 KiB
Go

// Package lifecycle provides agent runtime abstractions.
package lifecycle
import (
"context"
"strings"
"time"
"github.com/kandev/kandev/internal/agent/agents"
"github.com/kandev/kandev/internal/agent/executor"
agentctl "github.com/kandev/kandev/internal/agent/runtime/agentctl"
"github.com/kandev/kandev/internal/agentctl/server/process"
"github.com/kandev/kandev/internal/agentruntime"
v1 "github.com/kandev/kandev/pkg/api/v1"
)
// 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.
type ExecutorBackend interface {
// Name returns the runtime identifier (e.g., "docker", "standalone", "k8s")
Name() executor.Name
// HealthCheck verifies the runtime is available and operational
HealthCheck(ctx context.Context) error
// CreateInstance creates a new agentctl instance for a task.
// This starts the agentctl process/container with workspace access (shell, git, files).
// Agent subprocess is NOT started - use agentctl.Client.ConfigureAgent() + Start().
CreateInstance(ctx context.Context, req *ExecutorCreateRequest) (*ExecutorInstance, error)
// StopInstance stops an agentctl instance.
StopInstance(ctx context.Context, instance *ExecutorInstance, force bool) error
// RecoverInstances discovers and recovers instances that were running before a restart.
// Returns recovered instances that can be re-tracked by the manager.
RecoverInstances(ctx context.Context) ([]*ExecutorInstance, error)
// GetInteractiveRunner returns the interactive runner for passthrough mode.
// May return nil if the runtime doesn't support passthrough mode.
GetInteractiveRunner() *process.InteractiveRunner
// RequiresCloneURL reports whether this executor needs a git clone URL
// instead of a local filesystem path for repository access.
RequiresCloneURL() bool
// ShouldApplyPreferredShell reports whether the user's preferred shell
// should be injected into the agent environment.
ShouldApplyPreferredShell() bool
// IsAlwaysResumable reports whether sessions on this executor can be
// resumed even without an explicit resume token.
IsAlwaysResumable() bool
}
// McpServerConfig holds configuration for an MCP server.
// Type alias for agentctl.McpServerConfig to avoid conversion boilerplate.
type McpServerConfig = agentctl.McpServerConfig
// Metadata keys for runtime-specific configuration
const (
MetadataKeyMainRepoGitDir = "main_repo_git_dir"
MetadataKeyWorktreeID = "worktree_id"
MetadataKeyWorktreeBranch = "worktree_branch"
// Remote executor metadata keys
MetadataKeyRepositoryPath = "repository_path"
MetadataKeySetupScript = "setup_script"
MetadataKeyCleanupScript = "cleanup_script"
MetadataKeyRepoSetupScript = "repository_setup_script"
MetadataKeyBaseBranch = "base_branch"
// 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"
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
// for passthrough sessions, which restart the PTY to apply the new --model.
MetadataKeyModelOverride = "model_override"
// Office metadata keys
MetadataKeySkillManifestJSON = "skill_manifest_json"
// SSH runtime metadata keys (per-session, except SSHWorkdirRoot which is per-profile).
MetadataKeySSHHostAlias = "ssh_host_alias"
MetadataKeySSHHost = "ssh_host"
MetadataKeySSHPort = "ssh_port"
MetadataKeySSHUser = "ssh_user"
MetadataKeySSHHostFingerprint = "ssh_host_fingerprint"
MetadataKeySSHRemoteTaskDir = "ssh_remote_task_dir"
MetadataKeySSHRemoteSessionDir = "ssh_remote_session_dir"
MetadataKeySSHRemoteAgentctlPort = "ssh_remote_agentctl_port"
MetadataKeySSHRemoteAgentctlPID = "ssh_remote_agentctl_pid"
MetadataKeySSHLocalForwardPort = "ssh_local_forward_port"
MetadataKeySSHRemoteAgentctlURL = "ssh_remote_agentctl_url"
MetadataKeySSHWorkdirRoot = "ssh_workdir_root"
MetadataKeySSHProxyJump = "ssh_proxy_jump"
MetadataKeySSHIdentitySource = "ssh_identity_source"
MetadataKeySSHIdentityFile = "ssh_identity_file"
// MetadataKeySSHShell names the login shell used when running commands
// over SSH on the remote (probe, agentctl launch, install, setup
// scripts). Empty / unset falls back to "bash" at runtime — see
// WrapLoginShell. Stored per-profile so different profiles on the same
// host can use different shells; flows into req.Metadata via the
// standard executor-config merge in buildLaunchMetadata.
MetadataKeySSHShell = "ssh_shell"
)
// persistentMetadataKeys lists metadata keys carried forward from a previous
// ExecutorRunning record when a session is resumed. Keys not listed here
// (e.g., task_description, session_id) are treated as launch-time-only and
// are NOT copied on resume.
var persistentMetadataKeys = map[string]bool{
// Sprites runtime
MetadataKeySpriteName: true,
MetadataKeySpriteState: true,
MetadataKeySpriteCreatedAt: true,
MetadataKeyLocalPort: true,
// SSH runtime
MetadataKeySSHHost: true,
MetadataKeySSHPort: true,
MetadataKeySSHUser: true,
MetadataKeySSHHostFingerprint: true,
MetadataKeySSHRemoteTaskDir: true,
MetadataKeySSHRemoteSessionDir: true,
MetadataKeySSHRemoteAgentctlPort: true,
MetadataKeySSHRemoteAgentctlPID: true,
MetadataKeySSHLocalForwardPort: true,
MetadataKeySSHRemoteAgentctlURL: true,
MetadataKeySSHWorkdirRoot: true,
MetadataKeySSHProxyJump: true,
MetadataKeySSHIdentitySource: true,
MetadataKeySSHIdentityFile: true,
MetadataKeySSHShell: true,
// Executor type marker
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,
}
// persistentMetadataPrefixes lists key prefixes that should persist.
// Any key starting with one of these prefixes is carried forward.
var persistentMetadataPrefixes = []string{
"env_secret_id_", // Secret store UUIDs for profile env vars
}
// sessionScopedMetadataKeys lists metadata keys that point to per-session
// runtime resources (process PIDs, allocated ports, session directories on
// the remote). These keys ARE persisted across a SAME-session resume — that's
// how a backend restart reattaches to a still-running remote agent — but they
// MUST NOT be carried across SIBLING sessions on the same task. If they were,
// the second session would try to attach to the first session's agentctl
// process and end up sharing its ACP session and instance port.
var sessionScopedMetadataKeys = map[string]bool{
MetadataKeySSHRemoteSessionDir: true,
MetadataKeySSHRemoteAgentctlPort: true,
MetadataKeySSHRemoteAgentctlPID: true,
MetadataKeySSHLocalForwardPort: true,
MetadataKeySSHRemoteAgentctlURL: true,
}
// ShouldPersistMetadataKey returns true if the given metadata key should
// be carried forward when resuming a session from an ExecutorRunning record.
func ShouldPersistMetadataKey(key string) bool {
if persistentMetadataKeys[key] {
return true
}
for _, prefix := range persistentMetadataPrefixes {
if strings.HasPrefix(key, prefix) {
return true
}
}
return false
}
// IsSessionScopedMetadataKey reports whether key references per-session
// runtime resources that must not leak across sibling sessions on the same
// task environment.
func IsSessionScopedMetadataKey(key string) bool {
return sessionScopedMetadataKeys[key]
}
// FilterPersistentMetadata returns a copy of src containing only keys that
// should be carried forward across session resumes. Returns nil if no keys match.
func FilterPersistentMetadata(src map[string]interface{}) map[string]interface{} {
if src == nil {
return nil
}
filtered := make(map[string]interface{})
for k, v := range src {
if ShouldPersistMetadataKey(k) {
filtered[k] = v
}
}
if len(filtered) == 0 {
return nil
}
return filtered
}
// RemoteStatus describes runtime health/details for remote executors.
// It is intentionally generic so each executor can include extra details in Details.
type RemoteStatus struct {
RuntimeName agentruntime.Runtime `json:"runtime_name"`
RemoteName string `json:"remote_name,omitempty"`
State string `json:"state,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
LastCheckedAt time.Time `json:"last_checked_at"`
ErrorMessage string `json:"error_message,omitempty"`
Details map[string]interface{} `json:"details,omitempty"`
}
// RemoteSessionResumer is an optional capability for remote runtimes that need
// explicit reattachment logic on resume (e.g. reconnect to an existing sprite).
type RemoteSessionResumer interface {
ResumeRemoteInstance(ctx context.Context, req *ExecutorCreateRequest) error
}
// RemoteStatusProvider is an optional capability for runtimes that can expose
// remote environment status for UX (cloud icon tooltip, degraded state, etc.).
type RemoteStatusProvider interface {
GetRemoteStatus(ctx context.Context, instance *ExecutorInstance) (*RemoteStatus, error)
}
// ExecutorCreateRequest contains parameters for creating an agentctl instance.
type ExecutorCreateRequest struct {
InstanceID string
TaskID string
TaskTitle string
SessionID string
TaskEnvironmentID string // Env this execution belongs to (shared across sessions in same task)
AgentProfileID string
WorkspacePath string
Protocol string
Env map[string]string
Metadata map[string]interface{}
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) or "config"
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
// call this to report real-time progress to the frontend.
OnProgress PrepareProgressCallback
}
// ExecutorInstance represents an agentctl instance created by a runtime.
// This is returned by the runtime and contains enough info to build an AgentExecution.
type ExecutorInstance struct {
// Core identifiers
InstanceID string
TaskID string
SessionID string
// Runtime name (e.g., "docker", "standalone") - set by the runtime that created this instance
RuntimeName agentruntime.Runtime
// Agentctl client for communicating with this instance
Client *agentctl.Client
// Runtime-specific identifiers (only one set is populated)
ContainerID string // Docker
ContainerIP string // Docker
StandaloneInstanceID string // Standalone
StandalonePort int // Standalone
// Common fields
WorkspacePath string
Metadata map[string]interface{}
StopReason string
// AuthToken is the agentctl auth token retrieved via handshake.
// Populated by Docker executor for encrypted storage in SecretStore.
// Empty for standalone (launcher-owned token wired via cfg.Agent.StandaloneAuthToken)
// and Sprites (no agentctl auth).
AuthToken string
// BootstrapNonce is the one-time nonce injected into Docker container env.
// It is persisted so a restarted container can complete a fresh handshake
// against the newly started agentctl process.
BootstrapNonce string
}
// ToAgentExecution converts a ExecutorInstance to an AgentExecution.
func (ri *ExecutorInstance) ToAgentExecution(req *ExecutorCreateRequest) *AgentExecution {
metadata := req.Metadata
if metadata == nil {
metadata = make(map[string]interface{})
}
// Merge runtime metadata
for k, v := range ri.Metadata {
metadata[k] = v
}
workspacePath := ri.WorkspacePath
if workspacePath == "" {
workspacePath = req.WorkspacePath
}
var historyEnabled bool
var agentID string
if req.AgentConfig != nil {
agentID = req.AgentConfig.ID()
if rt := req.AgentConfig.Runtime(); rt != nil {
historyEnabled = rt.SessionConfig.HistoryContextInjection
}
}
return &AgentExecution{
ID: ri.InstanceID,
TaskID: req.TaskID,
SessionID: req.SessionID,
TaskEnvironmentID: req.TaskEnvironmentID,
AgentProfileID: req.AgentProfileID,
AgentID: agentID,
ContainerID: ri.ContainerID,
ContainerIP: ri.ContainerIP,
WorkspacePath: workspacePath,
RuntimeName: ri.RuntimeName,
Status: v1.AgentStatusRunning,
StartedAt: time.Now(),
Metadata: metadata,
agentctl: ri.Client,
standaloneInstanceID: ri.StandaloneInstanceID,
standalonePort: ri.StandalonePort,
historyEnabled: historyEnabled,
promptDoneCh: make(chan PromptCompletionSignal, 1),
}
}