-
Notifications
You must be signed in to change notification settings - Fork 0
fix(console): complete production data transports #128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2a2d5be
fix(console): complete production data transports
Travis-Gilbert 51ba110
fix(console): guard server transport modules
Travis-Gilbert 51f8939
fix(console): correlate MCP stream responses
Travis-Gilbert ceae0f9
fix(console): honor negotiated MCP protocol
Travis-Gilbert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // SOURCING: none. Railway startup configuration validation. | ||
|
|
||
| /** Fail before the Console server starts when its production data plane is | ||
| * incomplete. Local development does not use the Railway launcher, so its | ||
| * localhost fallback remains available. */ | ||
| export function assertRailwayEnvironment(environment = process.env) { | ||
| if (!environment.CONSOLE_DATA_API_URL?.trim()) { | ||
| throw new Error( | ||
| 'CONSOLE_DATA_API_URL is required for CommonPlace Console Railway startup.', | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { describe, it } from 'node:test'; | ||
|
|
||
| import { assertRailwayEnvironment } from './railway-env.mjs'; | ||
|
|
||
| describe('assertRailwayEnvironment', () => { | ||
| it('requires the production data API URL', () => { | ||
| assert.throws( | ||
| () => assertRailwayEnvironment({}), | ||
| /CONSOLE_DATA_API_URL is required/, | ||
| ); | ||
| assert.throws( | ||
| () => assertRailwayEnvironment({ CONSOLE_DATA_API_URL: ' ' }), | ||
| /CONSOLE_DATA_API_URL is required/, | ||
| ); | ||
| }); | ||
|
|
||
| it('accepts an explicit data API URL', () => { | ||
| assert.doesNotThrow(() => | ||
| assertRailwayEnvironment({ | ||
| CONSOLE_DATA_API_URL: 'http://commonplace-api.railway.internal:8080', | ||
| }), | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| vi.mock('server-only', () => ({})); | ||
|
|
||
| import { consumerGraphqlUrl } from './consumer-graphql'; | ||
|
|
||
| describe('consumerGraphqlUrl', () => { | ||
| it('uses the explicit CommonPlace consumer GraphQL endpoint', () => { | ||
| expect( | ||
| consumerGraphqlUrl({ | ||
| THEOREM_GRAPHQL_URL: ' https://commonplace.example/graphql/ ', | ||
| }), | ||
| ).toBe('https://commonplace.example/graphql'); | ||
| }); | ||
|
|
||
| it('accepts the existing CommonPlace base URL contract', () => { | ||
| expect( | ||
| consumerGraphqlUrl({ | ||
| THEOREM_GRAPHQL_URL: 'https://commonplace.example/', | ||
| }), | ||
| ).toBe('https://commonplace.example/graphql'); | ||
| }); | ||
|
|
||
| it('does not fall back to the Harness MCP host', () => { | ||
| expect( | ||
| consumerGraphqlUrl({ | ||
| CONSOLE_HARNESS_URL: 'https://api.theoremharness.com', | ||
| }), | ||
| ).toBeNull(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // SOURCING: none. Server-only consumer GraphQL endpoint selection. | ||
|
|
||
| import 'server-only'; | ||
|
|
||
| /** | ||
| * Proactivity and Filing belong to the CommonPlace consumer schema. They must | ||
| * not fall back to CONSOLE_HARNESS_URL, which is the Harness MCP service and | ||
| * does not own their fields. | ||
| */ | ||
| export function consumerGraphqlUrl( | ||
| environment: Readonly<Record<string, string | undefined>> = process.env, | ||
| ): string | null { | ||
| const configured = environment.THEOREM_GRAPHQL_URL?.trim(); | ||
| if (!configured) return null; | ||
|
|
||
| const base = configured.replace(/\/+$/, ''); | ||
| return base.endsWith('/graphql') ? base : `${base}/graphql`; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| const { principalTenantHeadersMock, resolveHarnessPrincipalMock } = vi.hoisted(() => ({ | ||
| principalTenantHeadersMock: vi.fn(), | ||
| resolveHarnessPrincipalMock: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock('server-only', () => ({})); | ||
| vi.mock('@/lib/server/harness-principal', () => ({ | ||
| principalTenantHeaders: principalTenantHeadersMock, | ||
| resolveHarnessPrincipal: resolveHarnessPrincipalMock, | ||
| })); | ||
|
|
||
| import { callHarnessMcp } from './harness-mcp'; | ||
|
|
||
| const principal = { | ||
| tenant: 'Travis-Gilbert', | ||
| githubLogin: 'Travis-Gilbert', | ||
| harnessIdentity: 'github:owner', | ||
| }; | ||
|
|
||
| function sse(payload: Record<string, unknown>, headers: Record<string, string> = {}): Response { | ||
| return new Response(`:\n\ndata: ${JSON.stringify(payload)}\n\n`, { | ||
| status: 200, | ||
| headers: { | ||
| 'Content-Type': 'text/event-stream', | ||
| ...headers, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| function notificationThenMultilineSse( | ||
| notification: Record<string, unknown>, | ||
| payload: Record<string, unknown>, | ||
| ): Response { | ||
| const serialized = JSON.stringify(payload); | ||
| const splitAt = serialized.indexOf(',') + 1; | ||
| return new Response( | ||
| `data: ${JSON.stringify(notification)}\n\n` | ||
| + `:\n\ndata: ${serialized.slice(0, splitAt)}\ndata: ${serialized.slice(splitAt)}\n\n`, | ||
| { | ||
| status: 200, | ||
| headers: { 'Content-Type': 'text/event-stream' }, | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| vi.stubEnv('CONSOLE_HARNESS_URL', 'https://api.theoremharness.com'); | ||
| vi.stubEnv('CONSOLE_HARNESS_TOKEN', 'test-harness-token'); | ||
| resolveHarnessPrincipalMock.mockResolvedValue({ ok: true, principal }); | ||
| principalTenantHeadersMock.mockReturnValue({ | ||
| 'x-theorem-tenant': principal.tenant, | ||
| 'x-theorem-principal': principal.harnessIdentity, | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllGlobals(); | ||
| vi.unstubAllEnvs(); | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| describe('callHarnessMcp', () => { | ||
| it('initializes a principal-bound MCP session before calling a tool', async () => { | ||
| const fetchMock = vi.fn() | ||
| .mockImplementationOnce((_input: string, init: RequestInit) => { | ||
| const id = JSON.parse(String(init.body)).id as string; | ||
| return Promise.resolve(sse({ | ||
| jsonrpc: '2.0', | ||
| id, | ||
| result: { | ||
| protocolVersion: '2025-03-26', | ||
| serverInfo: { name: 'theorem-mcp-server', version: '0.1.0' }, | ||
| }, | ||
| }, { 'MCP-Session-Id': 'session-1' })); | ||
| }) | ||
| .mockResolvedValueOnce(new Response(null, { status: 202 })) | ||
| .mockImplementationOnce((_input: string, init: RequestInit) => { | ||
| const id = JSON.parse(String(init.body)).id as string; | ||
| return Promise.resolve(notificationThenMultilineSse( | ||
| { | ||
| jsonrpc: '2.0', | ||
| method: 'notifications/progress', | ||
| params: { progressToken: 'probe', progress: 0.5 }, | ||
| }, | ||
| { | ||
| jsonrpc: '2.0', | ||
| id, | ||
| result: { | ||
| structuredContent: { | ||
| data: { observedModel: { eventCount: 0 } }, | ||
| }, | ||
| }, | ||
| }, | ||
| )); | ||
| }) | ||
| .mockResolvedValueOnce(new Response(null, { status: 200 })); | ||
| vi.stubGlobal('fetch', fetchMock); | ||
|
|
||
| const result = await callHarnessMcp('graphql_query', { | ||
| query: 'query { observedModel(topicId: "topic") }', | ||
| }); | ||
|
|
||
| expect(result).toEqual({ | ||
| ok: true, | ||
| data: { data: { observedModel: { eventCount: 0 } } }, | ||
| principal, | ||
| }); | ||
| expect(fetchMock).toHaveBeenCalledTimes(4); | ||
|
|
||
| const initialize = fetchMock.mock.calls[0] as [string, RequestInit]; | ||
| expect(initialize[1].headers).toMatchObject({ | ||
| Accept: 'application/json, text/event-stream', | ||
| Authorization: 'Bearer test-harness-token', | ||
| 'MCP-Protocol-Version': '2025-06-18', | ||
| }); | ||
| expect(JSON.parse(String(initialize[1].body))).toMatchObject({ | ||
| method: 'initialize', | ||
| params: { protocolVersion: '2025-06-18' }, | ||
| }); | ||
|
|
||
| const ready = fetchMock.mock.calls[1] as [string, RequestInit]; | ||
| expect(ready[1].headers).toMatchObject({ | ||
| 'MCP-Protocol-Version': '2025-03-26', | ||
| 'MCP-Session-Id': 'session-1', | ||
| }); | ||
| expect(JSON.parse(String(ready[1].body))).toMatchObject({ | ||
| method: 'notifications/initialized', | ||
| }); | ||
|
|
||
| const toolCall = fetchMock.mock.calls[2] as [string, RequestInit]; | ||
| expect(toolCall[1].headers).toMatchObject({ | ||
| 'MCP-Protocol-Version': '2025-03-26', | ||
| 'MCP-Session-Id': 'session-1', | ||
| }); | ||
| expect(JSON.parse(String(toolCall[1].body))).toMatchObject({ | ||
| method: 'tools/call', | ||
| params: { | ||
| name: 'graphql_query', | ||
| arguments: { | ||
| tenant: principal.tenant, | ||
| tenant_slug: principal.tenant, | ||
| actor: principal.harnessIdentity, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const close = fetchMock.mock.calls[3] as [string, RequestInit]; | ||
| expect(close[1]).toMatchObject({ | ||
| method: 'DELETE', | ||
| headers: expect.objectContaining({ | ||
| 'MCP-Protocol-Version': '2025-03-26', | ||
| 'MCP-Session-Id': 'session-1', | ||
| }), | ||
| }); | ||
| expect(close[1].signal).not.toBe(toolCall[1].signal); | ||
| }); | ||
|
|
||
| it('fails closed when initialization does not return a session id', async () => { | ||
| const fetchMock = vi.fn().mockResolvedValue(sse({ | ||
| jsonrpc: '2.0', | ||
| id: 'initialize', | ||
| result: { | ||
| protocolVersion: '2025-06-18', | ||
| serverInfo: { name: 'theorem-mcp-server', version: '0.1.0' }, | ||
| }, | ||
| })); | ||
| vi.stubGlobal('fetch', fetchMock); | ||
|
|
||
| const result = await callHarnessMcp('graphql_query', { query: 'query { status }' }); | ||
|
|
||
| expect(result.ok).toBe(false); | ||
| if (!result.ok) { | ||
| await expect(result.response.json()).resolves.toMatchObject({ | ||
| error: 'harness_mcp_initialization_failed', | ||
| }); | ||
| } | ||
| expect(fetchMock).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.