Agents and CI can't tell a revert from an RPC failure, or 'no tests ran' from success
Component
Cast, Forge
Describe the feature you would like
Scripts and agents that drive Foundry cannot tell two failure kinds apart without parsing English messages:
cast callrevert vs. transport failure. Both exit 1, and in--jsonmode both use the samecode:
$ cast call $BANK 'withdraw(uint256)' 1000 --from $ME --rpc-url $RPC --json # exit 1
{"schema_version":1,"success":false,"data":null,"errors":[
{"level":"error","code":"cast.error","message":"execution reverted: InsufficientBalance(500, 1000)"},
{"level":"error","code":"cast.error.context","message":"server returned an error response: error code 3: execution reverted: custom error 0xcf479181: …, data: \"0xcf479181…\""}],"warnings":[]}
$ cast call $BANK 'withdraw(uint256)' 1000 --from $ME --rpc-url http://127.0.0.1:1 --json # exit 1
{"schema_version":1,"success":false,"data":null,"errors":[
{"level":"error","code":"cast.error","message":"failed to retrieve chain ID from fork endpoint"},
{"level":"error","code":"cast.error.context","message":"MPP HTTP request to http://127.0.0.1:1/ failed: … Connection refused (os error 61)"}, …],"warnings":[]}
JsonMessage::code is documented as a "Stable machine-readable diagnostic code" (crates/cli/src/json.rs:135), but crates/cast/bin/main.rs:23 sets cast.error for every failure. The revert data (0xcf479181…) is only inside a context string.
forge testthat runs no test exits 0. Both a filter that matches nothing and a project with no tests:
$ forge test --match-test doesnotexist --json # prints {} (#16950), exit 0
$ forge test # "Warning: No tests found in project!", exit 0
A renamed test or a typo in a CI filter turns the job green.
Measured impact. We gave coding agents (Claude Sonnet and Haiku, fresh context, Foundry 1.8.7, --help and the public docs only) five CI-style tasks and graded the scripts they wrote in several situations. All passed, but only with workarounds:
- Asked for a script that exits 3 on a revert (with the decoded error) and 1 on any other failure, both agents matched the text
execution reverted; one needed 29 Foundry calls. - Asked for a CI test gate that fails when no test ran, both counted tests from the JSON output themselves, because the exit code does not say.
I know the machine-mode project (#14771, with an exit-code enum) was dropped in #15389, and #13855 settled on JSON output. This asks for much less, within the existing JSON envelope:
Proposal
- In
cast, give reverts their own envelope code, for examplecast.revert, with the raw revert data (and the decoded error when known) indetails. Transport and RPC errors keepcast.error, or getcast.rpc. The exit code can stay 1. Optionally, a distinct exit code for reverts as a follow-up, if you are open to it. - In
forge test, a way to fail when no test ran: either a non-zero exit by default, which changes behaviour for some setups, or an opt-in flag such as--fail-if-no-tests.
I can open PRs for either, in the form you prefer.
Checked on forge and cast 1.8.7 (985eeea), master d291e9722. Related: #17974 / #17975 (a script setting forge's exit code), #16950 (empty JSON on no match).
🤖 Generated with Claude Code
Would it be most useful to prioritize the cast revert classification or the forge test no-tests behavior for an initial PR?
Thanks for picking this up. No maintainer has weighed in yet, so it may be worth waiting for one before starting.
If you go ahead, I'd start with cast: a distinct code for reverts, with the revert data as its own field, only adds to the JSON output. Making forge test exit non-zero when no test runs would change an exit code that existing CI scripts may rely on, so that part needs a maintainer decision first.
@DefiAudit0x the forge script half of this shipped in v1.10.1 with #18696: --json failures now carry script.revert (with the revert data and decoded reason in details), rpc.error, rpc.transport and compiler.error. The RPC classification lives in crates/common/src/provider/mod.rs, so cast could reuse it instead of cast.error for everything (crates/cast/bin/main.rs:23 at v1.10.1), and a cast.revert with the same details shape would match what maintainers just accepted. That still looks like the best place to start; the forge test exit code part still needs a maintainer decision.
Following up on @aelmanaa's note that forge script already ships script.revert / rpc.error / rpc.transport in v1.10.1: would you be open to a first PR that only teaches cast the same shape (cast.revert with revert data/reason in details, reusing the provider classification) and leaves exit codes and forge test --fail-if-no-tests for a separate call?
Happy to keep that PR additive JSON only, no exit code change, if that is the preferred slice.
Thanks for offering, @kutluhaneth46. A JSON-only Cast PR sounds like a good first step. Reusing the provider classification from #18696 would give callers cast.revert with details.data and details.reason, alongside rpc.error and rpc.transport.
@stevencartavia, since you implemented the Forge script envelope in #18696, would this scope work for a first Cast PR? The forge test no-tests behavior can stay a separate discussion.
Thanks @aelmanaa. I will wait for @stevencartavia's take on the Cast JSON scope, then open a small PR that reuses the #18696 provider classification for cast.revert / rpc.error / rpc.transport.
Addressing only the Cast part (separate --json codes for reverts vs RPC failures); leaving forge test as is, since a non-zero exit when no tests run would break CI that relies on exit 0, and --json already prints {} in that case.
Thanks @stevencartavia. I will leave the Cast JSON work to you then and stay out of the way. Happy to review if useful.