MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / drainCommandQueue

Function drainCommandQueue

source code/cli/print.ts:1936–2368  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1934 // ask() call so messages that queued up during a long turn coalesce
1935 // into a single follow-up turn instead of N separate turns.
1936 const drainCommandQueue = async () => {
1937 while ((command = dequeue(isMainThread))) {
1938 if (
1939 command.mode !== 'prompt' &&
1940 command.mode !== 'orphaned-permission' &&
1941 command.mode !== 'task-notification'
1942 ) {
1943 throw new Error(
1944 'only prompt commands are supported in streaming mode',
1945 )
1946 }
1947
1948 // Non-prompt commands (task-notification, orphaned-permission) carry
1949 // side effects or orphanedPermission state, so they process singly.
1950 // Prompt commands greedily collect followers with matching workload.
1951 const batch: QueuedCommand[] = [command]
1952 if (command.mode === 'prompt') {
1953 while (canBatchWith(command, peek(isMainThread))) {
1954 batch.push(dequeue(isMainThread)!)
1955 }
1956 if (batch.length > 1) {
1957 command = {
1958 ...command,
1959 value: joinPromptValues(batch.map(c => c.value)),
1960 uuid: batch.findLast(c => c.uuid)?.uuid ?? command.uuid,
1961 }
1962 }
1963 }
1964 const batchUuids = batch.map(c => c.uuid).filter(u => u !== undefined)
1965
1966 // QueryEngine will emit a replay for command.uuid (the last uuid in
1967 // the batch) via its messagesToAck path. Emit replays here for the
1968 // rest so consumers that track per-uuid delivery (clank's
1969 // asyncMessages footer, CCR) see an ack for every message they sent,
1970 // not just the one that survived the merge.
1971 if (options.replayUserMessages && batch.length > 1) {
1972 for (const c of batch) {
1973 if (c.uuid && c.uuid !== command.uuid) {
1974 output.enqueue({
1975 type: 'user',
1976 message: { role: 'user', content: c.value },
1977 session_id: getSessionId(),
1978 parent_tool_use_id: null,
1979 uuid: c.uuid,
1980 isReplay: true,
1981 } satisfies SDKUserMessageReplay)
1982 }
1983 }
1984 }
1985
1986 // Combine all MCP clients. appState.mcp is populated incrementally
1987 // per-server by main.tsx (mirrors useManageMCPConnections). Reading
1988 // fresh per-command means late-connecting servers are visible on the
1989 // next turn. registerElicitationHandlers is idempotent (tracking set).
1990 const appState = getAppState()
1991 const allMcpClients = [
1992 ...appState.mcp.clients,
1993 ...sdkClients,

Callers 1

runFunction · 0.85

Calls 15

dequeueFunction · 0.85
canBatchWithFunction · 0.85
joinPromptValuesFunction · 0.85
getSessionIdFunction · 0.85
buildAllToolsFunction · 0.85
notifyCommandLifecycleFunction · 0.85
isValidStatusFunction · 0.85
logEventFunction · 0.85
logSuggestionOutcomeFunction · 0.85
createAbortControllerFunction · 0.85

Tested by

no test coverage detected