Skip to content

feat(sdk): mark HostErrorKind + LogLevel #[non_exhaustive] (COW-1029) - #35

Closed
brunota20 wants to merge 1 commit into
fix/supervisor-alive-on-init-errfrom
feat/non-exhaustive-sdk-enums-cow-1029
Closed

feat(sdk): mark HostErrorKind + LogLevel #[non_exhaustive] (COW-1029)#35
brunota20 wants to merge 1 commit into
fix/supervisor-alive-on-init-errfrom
feat/non-exhaustive-sdk-enums-cow-1029

Conversation

@brunota20

Copy link
Copy Markdown

What does this PR do?

Applies forward-compatibility hardening to the two SDK enums that mirror the WIT type system. Both `HostErrorKind` (7 variants) and `LogLevel` (5 variants) now carry `#[non_exhaustive]`. Once WIT adds a new variant (e.g. a future `WasmTrap` / `Critical`), the SDK side mirrors it without breaking downstream `match` sites.

First M4 production-hardening issue landed.

Why

The COW-1029 issue body originally proposed waiting until WIT actually grew before applying `#[non_exhaustive]`. Re-scoped for M4 production hardening: stable API contracts before mainnet matter more than the 10 wildcard arms the change costs.

Changes

  • `crates/shepherd-sdk/src/host.rs`: `#[non_exhaustive]` on `LogLevel` + `HostErrorKind`; rustdoc cites the policy + wildcard-arm guidance.
  • `modules/{twap-monitor,ethflow-watcher,examples/price-alert,examples/stop-loss}/src/lib.rs`: wildcard arms in `sdk_err_into_wit` (-> `HostErrorKind::Internal`) and `convert_level` (-> `logging::Level::Info`).
  • `docs/adr/0009-host-trait-surface.md`: Consequences section updated to reflect the M4 application of `#[non_exhaustive]`.

balance-tracker is unaffected (still on the M2-era direct-wit-bindgen pattern; not refactored to Host trait). Tracked as the optional follow-up from the COW-1063 QA matrix.

Out of scope

  • `RetryAction` + `PollOutcome` stay exhaustive. They are domain-locked to `OrderPostErrorKind::is_retriable` (cowprotocol) and the `IConditionalOrder` Solidity interface respectively; the issue body explicitly exempts them.
  • `HostError` struct stays without `#[non_exhaustive]`. Adding it would break the 5 modules that construct `HostError { ... }` inline. Worth a follow-up if/when the WIT struct grows a field.

Breaking changes

For downstream module authors: `match` on SDK `HostErrorKind` or `LogLevel` now requires a wildcard arm. The 4 production modules updated here are the only known consumers; outside that we're pre-publish so the impact window is zero.

Testing

  • `cargo test --workspace` -> 151 host tests + 6 doctests passing.
  • `cargo clippy --all-targets --workspace -- -D warnings` clean.
  • `cargo fmt --all --check` clean.
  • All 5 wasm modules build under `wasm32-wasip2 --release`.
  • 0 em-dashes in changed files.

AI assistance disclosure

AI Assistance: this change + description was produced by a Claude Code agent (Claude Opus 4.7 1M context). A human (Bruno) reviewed and is accountable for the result.

Linear: COW-1029. First M4 issue landed; stacks on #34 (supervisor alive fix).

Applies forward-compatibility hardening to the two SDK enums that
mirror the WIT type system. Both `HostErrorKind` (7 variants) and
`LogLevel` (5 variants) now carry `#[non_exhaustive]`. The WIT
contract drives the variant set: once a new variant is added in WIT
(e.g. a future `WasmTrap` HostErrorKind, or a `Critical` LogLevel),
the SDK side mirrors it without breaking downstream `match` sites.

`RetryAction` (3 variants) and `PollOutcome` (5 variants) stay
exhaustive - they're domain-locked to the cowprotocol
`OrderPostErrorKind::is_retriable()` contract and the
`IConditionalOrder` Solidity interface respectively. The
`#[non_exhaustive]` issue body (COW-1029 / BLEU-853) explicitly
exempts them.

## Match-site updates

The 4 modules that ported to the M3 Host trait pattern (price-alert,
stop-loss, twap-monitor, ethflow-watcher) match on SDK `HostErrorKind`
and `LogLevel` in their `sdk_err_into_wit` and `convert_level`
adapters. Each gains a wildcard arm:

  _ => HostErrorKind::Internal  // safest catch-all
  _ => logging::Level::Info     // most neutral default

balance-tracker is unaffected (it predates the host-trait refactor
and matches against wit-bindgen types directly, not SDK types). The
balance-tracker port is tracked as the COW-1063 QA matrix optional
follow-up.

## Considered alternatives

Issue body originally proposed "open as tracking ticket; trigger
when WIT adds the 8th HostErrorKind". Rejected for M4: production
hardening means stable API contracts BEFORE shipping. Applying
non_exhaustive proactively trades 10 wildcard arms (5 modules x 2
match blocks, minus 1 module that doesn't have the adapter) for
forward-compat stability.

## Doc + ADR updates

- `crates/shepherd-sdk/src/host.rs`: rustdoc on each enum cites the
  COW-1029 decision + wildcard-arm guidance for module adapters.
- `docs/adr/0009-host-trait-surface.md`: Consequences section
  updated. The "ADR-0009 follow-up COW-1029" stub is replaced with
  the applied state.

## Validation

- `cargo test --workspace` -> 151 host tests + 6 doctests passing
  (no change in count).
- `cargo clippy --all-targets --workspace -- -D warnings` clean.
- `cargo fmt --all --check` clean.
- 5 wasm modules build under `wasm32-wasip2 --release`.
- 0 em-dashes in changed files.

Linear: COW-1029. First M4 issue landed.
@linear-code

linear-code Bot commented Jun 18, 2026

Copy link
Copy Markdown

COW-1029

@brunota20

Copy link
Copy Markdown
Author

Work landed via dev/m4-base creation @ 64cb6d8 (M4 production hardening + PR #66 rust-idiomatic compliance squashed). The HEAD commit of this PR is now an ancestor of dev/m4-base. Closing as merged-by-ancestor.

@brunota20 brunota20 closed this Jun 24, 2026
brunota20 added a commit that referenced this pull request Jun 25, 2026
…1036)

Locks the M1 fuel + memory wiring (BLEU-818) against regression with
two evil-by-design wasm fixtures and three supervisor integration
tests that exercise the full trap path: dispatch -> wasmtime trap ->
supervisor catches -> module marked dead -> engine continues.

## New fixtures

`modules/fixtures/fuel-bomb/` (66 KB wasm)
    on_event runs an unbounded `wrapping_add` loop with
    `std::hint::black_box` so the optimiser cannot elide it.
    wasmtime exhausts the per-event DEFAULT_FUEL_PER_EVENT (1B) and
    traps with OutOfFuel.

`modules/fixtures/memory-bomb/` (67 KB wasm)
    on_event allocates 128 MiB which exceeds the per-store
    DEFAULT_MEMORY_LIMIT (64 MiB). wasmtime rejects the memory.grow
    and traps the module.

Both fixtures live under `modules/fixtures/` so they are obviously
test-only - the M2 / M3 testnet configs never reference them. Both
declare only the `logging` capability + a single block subscription.

## New supervisor integration tests

`resource_limit_fuel_bomb_traps_and_marks_module_dead`
    Boots fuel-bomb alone, dispatches a block, asserts:
      - dispatched == 0 (trap, not delivery)
      - alive_count() == 0 (module marked dead)
      - second dispatch returns 0 (dead module excluded)
    -> proves the fuel limit fires + the supervisor catches the
       trap without panicking.

`resource_limit_memory_bomb_traps_and_marks_module_dead`
    Same shape for the 64 MiB cap. The wasm32 allocator surfaces
    "memory allocation of 134217728 bytes failed" (the trap firing).

`resource_limit_dead_bomb_does_not_starve_healthy_module`
    Strongest isolation test: loads fuel-bomb + the M1 example
    module side-by-side, dispatches a block, asserts:
      - dispatched == 1 (example survived + accepted the dispatch
        even though the bomb trapped on the same block)
      - alive_count() == 1 (only example alive)
      - second dispatch == 1 (dead bomb skipped, example continues)
    -> proves a rogue module cannot starve the supervisor or
       starve sibling modules.

## Validation

- `cargo test -p nexum-engine resource_limit` -> 3 passed.
- `cargo test --workspace` -> 154 host tests + 6 doctests passing
  (was 151 + 6; +3 from the new tests).
- `cargo clippy --all-targets --workspace -- -D warnings` clean.
- `cargo fmt --all --check` clean.
- `cargo build --target wasm32-wasip2 --release -p {fuel-bomb,memory-bomb}`
  clean.
- 0 em-dashes in new files.

## Out of scope

- Fuel + memory limits made configurable per-module via `engine.toml`
  (today they are workspace constants in `runtime/limits.rs`).
  Already noted in the source comments as "configurable in 0.3";
  acknowledged not addressed here.
- Adversarial fuzz of the resource-limit defaults under sustained
  load. That is COW-1065 (security review) territory.
- CI integration of the fixtures into the build matrix (PR #27).
  Not needed - `cargo test --workspace` already builds them in the
  test profile, and the `module_wasm_or_skip` guard means CI does
  not need a separate fixture-build job.

Linear: COW-1036. Second M4 issue landed; stacks on #35 (COW-1029).
brunota20 added a commit that referenced this pull request Jun 25, 2026
…1036)

Locks the M1 fuel + memory wiring (BLEU-818) against regression with
two evil-by-design wasm fixtures and three supervisor integration
tests that exercise the full trap path: dispatch -> wasmtime trap ->
supervisor catches -> module marked dead -> engine continues.

## New fixtures

`modules/fixtures/fuel-bomb/` (66 KB wasm)
    on_event runs an unbounded `wrapping_add` loop with
    `std::hint::black_box` so the optimiser cannot elide it.
    wasmtime exhausts the per-event DEFAULT_FUEL_PER_EVENT (1B) and
    traps with OutOfFuel.

`modules/fixtures/memory-bomb/` (67 KB wasm)
    on_event allocates 128 MiB which exceeds the per-store
    DEFAULT_MEMORY_LIMIT (64 MiB). wasmtime rejects the memory.grow
    and traps the module.

Both fixtures live under `modules/fixtures/` so they are obviously
test-only - the M2 / M3 testnet configs never reference them. Both
declare only the `logging` capability + a single block subscription.

## New supervisor integration tests

`resource_limit_fuel_bomb_traps_and_marks_module_dead`
    Boots fuel-bomb alone, dispatches a block, asserts:
      - dispatched == 0 (trap, not delivery)
      - alive_count() == 0 (module marked dead)
      - second dispatch returns 0 (dead module excluded)
    -> proves the fuel limit fires + the supervisor catches the
       trap without panicking.

`resource_limit_memory_bomb_traps_and_marks_module_dead`
    Same shape for the 64 MiB cap. The wasm32 allocator surfaces
    "memory allocation of 134217728 bytes failed" (the trap firing).

`resource_limit_dead_bomb_does_not_starve_healthy_module`
    Strongest isolation test: loads fuel-bomb + the M1 example
    module side-by-side, dispatches a block, asserts:
      - dispatched == 1 (example survived + accepted the dispatch
        even though the bomb trapped on the same block)
      - alive_count() == 1 (only example alive)
      - second dispatch == 1 (dead bomb skipped, example continues)
    -> proves a rogue module cannot starve the supervisor or
       starve sibling modules.

## Validation

- `cargo test -p nexum-engine resource_limit` -> 3 passed.
- `cargo test --workspace` -> 154 host tests + 6 doctests passing
  (was 151 + 6; +3 from the new tests).
- `cargo clippy --all-targets --workspace -- -D warnings` clean.
- `cargo fmt --all --check` clean.
- `cargo build --target wasm32-wasip2 --release -p {fuel-bomb,memory-bomb}`
  clean.
- 0 em-dashes in new files.

## Out of scope

- Fuel + memory limits made configurable per-module via `engine.toml`
  (today they are workspace constants in `runtime/limits.rs`).
  Already noted in the source comments as "configurable in 0.3";
  acknowledged not addressed here.
- Adversarial fuzz of the resource-limit defaults under sustained
  load. That is COW-1065 (security review) territory.
- CI integration of the fixtures into the build matrix (PR #27).
  Not needed - `cargo test --workspace` already builds them in the
  test profile, and the `module_wasm_or_skip` guard means CI does
  not need a separate fixture-build job.

Linear: COW-1036. Second M4 issue landed; stacks on #35 (COW-1029).
brunota20 added a commit that referenced this pull request Jun 25, 2026
…1036)

Locks the M1 fuel + memory wiring (BLEU-818) against regression with
two evil-by-design wasm fixtures and three supervisor integration
tests that exercise the full trap path: dispatch -> wasmtime trap ->
supervisor catches -> module marked dead -> engine continues.

## New fixtures

`modules/fixtures/fuel-bomb/` (66 KB wasm)
    on_event runs an unbounded `wrapping_add` loop with
    `std::hint::black_box` so the optimiser cannot elide it.
    wasmtime exhausts the per-event DEFAULT_FUEL_PER_EVENT (1B) and
    traps with OutOfFuel.

`modules/fixtures/memory-bomb/` (67 KB wasm)
    on_event allocates 128 MiB which exceeds the per-store
    DEFAULT_MEMORY_LIMIT (64 MiB). wasmtime rejects the memory.grow
    and traps the module.

Both fixtures live under `modules/fixtures/` so they are obviously
test-only - the M2 / M3 testnet configs never reference them. Both
declare only the `logging` capability + a single block subscription.

## New supervisor integration tests

`resource_limit_fuel_bomb_traps_and_marks_module_dead`
    Boots fuel-bomb alone, dispatches a block, asserts:
      - dispatched == 0 (trap, not delivery)
      - alive_count() == 0 (module marked dead)
      - second dispatch returns 0 (dead module excluded)
    -> proves the fuel limit fires + the supervisor catches the
       trap without panicking.

`resource_limit_memory_bomb_traps_and_marks_module_dead`
    Same shape for the 64 MiB cap. The wasm32 allocator surfaces
    "memory allocation of 134217728 bytes failed" (the trap firing).

`resource_limit_dead_bomb_does_not_starve_healthy_module`
    Strongest isolation test: loads fuel-bomb + the M1 example
    module side-by-side, dispatches a block, asserts:
      - dispatched == 1 (example survived + accepted the dispatch
        even though the bomb trapped on the same block)
      - alive_count() == 1 (only example alive)
      - second dispatch == 1 (dead bomb skipped, example continues)
    -> proves a rogue module cannot starve the supervisor or
       starve sibling modules.

## Validation

- `cargo test -p nexum-engine resource_limit` -> 3 passed.
- `cargo test --workspace` -> 154 host tests + 6 doctests passing
  (was 151 + 6; +3 from the new tests).
- `cargo clippy --all-targets --workspace -- -D warnings` clean.
- `cargo fmt --all --check` clean.
- `cargo build --target wasm32-wasip2 --release -p {fuel-bomb,memory-bomb}`
  clean.
- 0 em-dashes in new files.

## Out of scope

- Fuel + memory limits made configurable per-module via `engine.toml`
  (today they are workspace constants in `runtime/limits.rs`).
  Already noted in the source comments as "configurable in 0.3";
  acknowledged not addressed here.
- Adversarial fuzz of the resource-limit defaults under sustained
  load. That is COW-1065 (security review) territory.
- CI integration of the fixtures into the build matrix (PR #27).
  Not needed - `cargo test --workspace` already builds them in the
  test profile, and the `module_wasm_or_skip` guard means CI does
  not need a separate fixture-build job.

Linear: COW-1036. Second M4 issue landed; stacks on #35 (COW-1029).
brunota20 added a commit that referenced this pull request Jun 25, 2026
…1036)

Locks the M1 fuel + memory wiring (BLEU-818) against regression with
two evil-by-design wasm fixtures and three supervisor integration
tests that exercise the full trap path: dispatch -> wasmtime trap ->
supervisor catches -> module marked dead -> engine continues.

## New fixtures

`modules/fixtures/fuel-bomb/` (66 KB wasm)
    on_event runs an unbounded `wrapping_add` loop with
    `std::hint::black_box` so the optimiser cannot elide it.
    wasmtime exhausts the per-event DEFAULT_FUEL_PER_EVENT (1B) and
    traps with OutOfFuel.

`modules/fixtures/memory-bomb/` (67 KB wasm)
    on_event allocates 128 MiB which exceeds the per-store
    DEFAULT_MEMORY_LIMIT (64 MiB). wasmtime rejects the memory.grow
    and traps the module.

Both fixtures live under `modules/fixtures/` so they are obviously
test-only - the M2 / M3 testnet configs never reference them. Both
declare only the `logging` capability + a single block subscription.

## New supervisor integration tests

`resource_limit_fuel_bomb_traps_and_marks_module_dead`
    Boots fuel-bomb alone, dispatches a block, asserts:
      - dispatched == 0 (trap, not delivery)
      - alive_count() == 0 (module marked dead)
      - second dispatch returns 0 (dead module excluded)
    -> proves the fuel limit fires + the supervisor catches the
       trap without panicking.

`resource_limit_memory_bomb_traps_and_marks_module_dead`
    Same shape for the 64 MiB cap. The wasm32 allocator surfaces
    "memory allocation of 134217728 bytes failed" (the trap firing).

`resource_limit_dead_bomb_does_not_starve_healthy_module`
    Strongest isolation test: loads fuel-bomb + the M1 example
    module side-by-side, dispatches a block, asserts:
      - dispatched == 1 (example survived + accepted the dispatch
        even though the bomb trapped on the same block)
      - alive_count() == 1 (only example alive)
      - second dispatch == 1 (dead bomb skipped, example continues)
    -> proves a rogue module cannot starve the supervisor or
       starve sibling modules.

## Validation

- `cargo test -p nexum-engine resource_limit` -> 3 passed.
- `cargo test --workspace` -> 154 host tests + 6 doctests passing
  (was 151 + 6; +3 from the new tests).
- `cargo clippy --all-targets --workspace -- -D warnings` clean.
- `cargo fmt --all --check` clean.
- `cargo build --target wasm32-wasip2 --release -p {fuel-bomb,memory-bomb}`
  clean.
- 0 em-dashes in new files.

## Out of scope

- Fuel + memory limits made configurable per-module via `engine.toml`
  (today they are workspace constants in `runtime/limits.rs`).
  Already noted in the source comments as "configurable in 0.3";
  acknowledged not addressed here.
- Adversarial fuzz of the resource-limit defaults under sustained
  load. That is COW-1065 (security review) territory.
- CI integration of the fixtures into the build matrix (PR #27).
  Not needed - `cargo test --workspace` already builds them in the
  test profile, and the `module_wasm_or_skip` guard means CI does
  not need a separate fixture-build job.

Linear: COW-1036. Second M4 issue landed; stacks on #35 (COW-1029).
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