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
66 changes: 21 additions & 45 deletions apps/code/src/main/services/agent/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
isOpenAIModel,
} from "@posthog/agent/gateway-models";
import { getLlmGatewayUrl } from "@posthog/agent/posthog-api";
import { extractCreatedPrUrl } from "@posthog/agent/pr-url-detector";
import type * as AgentTypes from "@posthog/agent/types";
import { getCurrentBranch } from "@posthog/git/queries";
import type { IAppMeta } from "@posthog/platform/app-meta";
Expand Down Expand Up @@ -1524,6 +1525,7 @@ For git operations while detached:
claudeCode?: {
toolName?: string;
toolResponse?: unknown;
bashCommand?: string;
};
};
content?: Array<{ type?: string; text?: string }>;
Expand All @@ -1542,10 +1544,7 @@ For git operations while detached:

const session = this.sessions.get(taskRunId);

// PR URLs only appear in Bash tool output
if (toolName.includes("Bash") || toolName.includes("bash")) {
this.detectAndAttachPrUrl(taskRunId, session, toolMeta, update.content);
}
this.detectAndAttachPrUrl(taskRunId, session, toolMeta, update.content);

this.trackAgentFileActivity(taskRunId, session, toolName);
} catch (err) {
Expand All @@ -1557,60 +1556,37 @@ For git operations while detached:
}

/**
* Detect GitHub PR URLs in bash tool results and attach to task.
* This enables webhook tracking by populating the pr_url in TaskRun output.
* Detect GitHub PR URLs in `gh pr create` output and attach to task.
* Gated on the originating bash command so that unrelated PR URLs (e.g.
* `gh pr view`, `gh search prs`) don't get latched onto the run.
*/
private detectAndAttachPrUrl(
taskRunId: string,
session: ManagedSession | undefined,
toolMeta: { toolName?: string; toolResponse?: unknown },
toolMeta:
| {
toolName?: string;
toolResponse?: unknown;
bashCommand?: string;
}
| undefined,
content?: Array<{ type?: string; text?: string }>,
): void {
let textToSearch = "";

// Check toolResponse (hook response with raw output)
const toolResponse = toolMeta?.toolResponse;
if (toolResponse) {
if (typeof toolResponse === "string") {
textToSearch = toolResponse;
} else if (typeof toolResponse === "object" && toolResponse !== null) {
// May be { stdout?: string, stderr?: string } or similar
const respObj = toolResponse as Record<string, unknown>;
textToSearch =
String(respObj.stdout || "") + String(respObj.stderr || "");
if (!textToSearch && respObj.output) {
textToSearch = String(respObj.output);
}
}
}

// Also check content array
if (Array.isArray(content)) {
for (const item of content) {
if (item.type === "text" && item.text) {
textToSearch += ` ${item.text}`;
}
}
}

if (!textToSearch) return;

// Match GitHub PR URLs
const prUrlMatch = textToSearch.match(
/https:\/\/github\.com\/[^/]+\/[^/]+\/pull\/\d+/,
);
if (!prUrlMatch) return;
const prUrl = extractCreatedPrUrl({
toolName: toolMeta?.toolName,
bashCommand: toolMeta?.bashCommand,
toolResponse: toolMeta?.toolResponse,
content,
});
if (!prUrl) return;

const prUrl = prUrlMatch[0];
log.info("Detected PR URL in bash output", { taskRunId, prUrl });
log.info("Detected PR URL from gh pr create", { taskRunId, prUrl });

// Attach PR URL
if (!session) {
log.warn("Session not found for PR attachment", { taskRunId });
return;
}

// Attach asynchronously without blocking message flow
session.agent
.attachPullRequestToTask(session.taskId, prUrl)
.then(() => {
Expand Down
4 changes: 4 additions & 0 deletions packages/agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
"types": "./dist/posthog-api.d.ts",
"import": "./dist/posthog-api.js"
},
"./pr-url-detector": {
"types": "./dist/pr-url-detector.d.ts",
"import": "./dist/pr-url-detector.js"
},
"./types": {
"types": "./dist/types.d.ts",
"import": "./dist/types.js"
Expand Down
31 changes: 28 additions & 3 deletions packages/agent/src/adapters/claude/conversion/sdk-to-acp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,23 @@ function toolMeta(
toolName: string,
toolResponse?: unknown,
parentToolCallId?: string,
bashCommand?: string,
): ToolUpdateMeta {
const meta: ToolUpdateMeta["claudeCode"] = { toolName };
if (toolResponse !== undefined) meta.toolResponse = toolResponse;
if (parentToolCallId) meta.parentToolCallId = parentToolCallId;
if (bashCommand) meta.bashCommand = bashCommand;
return { claudeCode: meta };
}

function bashCommandFromToolUse(
toolUse: ToolUseCache[string] | undefined,
): string | undefined {
if (!toolUse || toolUse.name !== "Bash") return undefined;
const command = (toolUse.input as { command?: unknown } | undefined)?.command;
return typeof command === "string" ? command : undefined;
}

function handleTextChunk(
chunk: { text: string },
role: Role,
Expand Down Expand Up @@ -173,7 +183,12 @@ function handleToolUseChunk(
await ctx.client.sessionUpdate({
sessionId: ctx.sessionId,
update: {
_meta: toolMeta(toolUse.name, toolResponse, ctx.parentToolCallId),
_meta: toolMeta(
toolUse.name,
toolResponse,
ctx.parentToolCallId,
bashCommandFromToolUse(toolUse),
),
toolCallId: toolUseId,
sessionUpdate: "tool_call_update",
...(editUpdate ? editUpdate : {}),
Expand Down Expand Up @@ -203,7 +218,12 @@ function handleToolUseChunk(
});

const meta: Record<string, unknown> = {
...toolMeta(chunk.name, undefined, ctx.parentToolCallId),
...toolMeta(
chunk.name,
undefined,
ctx.parentToolCallId,
bashCommandFromToolUse(chunk),
),
};
if (chunk.name === "Bash" && ctx.supportsTerminalOutput && !alreadyCached) {
meta.terminal_info = { terminal_id: chunk.id };
Expand Down Expand Up @@ -343,7 +363,12 @@ function handleToolResultChunk(
}

const meta: Record<string, unknown> = {
...toolMeta(toolUse.name, undefined, ctx.parentToolCallId),
...toolMeta(
toolUse.name,
undefined,
ctx.parentToolCallId,
bashCommandFromToolUse(toolUse),
),
...(resultMeta?.terminal_exit
? { terminal_exit: resultMeta.terminal_exit }
: {}),
Expand Down
1 change: 1 addition & 0 deletions packages/agent/src/adapters/claude/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export type ToolUpdateMeta = {
toolName: string;
toolResponse?: unknown;
parentToolCallId?: string;
bashCommand?: string;
};
terminal_info?: TerminalInfo;
terminal_output?: TerminalOutput;
Expand Down
137 changes: 137 additions & 0 deletions packages/agent/src/pr-url-detector.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import { describe, expect, it } from "vitest";
import { extractCreatedPrUrl } from "./pr-url-detector";

describe("extractCreatedPrUrl", () => {
const PR_URL = "https://github.com/PostHog/posthog/pull/12345";

it("returns the URL when gh pr create produced it (string toolResponse)", () => {
expect(
extractCreatedPrUrl({
toolName: "Bash",
bashCommand: 'gh pr create --title "x" --body "y"',
toolResponse: `${PR_URL}\n`,
}),
).toBe(PR_URL);
});

it("returns the URL when gh pr create produced it (object toolResponse)", () => {
expect(
extractCreatedPrUrl({
toolName: "Bash",
bashCommand: "gh pr create --fill",
toolResponse: { stdout: `${PR_URL}\n`, stderr: "" },
}),
).toBe(PR_URL);
});

it("ignores PR URLs from gh pr view", () => {
expect(
extractCreatedPrUrl({
toolName: "Bash",
bashCommand: `gh pr view ${PR_URL}`,
toolResponse: { stdout: PR_URL },
}),
).toBeNull();
});

it("ignores PR URLs from gh search prs", () => {
expect(
extractCreatedPrUrl({
toolName: "Bash",
bashCommand: 'gh search prs "fix login"',
toolResponse: PR_URL,
}),
).toBeNull();
});

it("ignores PR URLs from gh pr list", () => {
expect(
extractCreatedPrUrl({
toolName: "Bash",
bashCommand: "gh pr list --json url",
toolResponse: PR_URL,
}),
).toBeNull();
});

it("returns null when bashCommand is missing", () => {
expect(
extractCreatedPrUrl({
toolName: "Bash",
bashCommand: undefined,
toolResponse: PR_URL,
}),
).toBeNull();
});

it("returns null for non-Bash tools", () => {
expect(
extractCreatedPrUrl({
toolName: "Edit",
bashCommand: "gh pr create",
toolResponse: PR_URL,
}),
).toBeNull();
});

it("accepts the lowercase 'bash' tool variant", () => {
expect(
extractCreatedPrUrl({
toolName: "bash",
bashCommand: "gh pr create",
toolResponse: PR_URL,
}),
).toBe(PR_URL);
});

it("returns null when output has no PR URL", () => {
expect(
extractCreatedPrUrl({
toolName: "Bash",
bashCommand: "gh pr create",
toolResponse: "no pr was created",
}),
).toBeNull();
});

it("finds the URL in the content array when toolResponse is empty", () => {
expect(
extractCreatedPrUrl({
toolName: "Bash",
bashCommand: "gh pr create --fill",
toolResponse: undefined,
content: [{ type: "text", text: `Created: ${PR_URL}` }],
}),
).toBe(PR_URL);
});

it("handles output field on object toolResponse", () => {
expect(
extractCreatedPrUrl({
toolName: "Bash",
bashCommand: "gh pr create",
toolResponse: { output: PR_URL },
}),
).toBe(PR_URL);
});

it("matches gh pr create even with a chained command", () => {
expect(
extractCreatedPrUrl({
toolName: "Bash",
bashCommand: "git push -u origin feat/x && gh pr create --fill",
toolResponse: { stdout: PR_URL },
}),
).toBe(PR_URL);
});

it("does not match a fake command containing 'pr create' as text", () => {
expect(
extractCreatedPrUrl({
toolName: "Bash",
bashCommand: "echo 'i should pr create later'",
toolResponse: PR_URL,
}),
).toBeNull();
});
});
Comment on lines +7 to +137
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Prefer parameterised tests

All 13 test cases follow the exact same shape — vary toolName, bashCommand, toolResponse/content, and expect a URL or null. Per the project's simplicity rules, this should be a single it.each() table rather than 13 separate it() blocks.

it.each([
  ["gh pr create (string response)", "Bash", 'gh pr create --title "x"', `${PR_URL}\n`, undefined, PR_URL],
  ["gh pr create (object stdout)",   "Bash", "gh pr create --fill",      { stdout: `${PR_URL}\n` }, undefined, PR_URL],
  ["gh pr view ignored",             "Bash", `gh pr view ${PR_URL}`,      { stdout: PR_URL },        undefined, null],
  // … remaining rows
])(
  "%s",
  (_desc, toolName, bashCommand, toolResponse, content, expected) => {
    expect(extractCreatedPrUrl({ toolName, bashCommand, toolResponse, content }))
      .toBe(expected);
  },
);

This also makes it trivial to add new cases without boilerplate.

Context Used: Do not attempt to comment on incorrect alphabetica... (source)

Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/agent/src/pr-url-detector.test.ts
Line: 7-137

Comment:
**Prefer parameterised tests**

All 13 test cases follow the exact same shape — vary `toolName`, `bashCommand`, `toolResponse`/`content`, and expect a URL or `null`. Per the project's simplicity rules, this should be a single `it.each()` table rather than 13 separate `it()` blocks.

```typescript
it.each([
  ["gh pr create (string response)", "Bash", 'gh pr create --title "x"', `${PR_URL}\n`, undefined, PR_URL],
  ["gh pr create (object stdout)",   "Bash", "gh pr create --fill",      { stdout: `${PR_URL}\n` }, undefined, PR_URL],
  ["gh pr view ignored",             "Bash", `gh pr view ${PR_URL}`,      { stdout: PR_URL },        undefined, null],
  // … remaining rows
])(
  "%s",
  (_desc, toolName, bashCommand, toolResponse, content, expected) => {
    expect(extractCreatedPrUrl({ toolName, bashCommand, toolResponse, content }))
      .toBe(expected);
  },
);
```

This also makes it trivial to add new cases without boilerplate.

**Context Used:** Do not attempt to comment on incorrect alphabetica... ([source](https://app.greptile.com/review/custom-context?memory=instruction-0))

How can I resolve this? If you propose a fix, please make it concise.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

46 changes: 46 additions & 0 deletions packages/agent/src/pr-url-detector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const PR_URL_REGEX = /https:\/\/github\.com\/[^/]+\/[^/]+\/pull\/\d+/;
const GH_PR_CREATE_REGEX = /\bgh\s+pr\s+create\b/;

export interface ExtractCreatedPrUrlInput {
toolName: string | undefined;
bashCommand: string | undefined;
toolResponse: unknown;
content?: Array<{ type?: string; text?: string }>;
}

export function extractCreatedPrUrl(
input: ExtractCreatedPrUrlInput,
): string | null {
const { toolName, bashCommand, toolResponse, content } = input;

if (!toolName || !/bash/i.test(toolName)) return null;
if (!bashCommand || !GH_PR_CREATE_REGEX.test(bashCommand)) return null;

let textToSearch = "";

if (toolResponse) {
if (typeof toolResponse === "string") {
textToSearch = toolResponse;
} else if (typeof toolResponse === "object" && toolResponse !== null) {
const respObj = toolResponse as Record<string, unknown>;
textToSearch =
String(respObj.stdout || "") + String(respObj.stderr || "");
if (!textToSearch && respObj.output) {
textToSearch = String(respObj.output);
}
}
}

if (Array.isArray(content)) {
for (const item of content) {
if (item.type === "text" && item.text) {
textToSearch += ` ${item.text}`;
}
}
}

if (!textToSearch) return null;

const match = textToSearch.match(PR_URL_REGEX);
return match ? match[0] : null;
}
Loading
Loading