|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#28355] The `denied` audit row a single-item metadata door owes for a |
| 5 | + * refusal that writes no row of its own. |
| 6 | + * |
| 7 | + * `sys_metadata_audit.code` documents `not_overridable`, `not_creatable`, |
| 8 | + * `invalid_metadata` and `destructive_change` as `denied` values, and the four |
| 9 | + * single-item doors (`saveMetaItem`, `publishMetaItem`, `rollbackMetaItem`, |
| 10 | + * `deleteMetaItem`) do refuse with exactly those catalogued codes — but only |
| 11 | + * the lock gate (`item_locked`) and the optimistic-concurrency check |
| 12 | + * (`metadata_conflict`) ever wrote a row. Every other refusal left the trail |
| 13 | + * silent, so a reader of `/audit` concluded that a refused attempt never |
| 14 | + * happened. |
| 15 | + * |
| 16 | + * The row is stamped where the door hands the refusal back to its caller, |
| 17 | + * not at each of the many sites inside the door that can raise one: those |
| 18 | + * codes are thrown by shared helpers the doors reach in several places |
| 19 | + * (`refuseOrganizationScopedWrite`, the overlay and create gates, the |
| 20 | + * authoring-rule and spec gates, the destructive-change gate), so a per-site |
| 21 | + * write would leave any site added later silent again. One seam per door |
| 22 | + * reads the code the refusal carries and nothing else. |
| 23 | + * |
| 24 | + * What it deliberately does NOT do: |
| 25 | + * |
| 26 | + * - It mints no new value. The map below is closed, and each value is one |
| 27 | + * the column already documents. A refusal whose code is not in it — a |
| 28 | + * driver fault, `INVALID_REQUEST`, `RESOURCE_CONFLICT`, any 5xx — leaves |
| 29 | + * no row, exactly as before; lower-casing whatever `code` arrived would |
| 30 | + * turn the column's closed set into an open one (the `batch_aborted` |
| 31 | + * argument in the object file). |
| 32 | + * - It does not re-stamp `item_locked` or `metadata_conflict`: their gates |
| 33 | + * already write their own row, and neither code is in the map. |
| 34 | + * - It writes one row per refusal even when one door hands it back through |
| 35 | + * another: the first seam to record a refusal marks it, and every outer |
| 36 | + * seam it passes through after that leaves it alone. |
| 37 | + * - The batch publish keeps its own `batch_aborted` row with the causal code |
| 38 | + * in `note`; it is not one of these doors. |
| 39 | + * |
| 40 | + * `note` carries {@link clientFacingFailureText}: it is wire-visible through |
| 41 | + * `GET /meta/:type/:name/audit`, and these refusals declare a 4xx, so their |
| 42 | + * sentence is the one the caller was already shown. |
| 43 | + */ |
| 44 | + |
| 45 | +import type { MetadataAuditEntry } from './protocol.js'; |
| 46 | +import { canonicalMetaType, clientFacingFailureText } from './protocol.js'; |
| 47 | + |
| 48 | +/** |
| 49 | + * The catalogued refusal codes (ADR-0112, SCREAMING) whose door stamps a |
| 50 | + * `denied` row, and the persisted audit value each one is stored as (D6b — |
| 51 | + * the column keeps its own lower-case vocabulary). |
| 52 | + */ |
| 53 | +export const REFUSAL_AUDIT_CODES: Readonly<Record<string, string>> = Object.freeze({ |
| 54 | + NOT_OVERRIDABLE: 'not_overridable', |
| 55 | + NOT_CREATABLE: 'not_creatable', |
| 56 | + INVALID_METADATA: 'invalid_metadata', |
| 57 | + DESTRUCTIVE_CHANGE: 'destructive_change', |
| 58 | +}); |
| 59 | + |
| 60 | +/** The door a refusal left, and the request fields its row is keyed on. */ |
| 61 | +export interface RefusalAuditDoor { |
| 62 | + operation: 'save' | 'publish' | 'rollback' | 'delete'; |
| 63 | + type: unknown; |
| 64 | + name: unknown; |
| 65 | + actor: unknown; |
| 66 | + source: string; |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Name-mangled like `__pendingAudit`: an internal hand-off between two seams, |
| 71 | + * never a wire field. `clientFacingFailureText` / `clientFacingFailureCode` |
| 72 | + * read `message` / `code` only. |
| 73 | + */ |
| 74 | +type RefusalWithAuditMark = { code?: unknown; __refusalAudited?: true }; |
| 75 | + |
| 76 | +/** |
| 77 | + * The row `err` owes at `door`, or `null` when it owes none: its code is not |
| 78 | + * one of {@link REFUSAL_AUDIT_CODES}, an earlier seam already recorded it, or |
| 79 | + * the request names no item to key the row on. |
| 80 | + */ |
| 81 | +export function refusalAuditEntry(err: unknown, door: RefusalAuditDoor): MetadataAuditEntry | null { |
| 82 | + if (err === null || typeof err !== 'object') return null; |
| 83 | + const refusal = err as RefusalWithAuditMark; |
| 84 | + if (refusal.__refusalAudited) return null; |
| 85 | + if (typeof refusal.code !== 'string' || !Object.prototype.hasOwnProperty.call(REFUSAL_AUDIT_CODES, refusal.code)) return null; |
| 86 | + // No row without an identity: the trail is keyed on `(type, name)`, and |
| 87 | + // minting one for a request that names no item would file a fictional |
| 88 | + // item in a compliance ledger. |
| 89 | + if (typeof door.type !== 'string' || door.type === '') return null; |
| 90 | + if (typeof door.name !== 'string' || door.name === '') return null; |
| 91 | + return { |
| 92 | + // The writer refuses a non-canonical type; the door may be handed a |
| 93 | + // plural spelling it has not folded yet when the refusal comes first. |
| 94 | + type: canonicalMetaType(door.type), |
| 95 | + name: door.name, |
| 96 | + organizationId: null, |
| 97 | + operation: door.operation, |
| 98 | + outcome: 'denied', |
| 99 | + // adr0112-ok: D6b — persisted audit column, its own vocabulary |
| 100 | + code: REFUSAL_AUDIT_CODES[refusal.code]!, |
| 101 | + ...(typeof door.actor === 'string' && door.actor !== '' ? { actor: door.actor } : {}), |
| 102 | + source: door.source, |
| 103 | + note: clientFacingFailureText(err, refusal.code), |
| 104 | + }; |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * Run a door's body; when it refuses with a code that owes a row, record the |
| 109 | + * row and rethrow the same refusal untouched. |
| 110 | + * |
| 111 | + * `record` is the protocol's own `recordMetadataAudit`, handed in as a |
| 112 | + * closure so a spy set on the instance is the one reached. It is best-effort |
| 113 | + * by contract (ADR-0010 §3.6) and never replaces the refusal the caller gets. |
| 114 | + */ |
| 115 | +export async function withRefusalAudit<T>( |
| 116 | + record: (entry: MetadataAuditEntry) => Promise<void>, |
| 117 | + door: RefusalAuditDoor, |
| 118 | + run: () => Promise<T>, |
| 119 | +): Promise<T> { |
| 120 | + try { |
| 121 | + return await run(); |
| 122 | + } catch (err) { |
| 123 | + const entry = refusalAuditEntry(err, door); |
| 124 | + if (entry) { |
| 125 | + (err as RefusalWithAuditMark).__refusalAudited = true; |
| 126 | + await record(entry); |
| 127 | + } |
| 128 | + throw err; |
| 129 | + } |
| 130 | +} |
0 commit comments