Preload the box a cancelled run belongs to, so cancel can answer `POST /api/v1/boxes/:box_id/runs/:run_id/cancel` returned 500 in production after the cancellation had already been written. The run really was cancelled and the caller was told the request failed, which is the worst shape available: a client that retries repeats something that already happened, and a client that reports the error reports a failure that did not occur. `BoxRuns.cancel/1` builds its result from a locked `Repo.one!` inside the transaction, which returns a bare row. `start_run/6`, `list_runs/2`, and `get_run/3` all preload `:conversation_box`; `cancel/1` was the one path that did not, and both of its branches — the terminal early return and the one that goes through `cancel_worker/1` — handed back a run whose association was unloaded. So it now preloads once, after both branches, as its siblings do. One path diverging from three had two consumers, not one. `BoxRunController.projection/1` reads `run.conversation_box.box_id` and `Delegations.delegation_projection/2` reads `run.conversation_box.id`, so cancelling through the delegation surface raised the same `KeyError` on the same association. `BoxRunServer` survived only because its `box_id/1` falls back to a lookup when the match on the loaded association fails; the two projections have no such fallback. The tests assert the response body, not just the status. A 500 here still leaves correct data behind, so a test that checked only the database would have passed while the caller saw a crash. Reverting the preload fails all four: both controller tests, the delegation test, and the association assertion added to the existing `BoxRuns.cancel/1` test, which pins the contract at the source rather than at each consumer. Refs OpenAgentsInc/openagents#108
Preload the box a cancelled run belongs to, so cancel can answer
Deploy story
What this commit did to the running system — joined from the forge receipt chain, the part a commit page elsewhere cannot show.
- pushed
- by user · WAL seq 444 · 2026-08-26T09:58:08.630847Z
Changed files
-
modified
lib/openagents/box_runs.ex -
modified
test/openagents/box_runs_test.exs -
added
test/openagents/delegations/box_cancel_test.exs -
modified
test/openagents_web/controllers/box_run_controller_test.exs
Diff
4 files changed, +169 -3
lib/openagents/box_runs.ex modified +10 -3
@@ -80,9 +80,16 @@ defmodule OpenAgents.BoxRuns do
| 80 | 80 |
|
| 81 | 81 |
|
| 82 | 82 |
|
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
|
| 87 |
|
|
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 86 | 93 |
|
| 87 | 94 |
|
| 88 | 95 |
|
test/openagents/box_runs_test.exs modified +3
@@ -283,6 +283,9 @@ defmodule OpenAgents.BoxRunsTest do
| 283 | 283 |
|
| 284 | 284 |
|
| 285 | 285 |
|
| 286 |
|
|
| 287 |
|
|
| 288 |
|
|
| 286 | 289 |
|
| 287 | 290 |
|
| 288 | 291 |
|
test/openagents/delegations/box_cancel_test.exs added +70
@@ -0,0 +1,70 @@
| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
test/openagents_web/controllers/box_run_controller_test.exs modified +86
@@ -162,6 +162,92 @@ defmodule OpenAgentsWeb.BoxRunControllerTest do
| 162 | 162 |
|
| 163 | 163 |
|
| 164 | 164 |
|
| 165 |
|
|
| 166 |
|
|
| 167 |
|
|
| 168 |
|
|
| 169 |
|
|
| 170 |
|
|
| 171 |
|
|
| 172 |
|
|
| 173 |
|
|
| 174 |
|
|
| 175 |
|
|
| 176 |
|
|
| 177 |
|
|
| 178 |
|
|
| 179 |
|
|
| 180 |
|
|
| 181 |
|
|
| 182 |
|
|
| 183 |
|
|
| 184 |
|
|
| 185 |
|
|
| 186 |
|
|
| 187 |
|
|
| 188 |
|
|
| 189 |
|
|
| 190 |
|
|
| 191 |
|
|
| 192 |
|
|
| 193 |
|
|
| 194 |
|
|
| 195 |
|
|
| 196 |
|
|
| 197 |
|
|
| 198 |
|
|
| 199 |
|
|
| 200 |
|
|
| 201 |
|
|
| 202 |
|
|
| 203 |
|
|
| 204 |
|
|
| 205 |
|
|
| 206 |
|
|
| 207 |
|
|
| 208 |
|
|
| 209 |
|
|
| 210 |
|
|
| 211 |
|
|
| 212 |
|
|
| 213 |
|
|
| 214 |
|
|
| 215 |
|
|
| 216 |
|
|
| 217 |
|
|
| 218 |
|
|
| 219 |
|
|
| 220 |
|
|
| 221 |
|
|
| 222 |
|
|
| 223 |
|
|
| 224 |
|
|
| 225 |
|
|
| 226 |
|
|
| 227 |
|
|
| 228 |
|
|
| 229 |
|
|
| 230 |
|
|
| 231 |
|
|
| 232 |
|
|
| 233 |
|
|
| 234 |
|
|
| 235 |
|
|
| 236 |
|
|
| 237 |
|
|
| 238 |
|
|
| 239 |
|
|
| 240 |
|
|
| 241 |
|
|
| 242 |
|
|
| 243 |
|
|
| 244 |
|
|
| 245 |
|
|
| 246 |
|
|
| 247 |
|
|
| 248 |
|
|
| 249 |
|
|
| 250 |
|
|
| 165 | 251 |
|
| 166 | 252 |
|
| 167 | 253 |
|