MCPcopy
hub / github.com/codeaashu/claude-code / startSpeculation

Function startSpeculation

src/services/PromptSuggestion/speculation.ts:402–715  ·  view source on GitHub ↗
(
  suggestionText: string,
  context: REPLHookContext,
  setAppState: (f: (prev: AppState) => AppState) => void,
  isPipelined = false,
  cacheSafeParams?: CacheSafeParams,
)

Source from the content-addressed store, hash-verified

400}
401
402export async function startSpeculation(
403 suggestionText: string,
404 context: REPLHookContext,
405 setAppState: (f: (prev: AppState) => AppState) => void,
406 isPipelined = false,
407 cacheSafeParams?: CacheSafeParams,
408): Promise<void> {
409 if (!isSpeculationEnabled()) return
410
411 // Abort any existing speculation before starting a new one
412 abortSpeculation(setAppState)
413
414 const id = randomUUID().slice(0, 8)
415
416 const abortController = createChildAbortController(
417 context.toolUseContext.abortController,
418 )
419
420 if (abortController.signal.aborted) return
421
422 const startTime = Date.now()
423 const messagesRef = { current: [] as Message[] }
424 const writtenPathsRef = { current: new Set<string>() }
425 const overlayPath = getOverlayPath(id)
426 const cwd = getCwdState()
427
428 try {
429 await mkdir(overlayPath, { recursive: true })
430 } catch {
431 logForDebugging('[Speculation] Failed to create overlay directory')
432 return
433 }
434
435 const contextRef = { current: context }
436
437 setAppState(prev => ({
438 ...prev,
439 speculation: {
440 status: 'active',
441 id,
442 abort: () => abortController.abort(),
443 startTime,
444 messagesRef,
445 writtenPathsRef,
446 boundary: null,
447 suggestionLength: suggestionText.length,
448 toolUseCount: 0,
449 isPipelined,
450 contextRef,
451 },
452 }))
453
454 logForDebugging(`[Speculation] Starting speculation ${id}`)
455
456 try {
457 const result = await runForkedAgent({
458 promptMessages: [createUserMessage({ content: suggestionText })],
459 cacheSafeParams: cacheSafeParams ?? createCacheSafeParams(context),

Callers 2

handleSpeculationAcceptFunction · 0.85
executePromptSuggestionFunction · 0.85

Calls 15

isSpeculationEnabledFunction · 0.85
abortSpeculationFunction · 0.85
getOverlayPathFunction · 0.85
getCwdStateFunction · 0.85
mkdirFunction · 0.85
logForDebuggingFunction · 0.85
runForkedAgentFunction · 0.85
createUserMessageFunction · 0.85
createCacheSafeParamsFunction · 0.85
denySpeculationFunction · 0.85

Tested by

no test coverage detected