openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
6 lines (5 loc) • 7.16 kB
JavaScript
import{C as isOneShotRunSessionKey}from"./session-key-BPgKfU2m.mjs";import{n as isTimeoutReason}from"./cancellation-policy-BNa0Y54K.mjs";import{t as formatFullOutputFooter}from"./tool-contracts-FTrSdBeO.mjs";
//!src/agents/bash-tools.exec-output.ts
const EXEC_NO_OUTPUT_PLACEHOLDER=`(no output)`;const EXEC_MANUAL_COLLECTION_FOLLOW_UP=`Automatic completion wake is unavailable after ending this turn. If the task needs this result, use poll with a timeout to collect it before ending the turn, unless another continuation is already arranged.`;const MAX_EXEC_YIELD_MS=12e4;const BACKGROUND_EXEC_FOLLOW_UP=`Use process (list/poll/log/write/send-keys/submit/paste/kill/clear/remove) to control it. New output while it runs does not start another turn. Ending the turn does not stop it; use process kill when it is no longer needed. This is not a saved, restartable automation.`;const EXEC_WAIT_RECIPE=`To wait for a condition or a time, run one command that loops until it holds, exits non-zero on failure states and prints the result. To wait on a change you caused, pin the expected state: for example, wait for the exact commit you pushed and exit non-zero if it moved.`;function hasExitWake(e){return e.notifyOnExit===true&&e.notifyOnExitEmptySuccess===true&&e.notifySessionTarget!==void 0&&(e.exitWakeAfterYield===true?e.processActivity?.deadlineAtMs!==void 0:!isOneShotRunSessionKey(e.sessionKey))}function describeExitWake(e){if(!hasExitWake(e))return;return e.exitWakeAfterYield?`This run resumes with its result when it exits, silent success included, if you call sessions_yield. If only waiting remains, call sessions_yield`:`Its exit wakes this conversation with the result, silent success included. If only waiting remains, end this turn`}function buildBackgroundExecFollowUp(e,t,n){const r=describeExitWake(e);return[(t??0)>12e4&&`yieldMs is capped at ${12e4}.`,r&&`${r} now; do not poll it or start another command for the same result. ${`To wait for a condition or a time, run one command that loops until it holds, exits non-zero on failure states and prints the result. To wait on a change you caused, pin the expected state: for example, wait for the exact commit you pushed and exit non-zero if it moved.`}`,n.length>0&&`Also running in this conversation: ${n.join(`; `)}. Keep one command per awaited result.`,BACKGROUND_EXEC_FOLLOW_UP,!r&&`Automatic completion wake is unavailable after ending this turn. If the task needs this result, use poll with a timeout to collect it before ending the turn, unless another continuation is already arranged.`].filter(Boolean).join(` `)}const EXEC_TIMEOUT_RETRY_GUIDANCE=`The command was terminated, but external side effects may already have completed. Verify the resulting state before retrying. Do not automatically rerun commands unless repeating them is safe. Use a higher timeout only when the command is known to be safe to retry.`;const EXEC_RETENTION_CAP_NOTE=`[earlier output was discarded at the retention cap and cannot be recovered]
`;const EXEC_OUTPUT_FILE_MAX_BYTES=67108864;function renderExecOutputFile(e){const t=e.limitReached?`; output after the ${EXEC_OUTPUT_FILE_MAX_BYTES}-byte file limit was discarded`:``;return`${formatFullOutputFooter(e.path)} (${e.bytes} bytes${t})`}function renderExecDroppedOutputNote(e){if(e.totalOutputChars<=e.output.length)return``;return e.outputFile?`[earlier output omitted here; ${renderExecOutputFile(e.outputFile)}]\n\n`:EXEC_RETENTION_CAP_NOTE}function renderExecOutputText(e){return e||`(no output)`}function renderExecExitLabel(e){if(e.exitSignal!=null)return`signal ${e.exitSignal}`;return e.exitCode!=null?`code ${e.exitCode}`:`unknown exit code`}function renderExecUpdateText(e){return(e.warnings.length?`${e.warnings.join(`
`)}\n\n`:``)+renderExecOutputText(e.tailText)}function appendExecTimeoutRetryGuidance(e,t){if(t!==`overall-timeout`&&t!==`no-output-timeout`)return e;return`${e}\n\n${EXEC_TIMEOUT_RETRY_GUIDANCE}`}function normalizeNotifyOutput(e){return e.replace(/\s+/g,` `).trim()}function joinExecFailureOutput(e,t){return e?`${e}\n\n${t}`:t}function buildExecExitOutcome(e){const t=e.exit.exitCode??0;const n=e.exit.reason===`exit`;const r=t===126||t===127;if((n&&!r?`completed`:`failed`)===`completed`){const n=t!==0?`\n\n(Command exited with code ${t})`:``;return{status:`completed`,exitCode:t,exitSignal:e.exit.exitSignal,exitReason:e.exit.reason,durationMs:e.durationMs,aggregated:(n?renderExecOutputText(e.aggregated):e.aggregated)+n,timedOut:false,noOutputTimedOut:e.exit.noOutputTimedOut}}const i=e.exit.reason===`memory-limit`?`memory-limit`:r?t===127?`shell-command-not-found`:`shell-not-executable`:e.exit.reason===`overall-timeout`?`overall-timeout`:e.exit.reason===`no-output-timeout`?`no-output-timeout`:e.exit.exitSignal!=null?`signal`:`aborted`;let a;switch(i){case`memory-limit`:a=`Command killed for exceeding its host memory bound. Reduce concurrency or input size, or split the work.`;break;case`shell-command-not-found`:a=`Command not found`;break;case`shell-not-executable`:a=`Command not executable (permission denied)`;break;case`overall-timeout`:{const t=appendExecTimeoutRetryGuidance(typeof e.timeoutSec===`number`&&e.timeoutSec>0?`Command timed out after ${e.timeoutSec} seconds.`:`Command timed out.`,i);a=e.processContinuationAvailable?`${t}\n\nIf it should keep running, start it with exec background=true or yieldMs so OpenClaw can register a pollable process session. Do not rely on shell backgrounding with a trailing &.`:t;break}case`no-output-timeout`:a=appendExecTimeoutRetryGuidance(`Command timed out waiting for output.`,i);break;case`signal`:a=`Command aborted by signal ${e.exit.exitSignal}`;break;case`aborted`:a=`Command aborted before exit code was captured`;break;default:throw new Error(`Unsupported exec failure kind`)}return{status:`failed`,exitCode:e.exit.exitCode,exitSignal:e.exit.exitSignal,exitReason:e.exit.reason,durationMs:e.durationMs,aggregated:e.aggregated,timedOut:e.exit.timedOut,noOutputTimedOut:e.exit.noOutputTimedOut,failureKind:i,oomScoreWrapperSelected:e.exit.oomScoreWrapperSelected,message:a,reason:joinExecFailureOutput(e.aggregated,a)}}function buildStoppedRunExit(e,t){return{reason:e,exitCode:null,exitSignal:null,durationMs:t,stdout:``,stderr:``,timedOut:isTimeoutReason(e),noOutputTimedOut:e===`no-output-timeout`}}function buildExecRuntimeErrorOutcome(e){const t=String(e.error);return{status:`failed`,exitCode:null,exitSignal:null,durationMs:e.durationMs,aggregated:e.aggregated,timedOut:false,failureKind:`runtime-error`,message:t,reason:joinExecFailureOutput(e.aggregated,t)}}export{renderExecExitLabel as _,EXEC_WAIT_RECIPE as a,renderExecUpdateText as b,buildBackgroundExecFollowUp as c,buildStoppedRunExit as d,describeExitWake as f,renderExecDroppedOutputNote as g,normalizeNotifyOutput as h,EXEC_TIMEOUT_RETRY_GUIDANCE as i,buildExecExitOutcome as l,joinExecFailureOutput as m,EXEC_NO_OUTPUT_PLACEHOLDER as n,MAX_EXEC_YIELD_MS as o,hasExitWake as p,EXEC_OUTPUT_FILE_MAX_BYTES as r,appendExecTimeoutRetryGuidance as s,EXEC_MANUAL_COLLECTION_FOLLOW_UP as t,buildExecRuntimeErrorOutcome as u,renderExecOutputFile as v,renderExecOutputText as y};