-
Notifications
You must be signed in to change notification settings - Fork 1
[codex] Add chat-v2 attach-or-embed startup #154
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
| import { formatChatV2RuntimeNotice, resolveChatV2Runtime } from '@/cli-v2/commands/chat-v2-command.js'; | ||
| import type { ResolvedRuntimeHost } from '@/core/runtime/daemon/index.js'; | ||
| import type { HeddleControlPlaneServerHandle } from '@/server/index.js'; | ||
|
|
||
| describe('chat-v2 runtime bootstrap', () => { | ||
| it('attaches to a fresh live control-plane server', async () => { | ||
| const startServer = vi.fn(); | ||
| const runtime = await resolveChatV2Runtime({ | ||
| workspaceRoot: '/repo', | ||
| stateDir: '.heddle', | ||
| preferApiKey: false, | ||
| forceOwnerConflict: false, | ||
| runtimeHost: freshRuntimeHost, | ||
| }, { startServer }); | ||
|
|
||
| expect(startServer).not.toHaveBeenCalled(); | ||
| expect(runtime).toMatchObject({ | ||
| kind: 'attached', | ||
| trpcUrl: 'http://127.0.0.1:8765/trpc', | ||
| serverId: 'server-1', | ||
| }); | ||
| expect(formatChatV2RuntimeNotice(runtime)).toContain('attaching chat-v2'); | ||
| }); | ||
|
|
||
| it('starts an embedded control-plane server when no live server exists', async () => { | ||
| const close = vi.fn(async () => undefined); | ||
| const startServer = vi.fn(async (options) => createServerHandle({ | ||
| serverId: 'embedded-1', | ||
| host: options.host, | ||
| port: 8123, | ||
| close, | ||
| })); | ||
|
|
||
| const runtime = await resolveChatV2Runtime({ | ||
| workspaceRoot: '/repo', | ||
| stateDir: '.heddle-test', | ||
| preferApiKey: true, | ||
| forceOwnerConflict: false, | ||
| runtimeHost: { | ||
| kind: 'none', | ||
| registryPath: '/registry.json', | ||
| }, | ||
| }, { startServer }); | ||
|
|
||
| expect(startServer).toHaveBeenCalledWith(expect.objectContaining({ | ||
| mode: 'embedded-chat', | ||
| workspaceRoot: '/repo', | ||
| stateRoot: '/repo/.heddle-test', | ||
| preferApiKey: true, | ||
| host: '127.0.0.1', | ||
| port: 0, | ||
| serveAssets: false, | ||
| })); | ||
| expect(runtime).toMatchObject({ | ||
| kind: 'embedded', | ||
| trpcUrl: 'http://127.0.0.1:8123/trpc', | ||
| serverId: 'embedded-1', | ||
| }); | ||
| await runtime.close(); | ||
| expect(close).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('starts embedded when force-owner-conflict bypasses a live server', async () => { | ||
| const startServer = vi.fn(async () => createServerHandle({ | ||
| serverId: 'forced-embedded', | ||
| host: '127.0.0.1', | ||
| port: 8765, | ||
| })); | ||
|
|
||
| const runtime = await resolveChatV2Runtime({ | ||
| workspaceRoot: '/repo', | ||
| stateDir: '.heddle', | ||
| preferApiKey: false, | ||
| forceOwnerConflict: true, | ||
| runtimeHost: freshRuntimeHost, | ||
| }, { startServer }); | ||
|
|
||
| expect(startServer).toHaveBeenCalledTimes(1); | ||
| expect(runtime.kind).toBe('embedded'); | ||
| }); | ||
| }); | ||
|
|
||
| const freshRuntimeHost: ResolvedRuntimeHost = { | ||
| kind: 'server', | ||
| registryPath: '/registry.json', | ||
| serverId: 'server-1', | ||
| mode: 'daemon', | ||
| endpoint: { | ||
| host: '127.0.0.1', | ||
| port: 8765, | ||
| }, | ||
| startedAt: '2026-06-02T00:00:00.000Z', | ||
| lastSeenAt: '2026-06-02T00:00:01.000Z', | ||
| stale: false, | ||
| ageMs: 100, | ||
| }; | ||
|
|
||
| function createServerHandle(input: { | ||
| serverId: string; | ||
| host: string; | ||
| port: number; | ||
| close?: () => Promise<void>; | ||
| }): HeddleControlPlaneServerHandle { | ||
| return { | ||
| mode: 'embedded-chat', | ||
| serverId: input.serverId, | ||
| host: input.host, | ||
| port: input.port, | ||
| endpoint: { | ||
| host: input.host, | ||
| port: input.port, | ||
| }, | ||
| registryPath: '/registry.json', | ||
| workspaceRoot: '/repo', | ||
| stateRoot: '/repo/.heddle', | ||
| startedAt: '2026-06-02T00:00:00.000Z', | ||
| close: input.close ?? (async () => undefined), | ||
| }; | ||
| } |
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,147 @@ | ||
| import { resolve } from 'node:path'; | ||
| import type { ResolvedRuntimeHost } from '@/core/runtime/daemon/index.js'; | ||
| import type { HeddleControlPlaneServerHandle, HeddleControlPlaneServerOptions } from '@/server/index.js'; | ||
| import { startHeddleControlPlaneServer } from '@/server/index.js'; | ||
| import { startChatCliV2 } from '../index.js'; | ||
|
|
||
| const DEFAULT_CONTROL_PLANE_HOST = '127.0.0.1'; | ||
| const EMBEDDED_CONTROL_PLANE_PORT = 0; | ||
|
|
||
| export type ChatCliV2CommandOptions = { | ||
| workspaceRoot: string; | ||
| activeWorkspaceId: string; | ||
| model?: string; | ||
| maxSteps?: number; | ||
| preferApiKey: boolean; | ||
| stateDir: string; | ||
| searchIgnoreDirs: string[]; | ||
| systemContext?: string; | ||
| runtimeHost: ResolvedRuntimeHost; | ||
| forceOwnerConflict: boolean; | ||
| }; | ||
|
|
||
| export type ChatV2RuntimeInput = { | ||
| workspaceRoot: string; | ||
| stateDir: string; | ||
| preferApiKey: boolean; | ||
| runtimeHost: ResolvedRuntimeHost; | ||
| forceOwnerConflict: boolean; | ||
| }; | ||
|
|
||
| export type ChatV2Runtime = { | ||
| kind: 'attached' | 'embedded'; | ||
| trpcUrl: string; | ||
| endpoint: { | ||
| host: string; | ||
| port: number; | ||
| }; | ||
| serverId: string; | ||
| close: () => Promise<void>; | ||
| }; | ||
|
|
||
| type ChatV2RuntimeDependencies = { | ||
| startServer?: (options: HeddleControlPlaneServerOptions) => Promise<HeddleControlPlaneServerHandle>; | ||
| }; | ||
|
|
||
| export async function runChatCliV2Command(options: ChatCliV2CommandOptions): Promise<void> { | ||
| const runtime = await resolveChatV2Runtime(options); | ||
| process.stdout.write(`${formatChatV2RuntimeNotice(runtime)}\n`); | ||
| const uninstallRuntimeShutdown = | ||
| runtime.kind === 'embedded' ? installChatV2EmbeddedRuntimeShutdown(runtime) : () => undefined; | ||
| const app = startChatCliV2({ | ||
| trpcUrl: runtime.trpcUrl, | ||
| workspaceId: options.activeWorkspaceId, | ||
| model: options.model, | ||
| maxSteps: options.maxSteps, | ||
| searchIgnoreDirs: options.searchIgnoreDirs, | ||
| systemContext: options.systemContext, | ||
| preferApiKey: options.preferApiKey, | ||
| }); | ||
| try { | ||
| await app.waitUntilExit(); | ||
| } finally { | ||
| uninstallRuntimeShutdown(); | ||
| await runtime.close(); | ||
| } | ||
| } | ||
|
|
||
| export async function resolveChatV2Runtime( | ||
| input: ChatV2RuntimeInput, | ||
| dependencies: ChatV2RuntimeDependencies = {}, | ||
| ): Promise<ChatV2Runtime> { | ||
| if (!input.forceOwnerConflict && input.runtimeHost.kind === 'server' && !input.runtimeHost.stale) { | ||
| return { | ||
| kind: 'attached', | ||
| trpcUrl: buildTrpcUrl(input.runtimeHost.endpoint), | ||
| endpoint: input.runtimeHost.endpoint, | ||
| serverId: input.runtimeHost.serverId, | ||
| close: async () => undefined, | ||
| }; | ||
| } | ||
|
|
||
| const startServer = dependencies.startServer ?? startHeddleControlPlaneServer; | ||
| const handle = await startServer({ | ||
| mode: 'embedded-chat', | ||
| workspaceRoot: input.workspaceRoot, | ||
| stateRoot: resolve(input.workspaceRoot, input.stateDir), | ||
| preferApiKey: input.preferApiKey, | ||
| host: DEFAULT_CONTROL_PLANE_HOST, | ||
| port: EMBEDDED_CONTROL_PLANE_PORT, | ||
| serveAssets: false, | ||
| }); | ||
|
|
||
| return { | ||
| kind: 'embedded', | ||
| trpcUrl: buildTrpcUrl(handle.endpoint), | ||
| endpoint: handle.endpoint, | ||
| serverId: handle.serverId, | ||
| close: handle.close, | ||
| }; | ||
| } | ||
|
|
||
| export function formatChatV2RuntimeNotice(runtime: ChatV2Runtime): string { | ||
| if (runtime.kind === 'attached') { | ||
| return [ | ||
| 'Heddle notice: attaching chat-v2 to the live control-plane server.', | ||
| `server=http://${runtime.endpoint.host}:${runtime.endpoint.port}`, | ||
| `serverId=${runtime.serverId}`, | ||
| ].join(' '); | ||
| } | ||
|
|
||
| return [ | ||
| 'Heddle notice: started embedded chat-v2 control-plane server.', | ||
| `server=http://${runtime.endpoint.host}:${runtime.endpoint.port}`, | ||
| `serverId=${runtime.serverId}`, | ||
| ].join(' '); | ||
| } | ||
|
|
||
| export function installChatV2EmbeddedRuntimeShutdown(runtime: ChatV2Runtime): () => void { | ||
| let shuttingDown = false; | ||
| const shutdown = (signal: NodeJS.Signals) => { | ||
| if (shuttingDown) { | ||
| return; | ||
| } | ||
|
|
||
| shuttingDown = true; | ||
| runtime.close() | ||
| .catch((error) => { | ||
| process.stderr.write(`Heddle embedded chat-v2 server failed during ${signal} shutdown: ${String(error)}\n`); | ||
| process.exitCode = 1; | ||
| }) | ||
| .finally(() => { | ||
| process.exit(); | ||
| }); | ||
| }; | ||
|
|
||
| process.once('SIGINT', shutdown); | ||
| process.once('SIGTERM', shutdown); | ||
|
|
||
| return () => { | ||
| process.off('SIGINT', shutdown); | ||
| process.off('SIGTERM', shutdown); | ||
| }; | ||
| } | ||
|
|
||
| function buildTrpcUrl(endpoint: { host: string; port: number }): string { | ||
| return `http://${endpoint.host}:${endpoint.port}/trpc`; | ||
| } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When an external
SIGTERM/SIGINTreaches an embeddedchat-v2process after the TUI has started, this handler awaitsruntime.close()while the mounted app still owns live tRPC/EventSource subscriptions (ControlPlaneSessionSubscriptionServiceonly unsubscribes fromAppunmount). The HTTP server close path waits for active connections to finish, so those open subscriptions can keepruntime.close()from resolving and the.finally(process.exit)path is never reached. The signal path should first unmount/dispose the Ink app or otherwise force-close client/server connections before awaiting the server close.Useful? React with 👍 / 👎.