Skip to content

Commit dc8479e

Browse files
fix(service-settings): a map setting refuses every key when its key source answers an empty list (#31539)
<!-- claude-projects-attribution: {"github_login":"os-project-manager"} --> _Requested by **Leehom** · [project thread](https://claude.ai/code/project/chan_01TenzsRopZj4jMbvjX8uz1X?thread=cmsg_01TenzsRopZj4jMbvjX8uz1X1nhftM2hZHHvqk2GLFyVTN)_ domain:server#3 · os-project-manager · session_01FEkQ5nBSzc6ktJyjdFcDb3 Fixes #31422 Clause-②: no (narrowing) (a `map` key whose key source answered an empty list was accepted at both doors and is refused now; on such a host no key could name what the setting reads) Metadata-protocol: none ## 维护者速读 - 改了什么:`map` 类设置(`lifecycle.quotas` / `retention_overrides` 等)的键,按该键注册的选项来源判定。来源答了「空列表」时(例如没有任何声明 lifecycle 的对象),以前跳过键校验、任何键都存进去,`OS_*` 也照样生效;现在两道门都拒绝(保存门 `invalid_option`,环境变量门记一条 error 且不生效)。另补两个缺失的形状 pin(数组值、多列 map 里的标量条目),并把 `lifecycle-service.ts` 里仍把这些键叫 `json` 的注释改正。 - 为什么改:#31386 复审的三处非阻塞项。空列表时放行任何键,与「列表里没有的键就拒绝」不一致。 - 风险与回滚:收窄(Clause-② narrowing):只影响「来源答了空列表」的主机上非空的这些 map 值;这时没有任何键能指向清扫会读的对象。空 map 仍可保存;来源没注册或答不出时,仍按声明的选项判定,未声明选项则放行(有对照 pin)。service-settings 全量测试全绿。回滚即还原本 PR。 - 先看:`packages/services/service-settings/src/settings-service.ts` 的 `mapShapeVerdict` 与 `optionTableOf`(新增 `sourced`),`settings-map-shape-30208.test.ts` 末尾两个 `[#31422]` describe。 - 两分钟验证:`pnpm --filter @objectstack/service-settings exec vitest run src/settings-map-shape-30208.test.ts`,预期 22 条全绿;其中 2 条在 main 上是红的。 - 需要你做的:无,席位落地(收窄,落地前席位做契约复审)。 ## What changes - **`SettingsService.optionTableOf`** (`service-settings`): the option table now says which table it is, `sourced: true` when a registered source answered (even with no options), `false` for the declared fallback. Its other two callers (the option-bearing save branch and the env override for non-map keys) ignore the new member and are unchanged. - **`SettingsService.mapShapeVerdict`**: the key column's domain applies whenever a source answered (`sourced || allowed.length > 0`). The `allowed.length > 0` gate is kept only for the declared-table fallback: a key with no source, or whose source could not answer, still admits every key when it declares none. Both doors call this one verdict, so the save door (`invalid_option`, "'k' is not a key this setting admits.") and the env door (one `error` line, the override not in force) move together. The docblock states the rule. - **`LifecycleService`** (`objectql`): comment only. The docblock at ~l.1162 now says these keys are `map` specifiers (#30208), `json` before it. ## Tests `packages/services/service-settings/src/settings-map-shape-30208.test.ts`, 6 new rows (16 → 22): - `[#31422] a key source that answered an empty list admits no key`, over `lifecycleObjectKeySource` on a registry with no lifecycle-declared object: the save door refuses `{ quotas: { sys_audit_log: 1000 } }` with one `invalid_option` naming the entry, nothing stored; the env door does not put `OS_LIFECYCLE_QUOTAS` in force (one error line). Controls: an empty map still saves; a source that could not answer (no registry) leaves the declared table, which declares no key, so any key passes. - `[#31422] the two shape misses #30208 left unpinned`: an array value is `invalid_type` (`not_a_map`, "…not an array…"); a scalar entry in the several-column `retention_overrides` is `invalid_shape` (`not_an_entry`, naming the entry and `maxAge, expireAfter`). Both are coverage pins and green on main. ## Acceptance notes - **Responsibility.** The three non-blocking nits of PR #31386's contract review (6717755004), recorded on #30208 (6717781477). - **Narrowing, stated.** On a host whose lifecycle key source answers no objects, a non-empty `quotas` / `retention_overrides` value, saved or in `OS_LIFECYCLE_*`, was accepted and is refused now. With no lifecycle-declared object, no key could name an object the sweep reads, so nothing that worked stops working. `quota_defaults`' source (`lifecycleClassKeySource`) never answers empty. - **Noted, not changed (out of the claim's surface).** The option-bearing save branch and the env override for non-map keys keep their own `allowed.length > 0` gate, so a select key whose registered source answers an empty list still admits any value. Same shape as item 1, on a different family; not filed. - **Worktree.** The claim names `objectstack-issue-31422`; this ran in the chain's existing worktree `objectstack-issue-31317` on the same claimed branch, cut from fresh `origin/main` after #31480 merged. ## Verification Head `66137fbc` (base `f4b1f9ee`, fresh `origin/main`, which holds #31480's merge `ec297534`). <details><summary>Red first, fix, suites</summary> - The new pins on main's source: Tests 2 failed | 20 passed (22), the save row (`expected 200 to be 400`) and the env row (`expected 'env' not to be 'env'`); the controls and the two coverage pins green. With the fix: 22 passed. - `@objectstack/service-settings` vitest: Test Files 73 passed (73), Tests 1407 passed (1407). - `typecheck` for `@objectstack/service-settings` and `@objectstack/objectql`: exit 0. `eslint --no-inline-config packages/services/service-settings packages/objectql/src/lifecycle`: exit 0. </details> <details><summary>Ablation (ablation-replace, restored to HEAD)</summary> - `settings-service.ts`: `const admitsKey = sourced || allowed.length > 0 ?` made to ignore `sourced` (`(sourced && String("ABL31422").length < 0) || …`). Mutation landed (anchor 1 to 0, blob 07699b2 to e0d112878a7f). Pin: Tests 2 failed | 20 passed (22), the same two rows. Restored: blob == HEAD (07699b2), `git diff HEAD` empty. The suite reads its own `src/`, so no dist leg. </details> <details><summary>Derived gates</summary> `node scripts/pm/dispatch-gates.mjs --commands --repo objectstack-ai/objectstack` at `7d88feba`: 70 commands, each run with `timeout 1500` and stdin from `/dev/null`, exit codes captured before any pipe, after a `@objectstack/runtime` closure and `@objectstack/service-settings` build. First pass: 69 exit 0; `check-adr-0087-registration --base origin/main` exit 1, the narrowing changeset had no ADR-0087 disposition. Fixed in `66137fbc` (`adr-0087: not-required (no-migration-prescription)`), then the ten changeset, citation and closing-keyword gates re-run at `66137fbc`: all exit 0. `dispatch-gates --ran`: 70 derived, 70 run, 0 NOT-MEASURED, 0 UNRUN. Not run locally (CI): `test:repo`, dogfood shards, type-check lanes. </details> --- _Generated by [Claude Code](https://claude.ai/code/session_01FEkQ5nBSzc6ktJyjdFcDb3)_ --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 6f9f008 commit dc8479e

4 files changed

Lines changed: 121 additions & 8 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
'@objectstack/service-settings': patch
3+
---
4+
5+
fix(service-settings): a `map` setting refuses every key when its key source answers an empty list
6+
7+
Clause-②: no (narrowing)
8+
Metadata-protocol: none
9+
10+
<!-- adr-0087: not-required (no-migration-prescription) no key of any schema is added, removed or re-shaped and no stored row is read differently: the narrowing is one runtime refusal at the settings save door and the OS_* env door, for a map key on a host whose key source answers no keys, and its remedy is to clear the value, so there is nothing for `objectstack migrate meta` to rewrite. A value already stored is not re-judged. -->
11+
12+
A `map` specifier's keys are judged against the key's option source (`registerOptionSource`), the list the running system answers, such as the lifecycle-declared objects for `lifecycle.quotas` and `lifecycle.retention_overrides`. When that source answered an empty list, the key check was skipped and any key was saved and put in force from `OS_*`. It now refuses each key with `invalid_option` at the save door and logs the override as not in force at the env door, as for any key the list does not hold. This narrows what is accepted: on a host with no lifecycle-declared object, a non-empty value for those keys, saved or set in `OS_LIFECYCLE_*`, was accepted before and is refused now; no such key could name an object the sweep reads. An empty map still saves. A key whose source could not answer, or which has no source, is still judged against its declared options and admits any key when it declares none.

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,8 +1159,9 @@ export class LifecycleService {
11591159
* (`retention_overrides`, `quotas`, `quota_defaults`) as the map this service
11601160
* indexes, or `undefined` for "nothing configured".
11611161
*
1162-
* These keys are `json` specifiers, which persist an object (ADR-0007). Until
1163-
* #31241 the console's `json` field sent the textarea's TEXT and the settings
1162+
* These keys are `map` specifiers (#30208; `json` specifiers before it),
1163+
* which persist an object (ADR-0007). Until #31241, when they were still
1164+
* `json`, the console's `json` field sent the textarea's TEXT and the settings
11641165
* save door stored it, so a deployment that saved this page holds a JSON
11651166
* string where the map belongs; indexing that string reads every override and
11661167
* quota as absent, with nothing logged. The save door refuses a string now,

‎packages/services/service-settings/src/settings-map-shape-30208.test.ts‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,94 @@ describe('[#30208] the env door judges a lifecycle map the same way', () => {
279279
expect(errors[0]).toContain('must be an object of keyed rows');
280280
});
281281
});
282+
283+
describe('[#31422] a key source that answered an empty list admits no key', () => {
284+
/** As `ObjectQLPlugin` registers them, over a registry with no lifecycle-declared object. */
285+
const bootWith = (registry: () => LifecycleKeyRegistryLike | undefined) =>
286+
bootSettingsDoor(lifecycleSettingsManifest, {
287+
before: (svc: SettingsService) => {
288+
svc.registerOptionSource('lifecycle', 'quotas', lifecycleObjectKeySource(registry));
289+
svc.registerOptionSource('lifecycle', 'retention_overrides', lifecycleObjectKeySource(registry));
290+
},
291+
});
292+
const NO_LIFECYCLE_OBJECTS: LifecycleKeyRegistryLike = {
293+
getAllObjects: () => [{ name: 'account', label: 'Account', lifecycle: { class: 'record' } }, { name: 'contact' }],
294+
};
295+
296+
it('the save door refuses every key: invalid_option, naming the entry; nothing stored', async () => {
297+
const { save, stored } = await bootWith(() => NO_LIFECYCLE_OBJECTS);
298+
299+
const st = await save({ quotas: { sys_audit_log: 1000 } });
300+
301+
expect(st.status).toBe(400);
302+
const fields = st.body.error.details.fields as Fields;
303+
expect(fields).toHaveLength(1);
304+
expect(fields[0]).toMatchObject({ field: 'quotas', code: 'invalid_option', constraint: { type: 'map', entry: 'sys_audit_log' } });
305+
expect(fields[0].message).toBe("Row quotas: 'sys_audit_log' is not a key this setting admits.");
306+
expect(stored('quotas')).toBeUndefined();
307+
});
308+
309+
it('the env door does not put such a map in force either', async () => {
310+
const errors: string[] = [];
311+
const svc = new SettingsService({
312+
env: { OS_LIFECYCLE_QUOTAS: '{"sys_audit_log": 1000}' },
313+
logger: { error: (m: string) => void errors.push(m), warn: () => {} } as any,
314+
});
315+
svc.registerOptionSource('lifecycle', 'quotas', lifecycleObjectKeySource(() => NO_LIFECYCLE_OBJECTS));
316+
svc.registerManifest(lifecycleSettingsManifest);
317+
318+
const got = await svc.get('lifecycle', 'quotas');
319+
320+
expect(got.source).not.toBe('env');
321+
expect(errors).toHaveLength(1);
322+
expect(errors[0]).toContain("'sys_audit_log' is not a key this setting admits.");
323+
});
324+
325+
it('CONTROL: an empty map still saves (a map with no entry names no key)', async () => {
326+
const { save, stored } = await bootWith(() => NO_LIFECYCLE_OBJECTS);
327+
328+
const st = await save({ quotas: {} });
329+
330+
expect(st.status).toBe(200);
331+
expect(stored('quotas')?.value).toEqual({});
332+
});
333+
334+
it('CONTROL: a source that could not answer (no registry) leaves the declared table, which declares no key: any key passes', async () => {
335+
const { save, stored } = await bootWith(() => undefined);
336+
337+
const st = await save({ quotas: { sys_audit_log: 1000 } });
338+
339+
expect(st.status).toBe(200);
340+
expect(stored('quotas')?.value).toEqual({ sys_audit_log: 1000 });
341+
});
342+
});
343+
344+
describe('[#31422] the two shape misses #30208 left unpinned', () => {
345+
it('an array is not a map: invalid_type (not_a_map), naming what was sent', async () => {
346+
const { save, stored } = await boot();
347+
348+
const st = await save({ quotas: [1000] });
349+
350+
expect(st.status).toBe(400);
351+
const fields = st.body.error.details.fields as Fields;
352+
expect(fields).toHaveLength(1);
353+
expect(fields[0]).toMatchObject({ field: 'quotas', code: 'invalid_type', constraint: { type: 'map' } });
354+
expect(String(fields[0].message)).toMatch(/^Row quotas must be an object of keyed rows, such as .*, not an array/);
355+
expect(stored('quotas')).toBeUndefined();
356+
});
357+
358+
it('a scalar entry in a several-column map: invalid_shape (not_an_entry), naming the entry and the columns', async () => {
359+
const { save, stored } = await boot();
360+
361+
const st = await save({ retention_overrides: { sys_job_run: '1y' } });
362+
363+
expect(st.status).toBe(400);
364+
const fields = st.body.error.details.fields as Fields;
365+
expect(fields).toHaveLength(1);
366+
expect(fields[0]).toMatchObject({ field: 'retention_overrides', code: 'invalid_shape', constraint: { type: 'map', entry: 'sys_job_run' } });
367+
expect(String(fields[0].message)).toMatch(
368+
/^Retention overrides: 'sys_job_run' must be an object with at least one of maxAge, expireAfter, such as .*, not a string of 2 characters\.$/,
369+
);
370+
expect(stored('retention_overrides')).toBeUndefined();
371+
});
372+
});

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,17 +1942,20 @@ export class SettingsService {
19421942
* the judgment of a written value: a registered source's answer when it has
19431943
* one, else `declared`, judged by {@link firstRejectedOption}. Both doors ask
19441944
* here, the save (`validatePatch`) and the `OS_*` override
1945-
* (`effectiveEnvOverride`), so a source moves both at once.
1945+
* (`effectiveEnvOverride`), so a source moves both at once. `sourced` says
1946+
* which table it is: `true` when a registered source answered (even with no
1947+
* options), `false` for the declared fallback.
19461948
*/
19471949
private optionTableOf(
19481950
namespace: string,
19491951
key: string,
19501952
declared: string[],
1951-
): { allowed: string[]; judge: (value: unknown) => { value: unknown } | null } {
1953+
): { allowed: string[]; sourced: boolean; judge: (value: unknown) => { value: unknown } | null } {
19521954
const answer = this.optionSourceAnswer(namespace, key);
1953-
if (!answer) return { allowed: declared, judge: (value) => firstRejectedOption(declared, value) };
1955+
if (!answer) return { allowed: declared, sourced: false, judge: (value) => firstRejectedOption(declared, value) };
19541956
return {
19551957
allowed: answer.options.map((opt) => opt.value),
1958+
sourced: true,
19561959
judge: (value) => {
19571960
// Element-wise and in string form, as `firstRejectedOption` judges.
19581961
const picked = Array.isArray(value) ? value : [value];
@@ -1970,7 +1973,13 @@ export class SettingsService {
19701973
* ONE judgement for both doors ({@link firstMapShapeMiss}); the key column's
19711974
* domain is the key's option table as {@link optionTableOf} serves it, a
19721975
* registered source's answer else the declared `options`, so a source moves
1973-
* both doors at once. An empty table admits every key.
1976+
* both doors at once.
1977+
*
1978+
* [#31422] A source that answered is the domain even when it answered no
1979+
* keys: the running system has nothing a key could name (a host with no
1980+
* lifecycle-declared object), so every key is refused. Only the declared
1981+
* fallback with no keys (no source registered, or one that could not answer)
1982+
* admits every key, as a key with no declared `options` always has.
19741983
*/
19751984
private mapShapeVerdict(
19761985
reg: RegisteredManifest,
@@ -1980,8 +1989,8 @@ export class SettingsService {
19801989
): { shape: DeclaredMapShape; miss: MapShapeMiss; allowedKeys: string[] } | null {
19811990
const map = reg.maps.get(key);
19821991
if (!map) return null;
1983-
const { allowed, judge } = this.optionTableOf(namespace, key, map.declaredKeys);
1984-
const admitsKey = allowed.length > 0 ? (entry: string) => judge(entry) === null : undefined;
1992+
const { allowed, sourced, judge } = this.optionTableOf(namespace, key, map.declaredKeys);
1993+
const admitsKey = sourced || allowed.length > 0 ? (entry: string) => judge(entry) === null : undefined;
19851994
const miss = firstMapShapeMiss(map.shape, value, admitsKey);
19861995
return miss ? { shape: map.shape, miss, allowedKeys: allowed } : null;
19871996
}

0 commit comments

Comments
 (0)