@derwinjs/db 0.6.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/dist/budget-gate.d.ts +43 -0
  2. package/dist/budget-gate.d.ts.map +1 -0
  3. package/dist/budget-gate.js +110 -0
  4. package/dist/budget-gate.js.map +1 -0
  5. package/dist/classification-override-store.d.ts +24 -0
  6. package/dist/classification-override-store.d.ts.map +1 -0
  7. package/dist/classification-override-store.js +131 -0
  8. package/dist/classification-override-store.js.map +1 -0
  9. package/dist/drift-detector.d.ts +63 -0
  10. package/dist/drift-detector.d.ts.map +1 -0
  11. package/dist/drift-detector.js +0 -0
  12. package/dist/drift-detector.js.map +1 -0
  13. package/dist/freeze-window-evaluator.d.ts +62 -0
  14. package/dist/freeze-window-evaluator.d.ts.map +1 -0
  15. package/dist/freeze-window-evaluator.js +236 -0
  16. package/dist/freeze-window-evaluator.js.map +1 -0
  17. package/dist/index.d.ts +8 -0
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +8 -0
  20. package/dist/index.js.map +1 -1
  21. package/dist/kill-switch-evaluator.d.ts +68 -0
  22. package/dist/kill-switch-evaluator.d.ts.map +1 -0
  23. package/dist/kill-switch-evaluator.js +389 -0
  24. package/dist/kill-switch-evaluator.js.map +1 -0
  25. package/dist/path-tier-resolver.d.ts +47 -0
  26. package/dist/path-tier-resolver.d.ts.map +1 -0
  27. package/dist/path-tier-resolver.js +177 -0
  28. package/dist/path-tier-resolver.js.map +1 -0
  29. package/dist/rag-retriever.d.ts.map +1 -1
  30. package/dist/rag-retriever.js +38 -0
  31. package/dist/rag-retriever.js.map +1 -1
  32. package/dist/regression-detector.d.ts +45 -0
  33. package/dist/regression-detector.d.ts.map +1 -0
  34. package/dist/regression-detector.js +78 -0
  35. package/dist/regression-detector.js.map +1 -0
  36. package/dist/trust-threshold-config-store.d.ts +20 -0
  37. package/dist/trust-threshold-config-store.d.ts.map +1 -0
  38. package/dist/trust-threshold-config-store.js +88 -0
  39. package/dist/trust-threshold-config-store.js.map +1 -0
  40. package/package.json +3 -3
  41. package/prisma/migrations/20260507120000_sprint8_policy_governance/migration.sql +58 -0
  42. package/prisma/migrations/20260507120100_sprint8_phase2_thresholds_and_freeze/migration.sql +33 -0
  43. package/prisma/migrations/20260507120200_sprint8_phase3_budget_cap/migration.sql +21 -0
  44. package/prisma/migrations/20260507120300_sprint8_phase4_kill_switches/migration.sql +74 -0
  45. package/prisma/schema.prisma +183 -11
  46. package/prisma-client/edge.js +96 -19
  47. package/prisma-client/index-browser.js +93 -16
  48. package/prisma-client/index.d.ts +10997 -3426
  49. package/prisma-client/index.js +96 -19
  50. package/prisma-client/package.json +1 -1
  51. package/prisma-client/schema.prisma +183 -11
  52. package/prisma-client/wasm.js +93 -16
@@ -0,0 +1,43 @@
1
+ /**
2
+ * createPrismaBudgetGate — Prisma-backed implementation of the SDK
3
+ * BudgetGate contract introduced by QAP-084.
4
+ *
5
+ * Sprint 8 Phase 3 (Policy Governance). Per-project monthly auto-fix budget
6
+ * cap with soft-warn + hard-stop thresholds. The dispatcher consults
7
+ * `isHardStopped()` BEFORE running runFixCycle and refuses to dispatch
8
+ * when month-to-date spend has hit the cap.
9
+ *
10
+ * # Spend window
11
+ *
12
+ * Month-to-date is the calendar month in UTC: spend SUM is restricted to
13
+ * SpendLedger entries with `occurredAt >= startOfMonth(UTC)`. The window
14
+ * resets at 00:00 UTC on the 1st of each month — this matches operator
15
+ * mental model (`this month's budget`, not a rolling 30-day window).
16
+ *
17
+ * # Currency unit
18
+ *
19
+ * SpendLedger.costCents is INTEGER cents (legacy schema, preserved for
20
+ * back-compat with the audit trail and the per-attempt costCents column).
21
+ * The BudgetGate contract surfaces USD floats — we divide by 100 in the
22
+ * factory rather than mutating the schema. New consumers wire BudgetGate
23
+ * and never touch cents directly.
24
+ *
25
+ * # Tenant isolation
26
+ *
27
+ * Every method scopes by projectId. Pattern D — wrong-project / unknown-
28
+ * project lookups return state=`NO_CAP` (defense-in-depth: a missing
29
+ * Project row should not look different from one with no cap configured).
30
+ */
31
+ import type { PrismaClient } from './prisma.js';
32
+ import { type BudgetGate } from '@derwinjs/sdk';
33
+ export interface PrismaBudgetGateConfig {
34
+ /** Generated Prisma client. Pass an instance per process. */
35
+ prisma: PrismaClient;
36
+ /**
37
+ * Optional clock injection for deterministic month-boundary tests. Tests
38
+ * pass a fixed Date; production callers omit (defaults to `new Date()`).
39
+ */
40
+ now?: () => Date;
41
+ }
42
+ export declare function createPrismaBudgetGate(config: PrismaBudgetGateConfig): BudgetGate;
43
+ //# sourceMappingURL=budget-gate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"budget-gate.d.ts","sourceRoot":"","sources":["../src/budget-gate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,EAAmB,KAAK,UAAU,EAAqB,MAAM,eAAe,CAAC;AAIpF,MAAM,WAAW,sBAAsB;IACrC,6DAA6D;IAC7D,MAAM,EAAE,YAAY,CAAC;IACrB;;;OAGG;IACH,GAAG,CAAC,EAAE,MAAM,IAAI,CAAC;CAClB;AA8BD,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,sBAAsB,GAAG,UAAU,CA8DjF"}
@@ -0,0 +1,110 @@
1
+ /**
2
+ * createPrismaBudgetGate — Prisma-backed implementation of the SDK
3
+ * BudgetGate contract introduced by QAP-084.
4
+ *
5
+ * Sprint 8 Phase 3 (Policy Governance). Per-project monthly auto-fix budget
6
+ * cap with soft-warn + hard-stop thresholds. The dispatcher consults
7
+ * `isHardStopped()` BEFORE running runFixCycle and refuses to dispatch
8
+ * when month-to-date spend has hit the cap.
9
+ *
10
+ * # Spend window
11
+ *
12
+ * Month-to-date is the calendar month in UTC: spend SUM is restricted to
13
+ * SpendLedger entries with `occurredAt >= startOfMonth(UTC)`. The window
14
+ * resets at 00:00 UTC on the 1st of each month — this matches operator
15
+ * mental model (`this month's budget`, not a rolling 30-day window).
16
+ *
17
+ * # Currency unit
18
+ *
19
+ * SpendLedger.costCents is INTEGER cents (legacy schema, preserved for
20
+ * back-compat with the audit trail and the per-attempt costCents column).
21
+ * The BudgetGate contract surfaces USD floats — we divide by 100 in the
22
+ * factory rather than mutating the schema. New consumers wire BudgetGate
23
+ * and never touch cents directly.
24
+ *
25
+ * # Tenant isolation
26
+ *
27
+ * Every method scopes by projectId. Pattern D — wrong-project / unknown-
28
+ * project lookups return state=`NO_CAP` (defense-in-depth: a missing
29
+ * Project row should not look different from one with no cap configured).
30
+ */
31
+ import { BudgetGateError } from '@derwinjs/sdk';
32
+ // ─── Helpers ─────────────────────────────────────────────────────────────
33
+ /**
34
+ * Compute the start of the calendar month in UTC for the given instant.
35
+ * `2026-05-07T12:34:56Z` → `2026-05-01T00:00:00.000Z`. The aggregator query
36
+ * uses `occurredAt >= startOfMonth` so the boundary is inclusive.
37
+ */
38
+ function startOfMonthUtc(at) {
39
+ return new Date(Date.UTC(at.getUTCFullYear(), at.getUTCMonth(), 1, 0, 0, 0, 0));
40
+ }
41
+ /**
42
+ * Decide the gate state from utilization. Mirrors the BudgetGate contract's
43
+ * documented thresholds exactly.
44
+ */
45
+ function deriveState(monthlyBudgetUsd, utilizationPercent, softWarnPct) {
46
+ if (monthlyBudgetUsd === null || utilizationPercent === null)
47
+ return 'NO_CAP';
48
+ if (utilizationPercent >= 1.0)
49
+ return 'HARD_STOP';
50
+ if (utilizationPercent >= softWarnPct)
51
+ return 'SOFT_WARN';
52
+ return 'OK';
53
+ }
54
+ // ─── Factory ─────────────────────────────────────────────────────────────
55
+ export function createPrismaBudgetGate(config) {
56
+ const { prisma } = config;
57
+ const now = config.now ?? (() => new Date());
58
+ async function computeStatus(projectId) {
59
+ if (typeof projectId !== 'string' || projectId.length === 0) {
60
+ throw new BudgetGateError('invalid_input', 'BudgetGate: projectId is required');
61
+ }
62
+ // 1. Fetch the project's cap + soft-warn pct. Pattern D: unknown
63
+ // project → NO_CAP, no throw. Defense-in-depth so a tenant
64
+ // enumeration probe sees the same shape as a real cap-less project.
65
+ const project = await prisma.project.findUnique({
66
+ where: { id: projectId },
67
+ select: { monthlyBudgetUsd: true, budgetSoftWarnPct: true },
68
+ });
69
+ if (project === null) {
70
+ return {
71
+ monthToDateSpendUsd: 0,
72
+ monthlyBudgetUsd: null,
73
+ utilizationPercent: null,
74
+ state: 'NO_CAP',
75
+ };
76
+ }
77
+ // 2. Sum the SpendLedger entries for the current calendar month.
78
+ // SpendLedger.costCents is INT cents — we divide by 100 to surface USD.
79
+ const monthStart = startOfMonthUtc(now());
80
+ const aggregate = await prisma.spendLedger.aggregate({
81
+ where: { projectId, occurredAt: { gte: monthStart } },
82
+ _sum: { costCents: true },
83
+ });
84
+ const totalCents = aggregate._sum.costCents ?? 0;
85
+ const monthToDateSpendUsd = totalCents / 100;
86
+ // 3. Compute utilization + state.
87
+ const monthlyBudgetUsd = project.monthlyBudgetUsd ?? null;
88
+ const utilizationPercent = monthlyBudgetUsd === null || monthlyBudgetUsd === 0
89
+ ? null
90
+ : monthToDateSpendUsd / monthlyBudgetUsd;
91
+ const softWarnPct = project.budgetSoftWarnPct;
92
+ const state = deriveState(monthlyBudgetUsd, utilizationPercent, softWarnPct);
93
+ return {
94
+ monthToDateSpendUsd,
95
+ monthlyBudgetUsd,
96
+ utilizationPercent,
97
+ state,
98
+ };
99
+ }
100
+ return {
101
+ async getStatus(input) {
102
+ return computeStatus(input.projectId);
103
+ },
104
+ async isHardStopped(input) {
105
+ const status = await computeStatus(input.projectId);
106
+ return status.state === 'HARD_STOP';
107
+ },
108
+ };
109
+ }
110
+ //# sourceMappingURL=budget-gate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"budget-gate.js","sourceRoot":"","sources":["../src/budget-gate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6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C;QAED,KAAK,CAAC,aAAa,CAAC,KAA4B;YAC9C,MAAM,MAAM,GAAG,MAAM,aAAa,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;YACpD,OAAO,MAAM,CAAC,KAAK,KAAK,WAAW,CAAC;QACtC,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,24 @@
1
+ /**
2
+ * createPrismaClassificationOverrideStore — Prisma-backed implementation of
3
+ * the SDK ClassificationOverrideStore contract introduced by QAP-081.
4
+ *
5
+ * Sprint 8 (Policy Governance, Phase 1). Operator-defined overrides keyed
6
+ * by (classification, surface). Three modes:
7
+ *
8
+ * - BLOCK_AUTO_FIX — never auto-fix this (classification, surface)
9
+ * - FORCE_ESCALATION — route directly to ESCALATION bucket
10
+ * - DOWNGRADE_TIER — replace the resolved RiskTier with `downgradeTier`
11
+ *
12
+ * Tenant isolation: every method scopes by projectId. findOverride
13
+ * resolution: exact-surface match wins over wildcard (surface=null).
14
+ * Cross-project ids return null on findOverride / throw `not_found` on
15
+ * deleteOverride. Pattern D from docs/testing.md applies.
16
+ */
17
+ import type { PrismaClient } from './prisma.js';
18
+ import { type ClassificationOverrideStore } from '@derwinjs/sdk';
19
+ export interface PrismaClassificationOverrideStoreConfig {
20
+ /** Generated Prisma client. Pass an instance per process. */
21
+ prisma: PrismaClient;
22
+ }
23
+ export declare function createPrismaClassificationOverrideStore(config: PrismaClassificationOverrideStoreConfig): ClassificationOverrideStore;
24
+ //# sourceMappingURL=classification-override-store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"classification-override-store.d.ts","sourceRoot":"","sources":["../src/classification-override-store.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,EAKL,KAAK,2BAA2B,EAEjC,MAAM,eAAe,CAAC;AAIvB,MAAM,WAAW,uCAAuC;IACtD,6DAA6D;IAC7D,MAAM,EAAE,YAAY,CAAC;CACtB;AAyFD,wBAAgB,uCAAuC,CACrD,MAAM,EAAE,uCAAuC,GAC9C,2BAA2B,CAyF7B"}
@@ -0,0 +1,131 @@
1
+ /**
2
+ * createPrismaClassificationOverrideStore — Prisma-backed implementation of
3
+ * the SDK ClassificationOverrideStore contract introduced by QAP-081.
4
+ *
5
+ * Sprint 8 (Policy Governance, Phase 1). Operator-defined overrides keyed
6
+ * by (classification, surface). Three modes:
7
+ *
8
+ * - BLOCK_AUTO_FIX — never auto-fix this (classification, surface)
9
+ * - FORCE_ESCALATION — route directly to ESCALATION bucket
10
+ * - DOWNGRADE_TIER — replace the resolved RiskTier with `downgradeTier`
11
+ *
12
+ * Tenant isolation: every method scopes by projectId. findOverride
13
+ * resolution: exact-surface match wins over wildcard (surface=null).
14
+ * Cross-project ids return null on findOverride / throw `not_found` on
15
+ * deleteOverride. Pattern D from docs/testing.md applies.
16
+ */
17
+ import { ClassificationOverrideStoreError, OverrideModeValues, RiskTierValues, } from '@derwinjs/sdk';
18
+ // ─── Validation ──────────────────────────────────────────────────────────
19
+ function validateCreateInput(input) {
20
+ if (typeof input.classification !== 'string' || input.classification.length === 0) {
21
+ throw new ClassificationOverrideStoreError('invalid_input', 'ClassificationOverrideStore: classification is required');
22
+ }
23
+ if (!OverrideModeValues.includes(input.overrideMode)) {
24
+ throw new ClassificationOverrideStoreError('invalid_input', `ClassificationOverrideStore: overrideMode must be one of ${OverrideModeValues.join(', ')}`);
25
+ }
26
+ if (typeof input.reason !== 'string' || input.reason.trim().length === 0) {
27
+ throw new ClassificationOverrideStoreError('invalid_input', 'ClassificationOverrideStore: reason is required');
28
+ }
29
+ if (typeof input.createdBy !== 'string' || input.createdBy.trim().length === 0) {
30
+ throw new ClassificationOverrideStoreError('invalid_input', 'ClassificationOverrideStore: createdBy is required');
31
+ }
32
+ if (input.overrideMode === 'DOWNGRADE_TIER') {
33
+ if (input.downgradeTier === undefined || input.downgradeTier === null) {
34
+ throw new ClassificationOverrideStoreError('invalid_input', 'ClassificationOverrideStore: downgradeTier is required when overrideMode === DOWNGRADE_TIER');
35
+ }
36
+ if (!RiskTierValues.includes(input.downgradeTier)) {
37
+ throw new ClassificationOverrideStoreError('invalid_input', `ClassificationOverrideStore: downgradeTier must be one of ${RiskTierValues.join(', ')}`);
38
+ }
39
+ }
40
+ else {
41
+ // For non-DOWNGRADE_TIER modes, downgradeTier MUST be null/undefined.
42
+ if (input.downgradeTier !== undefined && input.downgradeTier !== null) {
43
+ throw new ClassificationOverrideStoreError('invalid_input', 'ClassificationOverrideStore: downgradeTier must be null unless overrideMode === DOWNGRADE_TIER');
44
+ }
45
+ }
46
+ }
47
+ function mapOverride(row) {
48
+ return {
49
+ id: row.id,
50
+ projectId: row.projectId,
51
+ classification: row.classification,
52
+ surface: row.surface,
53
+ overrideMode: row.overrideMode,
54
+ downgradeTier: row.downgradeTier,
55
+ reason: row.reason,
56
+ createdBy: row.createdBy,
57
+ createdAt: row.createdAt,
58
+ };
59
+ }
60
+ // ─── Factory ─────────────────────────────────────────────────────────────
61
+ export function createPrismaClassificationOverrideStore(config) {
62
+ const { prisma } = config;
63
+ return {
64
+ async findOverride(input) {
65
+ // Resolution: exact-surface match wins over wildcard (surface=null).
66
+ // Implemented as TWO queries (exact first, then wildcard) so the
67
+ // intent is unambiguous. Both findFirst calls scope by projectId for
68
+ // tenant isolation.
69
+ const exact = await prisma.classificationOverride.findFirst({
70
+ where: {
71
+ projectId: input.projectId,
72
+ classification: input.classification,
73
+ surface: input.surface,
74
+ },
75
+ orderBy: { createdAt: 'desc' },
76
+ });
77
+ if (exact !== null)
78
+ return mapOverride(exact);
79
+ const wildcard = await prisma.classificationOverride.findFirst({
80
+ where: {
81
+ projectId: input.projectId,
82
+ classification: input.classification,
83
+ surface: null,
84
+ },
85
+ orderBy: { createdAt: 'desc' },
86
+ });
87
+ if (wildcard !== null)
88
+ return mapOverride(wildcard);
89
+ return null;
90
+ },
91
+ async listOverrides(input) {
92
+ const rows = await prisma.classificationOverride.findMany({
93
+ where: { projectId: input.projectId },
94
+ orderBy: { createdAt: 'desc' },
95
+ });
96
+ return rows.map(mapOverride);
97
+ },
98
+ async createOverride(input) {
99
+ validateCreateInput(input);
100
+ const row = await prisma.classificationOverride.create({
101
+ data: {
102
+ projectId: input.projectId,
103
+ classification: input.classification,
104
+ surface: input.surface,
105
+ overrideMode: input.overrideMode,
106
+ downgradeTier: input.overrideMode === 'DOWNGRADE_TIER'
107
+ ? (input.downgradeTier ?? null)
108
+ : null,
109
+ reason: input.reason,
110
+ createdBy: input.createdBy,
111
+ },
112
+ select: { id: true },
113
+ });
114
+ return { id: row.id };
115
+ },
116
+ async deleteOverride(input) {
117
+ // Tenant-check first via findFirst — same defense-in-depth pattern as
118
+ // QAPatternStore.updatePatternStatus. Surfacing not_found early gives
119
+ // callers a typed error rather than a silent no-op.
120
+ const existing = await prisma.classificationOverride.findFirst({
121
+ where: { id: input.overrideId, projectId: input.projectId },
122
+ select: { id: true },
123
+ });
124
+ if (existing === null) {
125
+ throw new ClassificationOverrideStoreError('not_found', `ClassificationOverride ${input.overrideId} not found in project ${input.projectId}`);
126
+ }
127
+ await prisma.classificationOverride.delete({ where: { id: input.overrideId } });
128
+ },
129
+ };
130
+ }
131
+ //# sourceMappingURL=classification-override-store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"classification-override-store.js","sourceRoot":"","sources":["../src/classification-override-store.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAGH,OAAO,EACL,gCAAgC,EAChC,kBAAkB,EAClB,cAAc,GAIf,MAAM,eAAe,CAAC;AASvB,4EAA4E;AAE5E,SAAS,mBAAmB,CAAC,KAM5B;IACC,IAAI,OAAO,KAAK,CAAC,cAAc,KAAK,QAAQ,IAAI,KAAK,CAAC,cAAc,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAClF,MAAM,IAAI,gCAAgC,CACxC,eAAe,EACf,yDAAyD,CAC1D,CAAC;IACJ,CAAC;IACD,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gBAC7D,KAAK,EAAE,EAAE,EAAE,EAAE,KAAK,CAAC,UAAU,EAAE,SAAS,EAAE,KAAK,CAAC,SAAS,EAAE;gBAC3D,MAAM,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE;aACrB,CAAC,CAAC;YACH,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;gBACtB,MAAM,IAAI,gCAAgC,CACxC,WAAW,EACX,0BAA0B,KAAK,CAAC,UAAU,yBAAyB,KAAK,CAAC,SAAS,EAAE,CACrF,CAAC;YACJ,CAAC;YACD,MAAM,MAAM,CAAC,sBAAsB,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,EAAE,KAAK,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC;QAClF,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Default DriftDetector — QAP-Sprint7 Phase 6 / QAP-077.
3
+ *
4
+ * Stock implementation of the SDK DriftDetector contract
5
+ * (packages/sdk/src/types/drift-detector.ts). Compares per-(classification,
6
+ * surface) success rates over a recent CURRENT window against a longer
7
+ * BASELINE window (the period immediately preceding the current window) and
8
+ * emits a DriftAlarm[] for tuples whose current rate is materially worse
9
+ * than baseline.
10
+ *
11
+ * Window semantics:
12
+ * t0 = now
13
+ * currentStart = t0 - currentWindowDays
14
+ * baselineStart = currentStart - baselineWindowDays
15
+ *
16
+ * currentWindow = [currentStart, t0)
17
+ * baselineWindow = [baselineStart, currentStart)
18
+ *
19
+ * The two windows are NON-overlapping by construction so an attempt counts in
20
+ * exactly one window; the comparison is genuinely "recent vs prior" rather
21
+ * than "recent vs trailing-30-day-including-this-week".
22
+ *
23
+ * Sample selection mirrors LearningHealthReporter (Sprint 2 Group D-3):
24
+ * - dispatchStatus IN ('AUTO_MERGED', 'HUMAN_MERGED') — Lifeline's MERGED
25
+ * under Option β
26
+ * - mergedAt within the window
27
+ * - fixSuccessScore not null — only attempts that have been scored count
28
+ * toward the success rate; pending-outcome attempts are excluded from
29
+ * both numerator and denominator (otherwise a brand-new merge with no
30
+ * score yet would be miscounted as a failure).
31
+ *
32
+ * Success classification:
33
+ * fixSuccessScore > 0 → success (Layer F scoring tiers: +0.2 / +0.5 / +1.0)
34
+ * fixSuccessScore <= 0 → failure (-1.0 / -0.5 / 0.0)
35
+ *
36
+ * classification + surface live on QATicket, not QAFixAttempt, so the query
37
+ * pulls them via the `ticket` relation in a single findMany then aggregates
38
+ * in-memory. For Sprint 7 cron usage this is fine (tens to low-hundreds of
39
+ * MERGED attempts per project per 37-day span); a future scale-out could
40
+ * push the grouping into a raw SQL aggregate over the join.
41
+ *
42
+ * Failure semantics: throws DriftDetectorError('invalid_input') on malformed
43
+ * input (negative window, empty projectId). Empty projects return [].
44
+ *
45
+ * Tenant isolation: projectId flows directly into the Prisma `where` clause,
46
+ * matching the rest of Layer F.
47
+ */
48
+ import type { PrismaClient } from './prisma.js';
49
+ import { type DriftDetector } from '@derwinjs/sdk';
50
+ export interface PrismaDriftDetectorConfig {
51
+ /** Generated Prisma client. Pass an instance per process. */
52
+ prisma: PrismaClient;
53
+ }
54
+ /**
55
+ * Build a DriftDetector backed by Prisma over QAFixAttempt + QATicket.
56
+ *
57
+ * The returned `scan` is intended to be invoked from a periodic job
58
+ * (mirroring OutcomeRecorder + TrustScoreUpdater from QAP-075). Sprint 8
59
+ * may expose `GET /qa/drift-alarms` if operator-facing UI needs it; the
60
+ * scan itself stays cron-callable independent of an HTTP route.
61
+ */
62
+ export declare function createPrismaDriftDetector(config: PrismaDriftDetectorConfig): DriftDetector;
63
+ //# sourceMappingURL=drift-detector.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"drift-detector.d.ts","sourceRoot":"","sources":["../src/drift-detector.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAChD,OAA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C,KAAK,CAAC,aAAa,CAAC;YACrC,KAAK,CAAC,aAAa,GAAG,CAAC,EACvB,CAAC;YACD,MAAM,IAAI,kBAAkB,CAC1B,eAAe,EACf,mEAAmE,CACpE,CAAC;QACJ,CAAC;IACH,CAAC;AACH,CAAC;AAED,SAAS,oBAAoB,CAAC,KAAyB,EAAE,IAAY;IACnE,IAAI,KAAK,KAAK,SAAS;QAAE,OAAO;IAChC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;QACzD,MAAM,IAAI,kBAAkB,CAAC,eAAe,EAAE,kBAAkB,IAAI,0BAA0B,CAAC,CAAC;IAClG,CAAC;IACD,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;QACd,MAAM,IAAI,kBAAkB,CAAC,eAAe,EAAE,kBAAkB,IAAI,eAAe,CAAC,CAAC;IACvF,CAAC;AACH,CAAC"}
@@ -0,0 +1,62 @@
1
+ /**
2
+ * createPrismaFreezeWindowEvaluator — Prisma-backed implementation of the
3
+ * SDK FreezeWindowEvaluator contract introduced by QAP-083.
4
+ *
5
+ * Sprint 8 Phase 2 (Policy Governance). Operator-defined recurring time
6
+ * windows during which auto-fix dispatch is partially or fully blocked.
7
+ *
8
+ * # Cron evaluator
9
+ *
10
+ * Embedded, dependency-free. Supports a SUBSET of standard 5-field cron:
11
+ * `minute hour dayOfMonth month dayOfWeek`. Each field accepts:
12
+ * - `*` — wildcard (any value in the field's allowed range)
13
+ * - exact non-negative integer in the field's allowed range
14
+ *
15
+ * NOT supported (intentional MVP scope): `*\/N` step values, `M-N` ranges,
16
+ * comma-separated lists, named months/days, `?`, `L`, `W`, `#`. Sprint 9's
17
+ * policy editor may extend this when richer scheduling is needed; until
18
+ * then, operators compose multiple FreezeWindow rows for OR semantics
19
+ * (e.g., separate rows for "every Friday 5pm" and "every Saturday 9am").
20
+ *
21
+ * Algorithm: `mostRecentFire(cron, at)` walks BACKWARDS minute-by-minute
22
+ * from `at` for at most `MAX_LOOKBACK_MINUTES` and returns the first time
23
+ * the cron matches. The dispatcher only ever asks "did this fire within
24
+ * the past durationMinutes?"; we cap the search at 7 days of minutes
25
+ * (10080) which comfortably exceeds any practical durationMinutes.
26
+ *
27
+ * # Tenant isolation
28
+ *
29
+ * Every method scopes by projectId. Pattern D — wrong-project /
30
+ * unknown-project queries return empty arrays / not_found errors.
31
+ */
32
+ import type { PrismaClient } from './prisma.js';
33
+ import { type FreezeWindowEvaluator } from '@derwinjs/sdk';
34
+ export interface PrismaFreezeWindowEvaluatorConfig {
35
+ /** Generated Prisma client. Pass an instance per process. */
36
+ prisma: PrismaClient;
37
+ }
38
+ /**
39
+ * Parsed cron — each field is either null (wildcard `*`) or a literal int.
40
+ */
41
+ interface ParsedCron {
42
+ minute: number | null;
43
+ hour: number | null;
44
+ dayOfMonth: number | null;
45
+ month: number | null;
46
+ dayOfWeek: number | null;
47
+ }
48
+ /**
49
+ * Parse a 5-field cron expression. Throws FreezeWindowEvaluatorError
50
+ * (`invalid_cron`) on bad input. Whitespace-tolerant — splits on /\s+/.
51
+ */
52
+ export declare function parseCron(expr: string): ParsedCron;
53
+ /**
54
+ * Find the most recent time at or before `at` when the cron fires.
55
+ * Returns null if no fire within the past MAX_LOOKBACK_MINUTES (which
56
+ * exceeds any practical durationMinutes — windows beyond a week are out
57
+ * of scope for the MVP).
58
+ */
59
+ export declare function mostRecentFire(cron: ParsedCron, at: Date): Date | null;
60
+ export declare function createPrismaFreezeWindowEvaluator(config: PrismaFreezeWindowEvaluatorConfig): FreezeWindowEvaluator;
61
+ export {};
62
+ //# sourceMappingURL=freeze-window-evaluator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"freeze-window-evaluator.d.ts","sourceRoot":"","sources":["../src/freeze-window-evaluator.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,EAGL,KAAK,qBAAqB,EAC3B,MAAM,eAAe,CAAC;AAIvB,MAAM,WAAW,iCAAiC;IAChD,6DAA6D;IAC7D,MAAM,EAAE,YAAY,CAAC;CACtB;AAmBD;;GAEG;AACH,UAAU,UAAU;IAClB,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B;AAED;;;GAGG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,CAyClD;AAgCD;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,UAAU,EAAE,EAAE,EAAE,IAAI,GAAG,IAAI,GAAG,IAAI,CAoBtE;AA+ED,wBAAgB,iCAAiC,CAC/C,MAAM,EAAE,iCAAiC,GACxC,qBAAqB,CA4DvB"}