refactor(server): centralize daemon-client correlated RPC wiring

What changed:
- Added private  helper in  to centralize request-id resolution, schema parsing, and correlated RPC dispatch defaults.
- Migrated checkout/worktree/branch/file-explorer/provider/speech/command request methods to use the helper while preserving existing message types and per-method timeouts.
- Added a deterministic unit test covering  request/response wiring.

Reasoning:
- The prior implementation duplicated the same correlated-RPC boilerplate across many methods, increasing maintenance cost and risk of drift.
- Centralizing this path reduces logic density and keeps transport behavior consistent.
- Additional test coverage protects one of the checkout RPC flows touched by the refactor.

Accomplishments for next agent:
- Reduced repetitive correlated RPC scaffolding in  without changing external behavior.
- Verified with workspace typecheck and daemon-client test suite.

Challenges / follow-ups for next agent:
-  remains very large; this commit intentionally scoped to request plumbing only.
- Next high-value split is extracting cohesive domains (for example checkout/worktree flows) behind focused modules while keeping wire contracts unchanged.
This commit is contained in:
Mohamed Boudra
2026-02-14 10:06:51 +07:00
parent d9e14186ce
commit 21f191583f
2 changed files with 223 additions and 210 deletions
@@ -347,6 +347,68 @@ describe("DaemonClient", () => {
}); });
}); });
test("requests checkout merge from base via RPC", async () => {
const logger = createMockLogger();
const mock = createMockTransport();
const client = new DaemonClient({
url: "ws://test",
logger,
reconnect: { enabled: false },
transportFactory: () => mock.transport,
});
clients.push(client);
const connectPromise = client.connect();
mock.triggerOpen();
await connectPromise;
const promise = client.checkoutMergeFromBase(
"/tmp/project",
{ baseRef: "main", requireCleanTarget: true },
"req-merge-from-base"
);
expect(mock.sent).toHaveLength(1);
const request = JSON.parse(mock.sent[0]) as {
type: "session";
message: {
type: "checkout_merge_from_base_request";
cwd: string;
baseRef?: string;
requireCleanTarget?: boolean;
requestId: string;
};
};
expect(request.message.type).toBe("checkout_merge_from_base_request");
expect(request.message.cwd).toBe("/tmp/project");
expect(request.message.baseRef).toBe("main");
expect(request.message.requireCleanTarget).toBe(true);
expect(request.message.requestId).toBe("req-merge-from-base");
mock.triggerMessage(
JSON.stringify({
type: "session",
message: {
type: "checkout_merge_from_base_response",
payload: {
cwd: "/tmp/project",
requestId: "req-merge-from-base",
success: true,
error: null,
},
},
})
);
await expect(promise).resolves.toEqual({
cwd: "/tmp/project",
requestId: "req-merge-from-base",
success: true,
error: null,
});
});
test("resubscribes checkout diff streams after reconnect", async () => { test("resubscribes checkout diff streams after reconnect", async () => {
const logger = createMockLogger(); const logger = createMockLogger();
const mock = createMockTransport(); const mock = createMockTransport();
+161 -210
View File
@@ -871,6 +871,33 @@ export class DaemonClient {
}); });
} }
private sendCorrelatedSessionRequest<
TResponseType extends CorrelatedResponseType,
TResult = CorrelatedResponsePayload<TResponseType>,
>(
params: {
requestId?: string;
message: { type: SessionInboundMessage["type"] } & Record<string, unknown>;
responseType: TResponseType;
timeout: number;
selectPayload?: (payload: CorrelatedResponsePayload<TResponseType>) => TResult | null;
}
): Promise<TResult> {
const resolvedRequestId = this.createRequestId(params.requestId);
const message = SessionInboundMessageSchema.parse({
...params.message,
requestId: resolvedRequestId,
});
return this.sendCorrelatedRequest({
requestId: resolvedRequestId,
message,
responseType: params.responseType,
timeout: params.timeout,
options: { skipQueue: true },
...(params.selectPayload ? { selectPayload: params.selectPayload } : {}),
});
}
private sendSessionMessageStrict(message: SessionInboundMessage): void { private sendSessionMessageStrict(message: SessionInboundMessage): void {
if (!this.transport || this.connectionState.status !== "connected") { if (!this.transport || this.connectionState.status !== "connected") {
throw new Error("Transport not connected"); throw new Error("Transport not connected");
@@ -1898,20 +1925,16 @@ export class DaemonClient {
input: { message?: string; addAll?: boolean }, input: { message?: string; addAll?: boolean },
requestId?: string requestId?: string
): Promise<CheckoutCommitPayload> { ): Promise<CheckoutCommitPayload> {
const resolvedRequestId = this.createRequestId(requestId); return this.sendCorrelatedSessionRequest({
const message = SessionInboundMessageSchema.parse({ requestId,
type: "checkout_commit_request", message: {
cwd, type: "checkout_commit_request",
message: input.message, cwd,
addAll: input.addAll, message: input.message,
requestId: resolvedRequestId, addAll: input.addAll,
}); },
return this.sendCorrelatedRequest({
requestId: resolvedRequestId,
message,
responseType: "checkout_commit_response", responseType: "checkout_commit_response",
timeout: 60000, timeout: 60000,
options: { skipQueue: true },
}); });
} }
@@ -1920,21 +1943,17 @@ export class DaemonClient {
input: { baseRef?: string; strategy?: "merge" | "squash"; requireCleanTarget?: boolean }, input: { baseRef?: string; strategy?: "merge" | "squash"; requireCleanTarget?: boolean },
requestId?: string requestId?: string
): Promise<CheckoutMergePayload> { ): Promise<CheckoutMergePayload> {
const resolvedRequestId = this.createRequestId(requestId); return this.sendCorrelatedSessionRequest({
const message = SessionInboundMessageSchema.parse({ requestId,
type: "checkout_merge_request", message: {
cwd, type: "checkout_merge_request",
baseRef: input.baseRef, cwd,
strategy: input.strategy, baseRef: input.baseRef,
requireCleanTarget: input.requireCleanTarget, strategy: input.strategy,
requestId: resolvedRequestId, requireCleanTarget: input.requireCleanTarget,
}); },
return this.sendCorrelatedRequest({
requestId: resolvedRequestId,
message,
responseType: "checkout_merge_response", responseType: "checkout_merge_response",
timeout: 60000, timeout: 60000,
options: { skipQueue: true },
}); });
} }
@@ -1943,36 +1962,28 @@ export class DaemonClient {
input: { baseRef?: string; requireCleanTarget?: boolean }, input: { baseRef?: string; requireCleanTarget?: boolean },
requestId?: string requestId?: string
): Promise<CheckoutMergeFromBasePayload> { ): Promise<CheckoutMergeFromBasePayload> {
const resolvedRequestId = this.createRequestId(requestId); return this.sendCorrelatedSessionRequest({
const message = SessionInboundMessageSchema.parse({ requestId,
type: "checkout_merge_from_base_request", message: {
cwd, type: "checkout_merge_from_base_request",
baseRef: input.baseRef, cwd,
requireCleanTarget: input.requireCleanTarget, baseRef: input.baseRef,
requestId: resolvedRequestId, requireCleanTarget: input.requireCleanTarget,
}); },
return this.sendCorrelatedRequest({
requestId: resolvedRequestId,
message,
responseType: "checkout_merge_from_base_response", responseType: "checkout_merge_from_base_response",
timeout: 60000, timeout: 60000,
options: { skipQueue: true },
}); });
} }
async checkoutPush(cwd: string, requestId?: string): Promise<CheckoutPushPayload> { async checkoutPush(cwd: string, requestId?: string): Promise<CheckoutPushPayload> {
const resolvedRequestId = this.createRequestId(requestId); return this.sendCorrelatedSessionRequest({
const message = SessionInboundMessageSchema.parse({ requestId,
type: "checkout_push_request", message: {
cwd, type: "checkout_push_request",
requestId: resolvedRequestId, cwd,
}); },
return this.sendCorrelatedRequest({
requestId: resolvedRequestId,
message,
responseType: "checkout_push_response", responseType: "checkout_push_response",
timeout: 60000, timeout: 60000,
options: { skipQueue: true },
}); });
} }
@@ -1981,21 +1992,17 @@ export class DaemonClient {
input: { title?: string; body?: string; baseRef?: string }, input: { title?: string; body?: string; baseRef?: string },
requestId?: string requestId?: string
): Promise<CheckoutPrCreatePayload> { ): Promise<CheckoutPrCreatePayload> {
const resolvedRequestId = this.createRequestId(requestId); return this.sendCorrelatedSessionRequest({
const message = SessionInboundMessageSchema.parse({ requestId,
type: "checkout_pr_create_request", message: {
cwd, type: "checkout_pr_create_request",
title: input.title, cwd,
body: input.body, title: input.title,
baseRef: input.baseRef, body: input.body,
requestId: resolvedRequestId, baseRef: input.baseRef,
}); },
return this.sendCorrelatedRequest({
requestId: resolvedRequestId,
message,
responseType: "checkout_pr_create_response", responseType: "checkout_pr_create_response",
timeout: 60000, timeout: 60000,
options: { skipQueue: true },
}); });
} }
@@ -2003,18 +2010,14 @@ export class DaemonClient {
cwd: string, cwd: string,
requestId?: string requestId?: string
): Promise<CheckoutPrStatusPayload> { ): Promise<CheckoutPrStatusPayload> {
const resolvedRequestId = this.createRequestId(requestId); return this.sendCorrelatedSessionRequest({
const message = SessionInboundMessageSchema.parse({ requestId,
type: "checkout_pr_status_request", message: {
cwd, type: "checkout_pr_status_request",
requestId: resolvedRequestId, cwd,
}); },
return this.sendCorrelatedRequest({
requestId: resolvedRequestId,
message,
responseType: "checkout_pr_status_response", responseType: "checkout_pr_status_response",
timeout: 60000, timeout: 60000,
options: { skipQueue: true },
}); });
} }
@@ -2022,19 +2025,15 @@ export class DaemonClient {
input: { cwd?: string; repoRoot?: string }, input: { cwd?: string; repoRoot?: string },
requestId?: string requestId?: string
): Promise<PaseoWorktreeListPayload> { ): Promise<PaseoWorktreeListPayload> {
const resolvedRequestId = this.createRequestId(requestId); return this.sendCorrelatedSessionRequest({
const message = SessionInboundMessageSchema.parse({ requestId,
type: "paseo_worktree_list_request", message: {
cwd: input.cwd, type: "paseo_worktree_list_request",
repoRoot: input.repoRoot, cwd: input.cwd,
requestId: resolvedRequestId, repoRoot: input.repoRoot,
}); },
return this.sendCorrelatedRequest({
requestId: resolvedRequestId,
message,
responseType: "paseo_worktree_list_response", responseType: "paseo_worktree_list_response",
timeout: 60000, timeout: 60000,
options: { skipQueue: true },
}); });
} }
@@ -2042,20 +2041,16 @@ export class DaemonClient {
input: { worktreePath?: string; repoRoot?: string; branchName?: string }, input: { worktreePath?: string; repoRoot?: string; branchName?: string },
requestId?: string requestId?: string
): Promise<PaseoWorktreeArchivePayload> { ): Promise<PaseoWorktreeArchivePayload> {
const resolvedRequestId = this.createRequestId(requestId); return this.sendCorrelatedSessionRequest({
const message = SessionInboundMessageSchema.parse({ requestId,
type: "paseo_worktree_archive_request", message: {
worktreePath: input.worktreePath, type: "paseo_worktree_archive_request",
repoRoot: input.repoRoot, worktreePath: input.worktreePath,
branchName: input.branchName, repoRoot: input.repoRoot,
requestId: resolvedRequestId, branchName: input.branchName,
}); },
return this.sendCorrelatedRequest({
requestId: resolvedRequestId,
message,
responseType: "paseo_worktree_archive_response", responseType: "paseo_worktree_archive_response",
timeout: 20000, timeout: 20000,
options: { skipQueue: true },
}); });
} }
@@ -2063,19 +2058,15 @@ export class DaemonClient {
options: { cwd: string; branchName: string }, options: { cwd: string; branchName: string },
requestId?: string requestId?: string
): Promise<ValidateBranchPayload> { ): Promise<ValidateBranchPayload> {
const resolvedRequestId = this.createRequestId(requestId); return this.sendCorrelatedSessionRequest({
const message = SessionInboundMessageSchema.parse({ requestId,
type: "validate_branch_request", message: {
cwd: options.cwd, type: "validate_branch_request",
branchName: options.branchName, cwd: options.cwd,
requestId: resolvedRequestId, branchName: options.branchName,
}); },
return this.sendCorrelatedRequest({
requestId: resolvedRequestId,
message,
responseType: "validate_branch_response", responseType: "validate_branch_response",
timeout: 10000, timeout: 10000,
options: { skipQueue: true },
}); });
} }
@@ -2083,20 +2074,16 @@ export class DaemonClient {
options: { cwd: string; query?: string; limit?: number }, options: { cwd: string; query?: string; limit?: number },
requestId?: string requestId?: string
): Promise<BranchSuggestionsPayload> { ): Promise<BranchSuggestionsPayload> {
const resolvedRequestId = this.createRequestId(requestId); return this.sendCorrelatedSessionRequest({
const message = SessionInboundMessageSchema.parse({ requestId,
type: "branch_suggestions_request", message: {
cwd: options.cwd, type: "branch_suggestions_request",
query: options.query, cwd: options.cwd,
limit: options.limit, query: options.query,
requestId: resolvedRequestId, limit: options.limit,
}); },
return this.sendCorrelatedRequest({
requestId: resolvedRequestId,
message,
responseType: "branch_suggestions_response", responseType: "branch_suggestions_response",
timeout: 10000, timeout: 10000,
options: { skipQueue: true },
}); });
} }
@@ -2110,20 +2097,16 @@ export class DaemonClient {
mode: "list" | "file" = "list", mode: "list" | "file" = "list",
requestId?: string requestId?: string
): Promise<FileExplorerPayload> { ): Promise<FileExplorerPayload> {
const resolvedRequestId = this.createRequestId(requestId); return this.sendCorrelatedSessionRequest({
const message = SessionInboundMessageSchema.parse({ requestId,
type: "file_explorer_request", message: {
agentId, type: "file_explorer_request",
path, agentId,
mode, path,
requestId: resolvedRequestId, mode,
}); },
return this.sendCorrelatedRequest({
requestId: resolvedRequestId,
message,
responseType: "file_explorer_response", responseType: "file_explorer_response",
timeout: 10000, timeout: 10000,
options: { skipQueue: true },
}); });
} }
@@ -2132,19 +2115,15 @@ export class DaemonClient {
path: string, path: string,
requestId?: string requestId?: string
): Promise<FileDownloadTokenPayload> { ): Promise<FileDownloadTokenPayload> {
const resolvedRequestId = this.createRequestId(requestId); return this.sendCorrelatedSessionRequest({
const message = SessionInboundMessageSchema.parse({ requestId,
type: "file_download_token_request", message: {
agentId, type: "file_download_token_request",
path, agentId,
requestId: resolvedRequestId, path,
}); },
return this.sendCorrelatedRequest({
requestId: resolvedRequestId,
message,
responseType: "file_download_token_response", responseType: "file_download_token_response",
timeout: 10000, timeout: 10000,
options: { skipQueue: true },
}); });
} }
@@ -2152,18 +2131,14 @@ export class DaemonClient {
cwd: string, cwd: string,
requestId?: string requestId?: string
): Promise<ProjectIconResponse["payload"]> { ): Promise<ProjectIconResponse["payload"]> {
const resolvedRequestId = this.createRequestId(requestId); return this.sendCorrelatedSessionRequest({
const message = SessionInboundMessageSchema.parse({ requestId,
type: "project_icon_request", message: {
cwd, type: "project_icon_request",
requestId: resolvedRequestId, cwd,
}); },
return this.sendCorrelatedRequest({
requestId: resolvedRequestId,
message,
responseType: "project_icon_response", responseType: "project_icon_response",
timeout: 10000, timeout: 10000,
options: { skipQueue: true },
}); });
} }
@@ -2175,69 +2150,53 @@ export class DaemonClient {
provider: AgentProvider, provider: AgentProvider,
options?: { cwd?: string; requestId?: string } options?: { cwd?: string; requestId?: string }
): Promise<ListProviderModelsPayload> { ): Promise<ListProviderModelsPayload> {
const resolvedRequestId = this.createRequestId(options?.requestId); return this.sendCorrelatedSessionRequest({
const message = SessionInboundMessageSchema.parse({ requestId: options?.requestId,
type: "list_provider_models_request", message: {
provider, type: "list_provider_models_request",
cwd: options?.cwd, provider,
requestId: resolvedRequestId, cwd: options?.cwd,
}); },
return this.sendCorrelatedRequest({
requestId: resolvedRequestId,
message,
responseType: "list_provider_models_response", responseType: "list_provider_models_response",
timeout: 30000, timeout: 30000,
options: { skipQueue: true },
}); });
} }
async listAvailableProviders(options?: { async listAvailableProviders(options?: {
requestId?: string; requestId?: string;
}): Promise<ListAvailableProvidersPayload> { }): Promise<ListAvailableProvidersPayload> {
const resolvedRequestId = this.createRequestId(options?.requestId); return this.sendCorrelatedSessionRequest({
const message = SessionInboundMessageSchema.parse({ requestId: options?.requestId,
type: "list_available_providers_request", message: {
requestId: resolvedRequestId, type: "list_available_providers_request",
}); },
return this.sendCorrelatedRequest({
requestId: resolvedRequestId,
message,
responseType: "list_available_providers_response", responseType: "list_available_providers_response",
timeout: 30000, timeout: 30000,
options: { skipQueue: true },
}); });
} }
async listSpeechModels(requestId?: string): Promise<SpeechModelsListPayload> { async listSpeechModels(requestId?: string): Promise<SpeechModelsListPayload> {
const resolvedRequestId = this.createRequestId(requestId); return this.sendCorrelatedSessionRequest({
const message = SessionInboundMessageSchema.parse({ requestId,
type: "speech_models_list_request", message: {
requestId: resolvedRequestId, type: "speech_models_list_request",
}); },
return this.sendCorrelatedRequest({
requestId: resolvedRequestId,
message,
responseType: "speech_models_list_response", responseType: "speech_models_list_response",
timeout: 30000, timeout: 30000,
options: { skipQueue: true },
}); });
} }
async downloadSpeechModels( async downloadSpeechModels(
options?: { modelIds?: string[]; requestId?: string } options?: { modelIds?: string[]; requestId?: string }
): Promise<SpeechModelsDownloadPayload> { ): Promise<SpeechModelsDownloadPayload> {
const resolvedRequestId = this.createRequestId(options?.requestId); return this.sendCorrelatedSessionRequest({
const message = SessionInboundMessageSchema.parse({ requestId: options?.requestId,
type: "speech_models_download_request", message: {
modelIds: options?.modelIds, type: "speech_models_download_request",
requestId: resolvedRequestId, modelIds: options?.modelIds,
}); },
return this.sendCorrelatedRequest({
requestId: resolvedRequestId,
message,
responseType: "speech_models_download_response", responseType: "speech_models_download_response",
timeout: 30 * 60 * 1000, timeout: 30 * 60 * 1000,
options: { skipQueue: true },
}); });
} }
@@ -2245,18 +2204,14 @@ export class DaemonClient {
agentId: string, agentId: string,
requestId?: string requestId?: string
): Promise<ListCommandsPayload> { ): Promise<ListCommandsPayload> {
const resolvedRequestId = this.createRequestId(requestId); return this.sendCorrelatedSessionRequest({
const message = SessionInboundMessageSchema.parse({ requestId,
type: "list_commands_request", message: {
agentId, type: "list_commands_request",
requestId: resolvedRequestId, agentId,
}); },
return this.sendCorrelatedRequest({
requestId: resolvedRequestId,
message,
responseType: "list_commands_response", responseType: "list_commands_response",
timeout: 30000, timeout: 30000,
options: { skipQueue: true },
}); });
} }
@@ -2266,20 +2221,16 @@ export class DaemonClient {
args?: string, args?: string,
requestId?: string requestId?: string
): Promise<ExecuteCommandPayload> { ): Promise<ExecuteCommandPayload> {
const resolvedRequestId = this.createRequestId(requestId); return this.sendCorrelatedSessionRequest({
const message = SessionInboundMessageSchema.parse({ requestId,
type: "execute_command_request", message: {
agentId, type: "execute_command_request",
commandName, agentId,
args, commandName,
requestId: resolvedRequestId, args,
}); },
return this.sendCorrelatedRequest({
requestId: resolvedRequestId,
message,
responseType: "execute_command_response", responseType: "execute_command_response",
timeout: 30000, timeout: 30000,
options: { skipQueue: true },
}); });
} }