Repository navigation
Commit a251aaa
fix(metadata-protocol): a stopped or rolled-back bulk batch names the row that actually failed (#19700)
Fixes #19452
Clause-②: no
A stopped or rolled-back bulk batch named a causal row that did not
fail, and called the real error 「unknown error」 while that error was
sitting in the same array.
## The defect
`reconcileStoppedBatch` and `buildRolledBackBatchResponse` both located
the causal row with `findIndex(r => !r.success)`. That encoded one
invariant: **`!success` means this row failed, and it carries
`errors[0]`.**
PR #19432 broke that invariant deliberately and correctly: a row that
MATCHED and was NOT removed now answers `success: false` with **no
`errors` entry**, because a surviving record is an outcome, not a fault.
So the locator could land on that survivor, `errors?.[0]?.message` was
`undefined`, and the message named the **wrong index** while falling
back to 「unknown error」.
## Reproduced before the fix, verbatim
Taken by neutralising the fix on the final tree:
`packages/metadata-protocol/src/protocol.ts` restored to the blob
`origin/main` holds (`be9dd23ad9c865fbcc74ccf20bcc3f633f23845e`), proved
on disk by blob hash, then the pins run. Received strings, copied out of
the run:
| run | row | code | message on the unfixed tree | who really ended the
run |
|:--|:--|:--|:--|:--|
| `deleteMany ['t1'(survives), 'missing'(throws), 't3']` | 2 |
`NOT_ATTEMPTED` | `record 0 failed — unknown error; the batch stopped
there. Set options.continueOnError to process the remaining records.` |
record **1** |
| `batchData delete`, same three rows | 2 | `NOT_ATTEMPTED` | identical
| record **1** |
| `batchData atomic ['t3', 't1'(survives), 'missing'(throws), 't2']` | 0
| `ROLLED_BACK` | `record 1 failed — unknown error` | record **2** |
| same run | 3 | `NOT_ATTEMPTED` | `atomic batch aborted by record 1` |
record **2** |
| `batchData atomic ['t1', 't2'(survives), 't3']` | 0 and 2 |
`ROLLED_BACK` | `record 1 failed — unknown error` | nothing failed at
all |
## The fix
Both builders now call one shared locator, `locateBatchCause`, which
finds the causal row by its recorded **fault**: the row's `errors[]`
entry.
**Why that discriminator cannot drift back the way the boolean did.**
`success` is the envelope's outcome bit and its false arm is open by
construction — it means "this row is not a success", so every new
non-success ending widens it for free, which is exactly what happened.
`errors` is not a second boolean:
- its declared meaning is a failure. `BatchOperationResultSchema.errors`
is documented as *"Array of errors if operation failed"*, and the v17
ADR-0087 migration entry publishes `row.errors?.[0]?.message` /
`row.errors?.[0]?.code` to consumers as that read;
- its contents are a **closed vocabulary**. Every entry must carry an
`ApiError.code` from `StandardErrorCode` union `ERROR_CODE_LEDGER`; an
unregistered code fails `BatchOperationResultSchema.parse`. Giving a
non-fault ending an `errors[]` entry is therefore a ledger widening in
`packages/spec` — which is precisely the step BOTH survivor sites
declined to take, in writing, and the step that would have to be taken
deliberately for this locator to start lying;
- `ApiError.message` is **required**, so a located cause always has
text. The 「unknown error」 fallback is **deleted**, not merely unreached:
the string no longer appears in either message template.
The scan runs from the END of the attempted rows, because a run ends AT
the row it stops on — every stop is a `break` in a loop's `catch`,
immediately after that row was pushed. A fault that does not stop the
run (the `Unknown operation:` arm records one and keeps going) therefore
cannot shadow the row that did.
One ending has no fault to quote at all: an atomic batch aborted by a
lone survivor, where `runAtomicBatch` rolls back on `failed > 0` and
nothing ever threw. There the message names the row that did not succeed
— `record 1 did not succeed` — instead of inventing a failure. That is
the only place `!success` is still read, and it is read for the question
that boolean does answer: "which row stopped this batch committing",
never "which row failed".
## Acceptance
**1. Located by a fault, not by `!success`** — above. **2. The negative
case is pinned** —
`packages/metadata-protocol/src/protocol.batch-causal-row.test.ts`, 8
tests. Its central assertion is an agreement between the message and the
rows beside it, read out of the response rather than hard-coded: the
message names `record N failed` for the N that carries an error,
contains that row's own error text verbatim, and never contains the
string `unknown error`. **3. Both builders** — one locator, two call
sites; neither can be fixed apart from the other. **4. All three bulk
faces:**
| face | arm | covered how |
|:--|:--|:--|
| `batchData` (delete verb) | non-atomic and atomic | **pinned** — it is
one of the two faces that can produce an errors-less non-success row |
| `deleteManyData` | non-atomic | **pinned** — the other such face |
| `deleteManyData` | atomic | **reasoned** — same two builders, same
`runAtomicBatch`; the atomic delete pin runs through `batchData`, whose
loop pushes the identical survivor row |
| `updateManyData` | both | **reasoned, and the reading is asserted** —
`runUpdateManyLoop` has no producer of a non-success row without
`errors` (every push is `success: true` or a `toRowApiError` row), so
`!success` and "carries a fault" still coincide there. The pin asserts
that reading directly: every non-success row in an `updateMany` response
carries an `errors` entry. It shares the two builders, so the
attribution moves with them, which the same test also checks |
## Ablation
Neutralised on the final tree (merge commit `56e5fd90c`), mutation
proved on disk by blob hash `2204cd2 -> be9dd23`, restored, restore
proved: blob back to `2204cd2`, `git diff HEAD` zero bytes, whole-tree
`git status --porcelain` zero lines.
- **5 of the 8 new pins go red**, one per message site plus the two
non-atomic faces.
- **3 stay green** — they are the positive controls: the same assertions
over batches with no survivor in them, so a locator that simply stopped
naming anything could not pass this file.
- **All 5 sibling batch suites stay green (68 tests)** — no existing pin
covered this, which is why it shipped.
## Files outside the declared surface, declared rather than quietly
widened
The dispatch scoped this to `packages/metadata-protocol/src/`. Two files
outside it are in the diff, both mechanical and both demanded by the
repo's own gates for the in-surface change:
- `.changeset/19452-batch-causal-row-located-by-fault.md` — required by
the post-task checklist and by `check:empty-changeset`. Measured rather
than assumed: `@objectstack/metadata-protocol` is not private, its
`files[]` ships `dist`, and the changed symbol is in the built output
(`locateBatchCause` present in `dist/index.js`; a nonsense control
string returns zero from the same grep). `patch`.
- `scripts/engine-double-contract.pinned.json` —
`check:engine-double-contract` exited 1 on the new pin file with its own
prescription, `--write` and commit. The regeneration reports 3 rows
added, 0 lost, and every added row names the new test file (3 lines
match the file name, 3 lines match `.test.ts` — the same count, so no
other file moved).
## Verification
- `dispatch-gates.mjs --commands --repo objectstack-ai/objectstack` on
the merged tree (`56e5fd90c`): **69 families, all 69 run, every one exit
0**. Reconciled with `--ran` carrying exit codes: `69 derived, 69 run, 0
NOT-MEASURED, 0 UNRUN`. Three families first answered exit 3
(PREREQUISITE NOT MET — `dual-build-cjs-loads`, `lean-entry-closure`,
`type-check-debt`); each was discharged by building what it named
(`turbo run build` over all packages, 72/72) and re-running, never by
calling it inapplicable.
- `pnpm --filter @objectstack/metadata-protocol test` — 2653 passed, 19
skipped, 0 failed. `typecheck` — exit 0.
- `pnpm lint` — the whole-repo `eslint . --no-inline-config`, exit 0.
Run in full, so no narrowing needs declaring.
- `origin/main` merged before this reading; the gates above were run on
the merged tree.
## Acceptance notes
Noted here, not filed, per this seat's standing rule that it files
nothing:
- `protocol.batch-not-attempted.test.ts` asserts the causal index with
`expect(message).toContain('1')`. The assertion is satisfied by any `1`
anywhere in the string, so it is much weaker than it reads; it happens
to be correct today. Test-quality observation, no live defect, and this
PR does not touch that file.
- `runBatchDataLoop`'s `Unknown operation:` arm records a
`VALIDATION_FAILED` row per record and keeps going even with
`continueOnError` absent, because nothing is thrown.
`BatchOptionsSchema.continueOnError` declares the opposite default.
`batchData` does not parse the verb at its own door, so an in-process
caller can reach the arm; the effect is a longer `results` array and
nothing else — no write happens and the counters still reconcile.
Reported to the PM as a contract-violation candidate with its seam
rather than filed here.
---
_Generated by [Claude
Code](https://claude.ai/code/session_01NcPSwnmJHczmTu6FG7NMjE)_
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 4fba503 commit a251aaa
4 files changed
Lines changed: 419 additions & 7 deletions
File tree
- .changeset
- packages/metadata-protocol/src
- scripts
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
Lines changed: 324 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 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 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
0 commit comments