Skip to content
Merged
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 @@ -90,13 +90,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 @@ -181,7 +191,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 @@ -211,7 +226,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 @@ -351,7 +371,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 @@ -106,6 +106,7 @@ export type ToolUpdateMeta = {
toolName: string;
toolResponse?: unknown;
parentToolCallId?: string;
bashCommand?: string;
};
terminal_info?: TerminalInfo;
terminal_output?: TerminalOutput;
Expand Down
140 changes: 140 additions & 0 deletions packages/agent/src/pr-url-detector.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import { describe, expect, it } from "vitest";
import {
type ExtractCreatedPrUrlInput,
extractCreatedPrUrl,
} from "./pr-url-detector";

const PR_URL = "https://github.com/PostHog/posthog/pull/12345";

interface Case {
name: string;
input: ExtractCreatedPrUrlInput;
expected: string | null;
}

const cases: Case[] = [
{
name: "returns the URL when gh pr create produced it (string toolResponse)",
input: {
toolName: "Bash",
bashCommand: 'gh pr create --title "x" --body "y"',
toolResponse: `${PR_URL}\n`,
},
expected: PR_URL,
},
{
name: "returns the URL when gh pr create produced it (object toolResponse)",
input: {
toolName: "Bash",
bashCommand: "gh pr create --fill",
toolResponse: { stdout: `${PR_URL}\n`, stderr: "" },
},
expected: PR_URL,
},
{
name: "ignores PR URLs from gh pr view",
input: {
toolName: "Bash",
bashCommand: `gh pr view ${PR_URL}`,
toolResponse: { stdout: PR_URL },
},
expected: null,
},
{
name: "ignores PR URLs from gh search prs",
input: {
toolName: "Bash",
bashCommand: 'gh search prs "fix login"',
toolResponse: PR_URL,
},
expected: null,
},
{
name: "ignores PR URLs from gh pr list",
input: {
toolName: "Bash",
bashCommand: "gh pr list --json url",
toolResponse: PR_URL,
},
expected: null,
},
{
name: "returns null when bashCommand is missing",
input: {
toolName: "Bash",
bashCommand: undefined,
toolResponse: PR_URL,
},
expected: null,
},
{
name: "returns null for non-Bash tools",
input: {
toolName: "Edit",
bashCommand: "gh pr create",
toolResponse: PR_URL,
},
expected: null,
},
{
name: "accepts the lowercase 'bash' tool variant",
input: {
toolName: "bash",
bashCommand: "gh pr create",
toolResponse: PR_URL,
},
expected: PR_URL,
},
{
name: "returns null when output has no PR URL",
input: {
toolName: "Bash",
bashCommand: "gh pr create",
toolResponse: "no pr was created",
},
expected: null,
},
{
name: "finds the URL in the content array when toolResponse is empty",
input: {
toolName: "Bash",
bashCommand: "gh pr create --fill",
toolResponse: undefined,
content: [{ type: "text", text: `Created: ${PR_URL}` }],
},
expected: PR_URL,
},
{
name: "handles output field on object toolResponse",
input: {
toolName: "Bash",
bashCommand: "gh pr create",
toolResponse: { output: PR_URL },
},
expected: PR_URL,
},
{
name: "matches gh pr create even with a chained command",
input: {
toolName: "Bash",
bashCommand: "git push -u origin feat/x && gh pr create --fill",
toolResponse: { stdout: PR_URL },
},
expected: PR_URL,
},
{
name: "does not match a fake command containing 'pr create' as text",
input: {
toolName: "Bash",
bashCommand: "echo 'i should pr create later'",
toolResponse: PR_URL,
},
expected: null,
},
];

describe("extractCreatedPrUrl", () => {
it.each(cases)("$name", ({ input, expected }) => {
expect(extractCreatedPrUrl(input)).toBe(expected);
});
});
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