Skip to content

Vibecode: reducer return values.#4723

Draft
gefjon wants to merge 42 commits intomasterfrom
phoebe/tpcc/reducer-return-value
Draft

Vibecode: reducer return values.#4723
gefjon wants to merge 42 commits intomasterfrom
phoebe/tpcc/reducer-return-value

Conversation

@gefjon
Copy link
Copy Markdown
Contributor

@gefjon gefjon commented Mar 28, 2026

Description of Changes

Initial Codex-assissted prototype of reducer return values. Implemented only for Rust modules and the Rust client SDK, plus the HTTP API.

This is part of our weekend hackathon to implement distributed TPC-C.

Note that this PR is based on phoebe/tpcc-distributed-naive-http-requests. Reviewers (or cherry-pickers) should start from 4781c22 .

I asked Codex for some examples, and it spit out:

Reducer return value examples

1) cURL HTTP client calling a reducer

# Call reducer `return_greeting` which returns a String
curl -sS \
  -X POST \
  -H 'Authorization: Bearer <TOKEN>' \
  -H 'Content-Type: application/json' \
  'http://127.0.0.1:3000/v1/database/<DATABASE_ID_OR_NAME>/call/return_greeting' \
  -d '[]'

# Example response body:
# "Hello"

If the reducer returns (), the response body is empty on success.

2) Rust module defining a reducer

use spacetimedb::ReducerContext;

#[spacetimedb::reducer]
fn return_sum(_ctx: &ReducerContext, a: i32, b: i32) -> Result<i32, String> {
    Ok(a + b)
}

#[spacetimedb::reducer]
fn return_greeting(_ctx: &ReducerContext, name: String) -> Result<String, String> {
    Ok(format!("Hello, {name}!"))
}

Reducers may return () or Result<T, E>, where T: SpacetimeType and E: Display.

3) Rust client SDK calling a reducer

use spacetimedb_sdk::{DbConnectionBuilder, ReducerEvent, Status};
use my_module::RemoteModule;

let conn = DbConnectionBuilder::<RemoteModule>::new()
    .with_url("http://127.0.0.1:3000")
    .build()
    .unwrap();
conn.run_threaded();

conn.reducers
    .return_sum_then(2, 3, |ctx, status| {
        match status {
            Ok(Ok(value)) => {
                assert_eq!(value, 5);
                assert!(matches!(ctx.event.status, Status::Committed));
            }
            Ok(Err(msg)) => panic!("Reducer error: {msg}"),
            Err(err) => panic!("Reducer internal error: {err:?}"),
        }
    })
    .unwrap();

API and ABI breaking changes

Should be additive-only, but I don't trust that Codex did it write. This is marked draft for a reason.

Expected complexity level and risk

4: pretty deep changes, and it's all generated, so this will need thorough review.

Testing

  • New smoketest for the HTTP /v1/database/:name/call/:reducer POST behavior.
  • New SDK test for the Rust client SDK behavior.
  • We'll integrate this into our TPC-C experiment.

joshua-spacetime and others added 30 commits March 25, 2026 22:24
Incomplete. I've mostly rewritten `new_order`, which I think is the hard one.
I haven't yet started rewriting `payment`.

`new_order` is still incomplete in that I haven't wired up reporting of `s_quantity`.
This is an isolation hazard, as discussed out-of-band.
JSON was getting wacky case-conversion errors I didn't feel like fixing,
so I just modified the `database/call` route to accept `application/octet-stream` with BSATN.
Initial Codex-assissted prototype of reducer return values.
Implemented only for Rust modules and the Rust client SDK,
plus the HTTP API.
This reverts commit 84737a6.

Was supposed to be on a different branch.
gefjon and others added 12 commits March 28, 2026 17:36
…com:clockworklabs/SpacetimeDB into phoebe/tpcc-distributed-naive-http-requests
…ebe/tpcc/reducer-return-value

Resolved conflict in crates/client-api/src/routes/database.rs manually:
Shub and I had both implemented accepting BSATN via `application/octet-stream`,
with slightly different ways of checking the headers and returning errors.
This ditches our use of procedures for their return values
and of the procedure HTTP interface.
…tp-requests' into phoebe/tpcc/reducer-return-value

Manually resolved conflicts in the module and loader related to removing `spacetimedb_uri`.
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.

4 participants