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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
import { mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import type {
  DataEnvironment,
  DataLifecycleCheck,
  DataLifecycleDefinition,
  DataLifecycleExecution,
  DataLifecycleOperation,
  DataLifecycleOperationResult,
  DataLifecyclePhase,
  TesterProfile,
  UserJourney,
} from "../core/schemas.js";
import {
  DataLifecycleBlockedError,
  type DataLifecycleProvider,
  DataLifecycleProviderError,
} from "./types.js";
import { redactSensitiveText, redactSensitiveValue } from "../utils/redaction.js";

const phaseOrder: DataLifecyclePhase[] = [
  "setup",
  "preflight",
  "postconditions",
  "cleanup",
];

export interface DataLifecycleControllerOptions {
  definition: DataLifecycleDefinition;
  environments: Record<string, DataEnvironment>;
  provider: DataLifecycleProvider;
  scope: "suite" | "journey";
  runId: string;
  runDir: string;
  suiteRunId?: string;
  journeyRunId?: string;
  journey?: UserJourney;
  profile?: TesterProfile;
  suiteManifest?: unknown;
  keepData?: boolean;
}

export class DataLifecycleController {
  readonly result: DataLifecycleExecution;
  manifest: unknown;

  private readonly environment: DataEnvironment;
  private readonly startedAt: Date;
  private manifestRedactValues: string[] = [];
  private blocked = false;

  constructor(private readonly options: DataLifecycleControllerOptions) {
    const environment = options.environments[options.definition.environment];
    if (!environment) {
      throw new Error(
        `No data lifecycle environment named "${options.definition.environment}" was configured.`,
      );
    }

    this.environment = environment;
    this.startedAt = new Date();
    const namespace =
      options.definition.namespace ??
      `jt_${sanitizeNamespace(options.journey?.id ?? options.runId)}_${shortRunId(options.runId)}`;
    const artifacts = Object.fromEntries(
      phaseOrder.map((phase) => [phase, join(options.runDir, `${phase}.json`)]),
    ) as DataLifecycleExecution["artifacts"];

    this.result = {
      schemaVersion: "0.1",
      scope: options.scope,
      environment: options.definition.environment,
      namespace,
      status: "passed",
      startedAt: this.startedAt.toISOString(),
      ...(options.suiteManifest === undefined
        ? {}
        : { suiteManifest: redactSensitiveValue(options.suiteManifest) }),
      artifacts,
      setup: [],
      preflight: [],
      postconditions: [],
      cleanup: [],
    };
  }

  get artifactPaths(): DataLifecycleExecution["artifacts"] {
    return this.result.artifacts;
  }

  async runSetupAndPreflight(): Promise<void> {
    await this.runPhase("setup");
    await this.runPhase("preflight");

    const blockingFailures = [...this.result.setup, ...this.result.preflight].filter(
      (operation) => operation.status === "failed",
    );
    if (blockingFailures.length > 0) {
      this.blocked = true;
      this.finish("blocked");
      throw new DataLifecycleBlockedError(
        `Data lifecycle blocked the run with ${blockingFailures.length} setup/preflight failure(s).`,
        blockingFailures,
      );
    }
  }

  async runPostconditions(): Promise<void> {
    if (!this.blocked) {
      await this.runPhase("postconditions");
    } else {
      await this.writePhaseArtifact("postconditions", "skipped");
    }
  }

  async runCleanup(): Promise<void> {
    if (this.options.keepData) {
      await this.writePhaseArtifact("cleanup", "skipped");
      this.finish(this.result.status);
      return;
    }

    await this.runPhase("cleanup");
  }

  finish(status = this.result.status): void {
    const endedAt = new Date();
    this.result.status = status;
    this.result.endedAt = endedAt.toISOString();
    this.result.durationMs = Math.max(0, endedAt.getTime() - this.startedAt.getTime());
    if (this.manifest !== undefined) {
      this.result.manifest = redactSensitiveValue(this.manifest, {
        extraValues: this.manifestRedactValues,
      });
    }
  }

  private async runPhase(phase: DataLifecyclePhase): Promise<void> {
    const operations = operationsForPhase(this.options.definition, phase);
    if (operations.length === 0) {
      await this.writePhaseArtifact(phase, "skipped");
      return;
    }

    for (const [index, operation] of operations.entries()) {
      const { operationResult, rawResult, redactValues } = await this.runOperation(phase, operation, index);
      this.result[phase].push(operationResult);

      if (phase === "setup" && operationResult.status === "passed" && rawResult !== undefined) {
        this.captureManifest(operation, rawResult, redactValues);
      }
    }

    const failed = this.result[phase].some(
      (operation) => operation.status === "failed" && !operationAllowFailure(operations, operation),
    );
    if (failed) {
      this.result.status =
        phase === "setup" || phase === "preflight" ? "blocked" : "failed";
    }

    await this.writePhaseArtifact(phase, failed ? this.result.status : "passed");
  }

  private async runOperation(
    phase: DataLifecyclePhase,
    operation: DataLifecycleOperation,
    index: number,
  ): Promise<{
    operationResult: DataLifecycleOperationResult;
    rawResult?: unknown;
    redactValues?: string[];
  }> {
    const startedAt = new Date();
    const operationId = operation.id ?? `${phase}-${index + 1}`;
    const inheritedRedactValues = this.manifestRedactValues;
    const resolvedArgs = resolveTemplate(operation.args, {
      context: {
        scope: this.options.scope,
        runId: this.options.runId,
        suiteRunId: this.options.suiteRunId,
        journeyRunId: this.options.journeyRunId,
        journeyId: this.options.journey?.id,
        testerProfileId: this.options.profile?.id,
        namespace: this.result.namespace,
        environment: this.result.environment,
      },
      manifest: this.manifest,
      suiteManifest: this.options.suiteManifest,
      env: process.env,
    });

    try {
      assertCapabilities(this.environment, operation);
      const providerResult = await this.options.provider.runOperation({
        scope: this.options.scope,
        runId: this.options.runId,
        suiteRunId: this.options.suiteRunId,
        journeyRunId: this.options.journeyRunId,
        journey: this.options.journey,
        profile: this.options.profile,
        namespace: this.result.namespace,
        manifest: this.manifest,
        suiteManifest: this.options.suiteManifest,
        environmentName: this.result.environment,
        environment: this.environment,
        phase,
        operation,
        args: resolvedArgs,
      });
      const endedAt = new Date();
      const checks = extractChecks(providerResult.result);
      const assertionFailed = checks?.some((check) => check.status === "fail") ?? false;
      const status = assertionFailed && !operation.allowFailure ? "failed" : "passed";
      const redactValues = combineRedactValues(inheritedRedactValues, providerResult.redactValues);
      const assertionError =
        status === "failed" && assertionFailed
          ? {
              message: redactSensitiveText(assertionFailureMessage(checks), { extraValues: redactValues }),
              classification: "assertion" as const,
            }
          : undefined;

      return {
        operationResult: {
          id: operationId,
          phase,
          environment: this.result.environment,
          provider: this.environment.provider,
          transport: "transport" in this.environment ? this.environment.transport : undefined,
          kind: operation.kind,
          function: operation.function,
          status,
          startedAt: startedAt.toISOString(),
          endedAt: endedAt.toISOString(),
          durationMs: Math.max(0, endedAt.getTime() - startedAt.getTime()),
          ...(resolvedArgs === undefined
            ? {}
            : {
                args: redactSensitiveValue(resolvedArgs, {
                  extraKeys: operation.redactKeys,
                  extraValues: redactValues,
                }),
              }),
          ...(providerResult.result === undefined
            ? {}
            : {
                result: redactSensitiveValue(providerResult.result, {
                  extraKeys: operation.redactKeys,
                  extraValues: redactValues,
                }),
              }),
          ...(checks
            ? {
                checks: redactSensitiveValue(checks, {
                  extraKeys: operation.redactKeys,
                  extraValues: redactValues,
                }) as DataLifecycleCheck[],
              }
            : {}),
          ...(providerResult.stdout
            ? { stdout: redactSensitiveText(providerResult.stdout, { extraValues: redactValues }) }
            : {}),
          ...(providerResult.stderr
            ? { stderr: redactSensitiveText(providerResult.stderr, { extraValues: redactValues }) }
            : {}),
          ...(providerResult.exitCode === undefined ? {} : { exitCode: providerResult.exitCode }),
          ...(providerResult.signal ? { signal: providerResult.signal } : {}),
          ...(providerResult.httpStatus === undefined ? {} : { httpStatus: providerResult.httpStatus }),
          ...(assertionError ? { error: assertionError } : {}),
        },
        rawResult: providerResult.result,
        redactValues,
      };
    } catch (caught) {
      const endedAt = new Date();
      const error = caught instanceof Error ? caught : new Error(String(caught));
      const providerError = error instanceof DataLifecycleProviderError ? error : undefined;
      const redactValues = combineRedactValues(inheritedRedactValues, providerError?.redactValues);
      return {
        operationResult: {
          id: operationId,
          phase,
          environment: this.result.environment,
          provider: this.environment.provider,
          transport: "transport" in this.environment ? this.environment.transport : undefined,
          kind: operation.kind,
          function: operation.function,
          status: "failed",
          startedAt: startedAt.toISOString(),
          endedAt: endedAt.toISOString(),
          durationMs: Math.max(0, endedAt.getTime() - startedAt.getTime()),
          ...(resolvedArgs === undefined
            ? {}
            : {
                args: redactSensitiveValue(resolvedArgs, {
                  extraKeys: operation.redactKeys,
                  extraValues: redactValues,
                }),
              }),
          ...(providerError?.result === undefined
            ? {}
            : {
                result: redactSensitiveValue(providerError.result, {
                  extraKeys: operation.redactKeys,
                  extraValues: redactValues,
                }),
              }),
          ...(providerError?.stdout
            ? { stdout: redactSensitiveText(providerError.stdout, { extraValues: redactValues }) }
            : {}),
          ...(providerError?.stderr
            ? { stderr: redactSensitiveText(providerError.stderr, { extraValues: redactValues }) }
            : {}),
          ...(providerError?.exitCode === undefined ? {} : { exitCode: providerError.exitCode }),
          ...(providerError?.signal ? { signal: providerError.signal } : {}),
          ...(providerError?.httpStatus === undefined ? {} : { httpStatus: providerError.httpStatus }),
          error: {
            message: redactSensitiveText(error.message, { extraValues: redactValues }),
            stack: error.stack
              ? redactSensitiveText(error.stack, { extraValues: redactValues })
              : undefined,
            classification: classifyError(error),
          },
        },
      };
    }
  }

  private captureManifest(
    operation: DataLifecycleOperation,
    result: unknown,
    redactValues: string[] = [],
  ): void {
    const selected =
      operation.manifestPath === undefined ? result : selectJsonPath(result, operation.manifestPath);
    this.manifest = selected;
    this.manifestRedactValues = redactValues;
    this.result.manifest = redactSensitiveValue(selected, {
      extraKeys: operation.redactKeys,
      extraValues: redactValues,
    });
  }

  private async writePhaseArtifact(
    phase: DataLifecyclePhase,
    status: DataLifecycleExecution["status"],
  ): Promise<void> {
    const artifactPath = this.result.artifacts[phase];
    if (!artifactPath) {
      throw new Error(`No data lifecycle artifact path configured for phase "${phase}".`);
    }

    await mkdir(this.options.runDir, { recursive: true });
    await writeFile(
      artifactPath,
      `${JSON.stringify(
        {
          schemaVersion: "0.1",
          scope: this.options.scope,
          environment: this.result.environment,
          namespace: this.result.namespace,
          phase,
          status,
          manifest: this.result.manifest,
          suiteManifest: this.result.suiteManifest,
          operations: this.result[phase],
        },
        null,
        2,
      )}\n`,
      "utf8",
    );
  }
}

function operationsForPhase(
  definition: DataLifecycleDefinition,
  phase: DataLifecyclePhase,
): DataLifecycleOperation[] {
  return definition[phase] ?? [];
}

function operationAllowFailure(
  operations: DataLifecycleOperation[],
  result: DataLifecycleOperationResult,
): boolean {
  const operation = operations.find(
    (candidate, index) => (candidate.id ?? `${result.phase}-${index + 1}`) === result.id,
  );
  return operation?.allowFailure ?? false;
}

function assertCapabilities(
  environment: DataEnvironment,
  operation: DataLifecycleOperation,
): void {
  const requested = operation.requiredCapabilities ?? [];
  if (requested.length === 0) {
    return;
  }

  const defaults =
    environment.provider === "convex" && environment.transport === "cli"
      ? { publicFunctions: true, internalFunctions: true }
      : { publicFunctions: true, internalFunctions: false };
  const capabilities = { ...defaults, ...(environment.capabilities ?? {}) };

  for (const capability of requested) {
    if (!capabilities[capability]) {
      throw new Error(
        `Data lifecycle operation "${operation.function}" requires capability "${capability}", but environment does not declare it.`,
      );
    }
  }
}

function extractChecks(result: unknown): DataLifecycleCheck[] | undefined {
  if (!isRecord(result) || !Array.isArray(result.checks)) {
    return undefined;
  }

  return result.checks.map((raw, index) => {
    if (!isRecord(raw)) {
      return {
        id: `check-${index + 1}`,
        status: "fail",
        message: "Check result was not an object.",
        data: raw,
      };
    }

    const status = raw.status === "pass" || raw.status === "warn" || raw.status === "fail"
      ? raw.status
      : "fail";

    return {
      id: typeof raw.id === "string" && raw.id ? raw.id : `check-${index + 1}`,
      status,
      ...(typeof raw.message === "string" && raw.message ? { message: raw.message } : {}),
      ...("data" in raw ? { data: raw.data } : {}),
    };
  });
}

function assertionFailureMessage(checks: DataLifecycleCheck[] | undefined): string {
  const failures = checks
    ?.filter((check) => check.status === "fail")
    .map((check) => `${check.id}${check.message ? `: ${check.message}` : ""}`);
  return failures && failures.length > 0
    ? `Data lifecycle checks failed: ${failures.join("; ")}`
    : "Data lifecycle checks failed.";
}

function combineRedactValues(
  inherited: string[] = [],
  current: string[] = [],
): string[] {
  return [...new Set([...inherited, ...current].filter((value) => value.length > 0))];
}

function resolveTemplate(
  value: unknown,
  sources: {
    context: Record<string, unknown>;
    manifest?: unknown;
    suiteManifest?: unknown;
    env: NodeJS.ProcessEnv;
  },
): unknown {
  if (typeof value === "string" && value.startsWith("$")) {
    return resolveReference(value, sources);
  }
  if (Array.isArray(value)) {
    return value.map((item) => resolveTemplate(item, sources));
  }
  if (isRecord(value)) {
    return Object.fromEntries(
      Object.entries(value).map(([key, entry]) => [key, resolveTemplate(entry, sources)]),
    );
  }
  return value;
}

function resolveReference(
  value: string,
  sources: {
    context: Record<string, unknown>;
    manifest?: unknown;
    suiteManifest?: unknown;
    env: NodeJS.ProcessEnv;
  },
): unknown {
  if (value === "$manifest") {
    return sources.manifest;
  }
  if (value.startsWith("$manifest.")) {
    return selectJsonPath(sources.manifest, value.replace("$manifest", "$"));
  }
  if (value === "$suiteManifest") {
    return sources.suiteManifest;
  }
  if (value.startsWith("$suiteManifest.")) {
    return selectJsonPath(sources.suiteManifest, value.replace("$suiteManifest", "$"));
  }
  if (value.startsWith("$context.")) {
    return selectJsonPath(sources.context, value.replace("$context", "$"));
  }
  if (value.startsWith("$env.")) {
    const key = value.slice("$env.".length);
    const resolved = sources.env[key];
    if (resolved === undefined) {
      throw new Error(`Environment variable "${key}" is not set.`);
    }
    return resolved;
  }

  throw new Error(`Unknown data lifecycle template reference "${value}".`);
}

export function selectJsonPath(value: unknown, path: string): unknown {
  const normalized = path.startsWith("$.") ? path.slice(2) : path.startsWith("$") ? path.slice(1) : path;
  if (!normalized) {
    return value;
  }

  let current = value;
  for (const segment of normalized.split(".")) {
    if (!segment) {
      continue;
    }

    const match = /^([^[\]]+)(?:\[(\d+)])?$/.exec(segment);
    if (!match) {
      throw new Error(`Unsupported JSON path segment "${segment}" in "${path}".`);
    }

    const key = match[1];
    if (!isRecord(current) && !Array.isArray(current)) {
      throw new Error(`Cannot resolve "${path}" because "${key}" is not reachable.`);
    }
    current = (current as Record<string, unknown>)[key];

    if (match[2] !== undefined) {
      if (!Array.isArray(current)) {
        throw new Error(`Cannot resolve "${path}" because "${key}" is not an array.`);
      }
      current = current[Number(match[2])];
    }
  }

  return current;
}

function classifyError(error: Error): "configuration" | "capability" | "execution" | "assertion" {
  if (error instanceof DataLifecycleProviderError && error.classification) {
    return error.classification;
  }
  if (/environment|configured|not set|JSON path|template/i.test(error.message)) {
    return "configuration";
  }
  if (/capability/i.test(error.message)) {
    return "capability";
  }
  return "execution";
}

function sanitizeNamespace(value: string): string {
  return value.replace(/[^a-zA-Z0-9_]+/g, "_").replace(/^_+|_+$/g, "") || "run";
}

function shortRunId(value: string): string {
  return sanitizeNamespace(value).slice(-12) || "run";
}

function isRecord(value: unknown): value is Record<string, unknown> {
  return typeof value === "object" && value !== null && !Array.isArray(value);
}