Skip to content

Commit ae153a5

Browse files
committed
feat(objectql): lifecycle maps use the map specifier
retention_overrides, quotas and quota_defaults become map specifiers with their stored shapes unchanged. The plugin registers key sources before the manifest: lifecycle-declared objects, and lifecycle classes other than record. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017ihL2fBJ6xsLWJsnNJGMMx
1 parent 4fa7d9f commit ae153a5

5 files changed

Lines changed: 266 additions & 8 deletions

File tree

‎packages/objectql/src/index.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,16 @@ export type { RecycleBinRemovalOptions } from './recycle-bin/purge.js';
638638
// hook's api, and the runtime's sandbox to every body's.
639639
export { refuseAppAuthoredPermanentDelete } from './recycle-bin/app-authored-delete.js';
640640
export { lifecycleSettingsManifest } from './lifecycle/lifecycle-settings.js';
641+
// [#30208] The key columns of the three lifecycle map settings, served from the
642+
// running system; `ObjectQLPlugin` registers them with the settings service.
643+
export {
644+
lifecycleObjectKeySource,
645+
lifecycleClassKeySource,
646+
type LifecycleKeySource,
647+
type LifecycleKeyAnswer,
648+
type LifecycleKeyOption,
649+
type LifecycleKeyRegistryLike,
650+
} from './lifecycle/lifecycle-key-options.js';
641651

642652
// [#4551] Read-only referential-integrity audit — the reporting half of the
643653
// `isSystem` exemption #4441 deliberately left in the write-path guard.
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* [#30208 row 5, ruling A] The key columns of the three lifecycle `map`
5+
* settings are served from the running system, never listed in the manifest:
6+
*
7+
* - `retention_overrides` and `quotas` admit the objects the sweep walks
8+
* (a lifecycle class other than `record`), read at the moment of asking;
9+
* - `quota_defaults` admits the lifecycle classes the sweep reads a default
10+
* for (`LifecycleClassSchema` without `record`);
11+
* - `ObjectQLPlugin` registers the three sources with the settings service
12+
* BEFORE the manifest, booted through a real `ObjectKernel`.
13+
*/
14+
15+
import { describe, it, expect, afterEach } from 'vitest';
16+
import { ObjectKernel, type PluginContext } from '@objectstack/core';
17+
import { ObjectQLPlugin } from '../plugin.js';
18+
import type { ObjectQL } from '../engine.js';
19+
import {
20+
lifecycleClassKeySource,
21+
lifecycleObjectKeySource,
22+
type LifecycleKeyRegistryLike,
23+
type LifecycleKeySource,
24+
} from './lifecycle-key-options.js';
25+
26+
describe('lifecycleObjectKeySource', () => {
27+
const objects: ReturnType<LifecycleKeyRegistryLike['getAllObjects']> = [
28+
{ name: 'sys_job_run', label: 'Job run', lifecycle: { class: 'telemetry' } },
29+
{ name: 'sys_audit_log', lifecycle: { class: 'audit' } },
30+
{ name: 'account', label: 'Account', lifecycle: { class: 'record' } },
31+
{ name: 'contact', label: 'Contact' },
32+
];
33+
34+
it('admits exactly the objects the sweep walks, sorted, named by their label', () => {
35+
const answer = lifecycleObjectKeySource(() => ({ getAllObjects: () => objects }))()!;
36+
expect(answer.options).toEqual([
37+
{ value: 'sys_audit_log', label: 'sys_audit_log' },
38+
{ value: 'sys_job_run', label: 'Job run' },
39+
]);
40+
expect(answer.admits('sys_job_run')).toBe(true);
41+
expect(answer.admits('account')).toBe(false);
42+
expect(answer.admits('contact')).toBe(false);
43+
expect(answer.admits('sys_job_runs')).toBe(false);
44+
});
45+
46+
it('reads the storage objects when the registry has them, as the sweep does', () => {
47+
const answer = lifecycleObjectKeySource(() => ({
48+
getAllObjects: () => [],
49+
getAllStorageObjects: () => [{ name: 'sys_session', lifecycle: { class: 'transient' } }],
50+
}))()!;
51+
expect(answer.options.map((o) => o.value)).toEqual(['sys_session']);
52+
});
53+
54+
it('is asked at the moment of use: an object registered later is admitted from then on', () => {
55+
const live = [...objects];
56+
const source = lifecycleObjectKeySource(() => ({ getAllObjects: () => live }));
57+
expect(source()!.admits('sys_webhook_delivery')).toBe(false);
58+
live.push({ name: 'sys_webhook_delivery', lifecycle: { class: 'event' } });
59+
expect(source()!.admits('sys_webhook_delivery')).toBe(true);
60+
});
61+
62+
it('answers nothing while no registry is reachable (the declared table, none, then stands)', () => {
63+
expect(lifecycleObjectKeySource(() => undefined)()).toBeUndefined();
64+
});
65+
});
66+
67+
describe('lifecycleClassKeySource', () => {
68+
it('admits the lifecycle classes a quota default is read for — every class but `record`', () => {
69+
const answer = lifecycleClassKeySource()()!;
70+
expect(answer.options.map((o) => o.value)).toEqual(['audit', 'telemetry', 'transient', 'event']);
71+
expect(answer.admits('record')).toBe(false);
72+
expect(answer.admits('telemtry')).toBe(false);
73+
});
74+
});
75+
76+
describe('ObjectQLPlugin registers the three key sources before the lifecycle manifest', () => {
77+
const kernels: ObjectKernel[] = [];
78+
afterEach(async () => {
79+
while (kernels.length) {
80+
const kernel = kernels.pop()!;
81+
try { if (kernel.getState() === 'running') await kernel.shutdown(); } catch { /* noop */ }
82+
}
83+
});
84+
85+
it('at kernel:ready, against the engine registry', async () => {
86+
const calls: string[] = [];
87+
const sources = new Map<string, LifecycleKeySource>();
88+
const settings = {
89+
registerOptionSource(namespace: string, key: string, source: LifecycleKeySource) {
90+
calls.push(`source ${namespace}.${key}`);
91+
sources.set(key, source);
92+
},
93+
registerManifest(manifest: { namespace: string }) { calls.push(`manifest ${manifest.namespace}`); },
94+
};
95+
const kernel = new ObjectKernel({ logger: { level: 'silent' }, gracefulShutdown: false });
96+
kernels.push(kernel);
97+
await kernel.use(new ObjectQLPlugin());
98+
await kernel.use({
99+
name: 'test.settings-and-objects',
100+
dependencies: ['com.objectstack.engine.objectql'],
101+
init: async (ctx: PluginContext) => {
102+
ctx.registerService('settings', settings);
103+
const ql = ctx.getService<ObjectQL>('objectql');
104+
ql.registerDriver({
105+
name: 'primary', version: '0.0.0', supports: {},
106+
async connect() {}, async disconnect() {}, async checkHealth() { return true; },
107+
async find() { return []; }, async findOne() { return null; }, async count() { return 0; },
108+
} as any, true);
109+
await ctx.getService<{ register(a: unknown): Promise<void> | void }>('manifest').register({
110+
id: 'com.test.lifecycle-keys',
111+
name: 'com.test.lifecycle-keys',
112+
version: '1.0.0',
113+
objects: [{
114+
name: 'test_ping',
115+
fields: { note: { name: 'note', type: 'text' } },
116+
lifecycle: { class: 'telemetry', retention: { maxAge: '30d' } },
117+
}],
118+
});
119+
},
120+
} as any);
121+
await kernel.bootstrap();
122+
123+
expect(calls).toEqual([
124+
'source lifecycle.retention_overrides',
125+
'source lifecycle.quotas',
126+
'source lifecycle.quota_defaults',
127+
'manifest lifecycle',
128+
]);
129+
expect(sources.get('quotas')!()!.admits('test_ping')).toBe(true);
130+
expect(sources.get('retention_overrides')!()!.admits('test_ping')).toBe(true);
131+
expect(sources.get('quota_defaults')!()!.admits('telemetry')).toBe(true);
132+
});
133+
});
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
import { LifecycleClassSchema } from '@objectstack/spec/data';
4+
5+
/**
6+
* The key columns of the three `lifecycle` map settings (#30208), served from
7+
* the running system: the keys a `retention_overrides`, `quotas` or
8+
* `quota_defaults` row may name are what this deployment has, never a list
9+
* written in the manifest (ruling 6715997428, constraint 3).
10+
*
11+
* Each source has the shape of `@objectstack/service-settings`'
12+
* `SettingsOptionSource`, spelled structurally here so this package takes no
13+
* dependency on the settings service. The settings service registers them per
14+
* map key; both of its doors (a save, an `OS_LIFECYCLE_*` override) judge each
15+
* entry's key against the answer, and every manifest read serves the answer as
16+
* the map's `options`, the key picker's list.
17+
*/
18+
19+
/** One option a source serves: the stored key and its name. */
20+
export interface LifecycleKeyOption {
21+
value: string;
22+
label: string;
23+
}
24+
25+
/** What a key source answers — `SettingsOptionSourceAnswer`'s shape. */
26+
export interface LifecycleKeyAnswer {
27+
options: LifecycleKeyOption[];
28+
admits(value: string): boolean;
29+
}
30+
31+
/** A key source — `SettingsOptionSource`'s shape. */
32+
export type LifecycleKeySource = (input?: { current?: unknown; locale?: string }) => LifecycleKeyAnswer | undefined;
33+
34+
/** The registry surface the object source reads: the objects the sweep reads. */
35+
export interface LifecycleKeyRegistryLike {
36+
getAllObjects(): Array<{ name: string; label?: unknown; lifecycle?: { class?: string } | null }>;
37+
getAllStorageObjects?(): Array<{ name: string; label?: unknown; lifecycle?: { class?: string } | null }>;
38+
}
39+
40+
function answerOf(options: LifecycleKeyOption[]): LifecycleKeyAnswer {
41+
const values = new Set(options.map((o) => o.value));
42+
return { options, admits: (value) => values.has(value) };
43+
}
44+
45+
/**
46+
* The objects a lifecycle map row may name: every object that declares a
47+
* lifecycle class other than `record`, which is exactly the set the sweep walks
48+
* (`LifecycleService.sweep`). An object outside it is never read under its
49+
* override or its quota, so naming it would be stored and silently do nothing.
50+
*
51+
* Asked at the moment it is used (each save, each override read, each page
52+
* read), so an object a package registers later is admitted from then on.
53+
* `undefined` while the registry is not reachable: the settings service then
54+
* falls back to the manifest's declared table, which declares none, so every
55+
* key is admitted rather than every save refused.
56+
*/
57+
export function lifecycleObjectKeySource(getRegistry: () => LifecycleKeyRegistryLike | undefined): LifecycleKeySource {
58+
return () => {
59+
const registry = getRegistry();
60+
if (!registry) return undefined;
61+
const objects = typeof registry.getAllStorageObjects === 'function'
62+
? registry.getAllStorageObjects()
63+
: registry.getAllObjects();
64+
const options = objects
65+
.filter((o) => o?.lifecycle && o.lifecycle.class && o.lifecycle.class !== 'record')
66+
.map((o) => ({ value: o.name, label: typeof o.label === 'string' && o.label !== '' ? o.label : o.name }))
67+
.sort((a, b) => a.value.localeCompare(b.value));
68+
return answerOf(options);
69+
};
70+
}
71+
72+
/**
73+
* The lifecycle classes a `quota_defaults` row may name: `LifecycleClassSchema`
74+
* without `record`. A `record` object is never swept (its data is business
75+
* truth, immortal by contract), so a `record` default is never read.
76+
*/
77+
export function lifecycleClassKeySource(): LifecycleKeySource {
78+
const options = LifecycleClassSchema.options
79+
.filter((c) => c !== 'record')
80+
.map((c) => ({ value: c, label: c }));
81+
return () => answerOf(options);
82+
}

‎packages/objectql/src/lifecycle/lifecycle-settings.ts‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ import type { SettingsManifest } from '@objectstack/spec/system';
1010
* global > tenant > default), which is exactly ADR-0057 §3.2's "regulated
1111
* tenants set years; dev sets days — the same knob at different settings":
1212
* `retention_overrides` is tenant-scoped, so one deployment can carry both.
13+
*
14+
* The three maps are `map` specifiers (#30208, ADR-0007 as amended 2026-11-08):
15+
* a key column, then typed value columns. Their stored shapes are the ones the
16+
* sweep has always read (`Record<object, { maxAge?, expireAfter? }>`,
17+
* `Record<object, number>`, `Record<class, number>`), unchanged. The keys each
18+
* admits are served at run time (`lifecycle-key-options.ts`), never listed here.
1319
*/
1420
export const lifecycleSettingsManifest = {
1521
namespace: 'lifecycle',
@@ -36,34 +42,44 @@ export const lifecycleSettingsManifest = {
3642
'Master switch for the periodic sweep. Off = declared retention/ttl/rotation policies stop being enforced (data grows unbounded again).',
3743
},
3844
{
39-
type: 'json',
45+
type: 'map',
4046
key: 'retention_overrides',
4147
label: 'Retention overrides',
4248
required: false,
4349
scope: 'tenant',
4450
default: {},
51+
keyColumn: { label: 'Object' },
52+
columns: [
53+
{ name: 'maxAge', label: 'Keep rows for', kind: 'duration' },
54+
{ name: 'expireAfter', label: 'Expire rows after', kind: 'duration' },
55+
],
4556
description:
46-
'Per-object window overrides: { "<object>": { "maxAge": "1y", "expireAfter": "30d" } }. ' +
47-
'Durations use h/d/w/y. Set per organization, so a regulated organization can keep years while a development one keeps days. ' +
57+
'Per-object windows that replace the declared ones. Durations use h, d, w or y, for example 30d or 7y. ' +
58+
'Set per organization, so a regulated organization can keep years while a development one keeps days. ' +
4859
'An override shorter than the minimum a platform feature needs (for example the job queue\'s duplicate-detection window) ' +
4960
'is ignored and logged as an error, and the default window keeps applying.',
5061
},
5162
{
52-
type: 'json',
63+
type: 'map',
5364
key: 'quotas',
5465
label: 'Row quotas',
5566
required: false,
5667
default: {},
68+
keyColumn: { label: 'Object' },
69+
columns: [{ name: 'max_rows', label: 'Max rows', kind: 'positive_integer' }],
5770
description:
58-
'Per-object max row counts: { "<object>": 1000000 }. A breach raises a governance alert — quotas never delete beyond the declared policy.',
71+
'Per-object maximum row counts. A breach raises a governance alert — quotas never delete beyond the declared policy.',
5972
},
6073
{
61-
type: 'json',
74+
type: 'map',
6275
key: 'quota_defaults',
6376
label: 'Per-class quota defaults',
6477
required: false,
6578
default: {},
66-
description: 'Fallback quotas by lifecycle class: { "telemetry": 1000000, "transient": 500000, "event": 100000, "audit": 5000000 }.',
79+
keyColumn: { label: 'Lifecycle class' },
80+
columns: [{ name: 'max_rows', label: 'Max rows', kind: 'positive_integer' }],
81+
description:
82+
'Fallback row quotas by lifecycle class (telemetry, transient, event, audit), for objects with no quota of their own.',
6783
},
6884
{
6985
type: 'number',

‎packages/objectql/src/plugin.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ import { announceStoredExpressionHoles } from './stored-expression-backfill.js';
3838
import { LifecycleService } from './lifecycle/lifecycle-service.js';
3939
import { readRecycleBinRetentionEnv, type RecycleBinRetentionSettings } from './recycle-bin/retention.js';
4040
import { lifecycleSettingsManifest } from './lifecycle/lifecycle-settings.js';
41+
import {
42+
lifecycleClassKeySource,
43+
lifecycleObjectKeySource,
44+
type LifecycleKeySource,
45+
} from './lifecycle/lifecycle-key-options.js';
4146
import type { DanglingReferenceAuditOptions } from './integrity/dangling-reference-audit.js';
4247
import {
4348
GLOBAL_ACTION_OBJECT_KEY,
@@ -1211,7 +1216,19 @@ export class ObjectQLPlugin implements Plugin {
12111216
// ADR-0057 P4: surface the lifecycle governance namespace in Settings
12121217
// (overrides / quotas / growth alerts) when a SettingsService exists.
12131218
try {
1214-
const settings = ctx.getService('settings') as { registerManifest?: (m: unknown) => void };
1219+
const settings = ctx.getService('settings') as {
1220+
registerManifest?: (m: unknown) => void;
1221+
registerOptionSource?: (namespace: string, key: string, source: LifecycleKeySource) => void;
1222+
};
1223+
// [#30208] The keys each lifecycle map admits are what this
1224+
// deployment has: its lifecycle-declared objects, the lifecycle
1225+
// classes. Registered BEFORE the manifest, so its registration-time
1226+
// audit of `OS_LIFECYCLE_*` overrides leaves these keys to be judged
1227+
// when read, against the registry as it then stands.
1228+
const objectKeys = lifecycleObjectKeySource(() => this.ql?.registry);
1229+
settings?.registerOptionSource?.('lifecycle', 'retention_overrides', objectKeys);
1230+
settings?.registerOptionSource?.('lifecycle', 'quotas', objectKeys);
1231+
settings?.registerOptionSource?.('lifecycle', 'quota_defaults', lifecycleClassKeySource());
12151232
settings?.registerManifest?.(lifecycleSettingsManifest);
12161233
} catch {
12171234
// No settings service — governance stays at declared defaults.

0 commit comments

Comments
 (0)