Skip to content

Commit 30cdcc2

Browse files
committed
feat(service-settings): the config_change ledger row names the settings row it changed
upsertRow reports the id of the stored row a write landed on, and the config_change sink writes it as record_id beside the object the scope selects, so the ledger row and the CRUD mirror's row about the same write share one pointer pair. Claude-Session: https://claude.ai/code/session_016WV4cyf254wVgypzQ5Uj1a Co-authored-by: Claude <noreply@anthropic.com>
1 parent 4bf9a2d commit 30cdcc2

4 files changed

Lines changed: 75 additions & 15 deletions

File tree

‎packages/services/service-settings/src/config-change-audit.test.ts‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,12 @@ describe('#8145 — a settings write reaches sys_audit_log as `config_change`',
382382
expect(rows[0].object_name).toBe(CONFIG_CHANGE_GLOBAL_OBJECT_NAME);
383383
expect(rows[0].object_name).toBe('sys_platform_setting');
384384
expect(boot.scopedSettingRows()).toHaveLength(0);
385-
// A settings row has no single id — the composite key is in `metadata`.
386-
expect(rows[0].record_id).toBeNull();
385+
// The row names the stored settings row the write landed on, so
386+
// `object_name` + `record_id` point at it (#30209). WHICH setting changed is
387+
// still in `metadata`.
388+
const [stored] = boot.settingRows();
389+
expect(typeof stored?.id).toBe('string');
390+
expect(rows[0].record_id).toBe(stored.id);
387391
// Attribution: both channels, per ADR-0014 D2.
388392
expect(rows[0].user_id).toBe('usr_admin');
389393
expect(rows[0].actor).toBe('usr_admin');
@@ -429,6 +433,25 @@ describe('#8145 — a settings write reaches sys_audit_log as `config_change`',
429433
// row, and the global rung's store took nothing.
430434
expect(boot.scopedSettingRows()).toHaveLength(1);
431435
expect(boot.settingRows()).toHaveLength(0);
436+
// The pointer pair names that `sys_setting` row (#30209).
437+
expect(rows[0].record_id).toBe(boot.scopedSettingRows()[0].id);
438+
});
439+
440+
it('an OVERWRITE names the same settings row as the write that created it (#30209)', async () => {
441+
const boot = await bootPlugin({ userId: 'usr_admin' });
442+
await boot.service.setMany('branding_test', { workspace_name: 'A' }, boot.writeCtx);
443+
await boot.service.setMany('branding_test', { workspace_name: 'B' }, boot.writeCtx);
444+
await boot.service.setMany('branding_test', { workspace_name: null }, boot.writeCtx);
445+
446+
// One stored row, updated in place; every ledger row about it names it, a
447+
// clear included, so the ledger's record page lists all three together.
448+
const stored = boot.settingRows();
449+
expect(stored).toHaveLength(1);
450+
const rows = boot.ledgerRows();
451+
expect(rows.map((r) => metaOf(r).event)).toEqual(['settings.set', 'settings.set', 'settings.reset']);
452+
expect(rows.map((r) => r.record_id)).toEqual([stored[0].id, stored[0].id, stored[0].id]);
453+
// `metadata` keeps its shape: the record id is a column, never a new key.
454+
expect(Object.keys(metaOf(rows[1])).sort()).toEqual(['encrypted', 'event', 'key', 'namespace', 'requestId', 'scope']);
432455
});
433456

434457
it('records one row per CHANGED KEY, not one per request', async () => {

‎packages/services/service-settings/src/config-change-audit.ts‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,12 +262,20 @@ export function buildConfigChangeAuditSink(
262262
// principal, or null for an in-process/boot write.
263263
actor,
264264
object_name: entry.scope === 'global' ? CONFIG_CHANGE_GLOBAL_OBJECT_NAME : CONFIG_CHANGE_OBJECT_NAME,
265-
// A settings write has no single record id: `sys_setting` is keyed on
266-
// the composite `(namespace, key, scope, user_id)` and
267-
// `sys_platform_setting` on `(namespace, key)`. Null is the honest
268-
// answer and the shape `plugin-auth`'s run-level `import` row already
269-
// uses; WHICH setting changed is in `metadata` and `new_value`.
270-
record_id: null,
265+
// The stored settings row the write landed on, as `upsertRow`
266+
// reports it: the `sys_setting` row of a tenant or user value, the
267+
// `sys_platform_setting` row of a global one, so `object_name` and
268+
// `record_id` are one pointer pair. It is the id the CRUD mirror's own
269+
// row about the same write carries, so the ledger's record page lists
270+
// this row beside that one (`plugin-audit`'s `sys_audit_log` page).
271+
// Null only when the store returned no id (the in-memory fallback);
272+
// WHICH setting changed is still in `metadata`, unchanged.
273+
//
274+
// A row that names a record is read under the ledger's parent-record
275+
// gate (`plugin-audit`'s `audit-log-read-visibility.ts`): a reader
276+
// who cannot read that settings row is not served it, the rule the
277+
// mirror's row already follows.
278+
record_id: entry.recordId ?? null,
271279
// The digest, never the value — see the module header. A reset has no
272280
// new state to describe.
273281
new_value: isReset

‎packages/services/service-settings/src/settings-service.ts‎

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2760,7 +2760,7 @@ export class SettingsService {
27602760
updated_at: new Date().toISOString(),
27612761
updated_by: ctx.userId ?? null,
27622762
};
2763-
const { previousEnc, previous } = await this.upsertRow(rowForKey);
2763+
const { previousEnc, previous, rowId } = await this.upsertRow(rowForKey);
27642764

27652765
// The audit's `old_hash`, taken HERE: after the upsert's probe named the
27662766
// previous row, and before the reap below destroys the ciphertext a
@@ -2798,6 +2798,9 @@ export class SettingsService {
27982798
: digest ?? '',
27992799
encrypted: isEncrypted,
28002800
requestId: ctx.requestId,
2801+
// The stored settings row this write landed on, so the ledger row
2802+
// names it as its record (see `upsertRow`).
2803+
recordId: rowId,
28012804
});
28022805
} catch {
28032806
// [#8145] Never fail a write because a ledger is unhappy — the same
@@ -3725,10 +3728,17 @@ export class SettingsService {
37253728
* held BEFORE: `previousEnc` for the secret reaper (#8030), `previous` for
37263729
* the audit's `old_hash` (#26465). Both come off the existence probe the
37273730
* upsert makes anyway, so a write with no previous row costs no extra read.
3731+
*
3732+
* `rowId` is the id of the stored row the write landed on: the probed row's
3733+
* on an update, the engine's insert result's on an insert. The `config_change`
3734+
* ledger row records it as its `record_id`, so the ledger names the settings
3735+
* row the way the CRUD mirror's own row about that write does. `null` when the
3736+
* store hands back no id (the in-memory fallback, a host engine whose insert
3737+
* returns nothing).
37283738
*/
37293739
private async upsertRow(
37303740
row: SettingsRow,
3731-
): Promise<{ previousEnc: string | null; previous: SettingsRow | null }> {
3741+
): Promise<{ previousEnc: string | null; previous: SettingsRow | null; rowId: string | null }> {
37323742
if (this.engine) {
37333743
const { object, where, data } = this.rowIdentity(row);
37343744
// All three engine calls carry the explicit system opt-in
@@ -3750,7 +3760,7 @@ export class SettingsService {
37503760
data,
37513761
context: SETTINGS_SYSTEM_CONTEXT,
37523762
});
3753-
return { previousEnc, previous };
3763+
return { previousEnc, previous, rowId: SettingsService.rowIdOf(previous) };
37543764
}
37553765
// Under the opt-in the engine no longer checks that a user-scope row's
37563766
// `user_id` names a user, so the service keeps that refusal before the
@@ -3769,8 +3779,8 @@ export class SettingsService {
37693779
row.user_id,
37703780
);
37713781
}
3772-
await this.engine.insert(object, data, { context: SETTINGS_SYSTEM_CONTEXT });
3773-
return { previousEnc: null, previous: null };
3782+
const inserted = await this.engine.insert(object, data, { context: SETTINGS_SYSTEM_CONTEXT });
3783+
return { previousEnc: null, previous: null, rowId: SettingsService.rowIdOf(inserted) };
37743784
}
37753785
const idx = this.memoryIndexOf(row);
37763786
if (idx >= 0) {
@@ -3779,10 +3789,20 @@ export class SettingsService {
37793789
// carry keeps what the row held — above all its lock, which a write the
37803790
// platform rung makes through it must leave in place.
37813791
this.memory[idx] = { ...previous, ...row };
3782-
return { previousEnc: SettingsService.handleOf(previous.value_enc), previous };
3792+
return { previousEnc: SettingsService.handleOf(previous.value_enc), previous, rowId: null };
37833793
}
37843794
this.memory.push(row);
3785-
return { previousEnc: null, previous: null };
3795+
return { previousEnc: null, previous: null, rowId: null };
3796+
}
3797+
3798+
/**
3799+
* The stored id of a settings row, as the engine returned or read it; `null`
3800+
* when there is none. The in-memory fallback's rows carry no id, and nothing
3801+
* stores them anywhere a ledger row could point at.
3802+
*/
3803+
private static rowIdOf(row: unknown): string | null {
3804+
const id = (row as { id?: unknown } | null | undefined)?.id;
3805+
return typeof id === 'string' && id !== '' ? id : typeof id === 'number' ? String(id) : null;
37863806
}
37873807

37883808
/**

‎packages/services/service-settings/src/settings-service.types.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,15 @@ export interface SettingsAuditSink {
259259
valueDigest: string;
260260
encrypted: boolean;
261261
requestId?: string;
262+
/**
263+
* The id of the stored settings row the write landed on: a `sys_setting`
264+
* row for a tenant or user value, a `sys_platform_setting` row for a global
265+
* one. Recorded as the ledger row's `record_id`, beside the object
266+
* `scope` selects, so the ledger names the settings row the same way the
267+
* CRUD mirror's row about that write does. `null` or absent when the store
268+
* returned no id (the in-memory fallback).
269+
*/
270+
recordId?: string | null;
262271
}): Promise<void> | void;
263272
}
264273

0 commit comments

Comments
 (0)