Repository navigation
Expand file tree
/
Copy pathengine-execution-plumbing.ts
More file actions
216 lines (210 loc) · 11.9 KB
/
Copy pathengine-execution-plumbing.ts
File metadata and controls
216 lines (210 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
/**
* [#25522 cut 9] The caller-envelope builders of `ObjectQL`'s execution
* plumbing, moved out of `engine.ts` whole: `buildSession` (the
* `HookContext.session` a hook reads, WHO is calling), `buildProvenance` (the
* `HookContext.provenance` envelope, WHERE a write came from), `buildEvalUser`
* (the ADR-0068 `current_user` validation-time predicates read) and
* `buildUser` (the `HookContext.user` shortcut).
*
* A MOVE, not a redesign. Each exported function below is the body of the
* `ObjectQL` method of the same name as it stood, re-indented one level and
* otherwise unedited: the same copy list, the same flags carried only when
* set, and the same `undefined` for a context with nothing to say. Each
* method's docblock stays on the method in `engine.ts`, which is still the
* contract.
*
* What stays in the class: `executeWithMiddleware`, because ADR-0139 cites it
* by `engine.ts#executeWithMiddleware`; `buildReferentialFieldClear` and
* `claimReferentialCascadeDelete`, whose bodies are no longer than the call
* (and, for the second, the host literal) moving them would cost.
*
* ## How the moved bodies reach the class
*
* None of the four reads a member of the engine: each is a function of the
* `ExecutionContext` it is handed. So there is no host object here — there
* is no shared state to hand over — and each method keeps its name,
* modifiers, signature, docblock and position and calls the function of the
* same name directly, as the member-free bodies of earlier cuts do
* (`engine-validation-messages.ts#objectDisplayLabel`).
*
* This module imports nothing from the engine, so there is no module cycle at
* run time, and nothing re-exports it, so the published surface does not
* move.
*
* [#30053 codemod] A PRIVATE method's docblock has since moved here,
* onto its function (`--hand-docs` of `scripts/codemod-split-engine.mjs`),
* and the class keeps none for it; a public or protected method's docblock
* stays on the class, the contract, and its function keeps the placeholder.
*/
import type { HookContext } from '@objectstack/spec/data';
import type { ExecutionContext } from '@objectstack/spec/kernel';
/**
* Build a HookContext.session from ExecutionContext — WHO is calling.
*
* Returns `undefined` when the context yields nothing session-worthy, which
* keeps `!ctx.session` meaning exactly one thing for hooks: "no identity
* envelope was supplied" — the bare-kernel / programmatic call that
* caller-gating hooks skip for. A context carrying only write PROVENANCE
* (`{ flowRunId }`, all an identity-less flow run has — #3712) is such a
* case: it says what produced the write, not who is calling, and surfaces
* through {@link buildProvenance} instead. Every real transport resolves
* `positions` into the context, so an anonymous HTTP request still yields a
* session and stays gated.
*/
export function buildSession(execCtx?: ExecutionContext): HookContext['session'] {
if (!execCtx) return undefined;
const session = {
userId: execCtx.userId,
// `organizationId` is the blessed developer-facing name for the caller's
// active org (matches the `organization_id` column, `current_user`
// RLS shape, and seed rows). It comes from `execCtx.tenantId`, which the
// kernel resolves from `session.activeOrganizationId`. The deprecated
// `session.tenantId` alias (#3280) was removed here in v16 (#3290) — the
// driver-layer `execCtx.tenantId` knob is a separate axis and stays.
organizationId: execCtx.tenantId,
positions: execCtx.positions,
accessToken: execCtx.accessToken,
// Propagate system-elevated flag so hooks can distinguish engine
// self-writes (e.g. approval status mirror) from genuine user writes.
...((execCtx as any).isSystem ? { isSystem: true } : {}),
// Propagate the service-principal label (`ExecutionContext.actor`,
// e.g. `svc:flow:<name>`) so a non-user write stays attributable in the
// audit log — the writer's `userId ?? session.actor` fallback is dead
// without this hop (the `ExecutionContext.actor` audit-attribution
// contract, `@objectstack/spec` kernel/execution-context.zod.ts; #4366).
...(typeof (execCtx as any).actor === 'string' && (execCtx as any).actor
? { actor: (execCtx as any).actor }
: {}),
// Propagate the automation-suppression flag so the record-change trigger
// can skip flow dispatch for seed/bulk writes (ADR: seed loads end-state
// data, not user events). `skipAutomations` implies `skipTriggers` —
// suppressing metadata hooks while still dispatching flows would leave
// the "run automations & triggers" opt-out half-working (#2922).
...((execCtx as any).skipTriggers || (execCtx as any).skipAutomations ? { skipTriggers: true } : {}),
// Propagate the full automation opt-out so `triggerHooks` can skip
// metadata-bound hooks (import with "run automations" unchecked, undo).
...((execCtx as any).skipAutomations ? { skipAutomations: true } : {}),
// Propagate the historical-import audit-preservation flag so the built-in
// audit hook keeps a client-supplied updated_at/updated_by instead of
// stamping now (#3493). Opt-in, server-set only.
...((execCtx as any).preserveAudit ? { preserveAudit: true } : {}),
} as HookContext['session'];
// Nothing to say about the caller → say nothing. An object whose every
// property is `undefined` conveys no more than no session at all, and
// returning it would turn "no caller" into "an anonymous caller".
if (!Object.values(session as Record<string, unknown>).some((v) => v !== undefined)) return undefined;
// [#26170] The caller's locale rides on a session that exists, so the
// sandboxed body runner can answer a declared hook refusal in the caller's
// language. It is asked AFTER the presence test on purpose: a locale alone
// says how to speak, not who is speaking, and must not turn "no caller"
// into a session either.
const locale = (execCtx as { locale?: unknown }).locale;
if (typeof locale === 'string' && locale.length > 0) (session as { locale?: string }).locale = locale;
// [ADR-0151 D5 · O4] The principal's kind rides on the same existing session,
// copied verbatim — never defaulted to 'human', so a context that lost its
// kind reads as unstated rather than as a person — and asked after the
// presence test for the reason `locale` is: what sort of caller is not who.
const principalKind = execCtx.principalKind;
if (principalKind !== undefined) session!.principalKind = principalKind;
return session;
}
/**
* Build the HookContext.provenance envelope — WHERE this write came from.
*
* Deliberately separate from {@link buildSession}: provenance is server-
* stamped, evaluated by no security middleware, and can exist with no
* identity beside it. A schedule-triggered flow run resolves no principal
* yet still owns its writes, and that is the case the approvals record lock
* needs to recognize (#3456 / #3712).
*
* `attributedUserId` rides the SAME envelope for the same reason (#4586):
* a better-auth-originated write authorizes as the system, so the human who
* triggered it must reach the audit writer WITHOUT appearing in `session` —
* where every caller-gating hook would read them as the caller. Attribution
* here, authorization in `session`/`isSystem`, never the two mixed.
*
* `performedByClientId` rides it for the THIRD time on the same reasoning
* (#17022, ADR-0090 D10 rule 4): an MCP OAuth agent's write authorizes as
* the human it acts for, so the acting client can only reach the audit
* writer through a channel that is not `session` — putting it there would
* make every caller-gating hook read the client as the caller, which is the
* opposite of what the delegation means. This copy list is the whole reason
* the field is not inert: `HookContext` is built from a CLOSED literal whose
* `provenance` value is exactly what this function returns, so a key
* declared on `hook.zod.ts` and missing here is declared and never
* populated (ADR-0049).
*
* `impersonatedBy` rides it for the FOURTH time: an impersonation session's
* write authorizes as the impersonated user, so the administrator at the
* keyboard reaches the audit writer here and never through `session`, where
* every caller-gating hook would read the administrator as the caller.
*
* `performedByAgentId`, `performedByConversationId` and
* `performedByConfirmationId` ride it on the reasoning of
* `performedByClientId`: the server-constructed in-product AI path writes AS
* the signed-in person, so the AI that performed the write reaches the audit
* writer here, never through `session` (ADR-0090 D10 rule 4, as the owner's
* ruling on objectstack-ai/cloud#3826 reads it; #33786). Each is copied from
* its `ExecutionContext.performedBy` sibling and nothing else.
*/
export function buildProvenance(execCtx?: ExecutionContext): HookContext['provenance'] {
const flowRunId = (execCtx as any)?.flowRunId;
const attributedUserId = (execCtx as any)?.attributedUserId;
const performedBy = (execCtx as any)?.performedBy;
const performedByClientId = performedBy?.clientId;
const performedByAgentId = performedBy?.agentId;
const performedByConversationId = performedBy?.conversationId;
const performedByConfirmationId = performedBy?.confirmationId;
const impersonatedBy = (execCtx as any)?.impersonatedBy;
if (
!flowRunId && !attributedUserId && !performedByClientId && !performedByAgentId
&& !performedByConversationId && !performedByConfirmationId && !impersonatedBy
) return undefined;
return {
...(flowRunId ? { flowRunId: String(flowRunId) } : {}),
...(attributedUserId ? { attributedUserId: String(attributedUserId) } : {}),
...(performedByClientId ? { performedByClientId: String(performedByClientId) } : {}),
...(performedByAgentId ? { performedByAgentId: String(performedByAgentId) } : {}),
...(performedByConversationId ? { performedByConversationId: String(performedByConversationId) } : {}),
...(performedByConfirmationId ? { performedByConfirmationId: String(performedByConfirmationId) } : {}),
...(impersonatedBy ? { impersonatedBy: String(impersonatedBy) } : {}),
};
}
/**
* Build the acting-user object (ADR-0068 EvalUser shape) surfaced to
* validation-time predicates as `current_user` — notably per-option
* `visibleWhen` authorization gating (objectui#2284). Returns undefined for
* system / unauthenticated writes, where membership predicates then fail-open.
*/
export function buildEvalUser(
execCtx?: ExecutionContext,
): { id: string; positions: string[]; organizationId: string | null } | undefined {
if (!execCtx || execCtx.userId == null) return undefined;
return {
id: String(execCtx.userId),
positions: execCtx.positions ?? [],
organizationId: execCtx.tenantId != null ? String(execCtx.tenantId) : null,
};
}
/**
* Build the `HookContext.user` shortcut — the ergonomic "current user"
* object surfaced to JS hooks. Carries `organizationId` (the blessed name
* for the caller's active org, identical to `session.organizationId` and
* `current_user.organizationId`) so a hook author who needs "the current org
* to filter by" writes `ctx.user.organizationId` with zero relearning (#3280).
*
* Returns undefined for system / unauthenticated writes (no acting user) —
* hooks that need an org regardless of a resolved user read
* `ctx.session.organizationId`, which is populated whenever a session is.
*/
export function buildUser(execCtx?: ExecutionContext): HookContext['user'] {
if (!execCtx || execCtx.userId == null) return undefined;
return {
id: String(execCtx.userId),
...(execCtx.email ? { email: execCtx.email } : {}),
// Always equals `session.organizationId` (both from `execCtx.tenantId`).
// Undefined on unscoped (platform/community) calls.
...(execCtx.tenantId != null ? { organizationId: String(execCtx.tenantId) } : {}),
};
}