Skip to content

🩺 fix: Report Why a Worker Command Was Unavailable - #409

Merged
danny-avila merged 3 commits into
mainfrom
danny-avila/command-failure-reason
Dec 4, 2026
Merged

danny-avila merged 3 commits into
mainfrom
danny-avila/command-failure-reason

Conversation

@danny-avila

@danny-avila danny-avila commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

When a paired worker cannot run a command, the caller sees only that it failed, never why. LibreChat shows Workspace tool request rejected (upstreamStatus: 503) (upstreamCode: COMMAND_UNAVAILABLE), and the Code API stores just:

{"status":"rejected","errorCode":"COMMAND_UNAVAILABLE","error":"Native executor request failed"}

How it happens. The worker's executor child already classifies a failure into a sanitized diagnostic (stage, reason, allowlisted errno). When the error is not a WorkspaceToolError, the child sends no message, so the parent falls back to Native executor request failed. The worker logs the diagnostic only to its own stderr (workspace_command_unavailable, added in #349) and settles without it. The cause exists only in the worker machine's journal, which the operator of the Code API or LibreChat usually cannot reach. The failure is rare: about 2 in 11,000 commands on one worker over a week. That makes it hard to reproduce, which is why the settlement needs to carry the cause.

The fix. Rejected COMMAND_UNAVAILABLE settlements now carry an optional commandFailure built from that same diagnostic:

{
  "status": "rejected",
  "errorCode": "COMMAND_UNAVAILABLE",
  "error": "Native executor request failed",
  "commandFailure": { "stage": "dispatch", "reason": "executor_rejected", "osCode": "EAGAIN", "errorName": "Error" }
}
  • Protocol (packages/code/src/protocol.ts): BridgeCommandFailure and readBridgeCommandFailure, the single strict reader. stage and reason must be known values, or the whole value is dropped. Each optional field is dropped on its own when it fails its check: osCode must be an allowlisted errno (the existing list plus EAGAIN, EBUSY, E2BIG, ENOEXEC, ENOBUFS, ESRCH, ECHILD, EBADF, EROFS and ECONNREFUSED), nodeCode must match ^ERR_[A-Z0-9_]{1,60}$, and errorName must be a built-in error class. The reader reads only own data properties, never runs getters, and never throws. It cannot carry a path, command text, environment value, token or error message.
  • Worker: command-diagnostics.ts now uses the protocol's stages, reasons and reader instead of its own copies, and also records the error's Node.js code and class name. The class name comes from the nearest name data property; it never calls an accessor. The worker attaches commandFailure under exactly the condition that already emits the local workspace_command_unavailable record (COMMAND_UNAVAILABLE on execute_command or workspace-programmatic). It does so inside the same guarded block, so a diagnostic can never change the settlement's status, code or message.
  • Code API:
    • RedisBridgeStore.settle stores the validated value. An invalid value is dropped rather than refused, the same way laneGit is handled, and a valid value is stored byte-identical so retries still match.
    • Workspace tool errors return it beside code: as { error, code, commandFailure } for /workspace-tools/execute, and as error.commandFailure on the durable request status.
    • It is logged as commandFailure on Workspace tool request completed, and as codeapi.command_failure.{stage,reason,os_code,node_code,error_name} on the failed Durable workspace admission transition log line and span. Building those attributes moved into admissionTransitionAttributes so they can be tested.
  • The OpenAPI schema and the worker runbook describe the field.
  • The packed-worker smoke (scripts/smoke-code-package.mjs) now expects the executor's unprepared-close reply to name its error class, and checks that the reply still carries no message.

Compatibility

  • Old worker, new Code API: no commandFailure, which is the normal case. The settlement, status code, message and log line are unchanged apart from the missing attributes.
  • New worker, old Code API: older replicas have never rejected unknown settlement keys. They store the field as sent and ignore it.
  • Invalid or unknown content (for example a stage from a future worker): dropped field by field, never a 400. The rejection settles exactly as before.
  • Mixed replicas and retries: if a replica that predates this change stored a cause that a newer replica would drop, a retry to the newer replica is still recognized as the same rejection, not fenced as ASSIGNMENT_FENCED.
  • Quarantine, mutation-uncertainty and retry decisions are unchanged; this change only adds observability.

LibreChat does not display the new field yet. WorkspaceToolHttpError formats only upstreamStatus and upstreamCode, and the durable path rebuilds the error body from the code alone. Showing the cause needs a small LibreChat change, tracked separately. Until then, the Code API's response and logs carry it.

Change Type

  • Bug fix (non-breaking change which fixes an issue)
  • Documentation update

Testing

  • packages/code: full node --test dist/*.test.js on Node 24: 1016 passed, 24 failed, 22 skipped. The same 24 tests fail on unchanged main in this environment (Bash 5.2/jq programmatic prerequisites, worktree fixtures). Linux CI is the full-suite gate. tsc --noEmit passes.
  • New and updated tests:
    • Protocol: accepts every field, drops each invalid optional field on its own (unknown errno, bare or oversized or path-like ERR_ codes, embedded newline, custom class names), drops the whole value for an unknown stage or reason or a non-object, and never invokes getters or lets a throwing proxy escape.
    • Diagnostics: a spawn-style EAGAIN error with a path, syscall and arguments keeps only osCode and errorName; a Node ERR_IPC_CHANNEL_CLOSED TypeError keeps nodeCode and errorName; a name getter is never called.
    • Executor child: a real forked child replies to a non-WorkspaceToolError failure with the sanitized diagnostic and no message.
    • Worker: a real native executor whose workspace root is missing settles Native executor request failed with commandFailure: { stage: 'sandbox_startup', reason: 'startup_failed', osCode: 'ENOENT', errorName: 'Error' }. The settlement includes neither the root path nor the command. A failure that carries no diagnostic settles without the field.
  • service, against Redis 7.2:
    • requests.test.ts and fleet.test.ts cover the stored settlement, the durable error.commandFailure and the transition attributes, in four cases: valid, older worker, unknown stage, and non-object. They also check a partially valid cause and a retried rejection across a replica that predates the change, plus a genuinely different retry, which is still fenced. 82 passed.
    • workspace-tools/router.test.ts covers the response body and the outcome log for a valid, an invalid and an absent cause. 68 passed.
    • Full bun run test: 1306 passed, 17 skipped, 5 failed. The same 5 tests fail on unchanged main here (native mutation handoff, and jq number canonicalization).
    • tsc --noEmit reports the same diagnostics as main, all in untouched files. ESLint on the touched files adds one indent finding: one line added inside an import block whose existing lines use the same indentation.

Checklist

  • My code adheres to this project's style guidelines
  • I have performed a self-review of my own code
  • I have commented in any complex areas of my code
  • I have made pertinent documentation changes
  • I have written tests demonstrating that my changes are effective
  • Local unit tests pass with my changes

A paired worker that could not run a command settled only
`COMMAND_UNAVAILABLE` and, for an executor failure that was not a
WorkspaceToolError, the generic "Native executor request failed". The
sanitized diagnostic that explains the failure stayed in the worker's
local journal.

Rejected COMMAND_UNAVAILABLE settlements now carry an optional
`commandFailure` (stage, reason and, when known, an allowlisted errno
`osCode`, a Node.js `nodeCode` and a built-in `errorName`). The protocol
owns one strict reader that the worker's diagnostic extraction, the
executor IPC and the Code API all share. The Code API stores the
validated value, returns it beside `code` in workspace tool errors, and
logs it on the request outcome and the durable admission transition.

A missing field is the normal case for older workers, older Code API
replicas ignore it, and an invalid or unknown value is dropped rather
than refused. A retried rejection is still recognized when replicas
handled its cause differently. Quarantine and mutation decisions are
unchanged.
@danny-avila

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-21T13:42:10.018413Z 05b9eb2 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: bf926ba2d3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

The packed-package smoke forks the installed executor and checks its unprepared-close reply. That reply now names the built-in error class, and still carries no message.
@danny-avila

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 05b9eb2ce2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danny-avila

Copy link
Copy Markdown
Collaborator Author

@codex review

Final review pass for head 05b9eb2. Please check version skew in both directions (old worker or new Code API, new worker or old Code API, mixed replicas on settlement retries), and confirm that no settlement, quarantine or mutation decision changes.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 05b9eb2ce2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danny-avila
danny-avila merged commit 65cec60 into main Dec 4, 2026
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant