feat(daemon): reuse workdir across tasks on same agent+issue pair

Previously each task created a fresh workdir via execenv.Prepare(), even
when resuming work on the same (agent, issue). This caused the agent's
session context to be out of sync with a blank code state.

Now the server returns prior_work_dir in the claim response, and the
daemon tries execenv.Reuse() first — which wraps the existing directory,
detects git worktree state, and refreshes context files. Falls back to
Prepare() if the prior workdir no longer exists. Workdirs are no longer
cleaned up after task completion so they remain available for reuse.
This commit is contained in:
Jiayuan 2026-03-29 18:40:29 +08:00
parent 5211947104
commit ce2b263ea5
5 changed files with 57 additions and 15 deletions

View file

@ -147,6 +147,36 @@ func Prepare(params PrepareParams, logger *slog.Logger) (*Environment, error) {
return env, nil
}
// Reuse wraps an existing workdir into an Environment and refreshes context files.
// Returns nil if the workdir does not exist (caller should fall back to Prepare).
func Reuse(workDir, provider string, task TaskContextForEnv, logger *slog.Logger) *Environment {
if _, err := os.Stat(workDir); err != nil {
return nil
}
env := &Environment{
RootDir: filepath.Dir(workDir),
WorkDir: workDir,
Type: WorkspaceTypeDirectory,
logger: logger,
}
// Detect if this is a git worktree.
if gitRoot, ok := detectGitRepo(workDir); ok {
env.Type = WorkspaceTypeGitWorktree
env.BranchName = getDefaultBranch(workDir)
env.gitRoot = gitRoot
}
// Refresh context files (issue_context.md, skills).
if err := writeContextFiles(workDir, provider, task); err != nil {
logger.Warn("execenv: refresh context files failed", "error", err)
}
logger.Info("execenv: reusing env", "workdir", workDir, "type", env.Type, "branch", env.BranchName)
return env
}
// Cleanup tears down the execution environment.
// If removeAll is true, the entire env root is deleted. Otherwise, workdir is
// removed but output/ and logs/ are preserved for debugging.