Skip to content

Commit b177b50

Browse files
committed
feat(service-settings): the namespace read lists what is stored beneath an env pin
getNamespace (the read GET /api/settings/:namespace serves) now lists, below the env entry of a pinned key's cascadeChain, the entries the key's read carries with no override in force: the stored rows the caller's cascade draws on, then the default. The env entry stays first and the only effective one; value, source, locked and lockedReason are unchanged. A secret's entries beneath the pin carry the read mask or null and are never decrypted. get() and getMany() keep answering a pin from the env alone. Claude-Session: https://claude.ai/code/session_016WV4d13xV55y9KgsteqXK9 Co-authored-by: Claude <noreply@anthropic.com>
1 parent c17719d commit b177b50

4 files changed

Lines changed: 131 additions & 12 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
'@objectstack/service-settings': patch
3+
---
4+
5+
feat(service-settings): the namespace read lists what is stored beneath a field an `OS_*` variable pins, so Setup can tell whether "clear the saved value" has anything to clear
6+
7+
Clause-②: yes (widening)
8+
Metadata-protocol: none
9+
10+
- **Now.** `getNamespace()`, the read `GET /api/settings/:namespace` serves, answers a key whose `OS_*` override is in force with the same `value`, `source: 'env'`, `locked: true` and `lockedReason` as before, and its `cascadeChain` now lists, below the `env` entry, the entries the key's read carries with no override in force: the stored rows the caller's cascade draws on (`global`, `tenant`, `user`, each with its own lock), then `default`. The `env` entry stays first and is still the only one flagged `effective`. An entry at the key's declared scope with a non-null value is the value a clear (`null`, accepted through a pin since the ADR-0007 D6 amendment) removes; after that clear the entry reads `null`.
11+
- **Secrets.** A secret key's entries beneath a pin carry the read mask when a value is stored and `null` when none is, in process too; the stored secret is never decrypted for them, so a key pinned because its stored secret no longer decrypts still reads.
12+
- **Unchanged.** `get()` and `getMany()` still answer an override in force from the env alone, without reading the store: they are the reads request paths call. An override whose value coerces to `null` keeps its single `env` entry, because any non-null entry listed below it would read as the effective one.
13+
- **Cost.** A pinned key joins the namespace read's existing grouped row load, so the read costs one more load only when every key of a group is pinned.

‎content/docs/protocol/kernel/config-resolution.mdx‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,19 @@ OS_ALLOWED_ORIGINS=["a","b","c"] # default is array/object → JSON.parse
262262

263263
If the raw value cannot be parsed for the expected type, the raw string is used.
264264

265+
**What is stored beneath a pin is listed by the namespace read.** An override in
266+
force answers `source: 'env'`, `locked: true`, and `get()` / `getMany()` build
267+
that answer from the env alone, without reading the store. `getNamespace()` — the
268+
read `GET /api/settings/:namespace` serves to the Setup page — also lists, below
269+
the `env` entry of `cascadeChain`, the entries the key's read would carry with no
270+
override in force: its stored rows, then the default. The `env` entry stays the
271+
effective one. An entry at the key's declared scope with a non-null value is the
272+
value a clear (`null`) removes, so the page can tell whether "clear the saved
273+
value" has anything to clear. A secret's entries beneath the pin carry the read
274+
mask or `null`, never its value. An override whose value coerces to `null` keeps
275+
its single `env` entry, since any non-null entry below it would read as the
276+
effective one.
277+
265278
**A value outside a declared option table is ignored, not repaired.** When the
266279
specifier declares `options` and the env value matches none of them, the env
267280
layer contributes nothing at all — no value and no `cascadeChain` entry — and

‎packages/services/service-settings/README.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ Such fields are returned with `source: 'env', locked: true`. Every write
3434
(service or REST) but a clear (`null`) fails with HTTP 409. A clear removes
3535
the value stored beneath the override and changes nothing in force: the key
3636
still answers the env value, and falls back to its default once the variable
37-
is removed.
37+
is removed. The namespace read (`getNamespace`, `GET /api/settings/:namespace`)
38+
lists the stored entries beneath such a field's `env` entry in `cascadeChain`,
39+
so a client can tell whether a clear has a value to remove (a secret's entry
40+
shows the mask, never the value); `get()` and `getMany()` answer it from the
41+
env alone.
3842

3943
## Encryption
4044

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

Lines changed: 100 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1949,6 +1949,10 @@ export class SettingsService {
19491949
// picked up by the `lockedEntry` scan below and reported as locking a value
19501950
// it is not even providing, which is the opposite of what the read API owes
19511951
// its caller.
1952+
//
1953+
// An override in force answers here without touching the store: this is
1954+
// the lightweight read request paths call. What is stored beneath the pin
1955+
// is listed by the inspection read, `getNamespace` (#30823).
19521956
const envOverride = this.effectiveEnvOverride(reg, namespace, key);
19531957
if (envOverride) {
19541958
const { envName, value } = envOverride;
@@ -2053,14 +2057,37 @@ export class SettingsService {
20532057
namespace: string,
20542058
keys: readonly string[],
20552059
ctx: SettingsContext = {},
2060+
): Promise<Record<string, ResolvedSettingValue>> {
2061+
return this.resolveMany(namespace, keys, ctx, false);
2062+
}
2063+
2064+
/**
2065+
* {@link getMany}, and, when `beneathPins` is set, the stored cascade
2066+
* beneath every in-force env override too ({@link withStoredBeneath}).
2067+
*
2068+
* `beneathPins` is the inspection read's, {@link getNamespace}: the
2069+
* namespace read the settings page draws from, where a field an `OS_*`
2070+
* variable pins must say whether a value is stored beneath the pin, so the
2071+
* page can offer the clear {@link setMany} lets through it (ADR-0007 D6).
2072+
* `getMany` and {@link get} leave it off: they are the lightweight resolver
2073+
* that request paths call (the localization read behind every request
2074+
* context), and an override answers there without touching the store.
2075+
* A pinned key joins the grouped load below, so the inspection read costs
2076+
* no extra load unless every key of a group is pinned.
2077+
*/
2078+
private async resolveMany(
2079+
namespace: string,
2080+
keys: readonly string[],
2081+
ctx: SettingsContext,
2082+
beneathPins: boolean,
20562083
): Promise<Record<string, ResolvedSettingValue>> {
20572084
const reg = this.registry.get(namespace);
20582085
if (!reg) throw new UnknownNamespaceError(namespace);
20592086
for (const key of keys) {
20602087
if (!reg.scopes.has(key)) throw new UnknownKeyError(namespace, key);
20612088
}
20622089
const out: Record<string, ResolvedSettingValue> = {};
2063-
const pending: Array<{ key: string; scope: SpecifierScope }> = [];
2090+
const pending: Array<{ key: string; scope: SpecifierScope; pinned?: true }> = [];
20642091
for (const key of keys) {
20652092
const envOverride = this.effectiveEnvOverride(reg, namespace, key);
20662093
if (envOverride) {
@@ -2074,6 +2101,12 @@ export class SettingsService {
20742101
{ scope: 'env', value, locked: true, lockedReason: `Set via env: ${envName}`, effective: true },
20752102
],
20762103
};
2104+
// An override whose value is null keeps its one entry: anything listed
2105+
// beneath it would be the first non-null entry of the chain, which the
2106+
// read contract makes the effective source, and the env value is.
2107+
if (beneathPins && value !== null && value !== undefined) {
2108+
pending.push({ key, scope: reg.scopes.get(key)!, pinned: true });
2109+
}
20772110
continue;
20782111
}
20792112
pending.push({ key, scope: reg.scopes.get(key)! });
@@ -2098,11 +2131,26 @@ export class SettingsService {
20982131
const sets = await this.loadRowSets(namespace, groups, reach);
20992132
const userRows = userKeys.length > 0 ? sets[0] : [];
21002133
const otherRows = otherKeys.length > 0 ? sets[sets.length - 1] : [];
2101-
for (const { key, scope } of userKeys) {
2102-
out[key] = await this.resolveKeyFromRows(reg, key, scope, userRows, userId, reach);
2134+
const resolve = async (
2135+
{ key, scope, pinned }: (typeof pending)[number],
2136+
rows: SettingsRow[],
2137+
rowUserId: string | null,
2138+
): Promise<ResolvedSettingValue> => {
2139+
if (!pinned) return this.resolveKeyFromRows(reg, key, scope, rows, rowUserId, reach);
2140+
// A secret beneath a pin is not in force, so no caller needs its
2141+
// plaintext, and decrypting it could only fail the read: its rows say
2142+
// whether a value is stored, as the REST read's mask says it.
2143+
const materialise = reg.encryptedKeys.has(key)
2144+
? async (row: SettingsRow) => storedSecretPresence(row)
2145+
: undefined;
2146+
const stored = await this.resolveKeyFromRows(reg, key, scope, rows, rowUserId, reach, materialise);
2147+
return withStoredBeneath(out[key], stored);
2148+
};
2149+
for (const entry of userKeys) {
2150+
out[entry.key] = await resolve(entry, userRows, userId);
21032151
}
2104-
for (const { key, scope } of otherKeys) {
2105-
out[key] = await this.resolveKeyFromRows(reg, key, scope, otherRows, null, reach);
2152+
for (const entry of otherKeys) {
2153+
out[entry.key] = await resolve(entry, otherRows, null);
21062154
}
21072155
}
21082156
return out;
@@ -2123,6 +2171,9 @@ export class SettingsService {
21232171
* The tenant and user rungs each take ONE row, the one the reach owns
21242172
* ({@link preferredRow}) — never whichever row a positional `find` happens
21252173
* to meet first.
2174+
*
2175+
* `materialise` turns a row into the value its entry carries; by default
2176+
* {@link materialiseRow}, which decrypts a secret.
21262177
*/
21272178
private async resolveKeyFromRows<T = unknown>(
21282179
reg: RegisteredManifest,
@@ -2131,6 +2182,7 @@ export class SettingsService {
21312182
rows: SettingsRow[],
21322183
userId: string | null,
21332184
reach: OrganizationReach,
2185+
materialise: (row: SettingsRow) => Promise<unknown> = (row) => this.materialiseRow(row),
21342186
): Promise<ResolvedSettingValue<T>> {
21352187
// 2. cascade walk — OS_* env (handled by callers) > global > tenant > user > default
21362188
//
@@ -2141,7 +2193,7 @@ export class SettingsService {
21412193

21422194
const globalRow = rows.find((r) => r.key === key && r.scope === 'global');
21432195
if (globalRow) {
2144-
const value = await this.materialiseRow(globalRow);
2196+
const value = await materialise(globalRow);
21452197
chain.push({
21462198
scope: 'global',
21472199
value,
@@ -2158,7 +2210,7 @@ export class SettingsService {
21582210
if (tenantRow) {
21592211
chain.push({
21602212
scope: 'tenant',
2161-
value: await this.materialiseRow(tenantRow),
2213+
value: await materialise(tenantRow),
21622214
locked: !!tenantRow.locked,
21632215
lockedReason: tenantRow.locked_reason ?? undefined,
21642216
});
@@ -2177,7 +2229,7 @@ export class SettingsService {
21772229
if (userRow) {
21782230
chain.push({
21792231
scope: 'user',
2180-
value: await this.materialiseRow(userRow),
2232+
value: await materialise(userRow),
21812233
});
21822234
}
21832235
}
@@ -2281,7 +2333,17 @@ export class SettingsService {
22812333
return posture !== undefined && postureEnforcesWall(posture);
22822334
}
22832335

2284-
/** Resolve every value in a namespace + return the manifest. */
2336+
/**
2337+
* Resolve every value in a namespace + return the manifest.
2338+
*
2339+
* The inspection read: the one the settings page draws from
2340+
* (`GET /api/settings/:namespace` serves it, redacted). Each key resolves
2341+
* as {@link getMany} resolves it, and a key an `OS_*` override pins also
2342+
* lists, beneath its env entry, the stored cascade the key falls back to
2343+
* once the override is removed (#30823) — so the page can tell whether a
2344+
* value is stored beneath the pin, at the scope a clear would null. A
2345+
* secret's entries there carry the read mask or `null`, never its value.
2346+
*/
22852347
async getNamespace(
22862348
namespace: string,
22872349
ctx: SettingsContext = {},
@@ -2294,8 +2356,9 @@ export class SettingsService {
22942356
this.assertPermitted(reg.manifest, 'read', ctx);
22952357

22962358
// [#10826] One grouped row load instead of one per key — same resolution
2297-
// per key by construction (getMany shares get()'s extracted cascade).
2298-
const values = await this.getMany(namespace, Array.from(reg.scopes.keys()), ctx);
2359+
// per key by construction (getMany shares get()'s extracted cascade), and
2360+
// [#30823] the stored cascade beneath each pin from that same load.
2361+
const values = await this.resolveMany(namespace, Array.from(reg.scopes.keys()), ctx, true);
22992362
// A key whose options a source serves is offered what the running system
23002363
// offers, for the value in effect (`registerOptionSource`).
23012364
return { manifest: this.servedManifest(reg, namespace, values, display), values };
@@ -4110,6 +4173,32 @@ export class SettingsService {
41104173
// Local helpers
41114174
// ---------------------------------------------------------------------------
41124175

4176+
/**
4177+
* [#30823] An env-pinned answer with the stored cascade beneath the pin: the
4178+
* env entry stays first and the effective one, and below it come the entries
4179+
* the key's read lists when no override is in force (the rows the caller's
4180+
* cascade draws on, then the default), so a reader can tell whether a value is
4181+
* stored at the scope a clear would null. Their own `effective` flag goes:
4182+
* the stored pick is not what is in force. Nothing else of the answer moves.
4183+
*/
4184+
function withStoredBeneath(pinned: ResolvedSettingValue, stored: ResolvedSettingValue): ResolvedSettingValue {
4185+
const beneath = (stored.cascadeChain ?? []).map((entry) => {
4186+
const copy = { ...entry };
4187+
delete copy.effective;
4188+
return copy;
4189+
});
4190+
return { ...pinned, cascadeChain: [...(pinned.cascadeChain ?? []), ...beneath] };
4191+
}
4192+
4193+
/**
4194+
* What a secret's row shows beneath a pin: the read mask when it stores a
4195+
* value, else `null`. Read off the row, never decrypted.
4196+
*/
4197+
function storedSecretPresence(row: SettingsRow): string | null {
4198+
const stored = row.encrypted ? row.value_enc : row.value;
4199+
return stored === null || stored === undefined ? null : SETTINGS_SECRET_MASK;
4200+
}
4201+
41134202
/** Stable stringify so the audit digest is order-independent. */
41144203
function stableStringify(input: unknown): string {
41154204
if (input === null || typeof input !== 'object') return JSON.stringify(input);

0 commit comments

Comments
 (0)