diff --git a/packages/agent/src/server/agent-server.test.ts b/packages/agent/src/server/agent-server.test.ts index 88bae5505..72784f739 100644 --- a/packages/agent/src/server/agent-server.test.ts +++ b/packages/agent/src/server/agent-server.test.ts @@ -826,6 +826,9 @@ describe("AgentServer HTTP Mode", () => { "If the user explicitly asks you to open or update a pull request", "open a draft pull request", "unless the user explicitly asks", + ".github/pull_request_template.md", + "gh issue list --search", + "Closes #", "Generated-By: PostHog Code", "Task-Id: test-task-id", ], @@ -868,6 +871,13 @@ describe("AgentServer HTTP Mode", () => { expect(prompt).toContain("Generated-By: PostHog Code"); expect(prompt).toContain("Task-Id: test-task-id"); expect(prompt).toContain("Created with [PostHog Code]"); + // PR template detection (repo first, org `.github` fallback) + expect(prompt).toContain(".github/pull_request_template.md"); + expect(prompt).toContain("org's `.github` repo"); + // Related-issue linking + expect(prompt).toContain("gh issue list --state open --search"); + expect(prompt).toContain("Closes #"); + expect(prompt).toContain("Refs #"); delete process.env.POSTHOG_CODE_INTERACTION_ORIGIN; }); @@ -895,6 +905,13 @@ describe("AgentServer HTTP Mode", () => { ); expect(prompt).toContain("Push to the existing PR branch"); expect(prompt).not.toContain("Create a draft pull request"); + // Review-comment thread handling: reply + resolve + expect(prompt).toContain("review thread"); + expect(prompt).toContain("/pulls/{n}/comments/{id}/replies"); + expect(prompt).toContain("resolveReviewThread"); + expect(prompt).toContain( + "Do NOT push fixes for review comments without replying to and resolving each related thread.", + ); delete process.env.POSTHOG_CODE_INTERACTION_ORIGIN; }); diff --git a/packages/agent/src/server/agent-server.ts b/packages/agent/src/server/agent-server.ts index 12ea84c47..2d0ff1519 100644 --- a/packages/agent/src/server/agent-server.ts +++ b/packages/agent/src/server/agent-server.ts @@ -1633,9 +1633,14 @@ After completing the requested changes: 1. Check out the existing PR branch with \`gh pr checkout ${prUrl}\` 2. Stage and commit all changes with a clear commit message 3. Push to the existing PR branch +4. For every PR review comment or review thread you addressed, treat the thread as done only after BOTH of these: + - Reply on the thread with a short note describing what changed (reference the commit SHA when useful) using \`gh api -X POST /repos/{owner}/{repo}/pulls/{n}/comments/{id}/replies -f body='...'\`. + - Resolve the thread via the \`resolveReviewThread\` GraphQL mutation: \`gh api graphql -f query='mutation($id:ID!){resolveReviewThread(input:{threadId:$id}){thread{isResolved}}}' -f id=""\`. + List unresolved threads first with \`gh api graphql -f query='{repository(owner:"",name:""){pullRequest(number:){reviewThreads(first:100){nodes{id isResolved comments(first:1){nodes{body}}}}}}}'\` so you can resolve each one you fixed. Important: - Do NOT create a new branch or a new pull request. +- Do NOT push fixes for review comments without replying to and resolving each related thread. ${attributionInstructions} `; } @@ -1651,7 +1656,7 @@ When the user asks for code changes: When the user explicitly asks to clone or work in a GitHub repository: - Clone the repository into /tmp/workspace/repos// using \`gh repo clone / /tmp/workspace/repos//\` - Work from inside that cloned repository for follow-up code changes -- If the user explicitly asks you to open or update a pull request, create a branch, commit the requested changes, push it, and open a draft pull request from inside the clone +- If the user explicitly asks you to open or update a pull request, create a branch, commit the requested changes, push it, and open a draft pull request from inside the clone. Before opening the PR, check the cloned repo for a PR template at \`.github/pull_request_template.md\` (or variants; fall back to the org's \`.github\` repo via \`gh api\`) and use it as the body structure, and search for matching open issues with \`gh issue list --search\` to include \`Closes #\` / \`Refs #\` links. - Do NOT create branches, commits, push changes, or open pull requests unless the user explicitly asks for that`; return ` @@ -1694,7 +1699,11 @@ After completing the requested changes: 1. Create a new branch prefixed with \`posthog-code/\` (e.g. \`posthog-code/fix-login-redirect\`) based on the work done 2. Stage and commit all changes with a clear commit message 3. Push the branch to origin -4. Create a draft pull request using \`gh pr create --draft${this.config.baseBranch ? ` --base ${this.config.baseBranch}` : ""}\` with a descriptive title and body. Add the following footer at the end of the PR description: +4. Before opening the PR, prepare the body: + - Check the repo for a PR template at \`.github/pull_request_template.md\` (also try \`.github/PULL_REQUEST_TEMPLATE.md\`, \`docs/pull_request_template.md\`, and root variants). If one exists, use its exact section headings as the PR body — do NOT fall back to a generic Summary/Test plan format. + - If no repo-level template exists, check the org's \`.github\` repo via \`gh api /repos//.github/contents/.github/pull_request_template.md\` (and other common paths) and use that as a fallback. + - Search for matching open issues with \`gh issue list --state open --search ''\` (derive keywords from the branch name, commits, and changed files; \`gh issue view \` to confirm relevance). For every issue this PR would resolve, include a \`Closes #\` line in the body so GitHub auto-links and auto-closes it on merge. For issues that are related but not fully resolved, use \`Refs #\` instead. +5. Create a draft pull request using \`gh pr create --draft${this.config.baseBranch ? ` --base ${this.config.baseBranch}` : ""}\` with a descriptive title and the body prepared above. Add the following footer at the end of the PR description: \`\`\` --- *Created with [PostHog Code](https://posthog.com/code?ref=pr)*