Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(tasks): let the agent self-assign on GitHub through gh (#92130)

Vojta Bartoš4e60b54a8cd5
diff --git a/products/desktop/packages/agent/src/server/agent-server.test.ts b/products/desktop/packages/agent/src/server/agent-server.test.ts
index 33e204669863..8072a6e87523 100644
--- a/products/desktop/packages/agent/src/server/agent-server.test.ts
+++ b/products/desktop/packages/agent/src/server/agent-server.test.ts
@@ -4954,6 +4954,10 @@ describe("AgentServer HTTP Mode", () => {
});
describe("buildCloudSystemPrompt", () => {
+ afterEach(() => {
+ vi.unstubAllEnvs();
+ });
+
it.each([
{
delivery: "canvas_file" as const,
@@ -5210,6 +5214,49 @@ describe("AgentServer HTTP Mode", () => {
delete process.env.POSTHOG_CODE_INTERACTION_ORIGIN;
});
+ it("tells the agent to self-assign through gh rather than match a name", () => {
+ // `gh` already knows who the run acts as, so the agent must read it rather than
+ // ask the user for a handle it is forbidden from guessing.
+ vi.stubEnv("GITHUB_TOKEN", "ghu_actor");
+ const s = createServer();
+ const prompt = (s as unknown as TestableServer).buildCloudSystemPrompt();
+ expect(prompt).toContain('gh issue create --assignee "@me"');
+ expect(prompt).toContain("gh api user --jq .login");
+ // An installation token resolves to the app, so acting on it would assign a bot.
+ expect(prompt).toContain("ending in `[bot]`");
+ // Without this the "never guess a GitHub identity" rule forbids the handle we
+ // just told the agent to read.
+ expect(prompt).toContain(
+ "or one you read from `gh api user --jq .login`",
+ );
+ });
+
+ it.each(["slack", "signal_report"])(
+ "carries the GitHub identity guidance on %s-origin runs",
+ (origin) => {
+ // Non-Slack runs open issues under a person's credentials too, so this must not
+ // sit behind `isSlack`.
+ vi.stubEnv("GITHUB_TOKEN", "ghu_actor");
+ vi.stubEnv("POSTHOG_CODE_INTERACTION_ORIGIN", origin);
+ const s = createServer() as unknown as TestableServer;
+ expect(s.buildCloudSystemPrompt()).toContain(
+ "# Whose GitHub account you are using",
+ );
+ },
+ );
+
+ it("omits the guidance without a token but still permits a gh-read login", () => {
+ // The prompt is built once per session, so a run whose user connects GitHub
+ // partway through never re-renders. Dropping the exception too would keep the
+ // mention rule forbidding the login for the rest of that run.
+ const s = createServer();
+ const prompt = (s as unknown as TestableServer).buildCloudSystemPrompt();
+ expect(prompt).not.toContain("# Whose GitHub account you are using");
+ expect(prompt).toContain(
+ "or one you read from `gh api user --jq .login`",
+ );
+ });
+
it("returns auto-PR prompt for signal_report-origin runs", () => {
process.env.POSTHOG_CODE_INTERACTION_ORIGIN = "signal_report";
const s = createServer();
diff --git a/products/desktop/packages/agent/src/server/agent-server.ts b/products/desktop/packages/agent/src/server/agent-server.ts
index 8c1703d6a85a..5e97875e3c14 100644
--- a/products/desktop/packages/agent/src/server/agent-server.ts
+++ b/products/desktop/packages/agent/src/server/agent-server.ts
@@ -4185,7 +4185,19 @@ You may have no repository checked out, or no credentials to push and open a pul
const taskId = this.config.taskId;
const shouldAutoCreatePr = this.shouldAutoPublishCloudChanges();
const isSlack = this.getCloudInteractionOrigin() === "slack";
- const identityInstructions = isSlack
+ // Every instruction in this section runs through `gh`, so a sandbox holding no
+ // GitHub token cannot act on any of it. An empty token is an explicit logout.
+ const hasGithubToken = Boolean(resolveGithubToken());
+ const githubIdentityInstructions = hasGithubToken
+ ? `
+# Whose GitHub account you are using
+\`gh\` is authenticated as the person you are working for, so let GitHub resolve who that is. To put them on an issue or pull request, self-assign:
+ \`gh issue create --assignee "@me"\`, \`gh pr create --assignee "@me"\`, \`gh issue edit <number> --add-assignee "@me"\`
+To \`@\`-mention them in a body or a comment, read their handle with \`gh api user --jq .login\`. Read it once per reply rather than reusing one from an earlier reply, because a different person can take over between replies and \`gh\` follows that change.
+If the command fails, or returns a name ending in \`[bot]\`, you are acting as the PostHog app rather than as a person: ask them for their GitHub login instead of assigning or mentioning anyone.
+`
+ : "";
+ const slackIdentityInstructions = isSlack
? `
# Identity
You are the PostHog Slack app, PostHog's agent for helping users with their product data and coding tasks from Slack. When introducing yourself or referring to yourself in messages to the user, identify as "PostHog Slack app". Do NOT refer to yourself as Claude, an Anthropic assistant, or any underlying model name.
@@ -4212,6 +4224,7 @@ To ping a Slack user, reuse a \`<@U…|displayname>\` token that already appears
You can also open pull requests directly from this Slack thread. When the user's question describes a problem with a plausible code-side fix — a bug visible in errors or logs, missing or broken instrumentation, a broken funnel step traceable to UI code, a stale config that lives in a repo — end your reply with a one-sentence offer to open a PR for the fix and ask if they want you to proceed. Skip the offer for pure data lookups with no actionable code change (e.g. "what was DAU yesterday?"), and skip it when the fix would clearly live outside any repo you can reach.
`
: "";
+ const identityInstructions = `${slackIdentityInstructions}${githubIdentityInstructions}`;
const signedCommitInstructions = `
## Committing (signed commits required)
Commits MUST be signed. \`git commit\` and \`git push\` are blocked in this environment.
@@ -4306,7 +4319,7 @@ When you create a non-code file the user should be able to download (such as a r
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 publicRepoSafetyInstruction = ` - **Public-repo safety.** Treat the target repository as public-readable unless you have verified otherwise. The PR title, description, and commit messages must not contain private operational scale (exact event counts, internal row volumes, customer-usage percentages), customer names / emails / companies, references to internal tickets or incidents, the contents of Slack threads (do not quote or paraphrase what was said), or unreleased roadmap details. Linking to the originating Slack thread is fine and encouraged — Slack links are auth-gated and useful as context — as are channel references like "raised in #team-foo". Describe findings qualitatively ("present on nearly all X events, absent from Y") rather than with quantitative figures pulled from analytics queries — the reasoning that uses those numbers can stay in the thread; the PR copy cannot.`;
- const prMentionSafetyInstruction = ` - **Never guess a GitHub identity.** Do NOT \`@\`-mention, tag, assign, request review from, or attribute the PR to a person (in the title, description, commit message, or reviewers) using a name or handle taken from Slack or this thread. A Slack display name or handle is NOT a GitHub username. Finding a similar-looking handle in the repo's git history, CODEOWNERS, or existing PRs/issues does NOT confirm it belongs to this person: repository presence proves the handle exists, not that it is the person you mean, so treating it as a match still \`@\`-tags an unrelated account (e.g. Slack "Ross" is not necessarily GitHub \`@ross\`, even if some \`@ross\` has committed to the repo). Only \`@\`-mention a GitHub \`@handle\` the user gave you explicitly in this thread. Otherwise refer to people by plain-text name, or omit the mention entirely.`;
+ const prMentionSafetyInstruction = ` - **Never guess a GitHub identity.** Do NOT \`@\`-mention, tag, assign, request review from, or attribute the PR to a person (in the title, description, commit message, or reviewers) using a name or handle taken from Slack or this thread. A Slack display name or handle is NOT a GitHub username. Finding a similar-looking handle in the repo's git history, CODEOWNERS, or existing PRs/issues does NOT confirm it belongs to this person: repository presence proves the handle exists, not that it is the person you mean, so treating it as a match still \`@\`-tags an unrelated account (e.g. Slack "Ross" is not necessarily GitHub \`@ross\`, even if some \`@ross\` has committed to the repo). Only \`@\`-mention a GitHub \`@handle\` the user gave you explicitly in this thread, or one you read from \`gh api user --jq .login\`, which authenticates as the person you are working for. Otherwise refer to people by plain-text name, or omit the mention entirely.`;
// Slack- and inbox-originated PRs are attributed to PostHog, not the
// PostHog Desktop app — they come from the Slack app / Self-driving
// inbox, which users know as "PostHog".
@@ -5220,8 +5233,9 @@ ${commonInstructions}
...(checkout ? [checkout] : []),
...ownedBranchesFromOutput(freshOutput),
];
- // GitHub App installation tokens (all cloud runs) can't read `gh api user`, so
- // ghLogin is null there and the head-branch match is what proves ownership.
+ // Bot-authored runs hold a GitHub App installation token, which can't read
+ // `gh api user`, so ghLogin is null there and the head-branch match is what proves
+ // ownership. User-authored runs carry the actor's own token and do resolve a login.
const owned = wasCreatedByThisRun({
createdAt: attribution.createdAt,
nowMs: Date.now(),