Skip to content
Merged
27 changes: 27 additions & 0 deletions .changeset/34352-seal-default-chain-column.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
'@objectstack/spec': minor
---

feat(spec)!: a sealed object that names no `lifecycle.seal.chainField` is refused at parse when its default chain column `tenant_id` cannot chain its rows

Clause-②: no (narrowing)
Metadata-protocol: narrowed

<!-- adr-0087: registered seal-default-chain-column-refused -->

**BREAKING** accept-set narrowing, shipped as `minor`. A `lifecycle.seal` that names no `chainField` chains each row by the default column `tenant_id` (ADR-0154 D10, note on `chainField`). That default was never judged at authoring. It is now judged by the same rules as a named `chainField`, and the object is refused with one located issue at `lifecycle.seal` in three cases. What run time did with each before:

- (a) `tenant_id` is not a field of the object. The platform injects `organization_id`, never `tenant_id`, so it must be declared. Before: the object parsed, and the sealer reported it at `error` and sealed none of its rows.
- (b) `tenant_id` has a value that is not one text on every store (a number, boolean, date, datetime, JSON-shaped or multi-value column). Before: the object parsed, and the sealer sealed some or all of its rows (every row whose value the driver returns as a non-string) into the deployment chain with no report.
- (c) `tenant_id` is listed in `lifecycle.seal.valueFields`. Before: the object parsed and was sealed per organization, with the committed column's value carried in clear as the chain in every checkpoint.

The judgement reads the object's own declared fields plus the columns the platform injects. A `tenant_id` that reaches the object only through an `objectExtensions` entry does not count: such an object is refused at parse, although the sealer, which reads the folded field map, would have sealed it. Declare the column on the object itself.

The refusal fires wherever `ObjectSchema` parses: `ObjectSchema.create()` and `defineStack()` (strict by default) throw when a code-authored stack's config module is evaluated, and `os validate`, `os build` and the metadata save door refuse it. A definition that reaches the registry without the parse, including a stored `sys_metadata` row loaded at boot, still registers, and the sealer still treats it as before.

- FROM: `{ name: 'ai_audit_log', fields: { id, created_at, payload }, lifecycle: { class: 'audit', retention: { maxAge: '90d' }, seal: { valueFields: ['payload'] } } }` parsed, and its rows were not sealed.
- TO: the same object is refused at `lifecycle.seal`: "`lifecycle.seal` names no `chainField`, so the sealer chains each row by the default chain column `tenant_id`, which is not a field of this object."

Not moved: an object that declares a text or lookup `tenant_id`, and an object whose `chainField` names a column it has, parse as before.

The fix: declare a text or lookup `tenant_id` field that holds each row's organization id, or name the chain column, such as the injected tenant column: `seal: { valueFields: ['payload'], chainField: 'organization_id' }`. Removing `tenant_id` from `valueFields` (case (c)) keeps every chain: nothing is re-sealed and `os audit verify` stays ok, since each earlier checkpoint is checked against the columns it was signed with. A fix that changes a row's chain value on an object that already has checkpoints (case (b), or naming a different `chainField`) re-seals the last sealed window, `os audit verify` reports it `listed_twice`, and verifying one organization with `--org` reads the rows sealed before the change as late and unsealed.
14 changes: 10 additions & 4 deletions content/docs/data-modeling/objects.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,16 @@ lifecycle: {
datetime, JSON-shaped or multi-value column, the injected `updated_at`
included), `id` or `created_at` (they order the rows), and a column
`valueFields` also lists (the chain value travels in clear in every
checkpoint). Name a text or lookup column holding the organization id. An object that names no chain column and has
no `tenant_id` field still parses, but it is not sealed: the sealer reports
it at `error`. So is an object chained by `organization_id` that its
deployment declares platform-global, which withholds that column. A row whose chain column is empty is
checkpoint). Name a text or lookup column holding the organization id. An object that names no chain column is
chained by `tenant_id`, and that column is held to the same rules: an object
with no `tenant_id` field (the platform never injects one), with a
`tenant_id` whose value is not text, or with `tenant_id` in `valueFields` is
refused at parse time, at `lifecycle.seal`. Declare a text or lookup
`tenant_id`, or name a chain column such as `organization_id`. An object
chained by `organization_id` that its deployment declares platform-global,
which withholds that column, still parses but is not sealed: the sealer
reports it at `error`, as it does any definition that reaches the registry
without the parse. A row whose chain column is empty is
sealed with the rows about no organization. `os audit verify --org <id>`
reads the same column, so it reports that organization's rows whichever
column holds it. The chain column is fixed once the object has
Expand Down
2 changes: 1 addition & 1 deletion content/docs/references/data/object.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ const result = ApiMethod.parse(data);
| Property | Type | Required | Description |
| :--- | :--- | :--- | :--- |
| **valueFields** | `string[]` | ✅ | Columns that enter each sealed row as salted commitments instead of in clear, so a checkpoint never carries their values; every other declared column enters in clear. Each must name a declared field other than `id` and `created_at`, which order the leaves. |
| **chainField** | `string` | optional | The column whose value names the organization chain each sealed row joins (default `tenant_id`); a row whose value is empty seals into the deployment chain. It must name a declared field or a column the platform injects, such as the tenant column `organization_id`, whose value is one text on every store (not a number, boolean, date, datetime, JSON or multi-value column); not `id` or `created_at`, which order the leaves; and not a `valueFields` member. Fixed once the object has checkpoints. |
| **chainField** | `string` | optional | The column whose value names the organization chain each sealed row joins (default `tenant_id`); a row whose value is empty seals into the deployment chain. It must name a declared field or a column the platform injects, such as the tenant column `organization_id`, whose value is one text on every store (not a number, boolean, date, datetime, JSON or multi-value column); not `id` or `created_at`, which order the leaves; and not a `valueFields` member. Absent, the default `tenant_id` is held to the same rules, and the registry does not inject it: a seal that names no chain column needs a declared text or lookup `tenant_id` field. Fixed once the object has checkpoints. |


---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,4 +350,44 @@ describe('changing chainField on an object that already has checkpoints (ADR-015
expect(scoped.rows.map((r) => [r.chain, r.row, r.verdict, r.sealed])).toEqual([['org:org_b', 'r1', 'late', false]]);
expect(scoped.ok).toBe(false);
});

it('removing the chain column from valueFields changes no chain: nothing is re-sealed and the check stays ok', async () => {
// The fix the spec's default-chain-column refusal gives for `tenant_id` listed in `valueFields`. The chain key
// reads the row's chain value only, never the column set, and each checkpoint is checked against its own
// signed columns, so the earlier checkpoints stay valid under the set they were signed with.
const object = {
name: 'ledger_probe',
fields: { id: { type: 'text' }, created_at: { type: 'datetime' }, action: { type: 'text' }, tenant_id: { type: 'text' }, payload: { type: 'textarea' } },
lifecycle: { class: 'audit', retention: { maxAge: '90d' }, seal: { valueFields: ['payload', 'tenant_id'] } as Record<string, unknown> },
};
const store = memoryEngine([object, { name: 'sys_audit_amendment', fields: {} }] as never);
const crypto = bindingCrypto();
const signer = new LocalCheckpointSigner({ engine: store.engine, crypto });
const clock = { now: DAY0 + 12 * H + 30 * 60_000 };
const run = () => new AuditSealer({ engine: store.engine, crypto, signer: () => signer, logger: recordingLogger(), now: () => clock.now }).runPass();
const ledger = store.hot.table('ledger_probe');
ledger.push(
{ id: 'r1', created_at: at(8, 10), action: 'update', tenant_id: 'org_a', payload: 'one' },
{ id: 'r2', created_at: at(10, 5), action: 'update', tenant_id: 'org_a', payload: 'two' },
);
await run();
const sealed = () => store.hot.table('sys_audit_checkpoint').map((r) => {
const body = checkpointBodyFromRow(r);
return [r.chain_key, body.kind, body.row_ids.join(',')].join(' ');
});
const before = sealed();

object.lifecycle.seal = { valueFields: ['payload'] };
ledger.push({ id: 'r3', created_at: at(13, 10), action: 'update', tenant_id: 'org_a', payload: 'three' });
clock.now = DAY0 + 14 * H + 30 * 60_000;
await run();

expect(before).toEqual(['org:org_a window r1', 'org:org_a window r2']);
expect(sealed()).toEqual([...before, 'org:org_a window r3']);
const report = await verifyAuditLedger({ engine: store.engine, crypto, now: () => clock.now });
expect(report.faults).toEqual([]);
expect(report.ok).toBe(true);
const scoped = await verifyAuditLedger({ engine: store.engine, crypto, now: () => clock.now, orgs: ['org_a'] });
expect(scoped.ok).toBe(true);
});
});
4 changes: 2 additions & 2 deletions packages/plugins/plugin-audit/src/audit-seal/sealer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,8 @@ export class AuditSealer {
'have no canonical rule';
} else if (!Object.prototype.hasOwnProperty.call(fields, chainField)) {
// S3: a chain is the row's own chain column (D10 note: `seal.chainField`, default `tenant_id`); without
// it every organization would share one. A NAMED column the object lacks is refused at authoring; this
// stays for the default, and for a definition that reached the registry without the parse.
// it every organization would share one. A chain column the object lacks, named or the default, is
// refused at authoring; this stays for a definition that reached the registry without the parse.
reason = `it has no '${chainField}' field, the column its rows are chained by per organization`;
}
} catch (err) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ describe('lifecycle.seal.chainField — a column the object has parses, as autho
expect(sealOf(input)).toEqual({ valueFields: [], chainField: 'tenant_id' });
});

it('leaves a seal that names no chain column exactly as before: no key added, no new refusal', () => {
expect(sealOf(ledger({ valueFields: ['payload'] }))).toEqual({ valueFields: ['payload'] });
it('leaves a seal that names no chain column, on an object that declares tenant_id, as authored: no key added', () => {
// The default chain column is judged too (object-lifecycle-seal-default-chain-column-34352.test.ts).
const input = ledger({ valueFields: ['payload'] });
(input.fields as Record<string, unknown>).tenant_id = { type: 'text', label: 'Organization' };
expect(sealOf(input)).toEqual({ valueFields: ['payload'] });
});
});

Expand Down
Loading
Loading