Skip to content

Commit 7f2442b

Browse files
committed
fix(runtime,metadata-protocol): a thrown fallback seed apply reports its counts; pin a pass-2 throw
Contract-review round 1: the runtime fallback's thrown receipt carries inserted/updated and names the stopped object; the landed describe says it is empty when no dataset finished; a pass-2 throw lists every dataset as finished; the older catch comment agrees with the new one. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGufzht5n1PKh96TqTfrvV
1 parent 4c02bdd commit 7f2442b

7 files changed

Lines changed: 95 additions & 20 deletions

File tree

‎.changeset/30429-seed-receipt-landed-on-throw.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Metadata-protocol: semantic
1111

1212
- **`@objectstack/spec`: `seedApplied.landed`** (both publish responses) now says what it covers on a load that threw part-way (`success: false` with `error`): only the datasets that FINISHED before the throw. The dataset that threw is not listed (its rows are in `inserted` / `updated`, its object in `error`). Absent `landed` now means the apply failed before any load ran, or the producer reports no ids; a throw before any dataset finished reports `landed: []`.
1313
- **`@objectstack/metadata-protocol`**: `SeedLoadPartialWrites` gains `finished`, the finished datasets' `object` / `landedIds` / `insertedIds` in load order, and `applySeedBodies`' catch folds it through the same `seedAppliedLanded` projection (same two lists, same 200 cap) the returned path uses. `seedLoadPartialWrites` and `SeedLoadPartialWrites` are exported from the package root.
14-
- **`@objectstack/runtime`**: the package-publish door's fallback seed apply reads the same `seedLoadPartialWrites` off a thrown load and reports the same `landed`.
14+
- **`@objectstack/runtime`**: the package-publish door's fallback seed apply reads the same `seedLoadPartialWrites` off a thrown load and reports the same `landed`, plus the `inserted` / `updated` counts the response schema requires (it reported neither before) and the object whose seed stopped the load in `error`, as `metadata-protocol`'s apply does.
1515
- Why the dataset that threw is left out: its ids are only partly known when it throws. A batched insert whose response was lost may have committed rows whose ids never came back, so a list of the ids the loader did see would read as complete while missing rows.
1616

1717
Supersedes, in the same release: `.changeset/30348-seed-receipt-landed-ids.md` says absent `landed` means "not reported (a load that threw, or a producer that reports no ids)". After this change a load that threw part-way reports `landed` for its finished datasets; absent now means no load ran, or a producer that reports no ids.

‎content/docs/references/api/protocol.mdx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2546,7 +2546,7 @@ The authoring surface of a metadata write. Closed: `copilot` is an AI assistant'
25462546
| **updated** | `integer` | ✅ | Rows updated by the externalId-keyed upsert. |
25472547
| **error** | `string` | optional | Single failure message, present when the seed apply threw before the loader ran (including "no readable seed bodies"). |
25482548
| **errors** | `any[]` | optional | Per-record failures reported by the seed loader. Present only when the loader ran and returned a non-empty error list. |
2549-
| **landed** | `{ object: string; inserted: object; matched: object }[]` | optional | Per object, the stored rows the seed landed on: `inserted` (created by this publish) and `matched` (pre-existing rows a seed key matched), each capped at `SEED_APPLIED_LANDED_IDS_CAP` (200) ids with `total` and `truncated` stating any cut. One entry per object the loader ran, empty lists included. When the load threw part-way (`success: false` with `error`), it lists only the datasets that FINISHED before the throw; the dataset that threw is not listed (its rows are in the counts, its object in `error`). ABSENT means "not reported" — the apply failed before any load ran (see `error`), or the producer reports no ids — never "no rows landed". |
2549+
| **landed** | `{ object: string; inserted: object; matched: object }[]` | optional | Per object, the stored rows the seed landed on: `inserted` (created by this publish) and `matched` (pre-existing rows a seed key matched), each capped at `SEED_APPLIED_LANDED_IDS_CAP` (200) ids with `total` and `truncated` stating any cut. One entry per object the loader ran, empty lists included. When the load threw part-way (`success: false` with `error`), it lists only the datasets that FINISHED before the throw, and is EMPTY when none had; the dataset that threw is not listed (its rows are in the counts, its object in `error`). ABSENT means "not reported" — the apply failed before any load ran (see `error`), or the producer reports no ids — never "no rows landed". |
25502550

25512551
### Nested Shape: `PublishMetaItemResponse.materializeApplied`
25522552

@@ -2647,7 +2647,7 @@ The authoring surface of a metadata write. Closed: `copilot` is an AI assistant'
26472647
| **error** | `string` | optional | Single failure message, present when the apply failed before the loader ran (including "no readable seed bodies"). When the failure is the seed bodies' own schema refusal, this is a one-sentence headline and the per-path detail rides `issues[]`. |
26482648
| **errors** | `any[]` | optional | Per-record failures reported by the seed loader, plus any seed-body read failures. May be present and empty on a clean load. |
26492649
| **issues** | `{ path: string; message: string; code?: string }[]` | optional | Structured spec-validation findings behind `error`, present when the apply was refused by the seed bodies' own schema — the declared 422 `seedRequestValidationError` mints. The per-path detail lives HERE, once; `error` stays a one-sentence headline. Absent on non-validation failures (driver faults, unreadable bodies), whose whole story is `error` / `errors[]`. |
2650-
| **landed** | `{ object: string; inserted: object; matched: object }[]` | optional | Per object, the stored rows the seed landed on: `inserted` (created by this publish) and `matched` (pre-existing rows a seed key matched), each capped at `SEED_APPLIED_LANDED_IDS_CAP` (200) ids with `total` and `truncated` stating any cut. One entry per object the loader ran, empty lists included. When the load threw part-way (`success: false` with `error`), it lists only the datasets that FINISHED before the throw; the dataset that threw is not listed (its rows are in the counts, its object in `error`). ABSENT means "not reported" — the apply failed before any load ran (see `error`), or the producer reports no ids — never "no rows landed". |
2650+
| **landed** | `{ object: string; inserted: object; matched: object }[]` | optional | Per object, the stored rows the seed landed on: `inserted` (created by this publish) and `matched` (pre-existing rows a seed key matched), each capped at `SEED_APPLIED_LANDED_IDS_CAP` (200) ids with `total` and `truncated` stating any cut. One entry per object the loader ran, empty lists included. When the load threw part-way (`success: false` with `error`), it lists only the datasets that FINISHED before the throw, and is EMPTY when none had; the dataset that threw is not listed (its rows are in the counts, its object in `error`). ABSENT means "not reported" — the apply failed before any load ran (see `error`), or the producer reports no ids — never "no rows landed". |
26512651

26522652
### Nested Shape: `PublishPackageDraftsResponse.materializeApplied`
26532653

‎packages/metadata-protocol/src/protocol-publish-batch.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,11 @@ export async function applySeedBodies(
155155
// The counters describe what was WRITTEN. The loader writes dataset
156156
// by dataset and stops at the one that throws; the rows of the
157157
// datasets ahead of it stay written (nothing rolls them back), so the
158-
// receipt carries the loader's own count of them and names the seed
159-
// that stopped it. No reading means the throw came before the loader
160-
// wrote anything — the request parse, or the loader's import.
158+
// receipt carries the loader's own count of them (plus any rows the
159+
// dataset that threw wrote first) and names the seed that stopped
160+
// it. No reading means no load ran — the request parse refused, or
161+
// the loader's import failed — so nothing was written; a reading
162+
// with zero counts is a load that threw before writing a row.
161163
const written = partialWritesOf?.(e);
162164
const failure = clientFacingFailureText(e, 'seed apply failed');
163165
return {
@@ -172,8 +174,9 @@ export async function applySeedBodies(
172174
// ids a reader clearing a partial publish's sample data needs. The
173175
// dataset that threw is not listed (its ids are only partly known,
174176
// see `SeedLoadPartialWrites.finished`); its rows are in the
175-
// counts above and its object in `error`. No reading means no
176-
// load ran, so `landed` stays absent: "not reported".
177+
// counts above and its object in `error`. Empty when the throw
178+
// came before any dataset finished; absent when there is no
179+
// reading (no load ran, see above): "not reported".
177180
...(written ? { landed: seedAppliedLanded(written.finished) } : {}),
178181
// [#10524] A DECLARED refusal's structured findings ride the
179182
// receipt beside the headline `error` — the message is a

‎packages/metadata-protocol/src/seed-loader-deferred-failure.test.ts‎

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
22

33
import { describe, it, expect, vi } from 'vitest';
4-
import { SeedLoaderService } from './seed-loader.js';
4+
import { SeedLoaderService, seedLoadPartialWrites } from './seed-loader.js';
5+
import { seedAppliedLanded } from './seed-applied-landed.js';
56
import type { IDataEngine, IMetadataService } from '@objectstack/spec/contracts';
67
import { assertEngineDeleteDispatch, assertEngineUpdateDispatch, assertEngineFindOnePredicate } from '@objectstack/metadata-core';
78

@@ -300,3 +301,58 @@ describe('seed deferred back-fill failure is reported, not swallowed (framework#
300301
expect(result.summary.totalErrored).toBe(0);
301302
});
302303
});
304+
305+
/**
306+
* [#30429] A load that throws in PASS 2 — after every dataset finished — is
307+
* still a thrown load, and its partial-writes reading lists every dataset as
308+
* finished: no `failedObject`, and the receipt's `landed` (the same
309+
* `seedAppliedLanded` projection the publish catch uses) names every row the
310+
* load wrote.
311+
*
312+
* Pass 2 guards its own driver traffic (a failed back-fill write is counted,
313+
* a failed lookup probe is a miss), so the throw is staged at the one call in
314+
* it that is not guarded: the `logger.error` its back-fill failure reports
315+
* through — a log transport that fails while pass 2 reports. What is pinned is
316+
* the loader's state at that point, not the transport.
317+
*/
318+
describe('a load that throws in pass 2 reports every dataset as finished (#30429)', () => {
319+
it('finished lists both datasets with their store ids; no failed object; landed names every row', async () => {
320+
const { engine, store } = createFaithfulEngine();
321+
const metadata = createMetadata();
322+
const realUpdate = (engine.update as any).getMockImplementation();
323+
(engine.update as any).mockImplementation(async (obj: string, data: any, opts: any) => {
324+
if (obj === 'audit_department') throw new Error('fetch failed');
325+
return realUpdate(obj, data, opts);
326+
});
327+
const logger = createLogger();
328+
logger.error.mockImplementation(() => { throw new Error('the log transport is down'); });
329+
330+
const err = await new SeedLoaderService(engine, metadata, logger)
331+
.load({ seeds: SEEDS, config: CONFIG })
332+
.then(() => undefined, (e: unknown) => e);
333+
334+
expect((err as Error)?.message).toBe('the log transport is down');
335+
// Positive control: the throw came from pass 2's back-fill report, after
336+
// both datasets wrote their row.
337+
expect((engine.update as any).mock.calls.some(([obj]: [string]) => obj === 'audit_department')).toBe(true);
338+
const deptIds = (store.audit_department ?? []).map((r) => String(r.id));
339+
const workerIds = (store.audit_worker ?? []).map((r) => String(r.id));
340+
expect(deptIds).toHaveLength(1);
341+
expect(workerIds).toHaveLength(1);
342+
const partial = seedLoadPartialWrites(err)!;
343+
expect(partial).toEqual({
344+
inserted: 2,
345+
updated: 0,
346+
finished: [
347+
{ object: 'audit_department', landedIds: deptIds, insertedIds: deptIds },
348+
{ object: 'audit_worker', landedIds: workerIds, insertedIds: workerIds },
349+
],
350+
});
351+
expect('failedObject' in partial).toBe(false);
352+
const list = (ids: string[]) => ({ ids, total: ids.length, truncated: false });
353+
expect(seedAppliedLanded(partial.finished)).toEqual([
354+
{ object: 'audit_department', inserted: list(deptIds), matched: list([]) },
355+
{ object: 'audit_worker', inserted: list(workerIds), matched: list([]) },
356+
]);
357+
});
358+
});

‎packages/runtime/src/domains/packages-seed-apply-landed-thrown.test.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ async function publishDrafts(opts: Parameters<typeof makeDoor>[0] = {}) {
8888

8989
const list = (ids: string[]) => ({ ids, total: ids.length, truncated: false });
9090

91-
describe('#30429 · the runtime fallback reports a thrown load\'s finished datasets', () => {
91+
describe('#30429 · the runtime fallback reports a thrown load\'s finished datasets and counts', () => {
9292
it('positive control: a returned load lists the rows, created vs matched', async () => {
9393
const { seedApplied, insertedIds } = await publishDrafts();
9494

@@ -106,6 +106,13 @@ describe('#30429 · the runtime fallback reports a thrown load\'s finished datas
106106
expect(insertedIds).toEqual(['id_Apollo']);
107107
expect(seedApplied?.success).toBe(false);
108108
expect(seedApplied?.landed).toEqual([{ object: 'project', inserted: list(insertedIds), matched: list(['cust_gemini']) }]);
109+
// The counts the schema requires, read off the same partial-writes
110+
// record: the finished dataset's one created row. The object that
111+
// stopped the load is named; the driver's sentence is withheld.
112+
expect(seedApplied?.inserted).toBe(1);
113+
expect(seedApplied?.updated).toBe(0);
114+
expect(seedApplied?.error).toBe(`seed apply stopped at the seed for object '${BROKEN}': seed apply failed`);
115+
expect(JSON.stringify(seedApplied)).not.toContain('unreadable');
109116
});
110117

111118
it('a failure before any load ran leaves `landed` absent: "not reported"', async () => {

‎packages/runtime/src/domains/packages.ts‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2127,14 +2127,22 @@ export async function handlePackagesRequest(deps: DomainHandlerDeps, path: strin
21272127
`[handlePackages] seed apply failed: ${e?.message ?? e}`,
21282128
);
21292129
// [#30429] A load that threw mid-batch still reports
2130-
// the rows of the datasets that finished, through the
2131-
// same reader and projection as `metadata-protocol`'s
2132-
// catch; no reading (the throw came before a load
2133-
// ran) leaves `landed` absent.
2130+
// what it wrote, read off the error exactly as
2131+
// `metadata-protocol`'s catch reads it: the counts
2132+
// (finished datasets plus the one that threw), the
2133+
// object that stopped it, and `landed` for the
2134+
// datasets that finished, through the same
2135+
// projection. No reading (no load ran) means nothing
2136+
// was written: zero counts, `landed` absent.
21342137
const written = seedLoadPartialWrites(e);
2138+
const failure = clientFacingFailureText(e, 'seed apply failed');
21352139
(result as any).seedApplied = {
21362140
success: false,
2137-
error: clientFacingFailureText(e, 'seed apply failed'),
2141+
inserted: written?.inserted ?? 0,
2142+
updated: written?.updated ?? 0,
2143+
error: written?.failedObject
2144+
? `seed apply stopped at the seed for object '${written.failedObject}': ${failure}`
2145+
: failure,
21382146
...(written ? { landed: seedAppliedLanded(written.finished) } : {}),
21392147
// [#10524] The declared 422's structured
21402148
// findings ride beside the headline `error` —

‎packages/spec/src/api/protocol.zod.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,8 +1495,9 @@ const SEED_APPLIED_LANDED_DESCRIPTION =
14951495
+ '`matched` (pre-existing rows a seed key matched), each capped at '
14961496
+ '`SEED_APPLIED_LANDED_IDS_CAP` (200) ids with `total` and `truncated` stating any cut. One '
14971497
+ 'entry per object the loader ran, empty lists included. When the load threw part-way '
1498-
+ '(`success: false` with `error`), it lists only the datasets that FINISHED before the throw; '
1499-
+ 'the dataset that threw is not listed (its rows are in the counts, its object in `error`). '
1498+
+ '(`success: false` with `error`), it lists only the datasets that FINISHED before the throw, '
1499+
+ 'and is EMPTY when none had; the dataset that threw is not listed (its rows are in the counts, '
1500+
+ 'its object in `error`). '
15001501
+ 'ABSENT means "not reported" — the apply failed before any load ran (see `error`), or the '
15011502
+ 'producer reports no ids — never "no rows landed".';
15021503

@@ -1533,9 +1534,9 @@ const SEED_APPLIED_LANDED_DESCRIPTION =
15331534
* Each list carries at most {@link SEED_APPLIED_LANDED_IDS_CAP} (200) ids
15341535
* and declares a cut with `truncated: true` beside the uncut `total`.
15351536
* [#30429] A load that threw part-way still reports `landed` for the
1536-
* datasets that FINISHED before the throw; the dataset that threw is not
1537-
* listed (its ids are only partly known), though its rows are in the
1538-
* counts. Absent `landed` means "not reported" (no load ran, or the
1537+
* datasets that FINISHED before the throw, and an empty `landed` when the
1538+
* throw came before any had; the dataset that threw is not listed (its ids
1539+
* are only partly known), though its rows are in the counts. Absent `landed` means "not reported" (no load ran, or the
15391540
* producer reports no ids), never "nothing landed". The same
15401541
* key, same schema, rides {@link PublishPackageDraftsResponseSchema}'s
15411542
* `seedApplied`. Accepted cost (objectstack-ai/cloud#3598 ruling A): row

0 commit comments

Comments
 (0)