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
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { AgentServer } from "./agent-server";

interface TestableServer {
configureEnvironment(args?: { isInternal?: boolean }): void;
}

const ENV_KEYS_UNDER_TEST = [
"LLM_GATEWAY_URL",
"ANTHROPIC_BASE_URL",
"OPENAI_BASE_URL",
] as const;

describe("AgentServer.configureEnvironment", () => {
const originalEnv: Partial<Record<string, string | undefined>> = {};

beforeEach(() => {
for (const key of ENV_KEYS_UNDER_TEST) {
originalEnv[key] = process.env[key];
delete process.env[key];
}
});

afterEach(() => {
for (const key of ENV_KEYS_UNDER_TEST) {
const value = originalEnv[key];
if (value === undefined) {
delete process.env[key];
} else {
process.env[key] = value;
}
}
});

const buildServer = (mode: "background" | "interactive"): TestableServer =>
new AgentServer({
port: 0,
jwtPublicKey: "test-key",
apiUrl: "https://us.posthog.com",
apiKey: "test-api-key",
projectId: 1,
mode,
taskId: "test-task-id",
runId: "test-run-id",
}) as unknown as TestableServer;

it("tags as background_agents when the task is internal", () => {
buildServer("interactive").configureEnvironment({ isInternal: true });

expect(process.env.LLM_GATEWAY_URL).toBe(
"https://gateway.us.posthog.com/background_agents",
);
expect(process.env.ANTHROPIC_BASE_URL).toBe(
"https://gateway.us.posthog.com/background_agents",
);
expect(process.env.OPENAI_BASE_URL).toBe(
"https://gateway.us.posthog.com/background_agents/v1",
);
});

it("tags as posthog_code when the task is not internal", () => {
buildServer("background").configureEnvironment({ isInternal: false });

expect(process.env.LLM_GATEWAY_URL).toBe(
"https://gateway.us.posthog.com/posthog_code",
);
});

it("tags as posthog_code when isInternal is omitted (getTask failure fallback)", () => {
buildServer("background").configureEnvironment();

expect(process.env.LLM_GATEWAY_URL).toBe(
"https://gateway.us.posthog.com/posthog_code",
);
});
Comment on lines +61 to +75
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 for same-outcome cases

Tests at lines 61-67 and 69-75 both assert identical output (posthog_code) and differ only in argument shape ({ isInternal: false } vs omitted). Per the team's preference for parameterised tests, these could be collapsed into a single it.each:

it.each([
  ["explicit false", { isInternal: false } as Parameters<TestableServer["configureEnvironment"]>[0]],
  ["omitted (getTask failure fallback)", undefined],
])("tags as posthog_code when %s", (_, args) => {
  buildServer("background").configureEnvironment(args);

  expect(process.env.LLM_GATEWAY_URL).toBe("https://gateway.us.posthog.com/posthog_code");
  expect(process.env.ANTHROPIC_BASE_URL).toBe("https://gateway.us.posthog.com/posthog_code");
  expect(process.env.OPENAI_BASE_URL).toBe("https://gateway.us.posthog.com/posthog_code/v1");
});

This also adds the missing ANTHROPIC_BASE_URL / OPENAI_BASE_URL assertions (present in the background_agents case but absent here).

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/server/agent-server.configure-environment.test.ts
Line: 61-75

Comment:
**Prefer parameterised tests for same-outcome cases**

Tests at lines 61-67 and 69-75 both assert identical output (`posthog_code`) and differ only in argument shape (`{ isInternal: false }` vs omitted). Per the team's preference for parameterised tests, these could be collapsed into a single `it.each`:

```ts
it.each([
  ["explicit false", { isInternal: false } as Parameters<TestableServer["configureEnvironment"]>[0]],
  ["omitted (getTask failure fallback)", undefined],
])("tags as posthog_code when %s", (_, args) => {
  buildServer("background").configureEnvironment(args);

  expect(process.env.LLM_GATEWAY_URL).toBe("https://gateway.us.posthog.com/posthog_code");
  expect(process.env.ANTHROPIC_BASE_URL).toBe("https://gateway.us.posthog.com/posthog_code");
  expect(process.env.OPENAI_BASE_URL).toBe("https://gateway.us.posthog.com/posthog_code/v1");
});
```

This also adds the missing `ANTHROPIC_BASE_URL` / `OPENAI_BASE_URL` assertions (present in the `background_agents` case but absent here).

**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!


it("ignores mode when picking the gateway product", () => {
buildServer("background").configureEnvironment({ isInternal: false });
const fromBackground = process.env.LLM_GATEWAY_URL;

buildServer("interactive").configureEnvironment({ isInternal: false });
const fromInteractive = process.env.LLM_GATEWAY_URL;

expect(fromBackground).toBe(fromInteractive);
expect(fromBackground).toBe("https://gateway.us.posthog.com/posthog_code");
});

it("respects the LLM_GATEWAY_URL override regardless of internal flag", () => {
process.env.LLM_GATEWAY_URL = "http://ngrok.test/proxy";

buildServer("background").configureEnvironment({ isInternal: true });

expect(process.env.LLM_GATEWAY_URL).toBe("http://ngrok.test/proxy");
expect(process.env.ANTHROPIC_BASE_URL).toBe("http://ngrok.test/proxy");
expect(process.env.OPENAI_BASE_URL).toBe("http://ngrok.test/proxy/v1");
});
});
17 changes: 11 additions & 6 deletions packages/agent/src/server/agent-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import type {
} from "../types";
import { resourceLink } from "../utils/acp-content";
import { AsyncMutex } from "../utils/async-mutex";
import { getLlmGatewayUrl } from "../utils/gateway";
import { type GatewayProduct, getLlmGatewayUrl } from "../utils/gateway";
import { Logger } from "../utils/logger";
import { logAgentshRuntimeInfo } from "./agentsh-runtime";
import {
Expand Down Expand Up @@ -778,8 +778,6 @@ export class AgentServer {
name: process.env.HOSTNAME || "cloud-sandbox",
};

this.configureEnvironment();

const [preTaskRun, preTask] = await Promise.all([
this.posthogAPI
.getTaskRun(payload.task_id, payload.run_id)
Expand All @@ -800,6 +798,8 @@ export class AgentServer {
}),
]);

this.configureEnvironment({ isInternal: preTask?.internal === true });

const prUrl = getTaskRunStateString(preTaskRun, "slack_notified_pr_url");

if (prUrl) {
Expand Down Expand Up @@ -1710,10 +1710,15 @@ ${attributionInstructions}
}
}

private configureEnvironment(): void {
private configureEnvironment({
isInternal = false,
}: {
isInternal?: boolean;
} = {}): void {
const { apiKey, apiUrl, projectId } = this.config;
const product =
this.config.mode === "background" ? "background_agents" : "posthog_code";
const product: GatewayProduct = isInternal
? "background_agents"
: "posthog_code";
const gatewayUrl =
process.env.LLM_GATEWAY_URL || getLlmGatewayUrl(apiUrl, product);
const openaiBaseUrl = gatewayUrl.endsWith("/v1")
Expand Down
1 change: 1 addition & 0 deletions packages/agent/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export interface Task {
github_integration?: number | null;
repository: string; // Format: "organization/repository" (e.g., "posthog/posthog-js")
json_schema?: Record<string, unknown> | null; // JSON schema for task output validation
internal?: boolean;
created_at: string;
updated_at: string;
created_by?: {
Expand Down
Loading