@derwinjs/db 0.7.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 (41) 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/freeze-window-evaluator.d.ts +62 -0
  10. package/dist/freeze-window-evaluator.d.ts.map +1 -0
  11. package/dist/freeze-window-evaluator.js +236 -0
  12. package/dist/freeze-window-evaluator.js.map +1 -0
  13. package/dist/index.d.ts +6 -0
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +6 -0
  16. package/dist/index.js.map +1 -1
  17. package/dist/kill-switch-evaluator.d.ts +68 -0
  18. package/dist/kill-switch-evaluator.d.ts.map +1 -0
  19. package/dist/kill-switch-evaluator.js +389 -0
  20. package/dist/kill-switch-evaluator.js.map +1 -0
  21. package/dist/path-tier-resolver.d.ts +47 -0
  22. package/dist/path-tier-resolver.d.ts.map +1 -0
  23. package/dist/path-tier-resolver.js +177 -0
  24. package/dist/path-tier-resolver.js.map +1 -0
  25. package/dist/trust-threshold-config-store.d.ts +20 -0
  26. package/dist/trust-threshold-config-store.d.ts.map +1 -0
  27. package/dist/trust-threshold-config-store.js +88 -0
  28. package/dist/trust-threshold-config-store.js.map +1 -0
  29. package/package.json +3 -3
  30. package/prisma/migrations/20260507120000_sprint8_policy_governance/migration.sql +58 -0
  31. package/prisma/migrations/20260507120100_sprint8_phase2_thresholds_and_freeze/migration.sql +33 -0
  32. package/prisma/migrations/20260507120200_sprint8_phase3_budget_cap/migration.sql +21 -0
  33. package/prisma/migrations/20260507120300_sprint8_phase4_kill_switches/migration.sql +74 -0
  34. package/prisma/schema.prisma +183 -11
  35. package/prisma-client/edge.js +96 -19
  36. package/prisma-client/index-browser.js +93 -16
  37. package/prisma-client/index.d.ts +10997 -3426
  38. package/prisma-client/index.js +96 -19
  39. package/prisma-client/package.json +1 -1
  40. package/prisma-client/schema.prisma +183 -11
  41. 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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAGH,OAAO,EAAE,eAAe,EAAsC,MAAM,eAAe,CAAC;AAcpF,4EAA4E;AAE5E;;;;GAIG;AACH,SAAS,eAAe,CAAC,EAAQ;IAC/B,OAAO,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,cAAc,EAAE,EAAE,EAAE,CAAC,WAAW,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAClF,CAAC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E,CAAC,CAAC;QAC1C,MAAM,SAAS,GAAG,MAAM,MAAM,CAAC,WAAW,CAAC,SAAS,CAAC;YACnD,KAAK,EAAE,EAAE,SAAS,EAAE,UAAU,EAAE,EAAE,GAAG,EAAE,UAAU,EAAE,EAAE;YACrD,IAAI,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE;SAC1B,CAAC,CAAC;QACH,MAAM,UAAU,GAAG,SAAS,CAAC,IAAI,CAAC,SAAS,IAAI,CAAC,CAAC;QACjD,MAAM,mBAAmB,GAAG,UAAU,GAAG,GAAG,CAAC;QAE7C,kCAAkC;QAClC,MAAM,gBAAgB,GAAG,OAAO,CAAC,gBAAgB,IAAI,IAAI,CAAC;QAC1D,MAAM,kBAAkB,GACtB,gBAAgB,KAAK,IAAI,IAAI,gBAAgB,KAAK,CAAC;YACjD,CAAC,CAAC,IAAI;YACN,CAAC,CAAC,mBAAmB,GAAG,gBAAgB,CAAC;QAC7C,MAAM,WAAW,GAAG,OAAO,CAAC,iBAAiB,CAAC;QAC9C,MAAM,KAAK,GAAG,WAAW,CAAC,gBAAgB,EAAE,kBAAkB,EAAE,WAAW,CAAC,CAAC;QAE7E,OAAO;YACL,mBAAmB;YACnB,gBAAgB;YAChB,kBAAkB;YAClB,KAAK;SACN,CAAC;IACJ,CAAC;IAED,OAAO;QACL,KAAK,CAAC,SAAS,CAAC,KAA4B;YAC1C,OAAO,aAAa,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;QACxC,CAAC;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,IAAI,CAAC,kBAAkB,CAAC,QAAQ,CAAC,KAAK,CAAC,YAAY,CAAC,EAAE,CAAC;QACrD,MAAM,IAAI,gCAAgC,CACxC,eAAe,EACf,4DAA4D,kBAAkB,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAC5F,CAAC;IACJ,CAAC;IACD,IAAI,OAAO,KAAK,CAAC,MAAM,KAAK,QAAQ,IAAI,KAAK,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,MAAM,KAAK,CAAC,EA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ACrF,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,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"}
@@ -0,0 +1,236 @@
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 { FreezeWindowEvaluatorError, } from '@derwinjs/sdk';
33
+ // ─── Cron evaluator (embedded, dependency-free) ──────────────────────────
34
+ /**
35
+ * Per-field allowed range. dayOfMonth/month are 1-indexed; minute, hour,
36
+ * and dayOfWeek are 0-indexed. dayOfWeek: 0 = Sunday, 6 = Saturday
37
+ * (POSIX cron convention).
38
+ */
39
+ const FIELD_RANGES = [
40
+ [0, 59], // minute
41
+ [0, 23], // hour
42
+ [1, 31], // dayOfMonth
43
+ [1, 12], // month
44
+ [0, 6], // dayOfWeek
45
+ ];
46
+ const FIELD_NAMES = ['minute', 'hour', 'dayOfMonth', 'month', 'dayOfWeek'];
47
+ /**
48
+ * Parse a 5-field cron expression. Throws FreezeWindowEvaluatorError
49
+ * (`invalid_cron`) on bad input. Whitespace-tolerant — splits on /\s+/.
50
+ */
51
+ export function parseCron(expr) {
52
+ if (typeof expr !== 'string' || expr.trim().length === 0) {
53
+ throw new FreezeWindowEvaluatorError('invalid_cron', 'FreezeWindowEvaluator: cronExpression must be a non-empty string');
54
+ }
55
+ const fields = expr.trim().split(/\s+/);
56
+ if (fields.length !== 5) {
57
+ throw new FreezeWindowEvaluatorError('invalid_cron', `FreezeWindowEvaluator: cronExpression must have 5 fields (got ${String(fields.length)}: ${JSON.stringify(expr)})`);
58
+ }
59
+ const parsed = [];
60
+ for (const [i, raw] of fields.entries()) {
61
+ const range = FIELD_RANGES[i];
62
+ const fieldName = FIELD_NAMES[i];
63
+ if (range === undefined || fieldName === undefined)
64
+ continue;
65
+ const [min, max] = range;
66
+ if (raw === '*') {
67
+ parsed.push(null);
68
+ continue;
69
+ }
70
+ if (!/^\d+$/.test(raw)) {
71
+ throw new FreezeWindowEvaluatorError('invalid_cron', `FreezeWindowEvaluator: ${fieldName} must be '*' or an integer (got ${JSON.stringify(raw)})`);
72
+ }
73
+ const n = Number.parseInt(raw, 10);
74
+ if (n < min || n > max) {
75
+ throw new FreezeWindowEvaluatorError('invalid_cron', `FreezeWindowEvaluator: ${fieldName} must be in [${String(min)}, ${String(max)}] (got ${String(n)})`);
76
+ }
77
+ parsed.push(n);
78
+ }
79
+ const [minute = null, hour = null, dayOfMonth = null, month = null, dayOfWeek = null] = parsed;
80
+ return { minute, hour, dayOfMonth, month, dayOfWeek };
81
+ }
82
+ /**
83
+ * True iff the supplied Date matches the parsed cron.
84
+ *
85
+ * NOTE: cron's POSIX convention is that dayOfMonth and dayOfWeek are
86
+ * OR-combined when BOTH are restricted (non-`*`). When at least one is `*`,
87
+ * the other applies normally. This impl follows that convention.
88
+ */
89
+ function cronMatches(cron, at) {
90
+ const minute = at.getUTCMinutes();
91
+ const hour = at.getUTCHours();
92
+ const dom = at.getUTCDate();
93
+ const month = at.getUTCMonth() + 1; // JS month is 0-indexed
94
+ const dow = at.getUTCDay();
95
+ if (cron.minute !== null && cron.minute !== minute)
96
+ return false;
97
+ if (cron.hour !== null && cron.hour !== hour)
98
+ return false;
99
+ if (cron.month !== null && cron.month !== month)
100
+ return false;
101
+ // POSIX dom/dow OR semantics.
102
+ if (cron.dayOfMonth !== null && cron.dayOfWeek !== null) {
103
+ if (cron.dayOfMonth !== dom && cron.dayOfWeek !== dow)
104
+ return false;
105
+ }
106
+ else {
107
+ if (cron.dayOfMonth !== null && cron.dayOfMonth !== dom)
108
+ return false;
109
+ if (cron.dayOfWeek !== null && cron.dayOfWeek !== dow)
110
+ return false;
111
+ }
112
+ return true;
113
+ }
114
+ const MAX_LOOKBACK_MINUTES = 60 * 24 * 7; // 7 days
115
+ /**
116
+ * Find the most recent time at or before `at` when the cron fires.
117
+ * Returns null if no fire within the past MAX_LOOKBACK_MINUTES (which
118
+ * exceeds any practical durationMinutes — windows beyond a week are out
119
+ * of scope for the MVP).
120
+ */
121
+ export function mostRecentFire(cron, at) {
122
+ // Round down to the start of the current minute — cron fires at minute
123
+ // boundaries. seconds/milliseconds set to zero.
124
+ const start = new Date(Date.UTC(at.getUTCFullYear(), at.getUTCMonth(), at.getUTCDate(), at.getUTCHours(), at.getUTCMinutes(), 0, 0));
125
+ let cursor = start;
126
+ for (let i = 0; i <= MAX_LOOKBACK_MINUTES; i++) {
127
+ if (cronMatches(cron, cursor))
128
+ return cursor;
129
+ cursor = new Date(cursor.getTime() - 60_000);
130
+ }
131
+ return null;
132
+ }
133
+ /**
134
+ * True iff the freeze window is active at `at` — i.e., its cron fired
135
+ * within the past `durationMinutes`.
136
+ */
137
+ function windowIsActive(cronExpression, durationMinutes, at) {
138
+ let cron;
139
+ try {
140
+ cron = parseCron(cronExpression);
141
+ }
142
+ catch {
143
+ // A corrupted row should not crash the dispatcher. Treat it as inactive
144
+ // (defense-in-depth) — the createWindow validator prevents bad cron at
145
+ // write time, so this branch is for legacy / direct-DB-edit cases.
146
+ return false;
147
+ }
148
+ const lastFire = mostRecentFire(cron, at);
149
+ if (lastFire === null)
150
+ return false;
151
+ return at.getTime() - lastFire.getTime() < durationMinutes * 60_000;
152
+ }
153
+ // ─── Validation ──────────────────────────────────────────────────────────
154
+ function validateCreateInput(input) {
155
+ if (typeof input.projectId !== 'string' || input.projectId.length === 0) {
156
+ throw new FreezeWindowEvaluatorError('invalid_input', 'FreezeWindowEvaluator: projectId is required');
157
+ }
158
+ if (typeof input.label !== 'string' || input.label.trim().length === 0) {
159
+ throw new FreezeWindowEvaluatorError('invalid_input', 'FreezeWindowEvaluator: label is required');
160
+ }
161
+ if (typeof input.durationMinutes !== 'number' ||
162
+ !Number.isFinite(input.durationMinutes) ||
163
+ !Number.isInteger(input.durationMinutes) ||
164
+ input.durationMinutes <= 0) {
165
+ throw new FreezeWindowEvaluatorError('invalid_input', `FreezeWindowEvaluator: durationMinutes must be a positive integer (got ${String(input.durationMinutes)})`);
166
+ }
167
+ // parseCron throws invalid_cron on bad input.
168
+ parseCron(input.cronExpression);
169
+ }
170
+ function mapWindow(row) {
171
+ return {
172
+ id: row.id,
173
+ projectId: row.projectId,
174
+ cronExpression: row.cronExpression,
175
+ durationMinutes: row.durationMinutes,
176
+ label: row.label,
177
+ blocksDispatch: row.blocksDispatch,
178
+ enabled: row.enabled,
179
+ createdAt: row.createdAt,
180
+ };
181
+ }
182
+ // ─── Factory ─────────────────────────────────────────────────────────────
183
+ export function createPrismaFreezeWindowEvaluator(config) {
184
+ const { prisma } = config;
185
+ return {
186
+ async getActiveWindows(input) {
187
+ const at = input.at ?? new Date();
188
+ const rows = await prisma.freezeWindow.findMany({
189
+ where: { projectId: input.projectId, enabled: true },
190
+ });
191
+ return rows
192
+ .filter((row) => windowIsActive(row.cronExpression, row.durationMinutes, at))
193
+ .map(mapWindow);
194
+ },
195
+ async isDispatchBlocked(input) {
196
+ const at = input.at ?? new Date();
197
+ const rows = await prisma.freezeWindow.findMany({
198
+ where: { projectId: input.projectId, enabled: true, blocksDispatch: true },
199
+ });
200
+ return rows.some((row) => windowIsActive(row.cronExpression, row.durationMinutes, at));
201
+ },
202
+ async listWindows(input) {
203
+ const rows = await prisma.freezeWindow.findMany({
204
+ where: { projectId: input.projectId },
205
+ orderBy: { createdAt: 'desc' },
206
+ });
207
+ return rows.map(mapWindow);
208
+ },
209
+ async createWindow(input) {
210
+ validateCreateInput(input);
211
+ const row = await prisma.freezeWindow.create({
212
+ data: {
213
+ projectId: input.projectId,
214
+ cronExpression: input.cronExpression,
215
+ durationMinutes: input.durationMinutes,
216
+ label: input.label,
217
+ blocksDispatch: input.blocksDispatch,
218
+ enabled: input.enabled,
219
+ },
220
+ select: { id: true },
221
+ });
222
+ return { id: row.id };
223
+ },
224
+ async deleteWindow(input) {
225
+ const existing = await prisma.freezeWindow.findFirst({
226
+ where: { id: input.windowId, projectId: input.projectId },
227
+ select: { id: true },
228
+ });
229
+ if (existing === null) {
230
+ throw new FreezeWindowEvaluatorError('not_found', `FreezeWindow ${input.windowId} not found in project ${input.projectId}`);
231
+ }
232
+ await prisma.freezeWindow.delete({ where: { id: input.windowId } });
233
+ },
234
+ };
235
+ }
236
+ //# sourceMappingURL=freeze-window-evaluator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"freeze-window-evaluator.js","sourceRoot":"","sources":["../src/freeze-window-evaluator.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