Skip to content

Errors ​

Every error chamvi throws extends ChamviError, which extends ebec's BaseError. Each carries a code; the codes are exported as ErrorCode. ChamviError, ErrorCode, ValidationError and the generic HTTP errors (BadRequestError, NotFoundError, ..., InternalError) come from @chamvi/errors, which @chamvi/app re-exports; the engine errors below are in @chamvi/core. httpStatusOf(error) gives the HTTP status of an error (ERROR_CODE_TO_STATUS by code, else the built-in class), and isChamviError() and the is<Name>Error() guards also match an error that went through JSON.

ClassErrorCodeCodeThrown when
InvalidAgentDefinitionErrorINVALID_AGENT_DEFINITIONinvalid_agent_definitionAn agent or channel name, subscription id or cron expression is invalid; duplicate agent or channel names; a subscription to an unknown channel; a tool with needsApproval
AgentNotFoundErrorAGENT_NOT_FOUNDagent_not_founddispatch() for an agent the engine does not hold; @chamvi/http answers 404
AgentReplacedErrorAGENT_REPLACEDagent_replaceddispatch({ checked }) while the engine holds another agent object under the name (it was replaced after the caller's permission check); thrown before anything is stored, so a retry with the same idempotency key is safe; @chamvi/http answers 409
InvalidRunStateErrorINVALID_RUN_STATEinvalid_run_stateA run is not in the status an operation needs: resuming a run that is not waiting, cancelling a settled run, a store guard (expectStatus, expectOwner) that does not match; approve() outside a tool call or in a streaming tool's body; emit() outside a tool call
RunNotFoundErrorRUN_NOT_FOUNDrun_not_foundresume(), cancel(), runService.update() or runService.findOneOrFail() for an unknown run
ArtifactNotFoundErrorARTIFACT_NOT_FOUNDartifact_not_foundAn execution cannot read an artifact its messages refer to (the run fails); @chamvi/http answers 404 with it for an unknown artifact
BudgetExceededErrorBUDGET_EXCEEDEDBudgetExceededA run hit maxSteps or maxTokens while the model still wanted tools
TimeoutErrorTIMEOUTTimeoutAn execution exceeded timeoutMs
FatalToolErrorFATAL_TOOLFatalToolError (or a custom code)A tool decides the run must fail
IncompleteResponseErrorINCOMPLETE_RESPONSEincomplete_responseThe model gave no complete answer and the run fails: its stream ended without a finish part, or (with structured output) the answer was cut off at the output token limit or the loop ended on a tool call nothing executed
ToolNotAllowedErrorTOOL_NOT_ALLOWEDtool_not_allowedA call to a tool the agent's toolPolicy refuses: recorded on the tool step for a model's call to it, and returned to the model for a pending call that runs after an approval; a tool error, the run goes on
ToolDeniedErrorTOOL_DENIEDtool_deniedA gate refused a tool call, e.g. a beforeToolExecution hook after a denied approval; the message is the gate's, a tool error, the run goes on
ChannelNotFoundErrorCHANNEL_NOT_FOUNDchannel_not_foundctx.channels.findOneOrFail() named a channel the engine does not hold, or one without out; an approval delivery through a channel the engine no longer has (to onError)
LeaseLostErrorLEASE_LOSTlease_lostReported to onError when an engine stops executing a run whose claim it lost (carries runId); a write to a connection whose lease expired or was taken over, which writes nothing (carries lock, no runId)
ConnectionHeldErrorCONNECTION_HELDconnection_heldOpening a connection whose lease another owner holds (carries connection); the WhatsApp connector shows held-elsewhere and retries
DecryptionFailedErrorDECRYPTION_FAILEDdecryption_failedSealed bytes do not open: a wrong or tampered value, or a data key wrapped under a key the keyring lacks (carries that keyId); the message never holds key material; http answers 409
SandboxErrorSANDBOXsandbox_errorA workspace or its provider failed: a create, start or checkout (with git's stderr), a missing or oversized file, an unknown, expired or disposed workspace, a Docker or Kubernetes API error, a reached per-run workspace cap; a tool error, the run goes on
ValidationError (@chamvi/errors)VALIDATIONvalidation_errorA state document fails its schema (carries issues); an invalid attachment at dispatch() or artifacts.put(); a final answer that does not parse as the agent's structured output (the run fails); a workspace path, repository or ref the sandbox rules refuse (a tool error); an emit() without an address off the source channel (a tool error)
PeerStartBusyErrorPEER_START_BUSYpeer_start_busyA peer start while another replica counts a start of the same calling run and role (maxPerRun); nothing is written and a resend is safe; @chamvi/http answers 503 with Retry-After: 1
RepositoryNotFoundErrorREPOSITORY_NOT_FOUNDrepository_not_foundA repository OrFail query has no result; single run/artifact lookups use their specific not-found errors

Run errors ​

A failed run stores { code, message } in run.error. For budget, timeout and fatal tool failures the code is the error's code. Any other thrown value gets its own code if it has a string one, else Error.

A run the registry cancelled stores why in run.error too (AgentReplacedError shares the agent_replaced code, but no run stores the error itself):

ErrorCodeCodeStored when
AGENT_REPLACEDagent_replacedA folder change (a new revision) or a replaced agent without a revision cancelled the run while it was unsettled; or the run was dispatched with checked and its agent was replaced since (see Many agents per engine)
AGENT_REMOVEDagent_removedThe agent was removed (its folder deleted) while the run was unsettled

A remote agent's stand-in that fails with its remote run stores the remote run's error; when the peer reports none, it stores remote_failed (ErrorCode.REMOTE_FAILED, no class).

A run cancelled on another replica keeps the reason once its owner settles it; a run that completes anyway ends without one. A user's cancel stores no error.

ts
const run = await engine.getRun(runId);

if (run?.status === RunStatus.FAILED) {
    switch (run.error?.code) {
        case ErrorCode.BUDGET_EXCEEDED:
            return 'Raise maxSteps or maxTokens, or narrow the instructions.';
        case ErrorCode.TIMEOUT:
            return 'Raise timeoutMs or make the tools faster.';
        case ErrorCode.FATAL_TOOL:
            return `A tool stopped the run: ${run.error.message}`;
        default:
            return run.error?.message;
    }
}

Errors without a caller ​

Some errors happen where nobody awaits: a lost lease, a failed approval message, a listener that throws, a failed recovery pass. They go to onError(error, run?), by default console.error. Pass your logger. A LeaseLostError there is informational: another replica carries the run on.

ts
// your engine on the repositories every replica shares (see Database store)
const engine = createEngine({
    agents,
    onError: (error, run) => {
        if (error instanceof LeaseLostError) {
            // another replica carries the run on; nothing to do
            return;
        }

        logger.error({ err: error, runId: run?.id }, 'chamvi');
    },
});

@chamvi/peer adds PeerRequestError (peer_request): a peer request (start, read, cancel, wake) that failed for now, a 5xx, a 429, a retryable 409 or a network error, with the peer's Retry-After of a 429 or 503 as retryAfterMs. The engine retries it until the remote run's deadline and reports a failed read to onError; a permanent refusal is a ValidationError instead (a refused start fails the stand-in at once).

createEngine stands for the engine of your app, built on the repositories every replica shares (Database store); with @chamvi/app the app builds it.