Skip to content

feat(agent): route signal_report tasks to signals gateway product#2288

Merged
joshsny merged 4 commits into
mainfrom
posthog-code/route-signal-report-tasks-to-signals
May 21, 2026
Merged

feat(agent): route signal_report tasks to signals gateway product#2288
joshsny merged 4 commits into
mainfrom
posthog-code/route-signal-report-tasks-to-signals

Conversation

@joshsny
Copy link
Copy Markdown
Contributor

@joshsny joshsny commented May 21, 2026

Problem

Signals/report-generation traffic currently bills to the background_agents LLM gateway product, sharing a cost pool with every other internal task (Slack-triggered runs, session-summary work, etc.). This makes it hard to track signals spend separately and apply a dedicated budget.

Changes

Route LLM gateway calls so each task type lands in the right product bucket:

  • internal task with origin_product = \"signal_report\"signals
  • any other internal task → background_agents
  • any other task → posthog_code

The decision was pulled out of AgentServer.configureEnvironment into a small pure helper, resolveGatewayProduct, exported from @posthog/agent next to the GatewayProduct type so the rule is independently testable and easy to reuse.

Files touched:

  • `packages/agent/src/utils/gateway.ts` — add `signals` to `GatewayProduct`, add `resolveGatewayProduct` helper.
  • `packages/agent/src/utils/gateway.test.ts` — unit tests for every branch of the helper.
  • `packages/agent/src/server/agent-server.ts` — pass `origin_product` into `configureEnvironment` and delegate to `resolveGatewayProduct`.
  • `packages/agent/src/server/agent-server.configure-environment.test.ts` — add coverage for the new signals branch and the non-internal + signal_report carve-out.
  • `packages/agent/src/types.ts` — add `signal_report` to the `Task.origin_product` union.

Pairs with the gateway-side change posthog/posthog#59433 which adds the per-user cost limit for the signals product ($500/7d burst, $1000/30d sustained).

How did you test this?

I'm an agent. I ran:

  • `pnpm --filter @posthog/agent test src/utils/gateway.test.ts src/server/agent-server.configure-environment.test.ts` → 12/12 passing
  • biome + tsc via the pre-commit hook on the staged changes

I did not run the full test suite or any manual / end-to-end testing.

Publish to changelog?

no

joshsny added 3 commits May 21, 2026 14:17
Attribute LLM gateway spend per task type so signals/report-generation
cost can be tracked separately from background_agents and posthog_code:

  - internal task with origin_product "signal_report" -> "signals"
  - any other internal task                          -> "background_agents"
  - any other task                                   -> "posthog_code"

Extracted the decision into resolveGatewayProduct in @posthog/agent so
the rule lives next to the GatewayProduct type and is independently
testable.

Generated-By: PostHog Code
Task-Id: 31cea4e3-e0d7-49e7-bd01-d41bcafabe53
Send `x-posthog-property-task_origin_product` and
`x-posthog-property-task_internal` to the LLM gateway via
ANTHROPIC_CUSTOM_HEADERS. The gateway lifts any `x-posthog-property-*`
header onto the captured `$ai_generation` event, so this lands task
metadata on every generation we drive through Claude — letting
LLM analytics break spend down by task origin without needing the
gateway-product alone to encode it.

The OpenAI/codex path has no equivalent custom-headers env var, so
forwarding is Anthropic-only today.

Also inline the one-shot `signal_report` constant — the literal only
appears once now that `resolveGatewayProduct` is the single decision
point.

Generated-By: PostHog Code
Task-Id: 31cea4e3-e0d7-49e7-bd01-d41bcafabe53
Extend ANTHROPIC_CUSTOM_HEADERS with the IDs the agent server already
has in scope (from the JWT payload) so every $ai_generation captured
by the gateway can be joined back to the task, run, and triggering
user without an extra hop.

Generated-By: PostHog Code
Task-Id: 31cea4e3-e0d7-49e7-bd01-d41bcafabe53
@joshsny joshsny requested a review from a team May 21, 2026 15:37
@joshsny joshsny marked this pull request as ready for review May 21, 2026 15:38
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 21, 2026

Prompt To Fix All With AI
Fix the following 1 code review issue. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 1
packages/agent/src/utils/gateway.test.ts:4-42
The five `resolveGatewayProduct` cases share identical structure — call the function with `{isInternal, originProduct}`, assert the returned `GatewayProduct` — which is the canonical candidate for `it.each`. The first test also bundles two distinct inputs (`isInternal: false` and the default call) into one assertion block, hiding which input failed when the test breaks. A parameterised table makes the full input/output contract visible at a glance and is consistent with the team's stated preference.

```suggestion
describe("resolveGatewayProduct", () => {
  it.each([
    { isInternal: false, originProduct: undefined, expected: "posthog_code" },
    { isInternal: undefined, originProduct: undefined, expected: "posthog_code" },
    { isInternal: false, originProduct: "signal_report", expected: "posthog_code" },
    { isInternal: true, originProduct: undefined, expected: "background_agents" },
    { isInternal: true, originProduct: "session_summaries", expected: "background_agents" },
    { isInternal: true, originProduct: "signal_report", expected: "signals" },
  ])(
    "isInternal=$isInternal originProduct=$originProduct → $expected",
    ({ isInternal, originProduct, expected }) => {
      expect(resolveGatewayProduct({ isInternal, originProduct })).toBe(expected);
    },
  );
});
```

Reviews (1): Last reviewed commit: "feat(agent): forward task_id, task_run_i..." | Re-trigger Greptile

Comment thread packages/agent/src/utils/gateway.test.ts
Per review feedback, collapse the five near-identical resolveGatewayProduct
tests into a single it.each table so the input/output contract is visible
at a glance and each row reports independently on failure.

Generated-By: PostHog Code
Task-Id: 31cea4e3-e0d7-49e7-bd01-d41bcafabe53
@joshsny joshsny enabled auto-merge (squash) May 21, 2026 15:54
@joshsny joshsny merged commit f43502b into main May 21, 2026
15 checks passed
@joshsny joshsny deleted the posthog-code/route-signal-report-tasks-to-signals branch May 21, 2026 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants