Skip to content

Commit 94843b8

Browse files
fix(app-shell): the draft bar publishes the reloaded draft after a conflict "reload" (#37648) (#37715)
<!-- claude-projects-attribution: {"github_login":"os-sales"} --> _Requested by **Sales** · [project thread](https://claude.ai/code/project/chan_01D5bWFY3rFzgJcytD5gD4Uu?thread=cmsg_01D5bWFY3rFzgJcytD5gD4UuDJAbBe3iTrFcF7GZrucZ32)_ Fixes #37648 The three review follow-ups PR #37637 left on card #37440: N6 of #37387's review (the runtime draft bar keeps a stale token after "reload") and N1, N2 of contract review 7198547999 (the `publishItem` pin docs). ## What changes - **Draft bar after a conflict "reload" (`@object-ui/app-shell`).** A view settings Save refused because the draft moved opens the draft-conflict dialog. On "reload", `ViewConfigPanel` re-read the stored view into its buffer, but `handleViewConfigSave` answers `false` on `'reloaded'`, so no `savedSignal` reached `RuntimeDraftBar`, which kept the draft version it read on open. Its Publish was then pinned to a draft that had moved, refused `409 METADATA_CONFLICT`, until the panel was reopened. - The guard's reload callback is now `handleConflictReload`, which marks the next stored-view read as the reload's. That read, once it installs a buffer, hands the bar the version it found: `reloadedSignal` (bumped), `reloadedDraftVersion` (the `If-Match` of the read's pin; absent when the read named none) and `reloadedHasDraft` (whether a pending draft was served). - `RuntimeDraftBar` takes them as it takes `savedSignal`: the Publish is pinned to `reloadedDraftVersion`, and the indicator follows `reloadedHasDraft`. The bar never re-reads for it (#36350: a re-read would pin whatever draft is pending by then, which the panel never showed). - A reload whose read fails installs no buffer and hands the bar nothing, so its Publish stays pinned to the draft it showed and is refused, never sent unpinned. - **`client.meta.publishItem` (`@objectstack/client`).** The `parentVersion` JSDoc now says an empty string pins nothing and is not sent, as `draftVersion`'s does (N1). JSDoc only; `metaPublishHeaders` already treated `''` so. - **Client SDK page (`content/docs/api/client-sdk.mdx`).** The `publishItem` example now shows a publish pinned with `draftVersion` (a draft save's receipt `version`) and `parentVersion` (the version the earlier publish returned), with the refusal and the empty-string rule (N2). ## Decisions made here - **Only `ViewConfigPanel` is changed.** It is the one host of `RuntimeDraftBar` with a draft-conflict guard (`useDraftSaveGuard`); `ReportConfigPanel` and `ReportView` host the bar without one, so no "reload" reaches them. - **The bar takes the panel's read, not a read of its own,** for the #36350 reason above; and only the read that answers a "reload" is handed over, so an ordinary open, publish or discard behaves as before. ## Verification - New tests, red on `main` and green here (4 red with the two source files reverted): - `ViewConfigPanel.pinnedDraftSave-36137.test.tsx` (2): another writer's draft moves past the panel's receipt, the next Save is refused, "reload" hands the bar that writer's draft version with `reloadedHasDraft: true` (the landed-save signal unmoved); a reload that finds no pending draft hands `reloadedHasDraft: false` and no version. - `RuntimeDraftBar.test.tsx` (3): the Publish after `reloadedSignal` sends `draftVersion` = the reloaded version with no second draft read; a reload with no draft takes the indicator down; the mount's own signal changes nothing (control). - `vitest` over every `ViewConfigPanel*`, `RuntimeDraftBar*`, `ObjectView*`, `DraftConflictDialog*` and `viewCacheInvalidation*` test: 1,059 green. `tsc` for `app-shell` (`tsconfig.json` and `tsconfig.test.json`) on the changed files: clean; `eslint`: 0 errors. `check-doc-anchors`: pass. - `overlap-check`: exit 0 (no open PR touches these files). - Premise: static read on `main` (`handleViewConfigSave`'s `'reloaded'` → `false`; the bar sets its pin only on open and on `savedSignal`), and the red tests above. Clause-②: no Metadata-protocol: none Skills-docs: none ## Human Minimum - [x] 1. Schemas start as Zod; `packages/spec` holds no runtime logic. (No spec change.) - [x] 2. Every name I added uses the casing for its kind. - [x] 3. No formatter ran; `eslint` on the changed files passes (the full `pnpm lint` runs in CI). - [x] 4. One issue, one branch off an up-to-date `main`, one pull request. --- _Generated by [Claude Code](https://claude.ai/code/session_01L6bhgUuauSMJu1nVSQRQri)_ 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01L6bhgUuauSMJu1nVSQRQri Co-authored-by: Claude <noreply@anthropic.com>
1 parent 579b857 commit 94843b8

7 files changed

Lines changed: 230 additions & 3 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@object-ui/app-shell': patch
3+
'@objectstack/client': patch
4+
---
5+
6+
fix(app-shell): the view settings draft bar publishes the reloaded draft after a conflict is resolved with "reload"
7+
8+
When a view settings Save was refused because the draft moved and the author chose "reload" in the conflict dialog, the panel re-read the stored view but the draft bar kept the draft version it had read before the reload. Its Publish was then pinned to a draft that no longer existed and was refused until the panel was reopened. The panel now hands the bar the version its reload read found (`RuntimeDraftBar`'s new `reloadedSignal`, `reloadedDraftVersion` and `reloadedHasDraft`), so the Publish promotes the draft now shown, and a reload that found no pending draft takes the bar's indicator down. A reload whose read fails hands the bar nothing, so its Publish stays pinned and is refused rather than sent unpinned.
9+
10+
`client.meta.publishItem`'s `parentVersion` JSDoc now says an empty string pins nothing and is not sent, as `draftVersion`'s does, and the client SDK page shows a publish pinned with `draftVersion` and `parentVersion`.

‎content/docs/api/client-sdk.mdx‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,19 @@ const view = await client.meta.getItem('view', 'crm_lead.pipeline');
234234
const draft = await client.meta.getItem('object', 'account', { packageId: 'com.acme.crm', state: 'draft' });
235235
const draftItem = draft.item as { _diagnostics?: { valid: boolean } } | undefined;
236236
console.log(draftItem?._diagnostics?.valid);
237-
await client.meta.publishItem('object', 'account', { message: 'go live' });
237+
const published = await client.meta.publishItem('object', 'account', { message: 'go live' });
238+
// Pin what you publish (ADR-0008): `draftVersion` is the draft you mean, your own
239+
// draft save's receipt `version` (or the draft read's), never a fresh re-read;
240+
// `parentVersion` is the live row you mean to replace, here the one the publish
241+
// above wrote (`null`: no live row is stored). A draft or live row that moved
242+
// since is refused 409 METADATA_CONFLICT and nothing is published. An empty
243+
// string pins nothing.
244+
const saved = await client.meta.saveItem('object', 'account', { ...accountSchema, label: 'Customer' }, { mode: 'draft' });
245+
await client.meta.publishItem('object', 'account', {
246+
message: 'rename',
247+
draftVersion: saved.version,
248+
parentVersion: published.version,
249+
});
238250
await client.meta.rollbackItem('object', 'account', 3);
239251
// The authoring trail, like getHistory and getAudit: without studio.access or manage_metadata → 403 FORBIDDEN (setup.access alone is refused)
240252
const diff = await client.meta.diffItem('object', 'account', { from: 2, to: 5 });

‎packages/client/src/index.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2798,7 +2798,9 @@ export class ObjectStackClient {
27982798
* `409 METADATA_CONFLICT` and nothing is promoted. Omitted, the
27992799
* publish replaces whatever is live, as before. It pins the live
28002800
* row only; the draft's own pin is `draftVersion`, and the two
2801-
* may be sent together (the door judges the draft first).
2801+
* may be sent together (the door judges the draft first). An
2802+
* empty string pins nothing and is not sent, as for
2803+
* `draftVersion`.
28022804
*/
28032805
parentVersion?: string | null;
28042806
},

‎ui/packages/app-shell/src/views/RuntimeDraftBar.test.tsx‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,71 @@ describe('RuntimeDraftBar — Publish is pinned to the draft it showed (objectst
290290
await waitFor(() => expect(metadataClient.publish).toHaveBeenCalledWith('view', 'my_view'));
291291
});
292292
});
293+
294+
/**
295+
* objectstack#37648 — after the host resolves a refused save with "reload"
296+
* (`reloadedSignal`), the draft the bar shows is the one the host's reload
297+
* read found: the Publish is pinned to that read's version, not to the draft
298+
* the bar read before the reload, and never to a re-read of the bar's own.
299+
*/
300+
describe('RuntimeDraftBar — after the host\'s reload the Publish is pinned to the reloaded draft (objectstack#37648)', () => {
301+
afterEach(() => {
302+
vi.restoreAllMocks();
303+
});
304+
305+
it('takes the reloaded draft\'s version, with no re-read', async () => {
306+
const metadataClient = makeMetadataClient({ item: { columns: ['name'] }, version: 'hmac-sha256:draft-v1' });
307+
const { rerender } = render(
308+
<RuntimeDraftBar type="view" name="my_view" metadataClient={metadataClient} reloadedSignal={0} />,
309+
);
310+
await screen.findByTestId('runtime-draft-publish');
311+
// Another writer moved the draft (v2); the host's save was refused and the author chose "reload".
312+
rerender(
313+
<RuntimeDraftBar
314+
type="view"
315+
name="my_view"
316+
metadataClient={metadataClient}
317+
reloadedSignal={1}
318+
reloadedDraftVersion="hmac-sha256:moved-v2"
319+
reloadedHasDraft
320+
/>,
321+
);
322+
fireEvent.click(await screen.findByTestId('runtime-draft-publish'));
323+
324+
await waitFor(() =>
325+
expect(metadataClient.publish).toHaveBeenCalledWith('view', 'my_view', { draftVersion: 'hmac-sha256:moved-v2' }),
326+
);
327+
expect(metadataClient.getDraft).toHaveBeenCalledTimes(1);
328+
});
329+
330+
it('a reload that found no pending draft takes the indicator down', async () => {
331+
const metadataClient = makeMetadataClient({ item: { columns: ['name'] }, version: 'hmac-sha256:draft-v1' });
332+
const { rerender } = render(
333+
<RuntimeDraftBar type="view" name="my_view" metadataClient={metadataClient} reloadedSignal={0} />,
334+
);
335+
await screen.findByTestId('runtime-draft-publish');
336+
rerender(
337+
<RuntimeDraftBar type="view" name="my_view" metadataClient={metadataClient} reloadedSignal={1} reloadedHasDraft={false} />,
338+
);
339+
await waitFor(() => expect(screen.queryByTestId('runtime-draft-bar')).toBeNull());
340+
});
341+
342+
it('the mount\'s own signal changes nothing (control)', async () => {
343+
const metadataClient = makeMetadataClient({ item: { columns: ['name'] }, version: 'hmac-sha256:draft-v1' });
344+
render(
345+
<RuntimeDraftBar
346+
type="view"
347+
name="my_view"
348+
metadataClient={metadataClient}
349+
reloadedSignal={3}
350+
reloadedDraftVersion="hmac-sha256:other"
351+
reloadedHasDraft={false}
352+
/>,
353+
);
354+
fireEvent.click(await screen.findByTestId('runtime-draft-publish'));
355+
await waitFor(() =>
356+
expect(metadataClient.publish).toHaveBeenCalledWith('view', 'my_view', { draftVersion: 'hmac-sha256:draft-v1' }),
357+
);
358+
});
359+
});
360+

‎ui/packages/app-shell/src/views/RuntimeDraftBar.tsx‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,21 @@ export interface RuntimeDraftBarProps {
7373
* host that does not know it), the Publish after the save goes unpinned.
7474
*/
7575
savedDraftVersion?: string;
76+
/**
77+
* objectstack#37648 — monotonic counter the host bumps when it replaced its
78+
* buffer with a fresh read of the stored item (a refused save resolved with
79+
* "reload" in the draft-conflict dialog), with {@link reloadedDraftVersion}
80+
* and {@link reloadedHasDraft} from THAT read. The draft the bar now shows
81+
* is the one that read found, so its Publish is pinned to that read's
82+
* version, not to the draft the bar read before the reload (which the
83+
* reload exists because it moved). ⛔ Never a re-read of the bar's own: it
84+
* would pin whatever draft is pending by then, which the host never showed.
85+
*/
86+
reloadedSignal?: number;
87+
/** The draft version the host's reload read (absent: that read named none, and the Publish goes unpinned). */
88+
reloadedDraftVersion?: string;
89+
/** Whether the host's reload read found a pending draft. */
90+
reloadedHasDraft?: boolean;
7691
}
7792

7893
/**
@@ -100,6 +115,9 @@ export function RuntimeDraftBar({
100115
onAfterChange,
101116
savedSignal,
102117
savedDraftVersion,
118+
reloadedSignal,
119+
reloadedDraftVersion,
120+
reloadedHasDraft,
103121
}: RuntimeDraftBarProps) {
104122
const locale = useMetadataLocale();
105123
const refusalTitles = useRefusalTitles();
@@ -165,6 +183,19 @@ export function RuntimeDraftBar({
165183
}
166184
}, [savedSignal, savedDraftVersion]);
167185

186+
// objectstack#37648 — the host re-read the stored item into its buffer (a
187+
// conflict resolved with "reload"): the draft shown is the one that read
188+
// found, and the Publish is pinned to it. Skip the initial mount.
189+
const lastReloadedSignal = useRef(reloadedSignal);
190+
useEffect(() => {
191+
if (reloadedSignal === lastReloadedSignal.current) return;
192+
lastReloadedSignal.current = reloadedSignal;
193+
if (reloadedSignal) {
194+
shownDraftVersionRef.current = reloadedDraftVersion;
195+
setHasDraft(reloadedHasDraft === true);
196+
}
197+
}, [reloadedSignal, reloadedDraftVersion, reloadedHasDraft]);
198+
168199
const handlePublish = useCallback(async () => {
169200
if (!name) return;
170201
setBusy(true);

‎ui/packages/app-shell/src/views/ViewConfigPanel.pinnedDraftSave-36137.test.tsx‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,3 +465,74 @@ describe('the draft bar\'s Publish after a view settings save is pinned to that
465465
cleanup();
466466
});
467467
});
468+
469+
/**
470+
* objectstack#37648 — a refused save resolved with "reload" re-reads the
471+
* stored view into the panel; the draft that read found is the one the draft
472+
* bar shows from then on, so the panel hands the bar that read's version
473+
* (`reloadedSignal`, `reloadedDraftVersion`, `reloadedHasDraft`). Before, the
474+
* bar kept the version from before the reload, and its Publish was refused
475+
* until the panel was reopened.
476+
*/
477+
describe('after a conflict resolved with "reload", the draft bar takes the reloaded draft (objectstack#37648)', () => {
478+
it('another writer\'s draft: the bar is handed its version', async () => {
479+
const door = viewDoor();
480+
const { outcomes } = mountPanel(door);
481+
await loadedAndEdited();
482+
await act(async () => {
483+
fireEvent.click(saveButton());
484+
});
485+
await waitFor(() => expect(outcomes).toEqual(['saved']));
486+
const mine = door.draftOf(VIEW_ID)!.version;
487+
expect(bar.props?.reloadedSignal).toBe(0);
488+
489+
// Another writer's draft save moves the draft past the panel's receipt.
490+
await persistRuntimeMetadata('view', VIEW_ID, buildViewConfigSaveBody(OBJECT, { id: VIEW_ID, label: 'Theirs', type: 'grid' }), {
491+
metadataClient: door.client,
492+
});
493+
const theirs = door.draftOf(VIEW_ID)!.version;
494+
expect(theirs).not.toBe(mine);
495+
496+
fireEvent.click(screen.getByTestId('mock-inspector-label'));
497+
await act(async () => {
498+
fireEvent.click(saveButton());
499+
});
500+
await waitFor(() => expect(screen.getByTestId('draft-conflict-dialog')).toBeTruthy());
501+
await act(async () => {
502+
fireEvent.click(screen.getByTestId('draft-conflict-reload'));
503+
});
504+
await waitFor(() => expect(outcomes).toEqual(['saved', 'reloaded']));
505+
await waitFor(() => expect(bar.props?.reloadedSignal).toBe(1));
506+
expect(bar.props?.reloadedDraftVersion).toBe(theirs);
507+
expect(bar.props?.reloadedHasDraft).toBe(true);
508+
// The landed-save signal is not moved by the reload.
509+
expect(bar.props?.savedSignal).toBe(1);
510+
cleanup();
511+
});
512+
513+
it('a reload that finds no pending draft says so', async () => {
514+
const door = viewDoor();
515+
const { outcomes } = mountPanel(door);
516+
await loadedAndEdited();
517+
// Another writer's draft lands after the load: the save, pinned to "no draft", is refused.
518+
await persistRuntimeMetadata('view', VIEW_ID, buildViewConfigSaveBody(OBJECT, { id: VIEW_ID, label: 'Theirs', type: 'grid' }), {
519+
metadataClient: door.client,
520+
});
521+
await act(async () => {
522+
fireEvent.click(saveButton());
523+
});
524+
await waitFor(() => expect(screen.getByTestId('draft-conflict-dialog')).toBeTruthy());
525+
// The other draft is published before the author answers.
526+
await door.client.publish('view', VIEW_ID);
527+
expect(door.draftOf(VIEW_ID)).toBeUndefined();
528+
await act(async () => {
529+
fireEvent.click(screen.getByTestId('draft-conflict-reload'));
530+
});
531+
await waitFor(() => expect(outcomes).toEqual(['reloaded']));
532+
await waitFor(() => expect(bar.props?.reloadedSignal).toBe(1));
533+
expect(bar.props?.reloadedHasDraft).toBe(false);
534+
expect(bar.props?.reloadedDraftVersion).toBeUndefined();
535+
cleanup();
536+
});
537+
});
538+

‎ui/packages/app-shell/src/views/ViewConfigPanel.tsx‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,19 @@ export function ViewConfigPanel({ open, onClose, mode = 'edit', activeView, obje
205205
setAuthoredReads((n) => n + 1);
206206
onAfterChange?.();
207207
}, [onAfterChange]);
208+
// objectstack#37648 — a refused save resolved with "reload" re-reads the
209+
// stored view into the buffer (`handleAfterChange`); the draft that read
210+
// finds is the one the draft bar now shows, so the read hands the bar its
211+
// version (`reloadedDraft`) and the bar's Publish is pinned to it.
212+
const conflictReloadRef = useRef(false);
213+
const [reloadedDraft, setReloadedDraft] = useState<{ signal: number; version?: string; hasDraft: boolean }>({
214+
signal: 0,
215+
hasDraft: false,
216+
});
217+
const handleConflictReload = useCallback(() => {
218+
conflictReloadRef.current = true;
219+
handleAfterChange();
220+
}, [handleAfterChange]);
208221
// objectstack#36137 — the draft-version guard of this panel's buffer
209222
// (`useDraftSaveGuard`): the pin of the draft it was loaded from, then of
210223
// each save it sent, and the conflict dialog a refused save opens.
@@ -214,14 +227,15 @@ export function ViewConfigPanel({ open, onClose, mode = 'edit', activeView, obje
214227
forget: forgetDraftVersion,
215228
heldDraftVersion,
216229
dialog: draftConflictDialog,
217-
} = useDraftSaveGuard(metadataClient, handleAfterChange);
230+
} = useDraftSaveGuard(metadataClient, handleConflictReload);
218231
useEffect(() => {
219232
if (!open || !readsAuthored) return;
220233
const id = activeView.id;
221234
let cancelled = false;
222235
setSeed({ for: id, state: 'loading' });
223236
(async () => {
224237
let next: AuthoredSeed;
238+
let foundDraft = false;
225239
try {
226240
// objectstack#36137 — the served draft read is the envelope, so
227241
// its `version` pins this buffer's saves (`draftPinOf`), at the
@@ -231,6 +245,7 @@ export function ViewConfigPanel({ open, onClose, mode = 'edit', activeView, obje
231245
metadataClient.getDraft('view', id),
232246
]);
233247
const servedDraft = unwrapDraftBody(draftAnswer);
248+
foundDraft = servedDraft !== null;
234249
const authored = authoredBuffer('view', layers as AuthoredLayers, servedDraft);
235250
if (authored.kind === 'unserved') {
236251
next = { for: id, state: 'refused', message: tStudio('engine.studio.refusal.draftUnreadable', locale) };
@@ -266,6 +281,21 @@ export function ViewConfigPanel({ open, onClose, mode = 'edit', activeView, obje
266281
// next save sends; a read that installs none leaves nothing held.
267282
if (next.state === 'ready') recordDraftRead('view', id, null, next.pin);
268283
else forgetDraftVersion();
284+
// objectstack#37648 — this read answers a conflict's "reload": the
285+
// draft bar takes the draft it found. A read that could not
286+
// install a buffer hands the bar nothing, so its Publish stays
287+
// pinned to the draft it showed and is refused, never unpinned.
288+
if (conflictReloadRef.current) {
289+
conflictReloadRef.current = false;
290+
if (next.state === 'ready') {
291+
const pin = next.pin;
292+
setReloadedDraft((prev) => ({
293+
signal: prev.signal + 1,
294+
version: pin && 'ifMatch' in pin ? pin.ifMatch : undefined,
295+
hasDraft: foundDraft,
296+
}));
297+
}
298+
}
269299
setSeed(next);
270300
})();
271301
return () => { cancelled = true; };
@@ -506,6 +536,9 @@ export function ViewConfigPanel({ open, onClose, mode = 'edit', activeView, obje
506536
dirty={isDirty}
507537
savedSignal={savedSignal}
508538
savedDraftVersion={savedDraftVersion}
539+
reloadedSignal={reloadedDraft.signal}
540+
reloadedDraftVersion={reloadedDraft.version}
541+
reloadedHasDraft={reloadedDraft.hasDraft}
509542
onAfterChange={handleAfterChange}
510543
/>
511544
)}

0 commit comments

Comments
 (0)