fix(cli): stop async failure errors falling back to the whole poll response

Merged
TR
1d ago
+168-14

Why

When an async command fails in machine mode, the error envelope's error field holds the failure detail, read from the poll response's declared error member (error by default, or the configured errorField). If the poll response did not contain that member, MachineErrorFields fell back to the whole poll response. A job that reached a failure state with no error detail in its body then put the entire poll payload, which can include large result data, into error, so agents parsing the field got an unrelated object instead of the error text.

The same fallback is the only diagnostic for two other async errors: CLI_ASYNC_UNKNOWN_STATE (the poll response reported a state the CLI does not recognise) and CLI_PROTOCOL when the poll response has no state at all. For those, the poll response is what tells the user what the server actually sent, so it must stay.

What changed

  • asyncRuntimeError.MachineErrorFields still uses the declared member as error when the poll response object has it.
  • For CLI_ASYNC_FAILED, when that member is missing, the poll response no longer replaces error. The field keeps the error text (the same as message), as it does for every machine-mode error without more specific detail. The envelope still carries the reason, message, id and resume.
  • For every other async error reason (CLI_ASYNC_UNKNOWN_STATE, missing-state CLI_PROTOCOL), and for non-object payloads, the poll response is still passed through as error.

Testing

  • TestAsyncMachineErrorOnlyIncludesDeclaredErrorMember covers the default member, a declared member, a missing member, a non-object payload, and the unknown-state and protocol reasons keeping the payload.
  • Runtime test TestAsyncMachineErrorDetail runs an async command end to end in agent mode against a local server and checks the stderr envelope:
    • a failure state with no error member: the poll response is absent and error equals message;
    • an unrecognised state: the poll response is kept in error;
    • a poll response with no state: CLI_PROTOCOL, with the poll response kept in error.
  • Each case fails against the code it guards (the failure case against main, the unknown-state case against the first version of this PR, the missing-state case when the protocol error drops its payload, and the error text check when the field is cleared).
  • TARGET=review make test-cli, TARGET=primary make test-cli, make check-template-cli, npm run format, go test ./pkg/generate/snapshots/ -run TestSnapCLI and git diff --check pass.
  • make lint reports only an existing gofmt issue in templates/perms.go, which this PR does not touch.

Public-safety check

  • This change contains no credentials, customer documents, private repository URLs, private filesystem paths, or unredacted private logs.
  • Title, body, comments, and commit messages name no customers or customer-derived identifiers, private paths or trackers, or workflow provenance, and are understandable without private context (.claude/skills/public-repo-communication/SKILL.md).
  • Generated fixtures and review SDK changes are public-safe.
  • I reviewed git diff --check.