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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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/** Governed application policy for complete mutation graphs. */
import { MutationIntent } from './request-intent';

export const MISSING_MUTATION_POLICY = 'MUTATION-POLICY-001';
export const MISSING_MUTATION_POLICY_APPROVAL = 'MUTATION-POLICY-002';

export type MutationOperationKind = 'create' | 'update' | 'delete' | 'recover';

export type MutationPolicyIdentity = Readonly<{
  policyId: string;
  version: string;
  fingerprint: string;
}>;

export type MutationOperation = Readonly<{
  kind: MutationOperationKind;
  entity: string;
  entityId?: unknown;
  originalVersion?: number;
  changedValues: Readonly<Record<string, unknown>>;
}>;

export type MutationPlan = Readonly<{
  executionId: string;
  requestKey: string;
  rootEntityType: string;
  auditReason: string;
  operations: readonly MutationOperation[];
}>;

export type MutationDecision = Readonly<{
  verdict: 'allow' | 'deny';
  code?: string;
  message?: string;
  fieldPaths?: readonly string[];
}>;

export interface MutationPolicy {
  readonly identity: MutationPolicyIdentity;
  review(context: unknown, plan: MutationPlan): MutationDecision;
}

export interface MutationPolicyRegistry {
  resolve(requestKey: string): MutationPolicy | undefined;
}

export type MutationPolicyApproval = Readonly<{
  policy: MutationPolicyIdentity;
  approvedBy: string;
  approvedAt: Date;
}>;

export interface MutationPolicyApprovalProvider {
  findApproval(identity: MutationPolicyIdentity): MutationPolicyApproval | undefined;
}

export type MutationPolicySource = 'generated_default' | 'customer';
export type MutationPolicyApprovalStatus = 'not_applicable' | 'missing' | 'approved';

export type MutationOperationSummary = Readonly<{
  kind: MutationOperationKind;
  entity: string;
  entityId?: unknown;
  changedFields: readonly string[];
}>;

export type MutationGovernanceSnapshot = Readonly<{
  executionId: string;
  requestKey: string;
  source: MutationPolicySource;
  policy?: MutationPolicyIdentity;
  approvalStatus: MutationPolicyApprovalStatus;
  warningCodes: readonly string[];
  operations: readonly MutationOperationSummary[];
}>;

export type MutationGovernanceWarning = Readonly<{
  snapshot: MutationGovernanceSnapshot;
  warningCode: string;
  firstOccurrence: boolean;
}>;

export interface MutationGovernanceSink {
  onWarning(context: unknown, warning: MutationGovernanceWarning): void;
}

export class MutationPolicyError extends Error {
  constructor(message: string) {
    super(message);
    this.name = 'MutationPolicyError';
  }
}

export class DelegatingMutationPolicyRegistry implements MutationPolicyRegistry {
  constructor(private readonly resolver: (requestKey: string) => MutationPolicy | undefined) {}
  resolve(requestKey: string): MutationPolicy | undefined { return this.resolver(requestKey); }
}

export class DelegatingMutationPolicyApprovalProvider
implements MutationPolicyApprovalProvider {
  constructor(private readonly finder:
  (identity: MutationPolicyIdentity) => MutationPolicyApproval | undefined) {}
  findApproval(identity: MutationPolicyIdentity): MutationPolicyApproval | undefined {
    return this.finder(identity);
  }
}

export class DelegatingMutationGovernanceSink implements MutationGovernanceSink {
  constructor(private readonly consumer:
  (context: unknown, warning: MutationGovernanceWarning) => void) {}
  onWarning(context: unknown, warning: MutationGovernanceWarning): void {
    this.consumer(context, warning);
  }
}

class ConsoleMutationGovernanceSink implements MutationGovernanceSink {
  onWarning(_context: unknown, warning: MutationGovernanceWarning): void {
    if (!warning.firstOccurrence || typeof console === 'undefined') return;
    console.warn(
      `TeaQL mutation policy warning code=${warning.warningCode} ` +
      `requestKey=${warning.snapshot.requestKey} source=${warning.snapshot.source} ` +
      `approval=${warning.snapshot.approvalStatus}`,
    );
  }
}

type MutableMutationPolicyProfile = {
  registry?: MutationPolicyRegistry;
  approvalProvider?: MutationPolicyApprovalProvider;
  warningSink: MutationGovernanceSink;
};

type MutationExecution = Readonly<{
  snapshot: MutationGovernanceSnapshot;
}>;

let executionSequence = 0;

/** @internal Runtime state owned by one UserContext. */
export class MutationPolicyRuntimeState {
  private readonly profile: MutableMutationPolicyProfile = {
    warningSink: new ConsoleMutationGovernanceSink(),
  };
  private readonly emittedWarnings = new Set<string>();
  private graphActive = false;
  private graphReviewed = false;
  private preflight: MutationOperation[] = [];
  private preflightKeys: string[] = [];
  private rootEntity?: string;
  private auditReason?: string;
  private remaining = new Map<string, number>();
  private graphSnapshot?: MutationGovernanceSnapshot;

  setRegistry(registry: MutationPolicyRegistry): void { this.profile.registry = registry; }
  setApprovalProvider(provider: MutationPolicyApprovalProvider): void {
    this.profile.approvalProvider = provider;
  }
  setWarningSink(sink: MutationGovernanceSink): void { this.profile.warningSink = sink; }

  beginGraph(): void {
    if (this.graphActive) throw new MutationPolicyError('mutation policy graph is already active');
    this.graphActive = true;
    this.graphReviewed = false;
    this.preflight = [];
    this.preflightKeys = [];
    this.rootEntity = undefined;
    this.auditReason = undefined;
    this.remaining.clear();
    this.graphSnapshot = undefined;
  }

  endGraph(): void {
    this.graphActive = false;
    this.graphReviewed = false;
    this.preflight = [];
    this.preflightKeys = [];
    this.rootEntity = undefined;
    this.auditReason = undefined;
    this.remaining.clear();
    this.graphSnapshot = undefined;
  }

  recordPreflight(mutation: unknown): void {
    if (!this.graphActive) return;
    if (this.graphReviewed) {
      throw new MutationPolicyError('mutation preflight cannot change after policy review');
    }
    const operation = operationFromMutation(mutation);
    this.rootEntity ??= operation.entity;
    this.auditReason ??= mutationComment(mutation);
    this.preflight.push(operation);
    this.preflightKeys.push(operationMatchKey(mutation, operation));
  }

  enterMutation(context: unknown, mutation: unknown): MutationExecution {
    const operation = operationFromMutation(mutation);
    if (!this.graphActive) {
      return { snapshot: this.review(context, this.plan(operation.entity,
        mutationComment(mutation), [operation])) };
    }

    if (!this.graphReviewed) {
      if (this.profile.registry && this.preflight.length === 0) {
        throw new MutationPolicyError(
          'customer mutation policy requires complete graph preflight before provider mutation',
        );
      }
      // Legacy/manual graph callers without a customer policy remain usable.
      // Generated graphs provide the complete preflight and therefore share
      // one reviewed plan; the generated-default fallback reviews each legacy
      // operation independently and emits the stable missing-policy warning.
      if (!this.profile.registry && this.preflight.length === 0) {
        return { snapshot: this.review(context, this.plan(
          operation.entity, mutationComment(mutation), [operation],
        )) };
      }
      const operations = this.preflight.length ? this.preflight : [operation];
      this.graphSnapshot = this.review(context, this.plan(
        this.rootEntity ?? operations[0].entity,
        this.auditReason ?? mutationComment(mutation),
        operations,
      ));
      this.remaining.clear();
      const plannedKeys = this.preflight.length
        ? this.preflightKeys : operations.map(operationSignature);
      for (const signature of plannedKeys) {
        this.remaining.set(signature, (this.remaining.get(signature) ?? 0) + 1);
      }
      this.graphReviewed = true;
    }
    this.consume(operationMatchKey(mutation, operation));
    return { snapshot: this.graphSnapshot! };
  }

  ensureGraphComplete(): void {
    if (this.graphReviewed && this.remaining.size) {
      throw new MutationPolicyError(
        'reviewed mutation plan contains operations that were not executed',
      );
    }
  }

  review(context: unknown, input: MutationPlan): MutationGovernanceSnapshot {
    validatePlan(input);
    const plan = freezePlan(input);
    const policy = this.profile.registry?.resolve(plan.requestKey);
    let source: MutationPolicySource;
    let identity: MutationPolicyIdentity | undefined;
    let approvalStatus: MutationPolicyApprovalStatus;
    let warningCodes: readonly string[];

    if (!policy) {
      source = 'generated_default';
      approvalStatus = 'not_applicable';
      warningCodes = [MISSING_MUTATION_POLICY];
    } else {
      identity = freezeIdentity(policy.identity);
      const decision = policy.review(context, plan);
      if (!decision || (decision.verdict !== 'allow' && decision.verdict !== 'deny')) {
        throw new MutationPolicyError('customer mutation policy returned an invalid decision');
      }
      if (decision.verdict === 'deny') {
        throw new MutationPolicyError(
          `[MUTATION POLICY DENIED] ${decision.code || 'MUTATION-POLICY-DENIED'}: ` +
          `${decision.message || 'mutation rejected'}`,
        );
      }
      source = 'customer';
      const approval = this.profile.approvalProvider?.findApproval(identity);
      approvalStatus = validApproval(approval, identity) ? 'approved' : 'missing';
      warningCodes = approvalStatus === 'approved'
        ? [] : [MISSING_MUTATION_POLICY_APPROVAL];
    }

    const snapshot: MutationGovernanceSnapshot = Object.freeze({
      executionId: plan.executionId,
      requestKey: plan.requestKey,
      source,
      policy: identity,
      approvalStatus,
      warningCodes: Object.freeze([...warningCodes]),
      operations: Object.freeze(plan.operations.map(operation => Object.freeze({
        kind: operation.kind,
        entity: operation.entity,
        entityId: cloneAndFreeze(operation.entityId),
        changedFields: Object.freeze(Object.keys(operation.changedValues).sort()),
      }))),
    });
    for (const code of warningCodes) this.emitWarning(context, snapshot, code);
    return snapshot;
  }

  private plan(root: string, reason: string | undefined,
               operations: readonly MutationOperation[]): MutationPlan {
    executionSequence += 1;
    return freezePlan({
      executionId: `teaql-mutation-${executionSequence}`,
      requestKey: `${root}.saveGraph`,
      rootEntityType: root,
      auditReason: new MutationIntent(reason).comment,
      operations,
    });
  }

  private consume(signature: string): void {
    const count = this.remaining.get(signature) ?? 0;
    if (count < 1) {
      throw new MutationPolicyError(
        'provider mutation is not present in the reviewed graph plan',
      );
    }
    if (count === 1) this.remaining.delete(signature);
    else this.remaining.set(signature, count - 1);
  }

  private emitWarning(context: unknown, snapshot: MutationGovernanceSnapshot,
                      warningCode: string): void {
    const policy = snapshot.policy
      ? `${snapshot.policy.policyId}:${snapshot.policy.version}:${snapshot.policy.fingerprint}`
      : 'none';
    const key = `${snapshot.requestKey}|${policy}|${warningCode}`;
    const firstOccurrence = !this.emittedWarnings.has(key);
    this.emittedWarnings.add(key);
    try {
      this.profile.warningSink.onWarning(context, Object.freeze({
        snapshot, warningCode, firstOccurrence,
      }));
    } catch {
      // Warning delivery must not change persistence semantics.
    }
  }
}

function operationFromMutation(value: any): MutationOperation {
  const entity = String(value?.entity ?? '').trim();
  const action = String(value?.action ?? '').toLowerCase();
  const kinds: Record<string, MutationOperationKind> = {
    create: 'create', update: 'update', delete: 'delete', recover: 'recover',
  };
  if (!entity || !kinds[action]) {
    throw new MutationPolicyError('mutation must contain a supported entity operation');
  }
  const payload = value?.payload && typeof value.payload === 'object'
    ? value.payload as Record<string, unknown> : {};
  return Object.freeze({
    kind: kinds[action],
    entity,
    entityId: cloneAndFreeze(value?.id ?? payload.id),
    originalVersion: value?.version ?? value?.expectedVersion,
    changedValues: cloneRecord(payload),
  });
}

function mutationComment(value: any): string {
  return new MutationIntent(value?.comment).comment;
}

function validatePlan(plan: MutationPlan): void {
  new MutationIntent(plan?.auditReason);
  if (!plan.executionId?.trim()) throw new MutationPolicyError('execution id is required');
  if (!plan.requestKey?.trim()) throw new MutationPolicyError('request key is required');
  if (!plan.rootEntityType?.trim()) throw new MutationPolicyError('root entity type is required');
  if (!plan.operations.length) throw new MutationPolicyError('mutation plan is empty');
}

function freezeIdentity(identity: MutationPolicyIdentity): MutationPolicyIdentity {
  if (!identity || !identity.policyId?.trim() || !identity.version?.trim()
      || !identity.fingerprint?.trim()) {
    throw new MutationPolicyError('customer mutation policy identity is invalid');
  }
  return Object.freeze({
    policyId: identity.policyId.trim(), version: identity.version.trim(),
    fingerprint: identity.fingerprint.trim(),
  });
}

function validApproval(approval: MutationPolicyApproval | undefined,
                       identity: MutationPolicyIdentity): boolean {
  return !!approval && sameIdentity(approval.policy, identity)
    && !!approval.approvedBy?.trim() && approval.approvedAt instanceof Date
    && Number.isFinite(approval.approvedAt.getTime()) && approval.approvedAt.getTime() !== 0;
}

function sameIdentity(left: MutationPolicyIdentity, right: MutationPolicyIdentity): boolean {
  return left.policyId === right.policyId && left.version === right.version
    && left.fingerprint === right.fingerprint;
}

function freezePlan(plan: MutationPlan): MutationPlan {
  return Object.freeze({
    executionId: plan.executionId,
    requestKey: plan.requestKey,
    rootEntityType: plan.rootEntityType,
    auditReason: plan.auditReason,
    operations: Object.freeze(plan.operations.map(operation => Object.freeze({
      kind: operation.kind,
      entity: operation.entity,
      entityId: cloneAndFreeze(operation.entityId),
      originalVersion: operation.originalVersion,
      changedValues: cloneRecord(operation.changedValues),
    }))),
  });
}

function cloneAndFreeze(value: unknown, ancestors = new WeakSet<object>()): unknown {
  if (value === null || value === undefined || typeof value === 'string'
      || typeof value === 'number' || typeof value === 'boolean'
      || typeof value === 'bigint') return value;
  if (value instanceof Date) return value.toISOString();
  if (typeof value === 'object') {
    if (ancestors.has(value)) {
      throw new MutationPolicyError('mutation policy values must not contain cycles');
    }
    ancestors.add(value);
    try {
      if (Array.isArray(value)) {
        return Object.freeze(value.map(item => cloneAndFreeze(item, ancestors)));
      }
      const record = value as Record<string, unknown>;
      if ('id' in record && Object.keys(record).some(key => key !== 'id')) {
        return cloneAndFreeze(record.id, ancestors);
      }
      return Object.freeze(Object.fromEntries(
        Object.entries(record).map(([key, item]) => [key, cloneAndFreeze(item, ancestors)]),
      ));
    } finally {
      ancestors.delete(value);
    }
  }
  return String(value);
}

function cloneRecord(value: Readonly<Record<string, unknown>>): Readonly<Record<string, unknown>> {
  return Object.freeze(Object.fromEntries(
    Object.entries(value).map(([key, item]) => [key, cloneAndFreeze(item)]),
  ));
}

function operationSignature(operation: MutationOperation): string {
  return canonicalJSON({
    kind: operation.kind,
    entity: operation.entity,
    entityId: operation.entityId,
    originalVersion: operation.originalVersion,
    changedValues: operation.changedValues,
  });
}

function operationMatchKey(mutation: any, operation: MutationOperation): string {
  const ledgerKey = mutation?.ledgerKey;
  if (ledgerKey && typeof ledgerKey === 'object'
      && String(ledgerKey.entity ?? '').trim()
      && ledgerKey.id !== undefined && ledgerKey.id !== null) {
    return `ledger:${String(ledgerKey.entity).trim()}:${canonicalJSON(ledgerKey.id)}`;
  }
  return `operation:${operationSignature(operation)}`;
}

function canonicalJSON(value: unknown): string {
  if (typeof value === 'bigint') return JSON.stringify(`${value.toString()}n`);
  if (value === undefined) return '"<undefined>"';
  if (value === null || typeof value !== 'object') return JSON.stringify(value);
  if (Array.isArray(value)) return `[${value.map(canonicalJSON).join(',')}]`;
  return `{${Object.entries(value as Record<string, unknown>)
    .sort(([left], [right]) => left.localeCompare(right))
    .map(([key, item]) => `${JSON.stringify(key)}:${canonicalJSON(item)}`).join(',')}}`;
}