diff --git a/server.js b/server.js index 51a66d0..51079ab 100644 --- a/server.js +++ b/server.js @@ -1180,23 +1180,13 @@ async function executePromptStep(executionId, step, stepNumber, deadline = Infin // Include the last command output so the user can review results alongside the question const lastOutput = (execState?.state?._lastCommandOutput) || null; - const logEntry = { - step: stepNumber, step_name: step.name, action: 'prompt', - message, options, timestamp: new Date().toISOString() - }; - if (lastOutput) logEntry.output = lastOutput; - await addExecutionLog(executionId, logEntry); - - broadcast({ - type: 'execution_prompt', - execution_id: executionId, - prompt: { message, options, step: stepNumber, step_name: step.name, output: lastOutput || undefined } - }); - - return new Promise(resolve => { + // Register the pending answer before announcing the prompt: an answer that arrived + // between the log entry and the registration used to get "No pending prompt" (404). + let timer; + const answered = new Promise(resolve => { // Never wait past the execution's own deadline (#21). const ms = Math.max(0, Math.min(PROMPT_TIMEOUT_EFFECTIVE_MS, deadline - Date.now())); - const timer = setTimeout(() => { + timer = setTimeout(() => { _executionPrompts.delete(executionId); console.warn(`[Workflow] Prompt timed out for execution ${executionId}`); resolve(PROMPT_TIMED_OUT); @@ -1211,6 +1201,21 @@ async function executePromptStep(executionId, step, stepNumber, deadline = Infin options }); }); + + const logEntry = { + step: stepNumber, step_name: step.name, action: 'prompt', + message, options, timestamp: new Date().toISOString() + }; + if (lastOutput) logEntry.output = lastOutput; + await addExecutionLog(executionId, logEntry); + + broadcast({ + type: 'execution_prompt', + execution_id: executionId, + prompt: { message, options, step: stepNumber, step_name: step.name, output: lastOutput || undefined } + }); + + return answered; } async function executeCommandStep(executionId, step, stepNumber, params = {}) {