Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/agent/src/server/agent-server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 #<n>",
"Generated-By: PostHog Code",
"Task-Id: test-task-id",
],
Expand Down Expand Up @@ -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 #<n>");
expect(prompt).toContain("Refs #<n>");
delete process.env.POSTHOG_CODE_INTERACTION_ORIGIN;
});

Expand Down Expand Up @@ -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;
});

Expand Down
13 changes: 11 additions & 2 deletions packages/agent/src/server/agent-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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="<thread-node-id>"\`.
List unresolved threads first with \`gh api graphql -f query='{repository(owner:"<owner>",name:"<repo>"){pullRequest(number:<n>){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}
`;
}
Expand All @@ -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/<owner>/<repo> using \`gh repo clone <owner>/<repo> /tmp/workspace/repos/<owner>/<repo>\`
- 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 #<n>\` / \`Refs #<n>\` links.
- Do NOT create branches, commits, push changes, or open pull requests unless the user explicitly asks for that`;

return `
Expand Down Expand Up @@ -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/<owner>/.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 '<keywords>'\` (derive keywords from the branch name, commits, and changed files; \`gh issue view <n>\` to confirm relevance). For every issue this PR would resolve, include a \`Closes #<n>\` 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 #<n>\` 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)*
Expand Down
Loading