|
| 1 | +--- |
| 2 | +author: tejesh-vaish |
| 3 | +date: "2026-10-26" |
| 4 | +title: Modeling uncertain tool-call outcomes with variants |
| 5 | +badge: community |
| 6 | +description: | |
| 7 | + Keep a missing tool reply separate from rejection with a small, |
| 8 | + runnable ReScript example. |
| 9 | +--- |
| 10 | + |
| 11 | +What should the UI show when a tool call times out? In this hypothetical example inspired by [Lumian's agent workflows](https://lumian.ai/agents/), a seller approves a product-title update. The request leaves, but the UI times out before the tool's final reply arrives. The title might already have changed, so a "Failed" message and Retry button could mislead the seller. |
| 12 | + |
| 13 | +I'm new to ReScript and use it personally. I'd give the missing reply its own state. |
| 14 | + |
| 15 | +## Track what the UI knows |
| 16 | + |
| 17 | +A [variant](../docs/manual/variant.mdx) names the possible states and can carry data with each one: |
| 18 | + |
| 19 | +- `Pending`: we're waiting for a final reply. |
| 20 | +- `Unresolved`: the local waiting deadline passed without a final reply. |
| 21 | +- `Completed`: carries output, including an empty string. |
| 22 | +- `Rejected`: the tool sent a final reply refusing the update. |
| 23 | + |
| 24 | +A network or parse error doesn't count as a rejection reply. Neither does withheld approval. |
| 25 | + |
| 26 | +The example starts with validated events. Every event, including a deadline, carries an `attemptId`. Give each new attempt a fresh ID and never reuse it. The ID check matches events to attempts. ID allocation and sender authentication happen outside this example. |
| 27 | + |
| 28 | +Use the external-link icon on the ReScript tab to open the example in the playground. Select [ReScript 12.3.1](https://github.com/rescript-lang/rescript/releases/tag/v12.3.1) before running it. The Output tab below contains the recorded console output. |
| 29 | + |
| 30 | +<CodeTab labels={["ReScript", "Output"]}> |
| 31 | + |
| 32 | +```res example |
| 33 | +type status = |
| 34 | + | Pending |
| 35 | + | Unresolved |
| 36 | + | Completed(string) |
| 37 | + | Rejected(string) |
| 38 | +
|
| 39 | +type call = {attemptId: string, status: status} |
| 40 | +type signal = Deadline | CompletedReply(string) | RejectedReply(string) |
| 41 | +type event = {attemptId: string, signal: signal} |
| 42 | +
|
| 43 | +let begin = attemptId => {attemptId, status: Pending} |
| 44 | +
|
| 45 | +let applySignal = (call, signal) => |
| 46 | + switch signal { |
| 47 | + | Deadline => |
| 48 | + if call.status === Pending { |
| 49 | + {...call, status: Unresolved} |
| 50 | + } else { |
| 51 | + call |
| 52 | + } |
| 53 | + | CompletedReply(output) => {...call, status: Completed(output)} |
| 54 | + | RejectedReply(reason) => {...call, status: Rejected(reason)} |
| 55 | + } |
| 56 | +
|
| 57 | +let receive = (call: call, event) => { |
| 58 | + if call.attemptId !== event.attemptId { |
| 59 | + call |
| 60 | + } else { |
| 61 | + switch call.status { |
| 62 | + | Completed(_) | Rejected(_) => call |
| 63 | + | Pending | Unresolved => applySignal(call, event.signal) |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | +
|
| 68 | +let show = call => |
| 69 | + switch call.status { |
| 70 | + | Pending => "Pending" |
| 71 | + | Unresolved => "Unresolved" |
| 72 | + | Completed(output) => "Completed: " ++ output |
| 73 | + | Rejected(reason) => "Rejected: " ++ reason |
| 74 | + } |
| 75 | +
|
| 76 | +let pending = begin("title-1") |
| 77 | +pending->show->Console.log |
| 78 | +
|
| 79 | +let waiting = pending->receive({attemptId: "title-1", signal: Deadline}) |
| 80 | +waiting->show->Console.log |
| 81 | +
|
| 82 | +let stillWaiting = waiting->receive({ |
| 83 | + attemptId: "title-0", |
| 84 | + signal: CompletedReply("Old title"), |
| 85 | +}) |
| 86 | +stillWaiting->show->Console.log |
| 87 | +
|
| 88 | +let saved = stillWaiting->receive({ |
| 89 | + attemptId: "title-1", |
| 90 | + signal: CompletedReply("Title saved"), |
| 91 | +}) |
| 92 | +saved->show->Console.log |
| 93 | +
|
| 94 | +saved |
| 95 | +->receive({attemptId: "title-1", signal: RejectedReply("Too late")}) |
| 96 | +->receive({attemptId: "title-1", signal: Deadline}) |
| 97 | +->show->Console.log |
| 98 | +
|
| 99 | +begin("title-2") |
| 100 | +->receive({attemptId: "title-2", signal: RejectedReply("Title too long")}) |
| 101 | +->show->Console.log |
| 102 | +``` |
| 103 | + |
| 104 | +```text |
| 105 | +Pending |
| 106 | +Unresolved |
| 107 | +Unresolved |
| 108 | +Completed: Title saved |
| 109 | +Completed: Title saved |
| 110 | +Rejected: Title too long |
| 111 | +``` |
| 112 | + |
| 113 | +</CodeTab> |
| 114 | + |
| 115 | +## Read the result |
| 116 | + |
| 117 | +The deadline changes `Pending` to `Unresolved`. A late reply with the same attempt ID can still complete or reject the attempt. Another deadline leaves it unresolved. `receive` creates a new record when the status changes. The earlier `pending` value stays pending. |
| 118 | + |
| 119 | +The third output line stays `Unresolved`: the reply belongs to the old `title-0` attempt, so the ID check ignores it. The next reply matches `title-1` and completes the current attempt. The check also ignores rejections and deadlines from other attempts. |
| 120 | + |
| 121 | +The fifth output line stays completed despite a contradictory rejection and a deadline. Once the client accepts a matching final reply, every later event leaves that result intact. Keeping the first final reply is a client policy. Conflicting tool replies still need reconciliation. |
| 122 | + |
| 123 | +[Pattern matching](../docs/manual/pattern-matching-destructuring.mdx) makes `show` handle all four states and the data they carry. Remove a case from `show` and the compiler reports the missing state. That check covers these typed values. We still need message validation and the guards in `receive` that control when the state can change. |
| 124 | + |
| 125 | +For `Unresolved`, I'd show "No final reply yet." A deadline doesn't prove failure, cancellation, or rollback, or tell us whether retry is safe. Before offering a retry, a real integration needs to validate and authenticate incoming replies, supply fresh attempt IDs, and decide how to reconcile changes the tool may already have made. |
0 commit comments