|
| 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