Skip to content

Commit 299f4c2

Browse files
committed
refactor(metadata-protocol): move the protocol.ts preamble into 22 themed modules, verbatim
Every top-level statement above `export class ObjectStackProtocolImplementation` (183 declarations) moves, byte for byte with its leading comments, into a themed `protocol-*.ts` module. Only import lists, export lists and a module header comment are new; protocol.ts re-exports every name it exported before. Claude-Session: https://claude.ai/code/session_01ADHkmR4MjTzeUv5x6tNkbk Co-authored-by: Claude <noreply@anthropic.com>
1 parent eb14f7a commit 299f4c2

23 files changed

Lines changed: 6866 additions & 6302 deletions
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
// [#25467 E0] Part of the module preamble of `protocol.ts`, moved here verbatim: the metadata
4+
// audit entry, the pending-audit carrier and the preflight violation codes.
5+
//
6+
// A MOVE, not a redesign: every declaration below is byte-identical to the text that stood
7+
// above `ObjectStackProtocolImplementation`; only the imports and the export list at the foot
8+
// are new. `protocol.ts` re-exports what it exported.
9+
import {
10+
type MetadataLock,
11+
} from '@objectstack/spec/kernel';
12+
13+
/**
14+
* One `sys_metadata_audit` row as a VALUE — exactly the entry
15+
* `recordMetadataAudit` writes, named so a refusal can hand its row to whoever
16+
* is in a position to make it durable instead of writing it where it stands.
17+
*
18+
* [#8594] Why the entry became a value. `promoteDraftForPublish` used to audit
19+
* its own denials inline, and it has two callers standing on opposite sides of
20+
* a transaction boundary: `publishMetaItem` runs it in no transaction of its
21+
* own, `publishPackageDrafts` runs it inside ONE `engine.transaction()` for the
22+
* whole batch (ADR-0067 D2). A denial row written inline therefore landed on
23+
* the single-item route and ROLLED BACK on the batch route — destroyed by the
24+
* very rollback its own refusal had caused, which is the defect #7748 exists to
25+
* close. Making the row data lets the helper stay ignorant of whose transaction
26+
* it is inside (it cannot know, and guessing is what broke) and puts the
27+
* placement decision where the transaction actually is: in the caller.
28+
*/
29+
export interface MetadataAuditEntry {
30+
type: string;
31+
name: string;
32+
/**
33+
* [ADR-0131 D6/D7] Always environment-wide: the audit ledger is
34+
* deployment-level, and no metadata write is organization-scoped any more.
35+
* Typed `null` so an organization cannot be recorded by accident.
36+
*/
37+
organizationId?: null;
38+
operation: 'save' | 'publish' | 'rollback' | 'delete' | 'reset';
39+
outcome: 'allowed' | 'denied' | 'forced';
40+
code: string;
41+
lockState?: MetadataLock;
42+
lockOverridden?: boolean;
43+
actor?: string;
44+
source?: string;
45+
requestId?: string;
46+
note?: string;
47+
}
48+
49+
/** Attach the denial row `err` owes, and return `err` so call sites read as one `throw`. */
50+
function withPendingAudit<E extends Error>(err: E, audit: MetadataAuditEntry): E {
51+
(err as E & { __pendingAudit?: MetadataAuditEntry }).__pendingAudit = audit;
52+
return err;
53+
}
54+
55+
/**
56+
* [#8595] The PRE-FLIGHT gate's refusal vocabulary — the closed set of codes
57+
* `publishPackageDrafts` can refuse a batch with BEFORE it opens its
58+
* transaction, and the spelling each one takes in `sys_metadata_audit.code`.
59+
*
60+
* Why a declared map and not `code.toLowerCase()`. `batch_aborted` (#8400)
61+
* deliberately took ONE fixed value because its causal code is whatever the
62+
* error catalog threw, and lower-casing that would turn this column's
63+
* documented closed set into an open one that grows with the catalog. The
64+
* pre-flight codes are a different animal — they are minted in
65+
* `publishPackageDrafts`'s own pre-flight block, not caught from below — so the
66+
* violation's own code CAN be carried honestly, which is what a compliance
67+
* query filters on ("show me every publish refused by the namespace rule").
68+
*
69+
* The precedent's principle still binds, though: the column must not drift
70+
* silently. Hence `Record<PreflightViolationCode, …>` over an index signature —
71+
* a new pre-flight gate (ADR-0121's endpoint gates, should #2657 Part B bring
72+
* them back) cannot reach this column without failing to compile until its
73+
* audit spelling is declared here AND added to the column's documented set in
74+
* `sys-metadata-audit.object.ts`. A mechanical lower-case would have let it
75+
* land silently; a runtime fallback would have put an unreachable value in a
76+
* documented closed set. The compiler is the enforcement.
77+
*/
78+
type PreflightViolationCode = 'NAMESPACE_PREFIX' | 'STORED_TYPE_NOT_CANONICAL';
79+
80+
const PREFLIGHT_AUDIT_CODE: Record<PreflightViolationCode, string> = {
81+
// adr0112-ok: D6b — persisted audit column, its own lowercase vocabulary
82+
NAMESPACE_PREFIX: 'namespace_prefix',
83+
// adr0112-ok: D6b — persisted audit column, its own lowercase vocabulary
84+
STORED_TYPE_NOT_CANONICAL: 'stored_type_not_canonical',
85+
};
86+
87+
// Shared with `protocol.ts` and the sibling preamble modules; module-private before the move.
88+
export { PREFLIGHT_AUDIT_CODE, withPendingAudit };
89+
export type { PreflightViolationCode };
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
// [#25467 E0] Part of the module preamble of `protocol.ts`, moved here verbatim: the authoring
4+
// gate, the credential channel and the authoring channel contracts.
5+
//
6+
// A MOVE, not a redesign: every declaration below is byte-identical to the text that stood
7+
// above `ObjectStackProtocolImplementation`; only the imports and the export list at the foot
8+
// are new. `protocol.ts` re-exports what it exported.
9+
10+
/**
11+
* Pre-persistence authoring gate (ADR-0094 addendum seam; #3050).
12+
*
13+
* Unlike the post-persist {@link MetadataMutationProjector} (best-effort,
14+
* never thrown), an authoring gate runs BEFORE persistence and REJECTS the
15+
* write by throwing — it is the seam for domain invariants that must hold on
16+
* every runtime-authored body regardless of which HTTP surface produced it
17+
* (e.g. plugin-security's OWD posture gate: an environment may only TIGHTEN
18+
* a packaged object's `sharingModel`).
19+
*
20+
* Invoked inside `saveMetaItem` for BOTH draft and publish-mode saves, after
21+
* the ADR-0005 overlay/runtime-create authorization and the per-type spec
22+
* validation — so `publishMetaItem` promotes an already-gated body and needs
23+
* no second gate. Environment writes only: control-plane bootstrap writes
24+
* (`environmentId === undefined`) are the package author's own channel and
25+
* bypass the gate, mirroring the ADR-0005 gate above.
26+
*/
27+
export interface MetadataAuthoringGateContext {
28+
/** Singular type name (e.g. `object`). */
29+
type: string;
30+
name: string;
31+
/** Lifecycle the body is being saved into. */
32+
state: 'draft' | 'active';
33+
/** The body being persisted. */
34+
body: unknown;
35+
/** True when a packaged artifact backs this name — the write is an env overlay of shipped metadata. */
36+
isArtifactBacked: boolean;
37+
/** The packaged (code-layer) baseline body when {@link isArtifactBacked}; the declaration an overlay customizes. */
38+
declaredBody?: unknown;
39+
}
40+
41+
export type MetadataAuthoringGate = (ctx: MetadataAuthoringGateContext) => void | Promise<void>;
42+
43+
/**
44+
* [#20790] A metadata type's WRITE-ONLY credential channel — where the
45+
* credentials its bodies carry are stored instead of in the body, on the
46+
* platform's one secret seam (#7799: a `secret`-typed field the engine
47+
* encrypts, masks on every read and dereferences only through
48+
* `resolveSecretField`). Registered per type by the domain plugin that owns
49+
* the type's credential-location table (the automation plugin holds `flow`'s),
50+
* beside its authoring gate.
51+
*
52+
* Every write that lands a body at rest consults it:
53+
* - `saveMetaItem` calls {@link store} immediately before the put, after the
54+
* carry-forward, and persists what it returns;
55+
* - the runtime authoring gate reads {@link heldPaths} as restored positions,
56+
* on an active save and on the draft → active promotion;
57+
* - a restore (rollback, revert) stores {@link strip}'s body.
58+
*
59+
* ⛔ Not a second secret mechanism and not a per-door redaction: the read
60+
* projection stays the type's redactor; this only decides where a credential
61+
* is STORED.
62+
*/
63+
export interface MetadataCredentialChannel {
64+
/**
65+
* Move every explicit credential in `body` into the channel for
66+
* `(name, state)` and return the body without any — what is stored. An
67+
* absent credential means "unchanged". THROWS (with an ADR-0112
68+
* `code`/`status`) to refuse the save; nothing may have been put then.
69+
*/
70+
store(args: { name: string; state: 'draft' | 'active'; body: unknown }): Promise<unknown>;
71+
/**
72+
* The positions in `item` (dotted, item-relative — `redactedKeys`'
73+
* spelling) whose credential is withheld from the body and held by the
74+
* channel, so the gate reads them as present. `state: 'draft'` for a draft
75+
* being promoted.
76+
*/
77+
heldPaths(args: { name: string; state: 'draft' | 'active'; item: unknown }): Promise<readonly string[]>;
78+
/**
79+
* The body a restore stores: every credential removed, and nothing written
80+
* to the channel — it keeps its current credential.
81+
*/
82+
strip(body: unknown): unknown;
83+
}
84+
85+
/**
86+
* Which authoring channel a kernel's metadata writes arrive on (#6710).
87+
*
88+
* ADR-0005 carves out "the package author's own bootstrap channel" from the
89+
* runtime authoring rules: a control-plane kernel installing a package is not
90+
* an author publishing into a live tenant, and gating it would refuse the very
91+
* bodies the platform ships. The carve-out is legitimate and stays.
92+
*
93+
* What #6710 changed is **how a kernel says it is that channel**. Until this
94+
* type existed the answer was inferred from `environmentId === undefined` — a
95+
* ROW-SCOPING key pressed into service as a topology signal. That proxy lies:
96+
* the CLI's lightweight host-config assembler (`serve.ts`'s auto-register
97+
* branch, `new ObjectQLPlugin()` with no options) also leaves `environmentId`
98+
* undefined, and it serves an END-USER `PUT /api/v1/meta/*`. Both topologies
99+
* read identically, so the whole #4463 gate — all the shared `AUTHORING_RULES`
100+
* — was disengaged on a self-hosted app server. #5086 had already moved the
101+
* code-only refusal off the same proxy for the same reason ("keying
102+
* authorization off a row-scoping key is what made a type-level declaration
103+
* depend on deployment topology; the declaration decides it here instead").
104+
*
105+
* So the channel is now DECLARED, not inferred:
106+
*
107+
* - `'environment'` — metadata arrives from an author (Studio tenant, MCP/AI
108+
* agent, `PUT /api/v1/meta/*`). The gate runs. **This is the default**, and
109+
* the default is the whole point: an assembly that forgets to declare gets
110+
* MORE enforcement, never less, so the next assembly variant nobody thought
111+
* about cannot silently reopen this hole.
112+
* - `'package-author'` — this kernel IS the package author's own bootstrap
113+
* channel. Only the genuine control-plane assembly may state this.
114+
*
115+
* Deliberately a channel NAME and not a boolean: `skipAuthoringRules: true`
116+
* would be the same bytes with the opposite meaning — a kill switch any
117+
* assembly could reach for to make a red publish go away. A caller has to
118+
* claim to BE the package author to be treated as one.
119+
*/
120+
export type MetadataAuthoringChannel = 'environment' | 'package-author';

0 commit comments

Comments
 (0)