feat(orchestrator): generalize task environment reuse across executor types
Worktree, container, and sandbox IDs now flow uniformly from a persisted TaskEnvironment back into LaunchAgentRequest through a single helper. This gives Sprites session-to-session reuse the same first-class treatment that Docker already gained from the GitHub-token-injection PR, and lets a fallback launch refresh the environment row instead of leaving stale IDs in the DB. - applyExistingEnvironment propagates WorktreeID, ContainerID (via metadata), and SandboxID (as sprite_name metadata) onto the launch request - persistTaskEnvironment refreshes those IDs when the executor ends up creating new resources (e.g. the previous container was pruned) - extractSandboxID lifts the sprite identifier out of executor response metadata so it can be persisted on the TaskEnvironment row
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
package executor
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/kandev/kandev/internal/common/logger"
|
||||
"github.com/kandev/kandev/internal/task/models"
|
||||
)
|
||||
|
||||
func newEnvTestExecutor(t *testing.T) *Executor {
|
||||
t.Helper()
|
||||
log, err := logger.NewLogger(logger.LoggingConfig{Level: "error", Format: "json"})
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create logger: %v", err)
|
||||
}
|
||||
return &Executor{logger: log}
|
||||
}
|
||||
|
||||
func TestApplyExistingEnvironment_NilEnv(t *testing.T) {
|
||||
e := newEnvTestExecutor(t)
|
||||
req := &LaunchAgentRequest{TaskID: "task-1"}
|
||||
|
||||
e.applyExistingEnvironment(req, nil)
|
||||
|
||||
if req.Metadata != nil {
|
||||
t.Error("expected nil metadata for nil env")
|
||||
}
|
||||
if req.PreviousExecutionID != "" {
|
||||
t.Error("expected empty PreviousExecutionID for nil env")
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyExistingEnvironment_WorktreeReuse(t *testing.T) {
|
||||
e := newEnvTestExecutor(t)
|
||||
req := &LaunchAgentRequest{TaskID: "task-1", UseWorktree: true}
|
||||
env := &models.TaskEnvironment{
|
||||
WorktreeID: "wt-1",
|
||||
}
|
||||
|
||||
e.applyExistingEnvironment(req, env)
|
||||
|
||||
if req.WorktreeID != "wt-1" {
|
||||
t.Errorf("expected WorktreeID=wt-1, got %s", req.WorktreeID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyExistingEnvironment_WorktreeSkippedWhenNotRequested(t *testing.T) {
|
||||
e := newEnvTestExecutor(t)
|
||||
req := &LaunchAgentRequest{TaskID: "task-1", UseWorktree: false}
|
||||
env := &models.TaskEnvironment{
|
||||
WorktreeID: "wt-1",
|
||||
}
|
||||
|
||||
e.applyExistingEnvironment(req, env)
|
||||
|
||||
if req.WorktreeID != "" {
|
||||
t.Errorf("expected empty WorktreeID when UseWorktree=false, got %s", req.WorktreeID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyExistingEnvironment_ContainerReuse(t *testing.T) {
|
||||
e := newEnvTestExecutor(t)
|
||||
req := &LaunchAgentRequest{TaskID: "task-1"}
|
||||
env := &models.TaskEnvironment{
|
||||
ContainerID: "container-abc",
|
||||
}
|
||||
|
||||
e.applyExistingEnvironment(req, env)
|
||||
|
||||
if req.PreviousExecutionID != "" {
|
||||
t.Errorf("expected empty PreviousExecutionID, got %s", req.PreviousExecutionID)
|
||||
}
|
||||
if req.Metadata["container_id"] != "container-abc" {
|
||||
t.Errorf("expected metadata container_id=container-abc, got %v", req.Metadata["container_id"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyExistingEnvironment_SandboxReuse(t *testing.T) {
|
||||
e := newEnvTestExecutor(t)
|
||||
req := &LaunchAgentRequest{TaskID: "task-1"}
|
||||
env := &models.TaskEnvironment{
|
||||
SandboxID: "kandev-sprite-abc",
|
||||
}
|
||||
|
||||
e.applyExistingEnvironment(req, env)
|
||||
|
||||
if req.PreviousExecutionID != "" {
|
||||
t.Errorf("expected empty PreviousExecutionID, got %s", req.PreviousExecutionID)
|
||||
}
|
||||
if req.Metadata["sprite_name"] != "kandev-sprite-abc" {
|
||||
t.Errorf("expected metadata sprite_name=kandev-sprite-abc, got %v", req.Metadata["sprite_name"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyExistingEnvironment_WorktreeAndContainer(t *testing.T) {
|
||||
e := newEnvTestExecutor(t)
|
||||
req := &LaunchAgentRequest{TaskID: "task-1", UseWorktree: true}
|
||||
env := &models.TaskEnvironment{
|
||||
WorktreeID: "wt-1",
|
||||
ContainerID: "container-abc",
|
||||
}
|
||||
|
||||
e.applyExistingEnvironment(req, env)
|
||||
|
||||
if req.WorktreeID != "wt-1" {
|
||||
t.Errorf("expected WorktreeID=wt-1, got %s", req.WorktreeID)
|
||||
}
|
||||
if req.Metadata["container_id"] != "container-abc" {
|
||||
t.Errorf("expected metadata container_id=container-abc, got %v", req.Metadata["container_id"])
|
||||
}
|
||||
if req.PreviousExecutionID != "" {
|
||||
t.Errorf("expected empty PreviousExecutionID, got %s", req.PreviousExecutionID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyExistingEnvironment_EmptyEnvFieldsDoNothing(t *testing.T) {
|
||||
e := newEnvTestExecutor(t)
|
||||
req := &LaunchAgentRequest{TaskID: "task-1"}
|
||||
env := &models.TaskEnvironment{}
|
||||
|
||||
e.applyExistingEnvironment(req, env)
|
||||
|
||||
if req.Metadata != nil {
|
||||
t.Error("expected nil metadata when no container/sandbox IDs")
|
||||
}
|
||||
if req.PreviousExecutionID != "" {
|
||||
t.Error("expected empty PreviousExecutionID when no container/sandbox IDs")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractSandboxID(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
metadata map[string]interface{}
|
||||
want string
|
||||
}{
|
||||
{"nil metadata", nil, ""},
|
||||
{"no sprite_name", map[string]interface{}{"other": "val"}, ""},
|
||||
{"with sprite_name", map[string]interface{}{"sprite_name": "kandev-abc"}, "kandev-abc"},
|
||||
{"non-string sprite_name", map[string]interface{}{"sprite_name": 42}, ""},
|
||||
{"empty sprite_name", map[string]interface{}{"sprite_name": ""}, ""},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := extractSandboxID(tt.metadata)
|
||||
if got != tt.want {
|
||||
t.Errorf("extractSandboxID() = %q, want %q", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -429,13 +429,8 @@ func (e *Executor) LaunchPreparedSession(ctx context.Context, task *v1.Task, ses
|
||||
req.Attachments = opts.Attachments
|
||||
}
|
||||
|
||||
// Check for an existing task environment to reuse its worktree
|
||||
if existingEnv != nil && existingEnv.WorktreeID != "" && req.UseWorktree {
|
||||
req.WorktreeID = existingEnv.WorktreeID
|
||||
e.logger.Info("reusing existing task environment worktree",
|
||||
zap.String("task_id", task.ID),
|
||||
zap.String("worktree_id", existingEnv.WorktreeID))
|
||||
}
|
||||
// Check for an existing task environment to reuse worktree, container, or sandbox
|
||||
e.applyExistingEnvironment(req, existingEnv)
|
||||
|
||||
e.logger.Info("launching agent for prepared session",
|
||||
zap.String("task_id", task.ID),
|
||||
@@ -938,9 +933,11 @@ func (e *Executor) persistTaskEnvironment(
|
||||
// is being dropped (executors_running is the single source of truth).
|
||||
// Status, worktree, and container fields are still env-row-owned.
|
||||
existingEnv.Status = models.TaskEnvironmentStatusReady
|
||||
// Refresh worktree + workspace fields. The original update branch only
|
||||
// touched AgentExecutionID/Status, so envs created with empty paths
|
||||
// (e.g. before the worktree resolved) stayed permanently broken.
|
||||
// Refresh worktree + workspace + container/sandbox fields. The original
|
||||
// update branch only touched AgentExecutionID/Status, so envs created
|
||||
// with empty paths (e.g. before the worktree resolved) stayed
|
||||
// permanently broken. Sandbox ID gets refreshed too in case a fallback
|
||||
// created a new sprite.
|
||||
if resp.WorktreeID != "" {
|
||||
existingEnv.WorktreeID = resp.WorktreeID
|
||||
}
|
||||
@@ -956,6 +953,9 @@ func (e *Executor) persistTaskEnvironment(
|
||||
if resp.ContainerID != "" {
|
||||
existingEnv.ContainerID = resp.ContainerID
|
||||
}
|
||||
if sandboxID := extractSandboxID(resp.Metadata); sandboxID != "" {
|
||||
existingEnv.SandboxID = sandboxID
|
||||
}
|
||||
if err := e.repo.UpdateTaskEnvironment(ctx, existingEnv); err != nil {
|
||||
e.logger.Warn("failed to update task environment",
|
||||
zap.String("task_id", taskID),
|
||||
@@ -984,6 +984,7 @@ func (e *Executor) persistTaskEnvironment(
|
||||
WorkspacePath: workspacePath,
|
||||
ContainerID: resp.ContainerID,
|
||||
TaskDirName: req.TaskDirName,
|
||||
SandboxID: extractSandboxID(resp.Metadata),
|
||||
}
|
||||
// Embed per-repo rows in the same create transaction when multi-repo.
|
||||
if len(resp.Worktrees) > 0 {
|
||||
@@ -1054,3 +1055,46 @@ func (e *Executor) persistTaskEnvironmentRepos(ctx context.Context, envID string
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// applyExistingEnvironment propagates worktree, container, and sandbox IDs from
|
||||
// an existing TaskEnvironment into the launch request so that executor backends
|
||||
// can reuse the existing execution environment.
|
||||
func (e *Executor) applyExistingEnvironment(req *LaunchAgentRequest, env *models.TaskEnvironment) {
|
||||
if env == nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Worktree reuse
|
||||
if env.WorktreeID != "" && req.UseWorktree {
|
||||
req.WorktreeID = env.WorktreeID
|
||||
e.logger.Info("reusing existing task environment worktree",
|
||||
zap.String("task_id", req.TaskID),
|
||||
zap.String("worktree_id", env.WorktreeID))
|
||||
}
|
||||
|
||||
// Container/sandbox reuse
|
||||
if env.ContainerID != "" || env.SandboxID != "" {
|
||||
if req.Metadata == nil {
|
||||
req.Metadata = make(map[string]interface{})
|
||||
}
|
||||
if env.ContainerID != "" {
|
||||
req.Metadata["container_id"] = env.ContainerID
|
||||
}
|
||||
if env.SandboxID != "" {
|
||||
// Sprites executor reads "sprite_name" from metadata for reconnection
|
||||
req.Metadata["sprite_name"] = env.SandboxID
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// extractSandboxID extracts the sandbox identifier from launch response metadata.
|
||||
// For Sprites executors, this is the sprite_name.
|
||||
func extractSandboxID(metadata map[string]interface{}) string {
|
||||
if metadata == nil {
|
||||
return ""
|
||||
}
|
||||
if name, ok := metadata["sprite_name"].(string); ok {
|
||||
return name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user