Skip to content

Commit 77d1564

Browse files
feat(spec,rest): the record page payload answers the data lock read, so a record page learns a closed record in its one request (#38999)
<!-- claude-projects-attribution: {"github_login":"os-support-ai"} --> _Requested by **Yibo Wang** · [project thread](https://claude.ai/code/project/chan_01BVUyyPie8gHwoPM18iABwB?thread=cmsg_01BVUyyPie8gHwoPM18iABwB12LHqjnJsWFhAJNYDzW4BQ)_ Fixes #38995 Clause-②: yes (widening) Metadata-protocol: added Skills-docs: follow-up #38961 (its scope now includes this part, per the seat's comment on #38961) ## 维护者速读 - 做什么:记录页打开时那一次请求(`POST /data/:object/:id/page`)可以多要一份 `dataLock`,内容和 `GET /data/:object/:id/lock` 对同一个人的回答完全一样(锁没锁、锁来自本记录还是主记录、是已关闭还是审批中)。 - 为什么:控制台要在记录页上看出单据已关闭,又不能多发一次请求(「打开只发一次请求」是已发布的约定,席位裁决 7322183009)。 - 不改什么:审批部分自己的 `lock` 不变;不问就不给、也不去读。 - 后续:#37773 的记录页那一半由 osa-data 从备份分支重新接上。 - 要你做什么:无。 ## What changes - **`@objectstack/spec`, `record-page.zod.ts`.** `RecordPageRequestSchema` gains `dataLock?: boolean`; `RecordPageResponseSchema` gains `dataLock?: RecordLockSchema | RecordPageStreamRefusalSchema`: the refusal arm is the strict `{ error }` the stream parts use, so a part cannot carry a lock answer and a refusal at once (contract review S1). The header's part-to-read table names `dataLock` as `GET /data/{object}/{id}/lock` (ADR-0162 D9). Regenerated: `authorable-surface/api.json`, `declaration-map/api.json`, `docs/references/api/record-page.md`. - **`@objectstack/rest`, `record-page-routes.ts`.** A new `answerDataLock`, run beside the other parts: the data protocol's `getRecordLock({ object, id, environmentId?, context })` as the caller, served through the lock door's own projection (`recordLockAnswer`, `record-lock-routes.ts`), so only `locked`, `lockSource` and `lockReason` are served. No read on the protocol, or a read that answers `undefined`: the door's `501 NOT_IMPLEMENTED` as the part's refusal, never "unlocked". A throw: that read's refusal (`refusalOf`), the other parts still answer. `null` (unreadable): the by-id read's `404 RECORD_NOT_FOUND` as a part refusal; it cannot normally happen, since the record read refuses the whole request first. - **`@objectstack/client`.** `data.recordPage`'s docblock names the part; the types come from the spec. - **Docs.** `content/docs/api/data-api.mdx`, "Record locks": one paragraph on the part. ## Tests - New `packages/rest/src/record-page-data-lock-38995.test.ts` (13 cases): - Parity: for each D9 combination (closed own, closed master, approval own, approval master, unlocked), the part equals what `GET /data/:object/:id/lock` answers over the same protocol, and both doors ask `getRecordLock` with the same caller. The protocol's answer carries extra keys (`field`, `value`, `requestId`); neither door serves them. - A reader whose field projection hides the state field is told `closed_state`, and the part names neither field nor value. - No read / `undefined` → `501` part refusal (the door answers `501` too); a throw → its own refusal with other parts answered; `null` → `404` part refusal. - The approval part's `lock` stays its own read (a closed record the approvals read calls unlocked stays unlocked there). - Not asked → no part and no read; a non-boolean flag → `400 VALIDATION_FAILED` before anything is read. - Red first: with this PR's spec and test on the base's `record-page-routes.ts`, 11 failed and 2 passed (the two that pass are "not asked" and the strict-body refusal). On this head: 13 of 13. Existing `record-page-routes.test.ts` and `record-lock-routes.test.ts` still pass (66 of 66 across the three files). - `packages/spec/src/api/record-page.test.ts`: the flag takes a boolean only; the part takes each lock arm and a refusal, and refuses a locked answer without its source or reason, an unlocked one carrying them, unknown kinds, extra keys and `locked: null`; it also refuses a lock answer carrying a refusal (`{ locked: false, error }`) and a refusal with anything beside it (red on the non-strict arm, 1 of 17). 17 of 17. - The engine's half (combinations, hidden state field, master lines) is already pinned in `plugin-approvals/src/record-lock-one-read-37770.integration.test.ts`. ## Acceptance notes - Done-when 3: #37773's record-page half (backup branch `claude/issue-37773-record-page-half-wip`) calls the hook result `dataLock`; it can now seed it from `payload.dataLock` with no second request, so the five one-request pins stay as they are. The re-application is osa-data's, outside this PR. - `Metadata-protocol: added`: the authorable-surface baseline gains `RecordPageRequest:dataLock` and `RecordPageResponse:dataLock`, as for #37767's step-log keys. Semver `minor` on spec, rest and client. - File surface: claim 7322363888 plus amendment 7322798819 (the docs paragraph). `ui/**` is untouched. - Owed: L2 at the contract-review tier (the diff edits non-test `packages/spec/src/**`), by hand-off to os-justin, then L1. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01WGufzhiGvpFMQX4XptMEtQ <!-- signed: repo:objectstack@os-support-ai · os-support-ai · session_01WGufzhiGvpFMQX4XptMEtQ --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 202cc28 commit 77d1564

10 files changed

Lines changed: 388 additions & 13 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
'@objectstack/spec': minor
3+
'@objectstack/rest': minor
4+
'@objectstack/client': minor
5+
---
6+
7+
feat(spec,rest): the record page payload answers the data lock read, so a record page learns a closed record in its one request
8+
9+
Clause-②: yes (widening)
10+
Metadata-protocol: added
11+
12+
Widened: `RecordPageRequestSchema` gains an optional `dataLock: boolean`, and `RecordPageResponseSchema` an optional `dataLock` part: the record's lock exactly as `GET /api/v1/data/{object}/{id}/lock` answers the caller (`RecordLockSchema`: `locked`, and on a locked record `lockSource` and `lockReason`, ADR-0162 D9), or that read's refusal as a part error. `POST /api/v1/data/{object}/{id}/page` (`@objectstack/rest`) answers it from the data protocol's `getRecordLock`, served through the lock door's own projection, so nothing about the state field or value rides along; a deployment whose data engine has no lock read answers the part `501 NOT_IMPLEMENTED`, never "unlocked". A body that does not ask gets no part and the read is not asked. The approval part's own `lock` is the approvals-family read and is unchanged. `client.data.recordPage` takes and returns the new part through the spec types. Nothing an author writes in metadata changes.

‎content/docs/api/data-api.mdx‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,13 @@ parameter.
673673
A deployment whose data engine has no lock read answers both doors
674674
`501 NOT_IMPLEMENTED`, never "unlocked".
675675

676+
The record page payload (`POST /data/:object/:id/page`) answers the same read
677+
when its body asks `"dataLock": true`: its `dataLock` part is exactly what
678+
`GET /data/:object/:id/lock` answers the caller, or that read's refusal as a
679+
part error (`501 NOT_IMPLEMENTED` included), so a record page learns that a
680+
record is closed without a second request. The payload's `approvals.lock` is
681+
the approvals-family read and is unchanged.
682+
676683
---
677684

678685
## Analytics

‎packages/client/src/index.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8703,8 +8703,9 @@ export class ObjectStackClient {
87038703
* declared by `RecordPageRequestSchema` / `RecordPageResponseSchema`
87048704
* (`@objectstack/spec/api`): the record, the count and first rows of each
87058705
* related list `request.related` names, and — when asked — the record's
8706-
* field projection, its record-grained access decisions and its approval
8707-
* state, in one round trip. Each part is the answer of the read it replaces
8706+
* field projection, its record-grained access decisions, its approval
8707+
* state and its data lock (`request.dataLock`, the answer of
8708+
* `data.getRecordLock`, #38995), in one round trip. Each part is the answer of the read it replaces
87088709
* for the same caller; a part whose read refused carries `error` instead.
87098710
* A record that is absent or that the caller cannot read rejects with the
87108711
* record read's 404.
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* [#38995] The record page payload's `dataLock` part: the data layer's one lock
5+
* read (ADR-0162 D9, `GET /data/:object/:id/lock`), answered inside the one
6+
* request a record page opens with, so the page learns a closed record without
7+
* a second request.
8+
*
9+
* What each pin holds:
10+
*
11+
* - the part is the data protocol's `getRecordLock`, asked as the caller, and
12+
* it answers exactly what the single lock door answers that caller, for each
13+
* combination the read names (parity, both doors driven over one protocol);
14+
* - nothing the engine might add rides along: the part is the door's own
15+
* projection, field by field, so a reader whose field permissions hide the
16+
* state field is told `closed_state` and nothing about the field or value;
17+
* - a protocol without the read is the door's `501`, carried as the part's
18+
* refusal, never "unlocked"; a throwing read is that read's refusal, and the
19+
* other parts still answer;
20+
* - the approval part's own `lock` is a different read and is unchanged;
21+
* - a body that does not ask for the part gets none, and asks nothing.
22+
*
23+
* The engine's half (the combinations, the hidden state field, the master
24+
* line) is `plugin-approvals/src/record-lock-one-read-37770.integration.test.ts`.
25+
*/
26+
27+
import { describe, it, expect, vi } from 'vitest';
28+
import { RecordPageResponseSchema } from '@objectstack/spec/api';
29+
import { RestServer } from './rest-server.js';
30+
// The server reaches `@objectstack/objectql` only through a dynamic import inside a
31+
// handler; paid here at module scope so the first load lands during collection,
32+
// not inside a clocked window (`pnpm check:test-source-alias`).
33+
import '@objectstack/objectql';
34+
35+
const ACCOUNT = {
36+
name: 'account',
37+
label: 'Account',
38+
fields: { id: { name: 'id', type: 'text' }, name: { name: 'name', type: 'text' }, stage: { name: 'stage', type: 'text' } },
39+
};
40+
const CTX = { userId: 'u1', permissions: ['member_default'] };
41+
const RECORD = { id: 'a1', name: 'Acme' };
42+
43+
function mockServer() {
44+
const noop = () => { /* routes are driven directly, never through the adapter */ };
45+
return {
46+
get: noop, post: noop, put: noop, delete: noop, patch: noop, use: noop,
47+
listen: async () => { /* never started */ },
48+
close: async () => { /* never started */ },
49+
};
50+
}
51+
52+
function mockRes() {
53+
const res: any = {
54+
statusCode: 200,
55+
header: vi.fn(function (this: any) { return this; }),
56+
setHeader: vi.fn(function (this: any) { return this; }),
57+
json: vi.fn(function (this: any, body: any) { this._body = body; return this; }),
58+
status: vi.fn(function (this: any, code: number) { this.statusCode = code; return this; }),
59+
};
60+
return res;
61+
}
62+
63+
interface BootOpts {
64+
/** The protocol's lock read; absent leaves the protocol without one. */
65+
getRecordLock?: (req: any) => Promise<unknown>;
66+
security?: unknown;
67+
approvals?: unknown;
68+
}
69+
70+
function boot(opts: BootOpts = {}) {
71+
const getData = vi.fn(async () => ({ object: 'account', id: 'a1', record: RECORD }));
72+
const findData = vi.fn(async (req: any) => ({ object: req.object, records: [], total: 0 }));
73+
const getRecordLock = opts.getRecordLock ? vi.fn(opts.getRecordLock) : undefined;
74+
const protocol: any = {
75+
getDiscovery: vi.fn().mockResolvedValue({
76+
version: 'v0', routes: { data: '', metadata: '', ui: '', auth: '/auth' },
77+
}),
78+
getMetaTypes: vi.fn().mockResolvedValue([]),
79+
getMetaItems: vi.fn().mockResolvedValue({ items: [ACCOUNT] }),
80+
getMetaItem: vi.fn(async ({ name }: any) => (name === 'account' ? { type: 'object', name, item: ACCOUNT } : null)),
81+
getData,
82+
findData,
83+
...(getRecordLock ? { getRecordLock } : {}),
84+
};
85+
const rest = new RestServer(mockServer() as any, protocol as any, { api: { requireAuth: false } } as any);
86+
(rest as any).resolveExecCtx = async () => CTX;
87+
(rest as any).resolveSecurityService = async () => opts.security;
88+
(rest as any).approvalsServiceProvider = opts.approvals === undefined ? undefined : async () => opts.approvals;
89+
rest.registerRoutes();
90+
const routes = (rest as any).getRoutes();
91+
const page = routes.find((r: any) => r.method === 'POST' && r.path === '/api/v1/data/:object/:id/page');
92+
const door = routes.find((r: any) => r.method === 'GET' && r.path === '/api/v1/data/:object/:id/lock');
93+
const drive = async (route: any, req: Record<string, unknown>) => {
94+
if (!route) throw new Error('route not registered');
95+
const res = mockRes();
96+
await route.handler({ query: {}, headers: {}, ...req } as any, res);
97+
return { status: res.statusCode, body: res.json.mock.calls.at(-1)?.[0] };
98+
};
99+
const callPage = (body: unknown) => drive(page, { method: 'POST', params: { object: 'account', id: 'a1' }, body });
100+
const callDoor = () => drive(door, { method: 'GET', params: { object: 'account', id: 'a1' }, body: {} });
101+
return { callPage, callDoor, getRecordLock, getData };
102+
}
103+
104+
describe('[#38995] the record page\'s dataLock part is the data lock read', () => {
105+
// What the protocol's `getRecordLock` answers in each combination ADR-0162
106+
// D9 names. `field` / `value` / `requestId` stand for anything an engine
107+
// might add; neither door may serve them.
108+
const CASES: Array<[string, unknown]> = [
109+
['a record closed by its own state', { locked: true, lockSource: 'record', lockReason: 'closed_state', field: 'stage', value: 'done' }],
110+
['a line of a closed master', { locked: true, lockSource: 'master', lockReason: 'closed_state' }],
111+
['a record held by its own approval', { locked: true, lockSource: 'record', lockReason: 'approval', requestId: 'ar1' }],
112+
['a line of a held master', { locked: true, lockSource: 'master', lockReason: 'approval' }],
113+
['an unlocked record', { locked: false }],
114+
];
115+
116+
for (const [name, served] of CASES) {
117+
it(`${name}: the part equals GET /data/:object/:id/lock for the same caller`, async () => {
118+
const { callPage, callDoor, getRecordLock } = boot({ getRecordLock: async () => served });
119+
const page = await callPage({ dataLock: true });
120+
expect(page.status).toBe(200);
121+
const composite = RecordPageResponseSchema.parse(page.body.data).dataLock;
122+
123+
const single = await callDoor();
124+
expect(single.status).toBe(200);
125+
126+
expect(composite).toEqual(single.body.data);
127+
expect(Object.keys(composite as object).sort()).toEqual(
128+
(served as any).locked ? ['lockReason', 'lockSource', 'locked'] : ['locked'],
129+
);
130+
// Both doors asked the one read, as the caller, for this record.
131+
expect(getRecordLock!.mock.calls.map(([req]) => req)).toEqual([
132+
{ object: 'account', id: 'a1', context: CTX },
133+
{ object: 'account', id: 'a1', context: CTX },
134+
]);
135+
});
136+
}
137+
138+
it('a reader whose field permissions hide the state field is told closed_state, and nothing about the field', async () => {
139+
const getRecordFieldAccess = vi.fn().mockResolvedValue({
140+
object: 'account', recordId: 'a1', readable: ['id', 'name'], editable: [], writable: false,
141+
});
142+
const { callPage } = boot({
143+
security: { getRecordFieldAccess },
144+
getRecordLock: async () => ({ locked: true, lockSource: 'record', lockReason: 'closed_state', field: 'stage', value: 'done' }),
145+
});
146+
const page = await callPage({ dataLock: true, fieldAccess: true });
147+
expect(page.status).toBe(200);
148+
const data = RecordPageResponseSchema.parse(page.body.data);
149+
expect((data.fieldAccess as any).readable).not.toContain('stage');
150+
expect(data.dataLock).toEqual({ locked: true, lockSource: 'record', lockReason: 'closed_state' });
151+
expect(JSON.stringify(data.dataLock)).not.toMatch(/stage|done/);
152+
});
153+
154+
it('a protocol without the read is the door\'s 501, carried as the part\'s refusal, never "unlocked"', async () => {
155+
const { callPage, callDoor } = boot();
156+
const page = await callPage({ dataLock: true });
157+
expect(page.status).toBe(200);
158+
const data = RecordPageResponseSchema.parse(page.body.data);
159+
expect(data.dataLock).toMatchObject({ error: { code: 'NOT_IMPLEMENTED', status: 501 } });
160+
expect(data.record).toEqual(RECORD);
161+
const door = await callDoor();
162+
expect([door.status, door.body?.error?.code]).toEqual([501, 'NOT_IMPLEMENTED']);
163+
});
164+
165+
it('a read that answers nothing (undefined) is the same 501', async () => {
166+
const { callPage } = boot({ getRecordLock: async () => undefined });
167+
const data = RecordPageResponseSchema.parse((await callPage({ dataLock: true })).body.data);
168+
expect(data.dataLock).toMatchObject({ error: { code: 'NOT_IMPLEMENTED', status: 501 } });
169+
});
170+
171+
it('a read that refuses carries its own refusal, and the other parts answer', async () => {
172+
const getRecordFieldAccess = vi.fn().mockResolvedValue({
173+
object: 'account', recordId: 'a1', readable: ['id', 'name', 'stage'], editable: ['name'], writable: true,
174+
});
175+
const { callPage } = boot({
176+
security: { getRecordFieldAccess },
177+
getRecordLock: async () => {
178+
throw Object.assign(new Error('[Security] Access denied: read on account'), { code: 'PERMISSION_DENIED', status: 403 });
179+
},
180+
});
181+
const page = await callPage({ dataLock: true, fieldAccess: true });
182+
expect(page.status).toBe(200);
183+
const data = RecordPageResponseSchema.parse(page.body.data);
184+
expect(data.dataLock).toMatchObject({ error: { code: 'PERMISSION_DENIED', status: 403 } });
185+
expect((data.fieldAccess as any).readable).toContain('stage');
186+
});
187+
188+
it('a record the read calls unreadable (null) is the by-id read\'s 404, as a part refusal', async () => {
189+
const { callPage } = boot({ getRecordLock: async () => null });
190+
const data = RecordPageResponseSchema.parse((await callPage({ dataLock: true })).body.data);
191+
expect(data.dataLock).toMatchObject({ error: { code: 'RECORD_NOT_FOUND', status: 404 } });
192+
});
193+
194+
it('the approval part keeps its own lock: a closed record the approvals read calls unlocked stays unlocked there', async () => {
195+
const approvals = {
196+
listRequests: vi.fn().mockResolvedValue([]),
197+
getRecordLock: vi.fn().mockResolvedValue({ locked: false }),
198+
};
199+
const { callPage } = boot({
200+
approvals,
201+
getRecordLock: async () => ({ locked: true, lockSource: 'record', lockReason: 'closed_state' }),
202+
});
203+
const data = RecordPageResponseSchema.parse((await callPage({ dataLock: true, approvals: true })).body.data);
204+
expect(data.approvals).toEqual({ requests: [], lock: { locked: false } });
205+
expect(data.dataLock).toEqual({ locked: true, lockSource: 'record', lockReason: 'closed_state' });
206+
});
207+
208+
it('a body that does not ask for the part gets none, and the read is not asked', async () => {
209+
const { callPage, getRecordLock } = boot({ getRecordLock: async () => ({ locked: false }) });
210+
const page = await callPage({});
211+
expect(page.status).toBe(200);
212+
expect(page.body.data).not.toHaveProperty('dataLock');
213+
expect(getRecordLock).not.toHaveBeenCalled();
214+
});
215+
216+
it('a dataLock that is not a boolean is refused by the strict body, before anything is read', async () => {
217+
const { callPage, getData, getRecordLock } = boot({ getRecordLock: async () => ({ locked: false }) });
218+
const page = await callPage({ dataLock: 'yes' });
219+
expect(page.status).toBe(400);
220+
expect(page.body.error.code).toBe('VALIDATION_FAILED');
221+
expect(getData).not.toHaveBeenCalled();
222+
expect(getRecordLock).not.toHaveBeenCalled();
223+
});
224+
});

0 commit comments

Comments
 (0)