Skip to content

Commit 2a14593

Browse files
committed
feat(plugin-audit): a ledger row's record page lists the other rows about the same record
The sys_audit_log record page draws the row's fields in their groups and, beneath them, every ledger row whose record_id is this row's, so a config_change row's digest sits beside the CRUD mirror's row that carries the value. The page label is translated in the eleven shipped locales, and the read-gate prose names config_change among the rows about a record. Claude-Session: https://claude.ai/code/session_016WV4cyf254wVgypzQ5Uj1a Co-authored-by: Claude <noreply@anthropic.com>
1 parent 30cdcc2 commit 2a14593

9 files changed

Lines changed: 621 additions & 38 deletions

File tree

‎packages/plugins/plugin-audit/scripts/i18n-extract.config.ts‎

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@
3434
* recognised flag spelling out of this whole comment, so a second spelling
3535
* would keep the opt-in switched on after someone deleted it from the command
3636
* block — the one place that decides.
37+
*
38+
* `pages` declares the record page this plugin ships (`sys_audit_log_detail`).
39+
* The command above writes the objects sub-tree only, so the page's one bundle
40+
* key, its page-level `label`, is HAND-AUTHORED in `src/translations/pages.ts`
41+
* and rides in the `translations` baseline below. Declaring the page here is
42+
* what puts its label in the coverage ratchet's population.
3743
*/
3844

3945
import { defineStack, type ObjectStackDefinition } from '@objectstack/spec';
@@ -52,6 +58,8 @@ import { ptBRObjects } from '../src/translations/pt-BR.objects.generated.js';
5258
import { ruObjects } from '../src/translations/ru.objects.generated.js';
5359
import { deObjects } from '../src/translations/de.objects.generated.js';
5460
import { koObjects } from '../src/translations/ko.objects.generated.js';
61+
import { SysAuditLogDetailPage } from '../src/sys-audit-log.page.js';
62+
import { auditPageTranslations } from '../src/translations/pages.js';
5563

5664
/**
5765
* The annotation is load-bearing, not decoration (#10868). `defineStack`
@@ -69,18 +77,19 @@ import { koObjects } from '../src/translations/ko.objects.generated.js';
6977
*/
7078
const config: ObjectStackDefinition = defineStack({
7179
objects: [SysAuditLog, SysActivity, SysComment, SysCommentReaction] as any,
80+
pages: [SysAuditLogDetailPage],
7281
translations: [
73-
{ en: { objects: enObjects } },
74-
{ 'zh-CN': { objects: zhCNObjects } },
75-
{ 'ja-JP': { objects: jaJPObjects } },
76-
{ 'es-ES': { objects: esESObjects } },
77-
{ 'zh-Hant': { objects: zhHantObjects } },
78-
{ 'ar': { objects: arObjects } },
79-
{ 'fr': { objects: frObjects } },
80-
{ 'pt-BR': { objects: ptBRObjects } },
81-
{ 'ru': { objects: ruObjects } },
82-
{ 'de': { objects: deObjects } },
83-
{ 'ko': { objects: koObjects } },
82+
{ en: { objects: enObjects, pages: auditPageTranslations.en } },
83+
{ 'zh-CN': { objects: zhCNObjects, pages: auditPageTranslations['zh-CN'] } },
84+
{ 'ja-JP': { objects: jaJPObjects, pages: auditPageTranslations['ja-JP'] } },
85+
{ 'es-ES': { objects: esESObjects, pages: auditPageTranslations['es-ES'] } },
86+
{ 'zh-Hant': { objects: zhHantObjects, pages: auditPageTranslations['zh-Hant'] } },
87+
{ 'ar': { objects: arObjects, pages: auditPageTranslations['ar'] } },
88+
{ 'fr': { objects: frObjects, pages: auditPageTranslations['fr'] } },
89+
{ 'pt-BR': { objects: ptBRObjects, pages: auditPageTranslations['pt-BR'] } },
90+
{ 'ru': { objects: ruObjects, pages: auditPageTranslations['ru'] } },
91+
{ 'de': { objects: deObjects, pages: auditPageTranslations['de'] } },
92+
{ 'ko': { objects: koObjects, pages: auditPageTranslations['ko'] } },
8493
],
8594
});
8695

‎packages/plugins/plugin-audit/src/audit-log-read-visibility.ts‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@
3737
* which name the session, and its `login_failed` / `account_locked`, which
3838
* name the account's `sys_user` row (`auth-event-audit.ts`), and an
3939
* `action_invocation` row whose action named a subject record
40-
* (`action-invocation-audit.ts`). Judged by the record gate;
40+
* (`action-invocation-audit.ts`), and service-settings' `config_change`,
41+
* which names the stored settings row the write changed (`sys_setting`, or
42+
* `sys_platform_setting` for a global value), the row the mirror's own
43+
* `create` / `update` about that write names. Judged by the record gate;
4144
* the session is in the audit writer's `AUDIT_EXCLUDED_OBJECTS`, so the gate
4245
* names it ({@link LEDGER_NAMED_PARENTS}) to keep judging it.
4346
* A record that no longer exists is read by no caller, so its rows are
@@ -46,18 +49,20 @@
4649
* deleted record, and every `logout` row (sign-out deletes the session it
4750
* names).
4851
* - **Rows about no record** — they name no `record_id` and their action is
49-
* not a record action: the run-level `import` (plugin-auth's user import),
50-
* `config_change` (service-settings) and `platform_admin_standing_change`
51-
* (plugin-security) rows, an auth event that carried no session id, and an
52-
* `action_invocation` row of an action invoked with no subject record.
52+
* not a record action: the run-level `import` (plugin-auth's user import)
53+
* and `platform_admin_standing_change` (plugin-security) rows, a
54+
* `config_change` row stored before its writer recorded the settings row,
55+
* an auth event that carried no session id, and an `action_invocation` row
56+
* of an action invoked with no subject record.
5357
* There is no record to judge, and none carries a record's field values
5458
* (the per-writer measurement in `audit-log-field-redaction.ts`), so they
5559
* are OUTSIDE this gate's class and are served under the ledger's own grant
5660
* exactly as before — through a predicate on the ledger's own columns
5761
* ({@link ledgerRowAboutNoRecordWhere}), so a broad read is served whole. This is where the ledger differs from the activity
5862
* stream, which excludes every row naming no parent: the stream has no
5963
* platform producer of such rows, while these have three, and the shipped
60-
* `config_changes` list view reads two of them through the data door.
64+
* `config_changes` list view reads `import` rows (and the older
65+
* `config_change` rows) through the data door.
6166
* - **A record action that names no record** — a `create` / `read` / `update`
6267
* / `delete` row with no `record_id` (the CRUD mirror stamps `null` when it
6368
* cannot derive the id): about a record the gate cannot name, so excluded

‎packages/plugins/plugin-audit/src/audit-plugin.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type {
1616
ISharingService,
1717
} from '@objectstack/spec/contracts';
1818
import { SysAuditLog, SysActivity, SysComment, SysCommentReaction } from './objects/index.js';
19+
import { SysAuditLogDetailPage } from './sys-audit-log.page.js';
1920
// `sys_notification` was parked here "until that [ADR-0030] migration lands".
2021
// It has landed, so the contribution moved to @objectstack/service-messaging —
2122
// the service that writes the row on every `emit()` (#4154). This plugin never
@@ -273,6 +274,10 @@ export class AuditPlugin implements Plugin {
273274
defaultDatasource: 'cloud',
274275
namespace: 'sys',
275276
objects: [SysAuditLog, SysActivity, SysComment, SysCommentReaction],
277+
// The ledger row's record page: its fields, and every ledger row about
278+
// the same record beside it, so a `config_change` row's digest sits next
279+
// to the mirror's row that carries the value (`sys-audit-log.page.ts`).
280+
pages: [SysAuditLogDetailPage],
276281
// ADR-0029 D7 — contribute the one Audit Log entry into the Setup app's
277282
// `group_diagnostics` slot. The plugin owns sys_audit_log (K2), so the
278283
// entry lives and dies with this plugin and needs no item gate.

‎packages/plugins/plugin-audit/src/objects/sys-audit-log.object.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,17 @@ export const SysAuditLog = ObjectSchema.create({
229229
name: 'config_changes',
230230
label: 'Config',
231231
data: { provider: 'object', object: 'sys_audit_log' },
232-
// [#24611] No Object column. Its rows carry no `record_id`, so no pointer
233-
// names their target, and the column drew one of three stored API names
232+
// [#24611] No Object column: it drew one of three stored API names
234233
// (`sys_setting`, `sys_platform_setting` for a global-scope change,
235-
// `sys_user` for an import). Which setting changed is in `metadata` and
236-
// `new_value`, and the row's page still shows `object_name` and the
237-
// `tenant_id` a global change leaves empty.
234+
// `sys_user` for an import) as text. A `config_change` row names the
235+
// settings row it changed as its `record_id` (service-settings'
236+
// `config-change-audit.ts`), so the row's page draws that pair as the
237+
// setting's card and lists the ledger's other rows about the same
238+
// setting, the mirror's row with the value among them
239+
// (`sys-audit-log.page.ts`). An `import` row, and a `config_change` row
240+
// written before its writer recorded the id, carry no `record_id`.
241+
// Which setting changed is also in `metadata`, and the row's page shows
242+
// the `tenant_id` a global change leaves empty.
238243
columns: ['created_at', 'action', 'user_id'],
239244
// `export` removed (#8147) — retired from the enum, nothing ever wrote it.
240245
// `import` KEPT deliberately: `plugin-auth`'s admin user-import writes a
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* `sys_audit_log`'s record page: the full page this plugin serves through its
5+
* manifest `pages` (`sys-audit-log.page.ts`), whose job is the list of every
6+
* ledger row about the same record beside the row being read.
7+
*
8+
* Four questions, each answered against the shipped artifacts:
9+
*
10+
* 1. the plugin's manifest carries the page;
11+
* 2. the page parses as a page, and every node's props parse against its
12+
* component's own props row (the per-type door `os validate` judges);
13+
* 3. the body draws every field the object declares (a `full` page draws
14+
* only what it authors), and the same-record list is the ledger keyed on
15+
* `record_id` on both sides, with no "New" on an append-only object;
16+
* 4. its label is translated in every shipped locale, `en` byte-equal to
17+
* the page literal.
18+
*
19+
* What the list SELECTS on a real engine and driver, the mirror's rows about
20+
* a settings write included, is `sys-audit-log-same-record.integration.test.ts`.
21+
*/
22+
23+
import { describe, it, expect } from 'vitest';
24+
import { PageSchema, ComponentPropsMap } from '@objectstack/spec/ui';
25+
import { AuditPlugin } from './audit-plugin.js';
26+
import { SysAuditLogDetailPage } from './sys-audit-log.page.js';
27+
import { SysAuditLog } from './objects/index.js';
28+
import { AuditTranslations } from './translations/index.js';
29+
import { auditPageTranslations } from './translations/pages.js';
30+
31+
type AnyRec = Record<string, any>;
32+
33+
const PAGE = SysAuditLogDetailPage as AnyRec;
34+
const COMPONENTS = (PAGE.regions?.[0]?.components ?? []) as AnyRec[];
35+
const nodeOf = (type: string): AnyRec => COMPONENTS.find((c) => c.type === type) ?? {};
36+
const FIELDS = SysAuditLog.fields as AnyRec;
37+
38+
/** The plugin's one manifest, as `init` registers it. */
39+
async function registeredManifest(): Promise<AnyRec> {
40+
const registered: AnyRec[] = [];
41+
const ctx: any = {
42+
getService: (name: string) => {
43+
if (name === 'manifest') return { register: (m: AnyRec) => registered.push(m) };
44+
throw new Error(`no service '${name}'`);
45+
},
46+
registerService: () => {},
47+
hook: () => {},
48+
logger: { info: () => {}, warn: () => {}, error: () => {}, debug: () => {} },
49+
};
50+
await new AuditPlugin().init(ctx);
51+
expect(registered).toHaveLength(1);
52+
return registered[0];
53+
}
54+
55+
describe('sys_audit_log record page — served through the manifest', () => {
56+
it('the plugin manifest carries the page in `pages`', async () => {
57+
expect((await registeredManifest()).pages).toEqual([SysAuditLogDetailPage]);
58+
});
59+
60+
it('is the default full record page of sys_audit_log, and parses as a page', () => {
61+
expect(PAGE).toMatchObject({
62+
name: 'sys_audit_log_detail',
63+
type: 'record',
64+
object: 'sys_audit_log',
65+
kind: 'full',
66+
isDefault: true,
67+
});
68+
const parsed = PageSchema.safeParse(PAGE);
69+
expect(parsed.success, JSON.stringify(parsed.error?.issues ?? [])).toBe(true);
70+
});
71+
72+
it('every node\'s props parse against its own component props row', () => {
73+
const map = ComponentPropsMap as Record<string, { safeParse: (v: unknown) => { success: boolean; error?: any } }>;
74+
const verdicts = COMPONENTS.map((c) => {
75+
const row = map[c.type];
76+
const result = row?.safeParse(c.properties ?? {});
77+
return { type: c.type, known: !!row, ok: !!result?.success, issues: result?.error?.issues ?? [] };
78+
});
79+
expect(verdicts.filter((v) => !v.known || !v.ok)).toEqual([]);
80+
// Control: the door refuses what it should, so the green above is a verdict.
81+
expect(map['record:related_list'].safeParse({ objectName: 'sys_audit_log', relationshipField: 'record_id', relatedField: 'x' }).success)
82+
.toBe(false);
83+
});
84+
});
85+
86+
describe('sys_audit_log record page — the body', () => {
87+
it('authors the header, the strip, the details, the same-record list and the discussion, in that order', () => {
88+
expect(COMPONENTS.map((c) => c.type)).toEqual([
89+
'page:header', 'record:highlights', 'record:details', 'record:related_list', 'record:discussion',
90+
]);
91+
expect(nodeOf('page:header')).toEqual({ type: 'page:header', properties: { placedActions: true } });
92+
});
93+
94+
it('the strip is the object\'s highlightFields minus the title field, and holds neither half of the pointer pair', () => {
95+
const strip = nodeOf('record:highlights').properties.fields as string[];
96+
expect(strip).toEqual((SysAuditLog.highlightFields ?? []).filter((f: string) => f !== SysAuditLog.nameField));
97+
expect(strip).not.toContain('record_id');
98+
expect(strip).not.toContain('object_name');
99+
});
100+
101+
it('the details draw every field the object declares, the system id apart, in its declared groups', () => {
102+
const sections = nodeOf('record:details').properties.sections as AnyRec[];
103+
const drawn = sections.flatMap((s) => s.fields as string[]);
104+
const declared = Object.keys(FIELDS).filter((f) => f !== 'id');
105+
expect([...drawn].sort()).toEqual([...declared].sort());
106+
// Each section holds one declared group's fields, so the body reads as the object groups them.
107+
for (const section of sections) {
108+
const groups = new Set((section.fields as string[]).map((f) => FIELDS[f]?.group));
109+
expect(groups.size, String(section.label?.en)).toBe(1);
110+
expect([...groups][0]).toBe(section.label.en);
111+
}
112+
});
113+
114+
it('the same-record list is the ledger, keyed on `record_id` on both sides, newest first, with no "New"', () => {
115+
const list = nodeOf('record:related_list').properties;
116+
expect(list).toMatchObject({
117+
objectName: 'sys_audit_log',
118+
relationshipField: 'record_id',
119+
relationshipValueField: 'record_id',
120+
sort: [{ field: 'created_at', order: 'desc' }],
121+
// An authored empty action list withholds "New"; the object is append-only.
122+
actions: [],
123+
});
124+
expect((SysAuditLog as AnyRec).enable?.apiMethods).toEqual(['get', 'list']);
125+
// The value it keys on is the pointer's id half, a declared column of this object.
126+
expect(FIELDS.record_id?.referenceVia).toBe('object_name');
127+
// Every column it draws is a field of the object, the two snapshots included.
128+
const columns = list.columns as string[];
129+
expect(columns.filter((c) => !(c in FIELDS))).toEqual([]);
130+
expect(columns).toEqual(expect.arrayContaining(['old_value', 'new_value']));
131+
});
132+
133+
it('every inline label carries the eleven locales this plugin ships', () => {
134+
const locales = Object.keys(auditPageTranslations).sort();
135+
const labels: AnyRec[] = [
136+
...(nodeOf('record:details').properties.sections as AnyRec[]).map((s) => s.label),
137+
nodeOf('record:related_list').properties.title,
138+
];
139+
for (const label of labels) expect(Object.keys(label).sort(), String(label.en)).toEqual(locales);
140+
});
141+
});
142+
143+
describe('sys_audit_log record page — its label is translated', () => {
144+
it('`en` mirrors the page literal byte for byte', () => {
145+
expect(auditPageTranslations.en.sys_audit_log_detail?.label).toBe(PAGE.label);
146+
});
147+
148+
it('every shipped locale serves its own label, the object\'s label in that locale', () => {
149+
for (const [locale, bundle] of Object.entries(AuditTranslations)) {
150+
const served = (bundle as AnyRec).pages?.sys_audit_log_detail?.label;
151+
expect(served, locale).toBe((auditPageTranslations as AnyRec)[locale].sys_audit_log_detail.label);
152+
expect(served, locale).toBe((bundle as AnyRec).objects?.sys_audit_log?.label);
153+
}
154+
});
155+
});

0 commit comments

Comments
 (0)