mirror of
https://github.com/coder/coder.git
synced 2026-11-03 05:55:24 +08:00
> This PR was authored by Mux on behalf of Mike. Chats sharing one workspace (e.g. sibling subagents) all wrote to `/home/coder/PLAN.md`, causing plan file collisions. This change derives a unique plan path per chat from the workspace home directory and chat ID. ## Changes * `write_file`, `edit_files`, and `propose_plan` reject any `plan.md` variant (case-insensitive) at the workspace home root, with a clear error pointing to the chat-specific path. * Root chats receive a `<plan-file-path>` block inlined in the main system prompt with the concrete path. * Prompt and tool descriptions no longer hardcode `/home/coder/PLAN.md`. * Plan path handling is POSIX-only (forward-slash), relying on the contract that workspace agent paths are normalized before reaching chatd. * Updated `ProposePlanTool.stories.tsx` to use per-chat path examples. * Full test coverage for plan path detection, legacy-path rejection in all three tools, inline prompt rendering, and fallback behavior.
110 lines
3.7 KiB
Go
110 lines
3.7 KiB
Go
package chattool
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"charm.land/fantasy"
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/coder/coder/v2/codersdk/workspacesdk"
|
|
)
|
|
|
|
const maxProposePlanSize = 32 * 1024 // 32 KiB
|
|
|
|
// ProposePlanOptions configures the propose_plan tool.
|
|
type ProposePlanOptions struct {
|
|
GetWorkspaceConn func(context.Context) (workspacesdk.AgentConn, error)
|
|
ResolvePlanPath func(context.Context) (chatPath string, home string, err error)
|
|
StoreFile func(ctx context.Context, name string, mediaType string, data []byte) (uuid.UUID, error)
|
|
}
|
|
|
|
// ProposePlanArgs are the arguments for the propose_plan tool.
|
|
type ProposePlanArgs struct {
|
|
Path string `json:"path"`
|
|
}
|
|
|
|
// ProposePlan returns a tool that presents a Markdown plan file from the
|
|
// workspace for user review.
|
|
func ProposePlan(options ProposePlanOptions) fantasy.AgentTool {
|
|
return fantasy.NewAgentTool(
|
|
"propose_plan",
|
|
"Present a Markdown plan file from the workspace for user review. "+
|
|
"The file must already exist with a .md extension. Use write_file to create it or edit_files to refine it before calling this tool. "+
|
|
"Pass the absolute file path to the plan. Important: use the chat-specific absolute plan path, not a generic path like PLAN.md in the home directory. "+
|
|
"The tool reads the content from the workspace.",
|
|
func(ctx context.Context, args ProposePlanArgs, _ fantasy.ToolCall) (fantasy.ToolResponse, error) {
|
|
if options.GetWorkspaceConn == nil {
|
|
return fantasy.NewTextErrorResponse("workspace connection resolver is not configured"), nil
|
|
}
|
|
if options.StoreFile == nil {
|
|
return fantasy.NewTextErrorResponse("file storage is not configured"), nil
|
|
}
|
|
conn, err := options.GetWorkspaceConn(ctx)
|
|
if err != nil {
|
|
return fantasy.NewTextErrorResponse(err.Error()), nil
|
|
}
|
|
return executeProposePlanTool(ctx, conn, args, options.ResolvePlanPath, options.StoreFile)
|
|
},
|
|
)
|
|
}
|
|
|
|
func executeProposePlanTool(
|
|
ctx context.Context,
|
|
conn workspacesdk.AgentConn,
|
|
args ProposePlanArgs,
|
|
resolvePlanPath func(context.Context) (chatPath string, home string, err error),
|
|
storeFile func(ctx context.Context, name string, mediaType string, data []byte) (uuid.UUID, error),
|
|
) (fantasy.ToolResponse, error) {
|
|
requestedPath := strings.TrimSpace(args.Path)
|
|
if requestedPath == "" {
|
|
return fantasy.NewTextErrorResponse("path is required (use the chat-specific absolute plan path)"), nil
|
|
}
|
|
if !strings.HasSuffix(requestedPath, ".md") {
|
|
return fantasy.NewTextErrorResponse("path must end with .md"), nil
|
|
}
|
|
|
|
hasPlanFileName := looksLikePlanFileName(requestedPath)
|
|
if hasPlanFileName && !isAbsolutePath(requestedPath) {
|
|
return fantasy.NewTextErrorResponse(
|
|
"plan files must use absolute paths; use the chat-specific absolute plan path",
|
|
), nil
|
|
}
|
|
|
|
if resolvePlanPath != nil && hasPlanFileName {
|
|
chatPath, home, err := resolvePlanPath(ctx)
|
|
if resp, rejected := rejectSharedPlanPath(requestedPath, home, chatPath, err); rejected {
|
|
return resp, nil
|
|
}
|
|
}
|
|
|
|
rc, _, err := conn.ReadFile(ctx, requestedPath, 0, maxProposePlanSize+1)
|
|
if err != nil {
|
|
return fantasy.NewTextErrorResponse(err.Error()), nil
|
|
}
|
|
defer rc.Close()
|
|
|
|
data, err := io.ReadAll(rc)
|
|
if err != nil {
|
|
return fantasy.NewTextErrorResponse(err.Error()), nil
|
|
}
|
|
if int64(len(data)) > maxProposePlanSize {
|
|
return fantasy.NewTextErrorResponse("plan file exceeds 32 KiB size limit"), nil
|
|
}
|
|
|
|
fileID, err := storeFile(ctx, filepath.Base(requestedPath), "text/markdown", data)
|
|
if err != nil {
|
|
return fantasy.NewTextErrorResponse("failed to store plan file: " + err.Error()), nil
|
|
}
|
|
|
|
return toolResponse(map[string]any{
|
|
"ok": true,
|
|
"path": requestedPath,
|
|
"kind": "plan",
|
|
"file_id": fileID.String(),
|
|
"media_type": "text/markdown",
|
|
}), nil
|
|
}
|