: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(harness): send attribution headers on pi web tool gateway calls (#122375)
Brandon8a56f29f4f59
diff --git a/packages/agent/packages/harness/src/extensions/orchestration/run-agent.test.ts b/packages/agent/packages/harness/src/extensions/orchestration/run-agent.test.tsindex 6b9de24eb3de..c31d35316ec5 100644--- a/packages/agent/packages/harness/src/extensions/orchestration/run-agent.test.ts+++ b/packages/agent/packages/harness/src/extensions/orchestration/run-agent.test.ts@@ -10,9 +10,12 @@ import {} from "@earendil-works/pi-coding-agent";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";-const { createAgentSessionMock } = vi.hoisted(() => ({- createAgentSessionMock: vi.fn(),-}));+const { createAgentSessionMock, createWebAccessExtensionMock } = vi.hoisted(+ () => ({+ createAgentSessionMock: vi.fn(),+ createWebAccessExtensionMock: vi.fn(() => () => {}),+ }),+);vi.mock("@earendil-works/pi-coding-agent", async (importOriginal) => {const actual =@@ -20,6 +23,10 @@ vi.mock("@earendil-works/pi-coding-agent", async (importOriginal) => {return { ...actual, createAgentSession: createAgentSessionMock };});+vi.mock("../web-access/extension", () => ({+ createWebAccessExtension: createWebAccessExtensionMock,+}));+import type { AgentConfig } from "./agents";import type { RunStatus } from "./lifecycle";import { runDirectory, transcriptPath } from "./lifecycle";@@ -67,12 +74,12 @@ function readTranscript(runId: string): string | undefined {}}-function makeModel(): Model<Api> {+function makeModel(provider = "anthropic"): Model<Api> {return {id: "sonnet",name: "Sonnet",api: "anthropic-messages",- provider: "anthropic",+ provider,baseUrl: "https://api.anthropic.com",reasoning: false,input: ["text"],@@ -82,8 +89,12 @@ function makeModel(): Model<Api> {} as Model<Api>;}-function makeCtx(cwd: string, projectTrusted = false): ExtensionContext {- const model = makeModel();+function makeCtx(+ cwd: string,+ projectTrusted = false,+ provider?: string,+): ExtensionContext {+ const model = makeModel(provider);return {cwd,model,@@ -92,7 +103,10 @@ function makeCtx(cwd: string, projectTrusted = false): ExtensionContext {modelRegistry: {find: () => model,getAll: () => [model],- getApiKeyAndHeaders: async () => ({ ok: true, apiKey: "test-key" }),+ getApiKeyAndHeaders: vi.fn(async () => ({+ ok: true,+ apiKey: "test-key",+ })),},} as unknown as ExtensionContext;}@@ -115,6 +129,7 @@ describe("runAgent lifecycle persistence", () => {);process.env.HOME = tmpHome;createAgentSessionMock.mockReset();+ createWebAccessExtensionMock.mockClear();createAgentSessionMock.mockResolvedValue({session: createFakeSession(),});@@ -201,6 +216,27 @@ describe("runAgent lifecycle persistence", () => {expect(sessionOptions.settingsManager.isProjectTrusted()).toBe(false);});+ it.each([+ ["posthog", { "X-PostHog-Task-Run-Id": "run-1" }],+ ["anthropic", undefined],+ ])(+ "gives the child's web tools a %s model's headers only when it is a posthog model",+ async (provider, expectedHeaders) => {+ const ctx = makeCtx(tmpHome, false, provider);+ vi.mocked(ctx.modelRegistry.getApiKeyAndHeaders).mockResolvedValue({+ ok: true,+ apiKey: "test-key",+ headers: { "X-PostHog-Task-Run-Id": "run-1" },+ });++ await runAgent({ ctx, agent, task: "find it", includeWebAccess: true });++ expect(createWebAccessExtensionMock).toHaveBeenLastCalledWith({+ headers: expectedHeaders,+ });+ },+ );+it("loads shell and reliability settings for child sessions", async () => {fs.mkdirSync(getAgentDir(), { recursive: true });fs.writeFileSync(diff --git a/packages/agent/packages/harness/src/extensions/orchestration/run-agent.ts b/packages/agent/packages/harness/src/extensions/orchestration/run-agent.tsindex 140e60e2d27f..dc39dac81f1c 100644--- a/packages/agent/packages/harness/src/extensions/orchestration/run-agent.ts+++ b/packages/agent/packages/harness/src/extensions/orchestration/run-agent.ts@@ -16,6 +16,7 @@ import {createPiToolCallRecord,type PiSubagentToolCall,} from "@posthog/agent-contracts";+import { POSTHOG_PROVIDER_NAME } from "../posthog-provider/provider";import { createWebAccessExtension } from "../web-access/extension";import type { AgentConfig } from "./agents";import {@@ -319,7 +320,18 @@ export async function runAgent() ??false;const extensionFactories = wantsWebAccess- ? [{ name: "web-access", factory: createWebAccessExtension() }]+ ? [+ {+ name: "web-access",+ // Web tools always call the PostHog gateway; another provider's headers can carry its credentials.+ factory: createWebAccessExtension({+ headers:+ modelAuth.model.provider === POSTHOG_PROVIDER_NAME+ ? modelAuth.headers+ : undefined,+ }),+ },+ ]: [];const systemPrompt = effectiveAgent.systemPrompt.trim() || undefined;const resourceLoader = new DefaultResourceLoader({diff --git a/packages/agent/packages/harness/src/extensions/posthog-provider/gateway-auth.test.ts b/packages/agent/packages/harness/src/extensions/posthog-provider/gateway-auth.test.tsindex f4afbdb219e2..96dc41e936e3 100644--- a/packages/agent/packages/harness/src/extensions/posthog-provider/gateway-auth.test.ts+++ b/packages/agent/packages/harness/src/extensions/posthog-provider/gateway-auth.test.ts@@ -20,21 +20,24 @@ describe("resolveGatewayAuth", () => {expect(auth).toEqual({baseUrl: getLlmGatewayUrl("us"),apiKey: "pha_static",+ headers: {},});});- it("uses an explicit gateway override", async () => {+ it("uses an explicit gateway override and carries its attribution headers", async () => {const auth = await resolveGatewayAuth({region: "us",apiKey: "proxy-key",baseUrl: "http://127.0.0.1:1234",+ headers: { "X-PostHog-Task-Run-Id": "run-1" },},fakeCtx(undefined),);expect(auth).toEqual({baseUrl: "http://127.0.0.1:1234",apiKey: "proxy-key",+ headers: { "X-PostHog-Task-Run-Id": "run-1" },});});@@ -46,6 +49,7 @@ describe("resolveGatewayAuth", () => {expect(auth).toEqual({baseUrl: getLlmGatewayUrl("eu"),apiKey: "pha_from_registry",+ headers: {},});});@@ -73,6 +77,7 @@ describe("tryResolveGatewayAuth", () => {expect(auth).toEqual({baseUrl: getLlmGatewayUrl("us"),apiKey: "pha_static",+ headers: {},});});});diff --git a/packages/agent/packages/harness/src/extensions/posthog-provider/gateway-auth.ts b/packages/agent/packages/harness/src/extensions/posthog-provider/gateway-auth.tsindex c431953e02db..b0d3b2f51525 100644--- a/packages/agent/packages/harness/src/extensions/posthog-provider/gateway-auth.ts+++ b/packages/agent/packages/harness/src/extensions/posthog-provider/gateway-auth.ts@@ -5,6 +5,8 @@ import { POSTHOG_PROVIDER_NAME, type PosthogProviderOptions } from "./provider";export interface GatewayAuth {baseUrl: string;apiKey: string;+ /** The provider's attribution headers, so direct gateway calls are tagged like model calls. */+ headers: Record<string, string>;}/**@@ -37,7 +39,7 @@ export async function resolveGatewayAuth();}- return { baseUrl, apiKey };+ return { baseUrl, apiKey, headers: options.headers ?? {} };}/**diff --git a/packages/agent/packages/harness/src/extensions/web-access/web-fetch.test.ts b/packages/agent/packages/harness/src/extensions/web-access/web-fetch.test.tsindex e563af41d1e8..ebc868cf7cf1 100644--- a/packages/agent/packages/harness/src/extensions/web-access/web-fetch.test.ts+++ b/packages/agent/packages/harness/src/extensions/web-access/web-fetch.test.ts@@ -221,7 +221,14 @@ describe("createWebFetchTool", () => {});globalThis.fetch = fetchMock as unknown as typeof fetch;- const tool = createWebFetchTool({ region: "us", apiKey: "pha_test" });+ const tool = createWebFetchTool({+ region: "us",+ apiKey: "pha_test",+ headers: {+ "X-PostHog-Task-Run-Id": "run-1",+ Authorization: "Bearer not-the-key",+ },+ });const result = await tool.execute("call-1",{ url: "https://example.com/other-page", prompt: "extract title" },@@ -234,11 +241,13 @@ describe("createWebFetchTool", () => {type: "text",text: "Summary text",});- expect(- fetchMock.mock.calls.some(([url]) =>- String(url).includes("/v1/chat/completions"),- ),- ).toBe(true);+ const summarizeCall = fetchMock.mock.calls.find(([url]) =>+ String(url).includes("/v1/chat/completions"),+ ) as unknown as [string, RequestInit] | undefined;+ expect(summarizeCall?.[1].headers).toMatchObject({+ Authorization: "Bearer pha_test",+ "X-PostHog-Task-Run-Id": "run-1",+ });});it("reports cross-host redirects instead of following them", async () => {diff --git a/packages/agent/packages/harness/src/extensions/web-access/web-fetch.ts b/packages/agent/packages/harness/src/extensions/web-access/web-fetch.tsindex d12a6aeb06bb..d031c27c1ae0 100644--- a/packages/agent/packages/harness/src/extensions/web-access/web-fetch.ts+++ b/packages/agent/packages/harness/src/extensions/web-access/web-fetch.ts@@ -238,6 +238,7 @@ async function summarize(const response = await fetch(`${baseUrl}/v1/chat/completions`, {method: "POST",headers: {+ ...gateway.headers,"Content-Type": "application/json",Authorization: `Bearer ${gateway.apiKey}`,},diff --git a/packages/agent/packages/harness/src/extensions/web-access/web-search.test.ts b/packages/agent/packages/harness/src/extensions/web-access/web-search.test.tsindex f3aef40bb318..08eb52494a55 100644--- a/packages/agent/packages/harness/src/extensions/web-access/web-search.test.ts+++ b/packages/agent/packages/harness/src/extensions/web-access/web-search.test.ts@@ -1,5 +1,9 @@-import type { ExtensionToolContext } from "@earendil-works/pi-coding-agent";+import type {+ ExtensionAPI,+ ExtensionToolContext,+} from "@earendil-works/pi-coding-agent";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import { createWebAccessExtension } from "./extension";import {createWebSearchTool,formatSearchResult,@@ -134,7 +138,14 @@ describe("createWebSearchTool", () => {});globalThis.fetch = fetchMock as unknown as typeof fetch;- const tool = createWebSearchTool({ region: "us", apiKey: "pha_test" });+ const tool = createWebSearchTool({+ region: "us",+ apiKey: "pha_test",+ headers: {+ "X-PostHog-Task-Run-Id": "run-1",+ Authorization: "Bearer not-the-key",+ },+ });const result = await tool.execute("call-1",{ query: "what happened today" },@@ -147,7 +158,10 @@ describe("createWebSearchTool", () => {"https://gateway.us.posthog.com/posthog_code/v1/responses",expect.objectContaining({method: "POST",- headers: expect.objectContaining({ Authorization: "Bearer pha_test" }),+ headers: expect.objectContaining({+ Authorization: "Bearer pha_test",+ "X-PostHog-Task-Run-Id": "run-1",+ }),}),);expect(result.content[0]).toMatchObject({@@ -159,6 +173,35 @@ describe("createWebSearchTool", () => {});});+ it("sends the extension's attribution headers from the registered tool", async () => {+ const fetchMock = vi.fn().mockResolvedValue({+ ok: true,+ json: async () => ({ output: [] }),+ });+ globalThis.fetch = fetchMock as unknown as typeof fetch;+ const registerTool = vi.fn();++ createWebAccessExtension({+ region: "us",+ apiKey: "pha_test",+ headers: { "X-PostHog-Task-Run-Id": "run-1" },+ })({ registerTool } as unknown as ExtensionAPI);+ const tool = registerTool.mock.calls+ .map(([registered]) => registered)+ .find((registered) => registered.name === "web_search");+ await tool.execute(+ "call-1",+ { query: "x" },+ undefined,+ undefined,+ fakeCtx(undefined),+ );++ expect(fetchMock.mock.calls[0][1].headers).toMatchObject({+ "X-PostHog-Task-Run-Id": "run-1",+ });+ });+it("throws a descriptive error on a non-ok response", async () => {globalThis.fetch = vi.fn().mockResolvedValue({ok: false,diff --git a/packages/agent/packages/harness/src/extensions/web-access/web-search.ts b/packages/agent/packages/harness/src/extensions/web-access/web-search.tsindex bf620a63447e..aeb46a83c9d6 100644--- a/packages/agent/packages/harness/src/extensions/web-access/web-search.ts+++ b/packages/agent/packages/harness/src/extensions/web-access/web-search.ts@@ -98,12 +98,16 @@ export function createWebSearchTool(options: PosthogProviderOptions = {}) {return renderWebSearchResult(result, options, theme, context.isError);},async execute(_toolCallId, params, signal, _onUpdate, ctx) {- const { baseUrl, apiKey } = await resolveGatewayAuth(options, ctx);+ const { baseUrl, apiKey, headers } = await resolveGatewayAuth(+ options,+ ctx,+ );const contextSize = parseSearchContextSize(params.search_context_size);const response = await fetch(`${baseUrl}/v1/responses`, {method: "POST",headers: {+ ...headers,"Content-Type": "application/json",Authorization: `Bearer ${apiKey}`,},