Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
24a952f
feat(gateway): use server-minted scoped tokens for model calls
brandonleung Aug 21, 2026
e4298f3
fix(gateway): apply the effort clamp on the path that builds the session
brandonleung Aug 21, 2026
260db83
fix(gateway): send the run's own bearer on the tutorial path
brandonleung Aug 22, 2026
4df98ed
fix(gateway): drop the effort clamp, which pi already applies
brandonleung Aug 22, 2026
fc6d2b6
test(gateway): assert region derivation rather than argument echo
brandonleung Aug 22, 2026
efe54d0
refactor(gateway): drop the unsent expires_in branch and three TTL knobs
brandonleung Aug 22, 2026
4598a45
chore(gateway): tighten the session comments
brandonleung Aug 22, 2026
60c5db9
chore(gateway): drop the em dashes from the comments this branch added
brandonleung Aug 24, 2026
131d537
Merge branch 'main' into brandon/gateway-token-flow
gewenyu99 Aug 24, 2026
5b7eb77
fix(gateway): fall back when the mint's expiry is unreadable
brandonleung Aug 24, 2026
7c413aa
fix(gateway): fail the run on a mint refusal instead of falling back …
brandonleung Aug 24, 2026
c32042f
feat(gateway): send the run's program id to the mint
brandonleung Aug 25, 2026
1b1992e
fix(gateway): fail the run instead of downgrading when the mint canno…
brandonleung Aug 25, 2026
91267e1
fix(gateway): log every mint failure and isolate the response-field g…
brandonleung Aug 25, 2026
b2d2fff
fix(gateway): require a typed programId and pin the cache refresh bou…
brandonleung Aug 25, 2026
42df7b6
fix(gateway): repoint the 400 copy and give the mint call real timeou…
brandonleung Aug 25, 2026
65484f7
chore(gateway): cut the session comments to what the code cannot say
brandonleung Aug 25, 2026
d9d3b0a
chore(gateway): correct two comments this branch made false
brandonleung Aug 28, 2026
034d9c8
Merge branch 'main' into brandon/gateway-token-flow
brandonleung Aug 28, 2026
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
124 changes: 124 additions & 0 deletions src/lib/__tests__/agent-interface.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
createStopHook,
isWarlockDisabled,
buildAuthErrorContext,
buildAgentEnv,
} from '@lib/agent/agent-interface';
import { AgentOutputSignals } from '@lib/agent/output-signals';
import { Sequence } from '@lib/constants';
Expand Down Expand Up @@ -85,6 +86,14 @@ describe('runAgent', () => {
posthogApiKey: 'phx_test_token',
sequence: Sequence.linear,
triageProvider: () => Promise.resolve('false_positive'),
gatewayAuth: {
// Deliberately different from posthogApiKey above: the subprocess must
// take its bearer from the run's resolved auth, and identical values
// would make either source pass.
gatewayUrl: 'https://gateway.test',
token: 'phe_run_scoped_token',
edition: 'legacy' as const,
},
};

beforeEach(() => {
Expand Down Expand Up @@ -582,3 +591,118 @@ describe('buildAuthErrorContext', () => {
).toBe(true);
});
});

describe('buildAgentEnv header shape', () => {
const metadata = { run_id: 'r1', integration: 'nextjs' };
const flags = { 'wizard-orchestrator': 'test' };

it('sends per-key headers and the bedrock opt-in on the legacy gateway', () => {
const encoded = buildAgentEnv(metadata, flags, {
gatewayUrl: 'https://gateway.us.posthog.com/wizard',
token: 'pha_oauth',
edition: 'legacy',
});
expect(encoded).toContain('x-posthog-use-bedrock-fallback');
expect(encoded).toContain('X-POSTHOG-PROPERTY-run_id');
expect(encoded).not.toContain('X-PostHog-Properties');
});

it('sends one properties blob and no bedrock opt-in on the new gateway', () => {
const encoded = buildAgentEnv(metadata, flags, {
gatewayUrl: 'https://ai-gateway.us.posthog.com',
token: 'phe_minted',
edition: 'v2',
teamId: 42,
});
expect(encoded).toContain('X-PostHog-Properties');
expect(encoded).not.toContain('x-posthog-use-bedrock-fallback');
expect(encoded).not.toContain('X-POSTHOG-PROPERTY-run_id');
// Fallback is native in the new gateway's routing chain, and the run tags
// ride the blob rather than per-key headers.
expect(encoded).toContain('run_id');
expect(encoded).toContain('team_id');
});
});

describe('subprocess gateway credentials', () => {
// Self-contained: the fixtures inside the runAgent describe are not in scope
// here, and this test only needs a config plus a spinner.
const spinner = { start: vi.fn(), stop: vi.fn(), message: vi.fn() };
const config = {
workingDirectory: '/test/dir',
mcpServers: {},
model: 'claude-sonnet-4-6',
// Deliberately different from the gateway bearer below: identical values
// would let either source pass.
posthogApiKey: 'phx_user_oauth_token',
sequence: Sequence.linear,
triageProvider: () => Promise.resolve('false_positive'),
gatewayAuth: {
gatewayUrl: 'https://ai-gateway.us.posthog.com',
token: 'phe_run_scoped_token',
edition: 'v2' as const,
teamId: 42,
},
};
const options: WizardRunOptions = {
debug: false,
installDir: '/test/dir',
signup: false,
ci: false,
benchmark: false,
yaraReport: false,
};

beforeEach(() => {
vi.clearAllMocks();
mockUIInstance.spinner.mockReturnValue(spinner);
});

it('takes the base url and bearer from the run own auth', async () => {
function* ok() {
yield {
type: 'result',
subtype: 'success',
is_error: false,
result: 'done',
};
}
mockQuery.mockReturnValue(ok());

await runAgent(
config,
'test prompt',
options,
spinner as unknown as SpinnerHandle,
{
successMessage: 'ok',
errorMessage: 'err',
},
);

const env = mockQuery.mock.calls[0][0].options.env;
expect(env.ANTHROPIC_BASE_URL).toBe('https://ai-gateway.us.posthog.com');
expect(env.ANTHROPIC_AUTH_TOKEN).toBe('phe_run_scoped_token');
expect(env.CLAUDE_CODE_OAUTH_TOKEN).toBe('phe_run_scoped_token');
// The MCP token is the user's own OAuth key and must not be swapped for
// the gateway bearer.
expect(env.POSTHOG_MCP_TOKEN).toBe('phx_user_oauth_token');
// v2 carries one properties blob, not the per-key legacy headers.
expect(env.ANTHROPIC_CUSTOM_HEADERS).toContain('X-PostHog-Properties');
});
});

describe('auth error context', () => {
// The 401 screen's region comes from whichever url it is handed, which is why
// runAgent passes the run's resolved auth rather than the process global a
// concurrent run also writes.
it.each([
['https://ai-gateway.us.posthog.com', 'us'],
['https://gateway.eu.posthog.com/wizard', 'eu'],
['http://localhost:3308', 'local'],
])('derives the region from the url it is given (%s)', (url, region) => {
const ctx = buildAuthErrorContext('/test/dir', url);
expect(ctx.gatewayUrl).toBe(url);
expect(ctx.region).toBe(region);
});
});
Loading
Loading