From c0ee634af03f18f9804d914461bac428a59b90ee Mon Sep 17 00:00:00 2001 From: Marcel Poelker Date: Tue, 16 Jun 2026 10:59:12 +0200 Subject: [PATCH] fix(agent): don't gate draft-PR creation on long local checks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The cloud agent treated "all checks green" as a hard prerequisite for opening a draft PR — kicking off a full typecheck/test/build and polling for it, so the draft was sometimes never created. Add a reusable `draftPrVerificationGuidance` block to the cloud system prompt in `buildCloudSystemPrompt()` and wire it into every PR-capable branch (review-first, auto-publish, and the publish-enabled no-repository clone path). It instructs the agent to open the draft FIRST, then handle verification without blocking: rely on CI if the repo has it, otherwise offer to run checks as a follow-up task when the agent judges it warranted. Does not assume CI exists. Lives in the product prompt (injected into every cloud run) so the fix applies to any user repo, not just this one. Generated-By: PostHog Code Task-Id: f899b653-ef6d-4af0-bad6-1bce6334f06e --- .../agent/src/server/agent-server.test.ts | 17 ++++++++++++- packages/agent/src/server/agent-server.ts | 24 ++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/packages/agent/src/server/agent-server.test.ts b/packages/agent/src/server/agent-server.test.ts index e7b1c00d76..e79c3e2169 100644 --- a/packages/agent/src/server/agent-server.test.ts +++ b/packages/agent/src/server/agent-server.test.ts @@ -1374,6 +1374,12 @@ describe("AgentServer HTTP Mode", () => { expect(prompt).toContain( "Do NOT create a branch, commit, push, or open a pull request unless the user explicitly asks.", ); + expect(prompt).toContain( + "If the user does ask you to open a draft PR, open it first and do not block on long local checks", + ); + expect(prompt).toContain("Opening the PR — do not block on long checks"); + expect(prompt).toContain("assume the repo has CI"); + expect(prompt).toContain("run the relevant checks as a follow-up task"); expect(prompt).toContain("Generated-By: PostHog Code"); expect(prompt).toContain("Task-Id: test-task-id"); expect(prompt).not.toContain("gh pr create --draft"); @@ -1401,6 +1407,7 @@ describe("AgentServer HTTP Mode", () => { ".github/pull_request_template.md", "gh issue list --search", "Closes #", + "Opening the PR — do not block on long checks", "Generated-By: PostHog Code", "Task-Id: test-task-id", ], @@ -1414,7 +1421,11 @@ describe("AgentServer HTTP Mode", () => { "You may clone a repository and make local edits in that clone", "Do NOT create branches, commits, push changes, or open pull requests in this run", ], - shouldNotContain: ["open a draft pull request", "gh pr create --draft"], + shouldNotContain: [ + "open a draft pull request", + "gh pr create --draft", + "Opening the PR — do not block on long checks", + ], }, ])( "returns no-repository prompt for $label", @@ -1440,6 +1451,10 @@ describe("AgentServer HTTP Mode", () => { expect(prompt).toContain("posthog-code/"); expect(prompt).toContain("Create a draft pull request"); expect(prompt).toContain("gh pr create --draft"); + expect(prompt).toContain( + "Open the draft PR without blocking on long local checks", + ); + expect(prompt).toContain("Opening the PR — do not block on long checks"); expect(prompt).toContain("Generated-By: PostHog Code"); expect(prompt).toContain("Task-Id: test-task-id"); expect(prompt).toContain("Created with [PostHog Code]"); diff --git a/packages/agent/src/server/agent-server.ts b/packages/agent/src/server/agent-server.ts index 518b576056..ad54a49b4e 100644 --- a/packages/agent/src/server/agent-server.ts +++ b/packages/agent/src/server/agent-server.ts @@ -1791,6 +1791,22 @@ we want: Generated-By: PostHog Code Task-Id: ${taskId}`; + const draftPrVerificationGuidance = ` +## Opening the PR — do not block on long checks +When you open a draft pull request, open it FIRST, then handle verification. +- Do NOT gate draft-PR creation on long-running local checks (full typecheck, full + test suite, or package builds). A draft is the safety net, and by the time a draft is + requested the work is usually already at a point the user considers complete (often + with some tests already run). +- Committing is fast here: commits go through the \`git_signed_commit\` tool + (server-side), so there is no local pre-commit hook to wait on. +- After the draft is open, handle verification without blocking its creation. Do NOT + assume the repo has CI: if it does, let CI run the checks on the PR; if it has no CI + (or you are unsure) and you judge verification is warranted, tell the user and offer + to run the relevant checks as a follow-up task. For a quick local sanity check, prefer + fast, scoped checks (lint, or a typecheck of just the package you touched). Reserve the + full suite for before marking the PR ready for review.`; + const whyContextInstruction = ` - Add a brief **Why** to the body — one or two sentences capturing the reason the user asked for this change (the motivation, not a restatement of the diff). Keep it short.`; const prFooter = slackThreadUrl ? `*Created with [PostHog Code](https://posthog.com/code?ref=pr) from a [Slack thread](${slackThreadUrl})*` @@ -1848,7 +1864,9 @@ When the user explicitly asks to clone or work in a GitHub repository: - Keep the PR description brief overall. Summarize only the most important changes — do NOT enumerate every change you made. A few sentences or bullets is plenty. ${whyContextInstruction.trimStart()} - End the PR description with a horizontal rule followed by this footer line: ${prFooter} -- Do NOT create branches, commits, push changes, or open pull requests unless the user explicitly asks for that`; +- Do NOT create branches, commits, push changes, or open pull requests unless the user explicitly asks for that +- If the user does ask you to open a draft PR, open it first and do not block on long local checks (see below). +${draftPrVerificationGuidance}`; return `${identityInstructions} # Cloud Task Execution — No Repository Mode @@ -1879,6 +1897,8 @@ Do the requested work, but stop with local changes ready for review. Important: - Do NOT create a branch, commit, push, or open a pull request unless the user explicitly asks. +- If the user does ask you to open a draft PR, open it first and do not block on long local checks (see below). +${draftPrVerificationGuidance} ${signedCommitInstructions} `; } @@ -1903,6 +1923,8 @@ ${prFooter} Important: - Always create the PR as a draft. Do not ask for confirmation. +- Open the draft PR without blocking on long local checks (see below); handle verification after it is open. +${draftPrVerificationGuidance} ${signedCommitInstructions} `; }