Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

feat(agent): auto-create init issue on agent creation with workspace context

yushen994242d2db9a
diff --git a/server/internal/daemon/prompt.go b/server/internal/daemon/prompt.go
index 0c9ba1550e..7448abbc94 100644
--- a/server/internal/daemon/prompt.go
+++ b/server/internal/daemon/prompt.go
@@ -55,6 +55,12 @@ func BuildPrompt(task Task, workdir string) string {
b.WriteString("\n")
}
+ if task.Context.WorkspaceContext != "" {
+ b.WriteString("Workspace context:\n")
+ b.WriteString(task.Context.WorkspaceContext)
+ b.WriteString("\n\n")
+ }
+
if task.Context.Agent.Skills != "" {
b.WriteString("Agent skills/instructions:\n")
b.WriteString(task.Context.Agent.Skills)
diff --git a/server/internal/daemon/types.go b/server/internal/daemon/types.go
index 785edef179..5e241b133c 100644
--- a/server/internal/daemon/types.go
+++ b/server/internal/daemon/types.go
@@ -45,9 +45,10 @@ type Task struct {
// TaskContext contains the snapshot context for a task.
type TaskContext struct {
- Issue IssueContext `json:"issue"`
- Agent AgentContext `json:"agent"`
- Runtime RuntimeContext `json:"runtime"`
+ Issue IssueContext `json:"issue"`
+ Agent AgentContext `json:"agent"`
+ Runtime RuntimeContext `json:"runtime"`
+ WorkspaceContext string `json:"workspace_context,omitempty"`
}
// IssueContext holds issue details for task execution.
diff --git a/server/internal/handler/agent.go b/server/internal/handler/agent.go
index 39235b3b4f..e05a0215e9 100644
--- a/server/internal/handler/agent.go
+++ b/server/internal/handler/agent.go
@@ -1,7 +1,9 @@
package handler
import (
+ "context"
"encoding/json"
+ "fmt"
"net/http"
"github.com/go-chi/chi/v5"
@@ -240,9 +242,52 @@ func (h *Handler) CreateAgent(w http.ResponseWriter, r *http.Request) {
agent, _ = h.Queries.GetAgent(r.Context(), agent.ID)
}
+ // Best-effort: create an initialization issue assigned to the new agent.
+ h.createAgentInitIssue(r.Context(), agent, parseUUID(ownerID))
+
writeJSON(w, http.StatusCreated, agentToResponse(agent))
}
+// createAgentInitIssue creates an initialization issue assigned to a newly created agent.
+// It incorporates workspace context so the agent can set up its environment.
+// Failures are silently ignored — the agent creation itself has already succeeded.
+func (h *Handler) createAgentInitIssue(ctx context.Context, agent db.Agent, creatorID pgtype.UUID) {
+ ws, err := h.Queries.GetWorkspace(ctx, agent.WorkspaceID)
+ if err != nil {
+ return
+ }
+
+ var desc string
+ if ws.Context.Valid && ws.Context.String != "" {
+ desc = fmt.Sprintf("Initialize the development environment for agent **%s**.\n\n## Workspace Context\n\n%s\n\n## Instructions\n\n- Set up the local development environment based on the workspace context above\n- Clone and configure any referenced repositories\n- Verify access to the codebase and tools\n- Report back on what was set up and any issues encountered", agent.Name, ws.Context.String)
+ } else {
+ desc = fmt.Sprintf("Initialize the development environment for agent **%s**.\n\n## Instructions\n\n- Explore the local working directory and understand the project structure\n- Verify access to the codebase and tools\n- Report back on what was found and any issues encountered", agent.Name)
+ }
+
+ issue, err := h.Queries.CreateIssue(ctx, db.CreateIssueParams{
+ WorkspaceID: agent.WorkspaceID,
+ Title: "Initialize environment for " + agent.Name,
+ Description: strToText(desc),
+ Status: "todo",
+ Priority: "medium",
+ AssigneeType: pgtype.Text{String: "agent", Valid: true},
+ AssigneeID: agent.ID,
+ CreatorType: "member",
+ CreatorID: creatorID,
+ AcceptanceCriteria: []byte("[]"),
+ ContextRefs: []byte("[]"),
+ Position: 0,
+ })
+ if err != nil {
+ return
+ }
+
+ h.broadcast("issue:created", map[string]any{"issue": issueToResponse(issue)})
+
+ // Enqueue the task directly — we know the agent is assigned and status is "todo".
+ h.TaskService.EnqueueTaskForIssue(ctx, issue)
+}
+
type UpdateAgentRequest struct {
Name *string `json:"name"`
Description *string `json:"description"`
diff --git a/server/internal/service/task.go b/server/internal/service/task.go
index df04849c58..493e91d0f6 100644
--- a/server/internal/service/task.go
+++ b/server/internal/service/task.go
@@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
+ "log"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
@@ -42,7 +43,13 @@ func (s *TaskService) EnqueueTaskForIssue(ctx context.Context, issue db.Issue) (
return db.AgentTaskQueue{}, fmt.Errorf("load runtime: %w", err)
}
- snapshot := buildContextSnapshot(issue, agent, runtime)
+ // Include workspace context in the snapshot when available.
+ var workspaceContext string
+ if ws, err := s.Queries.GetWorkspace(ctx, issue.WorkspaceID); err == nil && ws.Context.Valid {
+ workspaceContext = ws.Context.String
+ }
+
+ snapshot := buildContextSnapshot(issue, agent, runtime, workspaceContext)
contextJSON, _ := json.Marshal(snapshot)
task, err := s.Queries.CreateAgentTaskWithContext(ctx, db.CreateAgentTaskWithContextParams{
@@ -105,6 +112,14 @@ func (s *TaskService) ClaimTaskForRuntime(ctx context.Context, runtimeID pgtype.
return nil, fmt.Errorf("list pending tasks: %w", err)
}
+ if len(tasks) > 0 {
+ log.Printf("[ClaimTaskForRuntime] runtime=%s found %d pending tasks", util.UUIDToString(runtimeID), len(tasks))
+ for _, t := range tasks {
+ log.Printf("[ClaimTaskForRuntime] task=%s agent=%s status=%s",
+ util.UUIDToString(t.ID), util.UUIDToString(t.AgentID), t.Status)
+ }
+ }
+
triedAgents := map[string]struct{}{}
for _, candidate := range tasks {
agentKey := util.UUIDToString(candidate.AgentID)
@@ -115,11 +130,16 @@ func (s *TaskService) ClaimTaskForRuntime(ctx context.Context, runtimeID pgtype.
task, err := s.ClaimTask(ctx, candidate.AgentID)
if err != nil {
+ log.Printf("[ClaimTaskForRuntime] ClaimTask for agent %s failed: %v", agentKey, err)
return nil, err
}
if task != nil && task.RuntimeID == runtimeID {
return task, nil
}
+ if task != nil {
+ log.Printf("[ClaimTaskForRuntime] task %s runtime mismatch: task.RuntimeID=%s != requested=%s",
+ util.UUIDToString(task.ID), util.UUIDToString(task.RuntimeID), util.UUIDToString(runtimeID))
+ }
}
return nil, nil
@@ -146,29 +166,45 @@ func (s *TaskService) StartTask(ctx context.Context, taskID pgtype.UUID) (*db.Ag
// CompleteTask marks a task as completed and syncs issue/agent status.
func (s *TaskService) CompleteTask(ctx context.Context, taskID pgtype.UUID, result []byte) (*db.AgentTaskQueue, error) {
+ log.Printf("[CompleteTask] task=%s result_len=%d result_preview=%s",
+ util.UUIDToString(taskID), len(result), truncate(string(result), 200))
+
task, err := s.Queries.CompleteAgentTask(ctx, db.CompleteAgentTaskParams{
ID: taskID,
Result: result,
})
if err != nil {
+ log.Printf("[CompleteTask] CompleteAgentTask failed: %v (task may not be in 'running' state)", err)
return nil, fmt.Errorf("complete task: %w", err)
}
+ log.Printf("[CompleteTask] task %s marked completed, issue=%s agent=%s",
+ util.UUIDToString(task.ID), util.UUIDToString(task.IssueID), util.UUIDToString(task.AgentID))
// Sync issue → in_review
issue, issueErr := s.Queries.UpdateIssueStatus(ctx, db.UpdateIssueStatusParams{
ID: task.IssueID,
Status: "in_review",
})
- if issueErr == nil {
+ if issueErr != nil {
+ log.Printf("[CompleteTask] UpdateIssueStatus to in_review failed: %v", issueErr)
+ } else {
+ log.Printf("[CompleteTask] issue %s status updated to in_review", util.UUIDToString(issue.ID))
s.broadcastIssueUpdated(issue)
}
var payload protocol.TaskCompletedPayload
if err := json.Unmarshal(result, &payload); err == nil {
+ log.Printf("[CompleteTask] parsed payload: output_len=%d", len(payload.Output))
if payload.Output != "" {
s.createAgentComment(ctx, task.IssueID, task.AgentID, payload.Output, "comment")
+ log.Printf("[CompleteTask] created agent comment for issue %s", util.UUIDToString(task.IssueID))
+ } else {
+ log.Printf("[CompleteTask] payload.Output is empty, skipping comment")
}
+ } else {
+ log.Printf("[CompleteTask] failed to unmarshal result into TaskCompletedPayload: %v", err)
}
+
if issueErr == nil {
s.createInboxForIssueCreator(ctx, issue, "review_requested", "attention", "Review requested: "+issue.Title, "")
}
@@ -250,7 +286,7 @@ func (s *TaskService) updateAgentStatus(ctx context.Context, agentID pgtype.UUID
s.broadcast(protocol.EventAgentStatus, map[string]any{"agent": agentToMap(agent)})
}
-func buildContextSnapshot(issue db.Issue, agent db.Agent, runtime db.AgentRuntime) map[string]any {
+func buildContextSnapshot(issue db.Issue, agent db.Agent, runtime db.AgentRuntime, workspaceContext string) map[string]any {
var ac []string
if issue.AcceptanceCriteria != nil {
json.Unmarshal(issue.AcceptanceCriteria, &ac)
@@ -274,7 +310,7 @@ func buildContextSnapshot(issue db.Issue, agent db.Agent, runtime db.AgentRuntim
json.Unmarshal(runtime.Metadata, &metadata)
}
- return map[string]any{
+ m := map[string]any{
"issue": map[string]any{
"id": util.UUIDToString(issue.ID),
"title": issue.Title,
@@ -298,6 +334,10 @@ func buildContextSnapshot(issue db.Issue, agent db.Agent, runtime db.AgentRuntim
"metadata": metadata,
},
}
+ if workspaceContext != "" {
+ m["workspace_context"] = workspaceContext
+ }
+ return m
}
func priorityToInt(p string) int32 {
@@ -451,6 +491,13 @@ func inboxToMap(item db.InboxItem) map[string]any {
}
}
+func truncate(s string, n int) string {
+ if len(s) <= n {
+ return s
+ }
+ return s[:n] + "..."
+}
+
// agentToMap builds a simple map for broadcasting agent status updates.
func agentToMap(a db.Agent) map[string]any {
var rc any