Skip to content

Commit 1fed7aa

Browse files
committed
fix(spec): a connector read serves only the declared nested keys, and a save of the served form keeps the rest
Inside retryConfig and each actions element, a connector read now serves only the keys those shapes declare; each withheld position is named in redactedKeys and the stored row is unchanged. The save door's carry-forward follows an actions element by its key, so a reordered list keeps each element's value. getPublished redacts the authored document alone and serves the metadata layer's bookkeeping beside it unchanged. What a save accepts is unchanged. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FEkQ5mgePB9NQ4PNhvYVxk
1 parent 3ac22c9 commit 1fed7aa

8 files changed

Lines changed: 583 additions & 19 deletions
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
'@objectstack/spec': patch
3+
'@objectstack/metadata-protocol': patch
4+
'@objectstack/metadata': patch
5+
---
6+
7+
fix(spec): a connector read serves only the keys `retryConfig` and each `actions` element declare, and a save of the served form keeps the rest
8+
9+
Clause-②: no
10+
Metadata-protocol: none
11+
12+
The built-in `connector` read-path redactor now serves, inside `retryConfig` and inside each element of `actions`, only the keys those shapes declare. Every metadata read exit that serves a stored connector applies it. Each withheld position is named in `redactedKeys` (an `actions` element by its index), and the stored row is never changed.
13+
14+
A save of the served form keeps every withheld nested value: the save door carries each one forward from the stored row. An `actions` element is followed by its `key`, so a reordered list keeps each element's value on that element; for every carried-forward position, an array element with no `id` is now identified by its `key`. A `retryConfig` or an `actions` element the author removes is removed.
15+
16+
`MetadataManager.getPublished` now hands the type's redactor the authored document alone and serves the metadata layer's own bookkeeping beside the answer unchanged (`packageId`, `state`, and the publish keys).
17+
18+
What a save accepts is unchanged.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
//
3+
// The write-path inverse of the connector read projection, for the keys a
4+
// connector does not declare: a save of the served form keeps what the read
5+
// withheld (top level, `retryConfig`, and an `actions` element addressed by
6+
// its `key`), and a container the author removed is never minted back.
7+
//
8+
// Every value below is an obviously-fake placeholder.
9+
10+
import { describe, it, expect } from 'vitest';
11+
import { carryForwardRedactedValues, redactMetadataItem } from './metadata-redaction.js';
12+
13+
const P = (n: number) => `TEST_PLACEHOLDER_UNDECLARED_${n}`;
14+
15+
const stored = {
16+
name: 'vendor_catalog',
17+
label: 'Vendor catalog',
18+
type: 'api',
19+
apiKey: P(1),
20+
retryConfig: { strategy: 'exponential_backoff', secret: P(2) },
21+
actions: [
22+
{ key: 'get_invoice', label: 'Get invoice', token: P(3) },
23+
{ key: 'list_invoices', label: 'List invoices' },
24+
],
25+
};
26+
27+
/** What a read exit serves for `stored`: the projection, as a client would send it back. */
28+
const served = () => JSON.parse(JSON.stringify(redactMetadataItem('connector', stored))) as Record<string, any>;
29+
30+
describe('carry-forward of the keys a connector does not declare', () => {
31+
it('the served form withholds them (positive control for every case below)', () => {
32+
const body = served();
33+
expect(JSON.stringify(body)).not.toContain('TEST_PLACEHOLDER_UNDECLARED_');
34+
expect(body.retryConfig).toEqual({ strategy: 'exponential_backoff' });
35+
expect(body.actions[0]).toEqual({ key: 'get_invoice', label: 'Get invoice' });
36+
});
37+
38+
it('a save of the served form keeps every withheld value, the edited label included', () => {
39+
const out = carryForwardRedactedValues('connector', { ...served(), label: 'Vendor catalog (edited)' }, stored);
40+
expect(out).toEqual({ ...stored, label: 'Vendor catalog (edited)' });
41+
});
42+
43+
it('an `actions` element is followed by its `key` when the author reorders the list', () => {
44+
const body = served();
45+
body.actions.reverse();
46+
const out = carryForwardRedactedValues('connector', body, stored) as Record<string, any>;
47+
expect(out.actions).toEqual([
48+
{ key: 'list_invoices', label: 'List invoices' },
49+
{ key: 'get_invoice', label: 'Get invoice', token: P(3) },
50+
]);
51+
});
52+
53+
it('the author\'s own value at a withheld nested position wins', () => {
54+
const body = served();
55+
body.retryConfig.secret = 'author-value';
56+
const out = carryForwardRedactedValues('connector', body, stored) as Record<string, any>;
57+
expect(out.retryConfig.secret).toBe('author-value');
58+
});
59+
60+
it('a removed `retryConfig` or `actions` element is not minted back', () => {
61+
const body = served();
62+
delete body.retryConfig;
63+
body.actions = body.actions.filter((a: { key: string }) => a.key !== 'get_invoice');
64+
const out = carryForwardRedactedValues('connector', body, stored) as Record<string, any>;
65+
expect(out).not.toHaveProperty('retryConfig');
66+
expect(out.actions).toEqual([{ key: 'list_invoices', label: 'List invoices' }]);
67+
expect(out.apiKey).toBe(P(1));
68+
});
69+
70+
it('two `actions` elements sharing a `key` cannot be told apart: neither is carried', () => {
71+
const twin = { ...stored, actions: [stored.actions[0], { key: 'get_invoice', label: 'Twin' }] };
72+
const body = JSON.parse(JSON.stringify(redactMetadataItem('connector', twin)));
73+
const out = carryForwardRedactedValues('connector', body, twin) as Record<string, any>;
74+
expect(JSON.stringify(out.actions)).not.toContain(P(3));
75+
});
76+
});

‎packages/metadata-protocol/src/metadata-redaction.ts‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,17 @@ function isPlainRecord(value: unknown): value is Record<string, unknown> {
151151
return !!value && typeof value === 'object' && !Array.isArray(value);
152152
}
153153

154-
/** An element's identity for an array hop: a non-empty string `id`, or none. */
154+
/**
155+
* An element's identity for an array hop: a non-empty string `id`; for an
156+
* element with no `id`, a non-empty string `key` (a connector action's machine
157+
* name); otherwise none.
158+
*/
155159
function identityOf(element: unknown): string | undefined {
156160
if (!isPlainRecord(element)) return undefined;
157161
const id = element.id;
158-
return typeof id === 'string' && id !== '' ? id : undefined;
162+
if (typeof id === 'string' && id !== '') return id;
163+
const key = element.key;
164+
return typeof key === 'string' && key !== '' ? key : undefined;
159165
}
160166

161167
/**
@@ -190,7 +196,9 @@ function elementWithIdentity(
190196
* into an identity, and every body — served and incoming — is then walked by
191197
* that identity, never by the index. Two kinds of identity:
192198
*
193-
* - **the element's own `id`** (`{ elementId }`) — a flow node;
199+
* - **the element's own `id`** (`{ elementId }`) — a flow node — or, for an
200+
* element with no `id`, its own `key` — a connector action, whose `key` is
201+
* its machine name;
194202
* - **for an element with no `id`, the identified element below it on the
195203
* same path** (`{ anchor }`, #20590). A `parallel` block's branch carries no
196204
* `id`, and a credential inside one sits at
@@ -201,8 +209,8 @@ function elementWithIdentity(
201209
* is that walk: the hops from inside the element down to, and including,
202210
* the first identified element beneath it.
203211
*
204-
* An array hop that reaches neither — an element with no `id` and no
205-
* identified element below it on the path, or an `id` shared with a sibling —
212+
* An array hop that reaches neither — an element with no `id` or `key` and no
213+
* identified element below it on the path, or an identity shared with a sibling —
206214
* resolves to nothing, and the path is skipped.
207215
*/
208216
type PathHop =

‎packages/metadata/src/metadata-manager.ts‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ import type {
8787
import { EndpointMatcher } from './endpoint-matcher.js';
8888
// [#5309] The stored ENVELOPE / authored BODY split — peeled before any spec
8989
// schema parses a stored row. See `stored-envelope.ts`.
90-
import { peelStoredEnvelope } from './stored-envelope.js';
90+
import { peelStoredEnvelope, STORED_ENVELOPE_KEYS } from './stored-envelope.js';
9191
// [#13913] Registry-free expansion of an aggregated view container reached
9292
// through THIS manager's own store — see `getViewsByObject`.
9393
import { expandRuntimeViewContainer } from './view-container-expansion.js';
@@ -2285,14 +2285,33 @@ export class MetadataManager implements IMetadataService {
22852285
return this.redactServedBody(type, body);
22862286
}
22872287

2288-
/** [#20552] The per-type read-path redaction, applied to one served body. */
2288+
/**
2289+
* [#20552] The per-type read-path redaction, applied to one served body.
2290+
*
2291+
* The body can carry this layer's own bookkeeping beside the authored
2292+
* document (`packageId`, `state`, and the publish keys: the fallback serves
2293+
* the stored row itself, and a publish snapshot copies it whole). Those keys
2294+
* are not the type's vocabulary ({@link STORED_ENVELOPE_KEYS}), and a
2295+
* redactor that withholds every key its type does not declare would withhold
2296+
* them too. So the redactor is handed the authored document alone, and the
2297+
* bookkeeping is served beside its answer unchanged.
2298+
*/
22892299
private redactServedBody(type: string, body: unknown): unknown {
22902300
if (!body || typeof body !== 'object' || Array.isArray(body)) return body;
22912301
// Keyed on the singular type name (Prime Directive #3); a plural spelling folds first.
22922302
const redactor = getMetadataTypeRedactor(canonicalMetadataServiceType(type));
22932303
if (!redactor) return body;
2294-
const result = redactor(body as Record<string, unknown>);
2295-
return result.redactedKeys.length > 0 ? result.item : body;
2304+
const record = body as Record<string, unknown>;
2305+
const bookkeeping: Record<string, unknown> = {};
2306+
const authored: Record<string, unknown> = {};
2307+
for (const [key, value] of Object.entries(record)) {
2308+
if ((STORED_ENVELOPE_KEYS as readonly string[]).includes(key)) bookkeeping[key] = value;
2309+
else authored[key] = value;
2310+
}
2311+
const peeled = Object.keys(bookkeeping).length > 0;
2312+
const result = redactor(peeled ? authored : record);
2313+
if (result.redactedKeys.length === 0) return body;
2314+
return peeled ? { ...result.item, ...bookkeeping } : result.item;
22962315
}
22972316

22982317
// ==========================================
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
//
3+
// `MetadataManager.getPublished` serves a body that can carry this layer's own
4+
// bookkeeping (`packageId`, `state`, the publish keys) beside the authored
5+
// document. The type's read-path redactor judges the authored document alone:
6+
// a key the type does not declare is withheld, the bookkeeping is served
7+
// unchanged. Measured with the built-in `connector` redactor, which withholds
8+
// every key a connector does not declare.
9+
//
10+
// Every value below is an obviously-fake placeholder.
11+
12+
import { describe, it, expect, vi } from 'vitest';
13+
import { MetadataManager } from './metadata-manager.js';
14+
import { MemoryLoader } from './loaders/memory-loader.js';
15+
import { DEFAULT_METADATA_TYPE_REGISTRY } from '@objectstack/spec/kernel';
16+
17+
vi.mock('@objectstack/core', async (orig) => ({
18+
...((await orig()) as object),
19+
createLogger: () => ({ info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }),
20+
}));
21+
22+
const SECRET = 'TEST_PLACEHOLDER_PUBLISHED_UNDECLARED';
23+
const authored = { label: 'Vendor', type: 'api', retryConfig: { maxAttempts: 2, secret: SECRET } };
24+
25+
function manager(): MetadataManager {
26+
const m = new MetadataManager({ formats: ['json'], loaders: [new MemoryLoader()] });
27+
m.setTypeRegistry(DEFAULT_METADATA_TYPE_REGISTRY);
28+
return m;
29+
}
30+
31+
describe('getPublished: the redactor judges the authored body, the bookkeeping is served as stored', () => {
32+
it('a never-published row (the row itself is served): undeclared key withheld, packageId and state kept', async () => {
33+
const m = manager();
34+
await m.register('connector', 'vendor', { name: 'vendor', ...authored, apiKey: SECRET, packageId: 'com.acme.x', state: 'draft' });
35+
const served = (await m.getPublished('connector', 'vendor')) as Record<string, unknown>;
36+
expect(JSON.stringify(served)).not.toContain(SECRET);
37+
expect(served).toEqual({ name: 'vendor', label: 'Vendor', type: 'api', retryConfig: { maxAttempts: 2 }, packageId: 'com.acme.x', state: 'draft' });
38+
});
39+
40+
it('a published snapshot that copied the row whole: the same answer', async () => {
41+
const m = manager();
42+
await m.register('connector', 'vendor', { name: 'vendor', ...authored, packageId: 'com.acme.x', state: 'draft' });
43+
await m.publishPackage('com.acme.x', { publishedBy: 'admin' });
44+
const served = (await m.getPublished('connector', 'vendor')) as Record<string, unknown>;
45+
expect(JSON.stringify(served)).not.toContain(SECRET);
46+
expect(served.packageId).toBe('com.acme.x');
47+
expect(served).toHaveProperty('state');
48+
expect(served.retryConfig).toEqual({ maxAttempts: 2 });
49+
});
50+
51+
it('control: a body with nothing to withhold is served by reference-equal content, bookkeeping included', async () => {
52+
const m = manager();
53+
const row = { name: 'clean', label: 'Clean', type: 'api', packageId: 'com.acme.x', state: 'draft' };
54+
await m.register('connector', 'clean', row);
55+
expect(await m.getPublished('connector', 'clean')).toEqual(row);
56+
});
57+
});

0 commit comments

Comments
 (0)