@dads-framework/audit 0.0.0-stage → 0.1.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 (185) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +69 -2
  3. package/dist/audit.tsbuildinfo +1 -0
  4. package/dist/index.d.ts +25 -0
  5. package/dist/index.d.ts.map +1 -0
  6. package/dist/index.js +24 -0
  7. package/dist/index.js.map +1 -0
  8. package/dist/lib/actor/compose-subject-to-audit-actor.d.ts +32 -0
  9. package/dist/lib/actor/compose-subject-to-audit-actor.d.ts.map +1 -0
  10. package/dist/lib/actor/compose-subject-to-audit-actor.js +66 -0
  11. package/dist/lib/actor/compose-subject-to-audit-actor.js.map +1 -0
  12. package/dist/lib/audit-plugin.d.ts +21 -0
  13. package/dist/lib/audit-plugin.d.ts.map +1 -0
  14. package/dist/lib/audit-plugin.js +61 -0
  15. package/dist/lib/audit-plugin.js.map +1 -0
  16. package/dist/lib/client-diagnostics/client-diagnostic-event.ids.d.ts +5 -0
  17. package/dist/lib/client-diagnostics/client-diagnostic-event.ids.d.ts.map +1 -0
  18. package/dist/lib/client-diagnostics/client-diagnostic-event.ids.js +4 -0
  19. package/dist/lib/client-diagnostics/client-diagnostic-event.ids.js.map +1 -0
  20. package/dist/lib/client-diagnostics/client-diagnostics.route-handlers.d.ts +23 -0
  21. package/dist/lib/client-diagnostics/client-diagnostics.route-handlers.d.ts.map +1 -0
  22. package/dist/lib/client-diagnostics/client-diagnostics.route-handlers.js +27 -0
  23. package/dist/lib/client-diagnostics/client-diagnostics.route-handlers.js.map +1 -0
  24. package/dist/lib/client-diagnostics/client-diagnostics.routes.d.ts +18 -0
  25. package/dist/lib/client-diagnostics/client-diagnostics.routes.d.ts.map +1 -0
  26. package/dist/lib/client-diagnostics/client-diagnostics.routes.js +41 -0
  27. package/dist/lib/client-diagnostics/client-diagnostics.routes.js.map +1 -0
  28. package/dist/lib/client-diagnostics/logic/client-diagnostic-row.d.ts +30 -0
  29. package/dist/lib/client-diagnostics/logic/client-diagnostic-row.d.ts.map +1 -0
  30. package/dist/lib/client-diagnostics/logic/client-diagnostic-row.js +39 -0
  31. package/dist/lib/client-diagnostics/logic/client-diagnostic-row.js.map +1 -0
  32. package/dist/lib/client-diagnostics/record-client-diagnostics.d.ts +29 -0
  33. package/dist/lib/client-diagnostics/record-client-diagnostics.d.ts.map +1 -0
  34. package/dist/lib/client-diagnostics/record-client-diagnostics.js +40 -0
  35. package/dist/lib/client-diagnostics/record-client-diagnostics.js.map +1 -0
  36. package/dist/lib/compose-audit.d.ts +27 -0
  37. package/dist/lib/compose-audit.d.ts.map +1 -0
  38. package/dist/lib/compose-audit.js +38 -0
  39. package/dist/lib/compose-audit.js.map +1 -0
  40. package/dist/lib/database/attach-audit-trigger.d.ts +34 -0
  41. package/dist/lib/database/attach-audit-trigger.d.ts.map +1 -0
  42. package/dist/lib/database/attach-audit-trigger.js +40 -0
  43. package/dist/lib/database/attach-audit-trigger.js.map +1 -0
  44. package/dist/lib/database/audit-log-position.ids.d.ts +13 -0
  45. package/dist/lib/database/audit-log-position.ids.d.ts.map +1 -0
  46. package/dist/lib/database/audit-log-position.ids.js +11 -0
  47. package/dist/lib/database/audit-log-position.ids.js.map +1 -0
  48. package/dist/lib/database/audit-row-json.d.ts +10 -0
  49. package/dist/lib/database/audit-row-json.d.ts.map +1 -0
  50. package/dist/lib/database/audit-row-json.js +2 -0
  51. package/dist/lib/database/audit-row-json.js.map +1 -0
  52. package/dist/lib/database/audit-trigger-allowlist.d.ts +4 -0
  53. package/dist/lib/database/audit-trigger-allowlist.d.ts.map +1 -0
  54. package/dist/lib/database/audit-trigger-allowlist.js +8 -0
  55. package/dist/lib/database/audit-trigger-allowlist.js.map +1 -0
  56. package/dist/lib/database/configure-audit-triggers.d.ts +8 -0
  57. package/dist/lib/database/configure-audit-triggers.d.ts.map +1 -0
  58. package/dist/lib/database/configure-audit-triggers.js +27 -0
  59. package/dist/lib/database/configure-audit-triggers.js.map +1 -0
  60. package/dist/lib/database/configure-plugin-audit-triggers.d.ts +11 -0
  61. package/dist/lib/database/configure-plugin-audit-triggers.d.ts.map +1 -0
  62. package/dist/lib/database/configure-plugin-audit-triggers.js +23 -0
  63. package/dist/lib/database/configure-plugin-audit-triggers.js.map +1 -0
  64. package/dist/lib/database/generated-tables/audit-log.tables.d.ts +363 -0
  65. package/dist/lib/database/generated-tables/audit-log.tables.d.ts.map +1 -0
  66. package/dist/lib/database/generated-tables/audit-log.tables.js +53 -0
  67. package/dist/lib/database/generated-tables/audit-log.tables.js.map +1 -0
  68. package/dist/lib/database/generated-tables/client-diagnostic-events.tables.d.ts +376 -0
  69. package/dist/lib/database/generated-tables/client-diagnostic-events.tables.d.ts.map +1 -0
  70. package/dist/lib/database/generated-tables/client-diagnostic-events.tables.js +45 -0
  71. package/dist/lib/database/generated-tables/client-diagnostic-events.tables.js.map +1 -0
  72. package/dist/lib/database/generated-tables/known-fault-fingerprints.tables.d.ts +76 -0
  73. package/dist/lib/database/generated-tables/known-fault-fingerprints.tables.d.ts.map +1 -0
  74. package/dist/lib/database/generated-tables/known-fault-fingerprints.tables.js +32 -0
  75. package/dist/lib/database/generated-tables/known-fault-fingerprints.tables.js.map +1 -0
  76. package/dist/lib/database/generated-tables/request-log.tables.d.ts +678 -0
  77. package/dist/lib/database/generated-tables/request-log.tables.d.ts.map +1 -0
  78. package/dist/lib/database/generated-tables/request-log.tables.js +59 -0
  79. package/dist/lib/database/generated-tables/request-log.tables.js.map +1 -0
  80. package/dist/lib/database/migrations/20260910233455_audit.migration.d.ts +5 -0
  81. package/dist/lib/database/migrations/20260910233455_audit.migration.d.ts.map +1 -0
  82. package/dist/lib/database/migrations/20260910233455_audit.migration.js +304 -0
  83. package/dist/lib/database/migrations/20260910233455_audit.migration.js.map +1 -0
  84. package/dist/lib/database/readers/audit-log-reader.interface.d.ts +82 -0
  85. package/dist/lib/database/readers/audit-log-reader.interface.d.ts.map +1 -0
  86. package/dist/lib/database/readers/audit-log-reader.interface.js +10 -0
  87. package/dist/lib/database/readers/audit-log-reader.interface.js.map +1 -0
  88. package/dist/lib/database/readers/drizzle-audit-log-reader.d.ts +16 -0
  89. package/dist/lib/database/readers/drizzle-audit-log-reader.d.ts.map +1 -0
  90. package/dist/lib/database/readers/drizzle-audit-log-reader.js +197 -0
  91. package/dist/lib/database/readers/drizzle-audit-log-reader.js.map +1 -0
  92. package/dist/lib/database/readers/drizzle-fault-alert-reader.d.ts +36 -0
  93. package/dist/lib/database/readers/drizzle-fault-alert-reader.d.ts.map +1 -0
  94. package/dist/lib/database/readers/drizzle-fault-alert-reader.js +113 -0
  95. package/dist/lib/database/readers/drizzle-fault-alert-reader.js.map +1 -0
  96. package/dist/lib/database/readers/drizzle-request-log-reader.d.ts +16 -0
  97. package/dist/lib/database/readers/drizzle-request-log-reader.d.ts.map +1 -0
  98. package/dist/lib/database/readers/drizzle-request-log-reader.js +88 -0
  99. package/dist/lib/database/readers/drizzle-request-log-reader.js.map +1 -0
  100. package/dist/lib/database/readers/fault-alert-reader.interface.d.ts +73 -0
  101. package/dist/lib/database/readers/fault-alert-reader.interface.d.ts.map +1 -0
  102. package/dist/lib/database/readers/fault-alert-reader.interface.js +13 -0
  103. package/dist/lib/database/readers/fault-alert-reader.interface.js.map +1 -0
  104. package/dist/lib/database/readers/request-log-reader.interface.d.ts +64 -0
  105. package/dist/lib/database/readers/request-log-reader.interface.d.ts.map +1 -0
  106. package/dist/lib/database/readers/request-log-reader.interface.js +9 -0
  107. package/dist/lib/database/readers/request-log-reader.interface.js.map +1 -0
  108. package/dist/lib/database/testing/audited-fixture-tables.d.ts +14 -0
  109. package/dist/lib/database/testing/audited-fixture-tables.d.ts.map +1 -0
  110. package/dist/lib/database/testing/audited-fixture-tables.js +46 -0
  111. package/dist/lib/database/testing/audited-fixture-tables.js.map +1 -0
  112. package/dist/lib/database/writers/client-diagnostic-writer.interface.d.ts +20 -0
  113. package/dist/lib/database/writers/client-diagnostic-writer.interface.d.ts.map +1 -0
  114. package/dist/lib/database/writers/client-diagnostic-writer.interface.js +3 -0
  115. package/dist/lib/database/writers/client-diagnostic-writer.interface.js.map +1 -0
  116. package/dist/lib/database/writers/drizzle-client-diagnostic-writer.d.ts +11 -0
  117. package/dist/lib/database/writers/drizzle-client-diagnostic-writer.d.ts.map +1 -0
  118. package/dist/lib/database/writers/drizzle-client-diagnostic-writer.js +46 -0
  119. package/dist/lib/database/writers/drizzle-client-diagnostic-writer.js.map +1 -0
  120. package/dist/lib/database/writers/drizzle-request-log-writer.d.ts +9 -0
  121. package/dist/lib/database/writers/drizzle-request-log-writer.d.ts.map +1 -0
  122. package/dist/lib/database/writers/drizzle-request-log-writer.js +17 -0
  123. package/dist/lib/database/writers/drizzle-request-log-writer.js.map +1 -0
  124. package/dist/lib/database/writers/request-log-writer.interface.d.ts +17 -0
  125. package/dist/lib/database/writers/request-log-writer.interface.d.ts.map +1 -0
  126. package/dist/lib/database/writers/request-log-writer.interface.js +10 -0
  127. package/dist/lib/database/writers/request-log-writer.interface.js.map +1 -0
  128. package/dist/lib/fault-alerts/alert-new-faults.job.d.ts +48 -0
  129. package/dist/lib/fault-alerts/alert-new-faults.job.d.ts.map +1 -0
  130. package/dist/lib/fault-alerts/alert-new-faults.job.js +105 -0
  131. package/dist/lib/fault-alerts/alert-new-faults.job.js.map +1 -0
  132. package/dist/lib/fault-alerts/logic/describe-fault-line.d.ts +16 -0
  133. package/dist/lib/fault-alerts/logic/describe-fault-line.d.ts.map +1 -0
  134. package/dist/lib/fault-alerts/logic/describe-fault-line.js +21 -0
  135. package/dist/lib/fault-alerts/logic/describe-fault-line.js.map +1 -0
  136. package/dist/lib/fault-alerts/logic/summarize-new-faults.d.ts +51 -0
  137. package/dist/lib/fault-alerts/logic/summarize-new-faults.d.ts.map +1 -0
  138. package/dist/lib/fault-alerts/logic/summarize-new-faults.js +56 -0
  139. package/dist/lib/fault-alerts/logic/summarize-new-faults.js.map +1 -0
  140. package/dist/lib/fault-alerts/new-faults.email-template.d.ts +8 -0
  141. package/dist/lib/fault-alerts/new-faults.email-template.d.ts.map +1 -0
  142. package/dist/lib/fault-alerts/new-faults.email-template.js +66 -0
  143. package/dist/lib/fault-alerts/new-faults.email-template.js.map +1 -0
  144. package/dist/lib/jobs/prune-client-diagnostics.job.d.ts +25 -0
  145. package/dist/lib/jobs/prune-client-diagnostics.job.d.ts.map +1 -0
  146. package/dist/lib/jobs/prune-client-diagnostics.job.js +38 -0
  147. package/dist/lib/jobs/prune-client-diagnostics.job.js.map +1 -0
  148. package/dist/lib/jobs/prune-request-log.job.d.ts +23 -0
  149. package/dist/lib/jobs/prune-request-log.job.d.ts.map +1 -0
  150. package/dist/lib/jobs/prune-request-log.job.js +36 -0
  151. package/dist/lib/jobs/prune-request-log.job.js.map +1 -0
  152. package/dist/lib/jobs/retention-window.d.ts +3 -0
  153. package/dist/lib/jobs/retention-window.d.ts.map +1 -0
  154. package/dist/lib/jobs/retention-window.js +10 -0
  155. package/dist/lib/jobs/retention-window.js.map +1 -0
  156. package/dist/lib/request-log/insert-foreign-rows.d.ts +21 -0
  157. package/dist/lib/request-log/insert-foreign-rows.d.ts.map +1 -0
  158. package/dist/lib/request-log/insert-foreign-rows.js +86 -0
  159. package/dist/lib/request-log/insert-foreign-rows.js.map +1 -0
  160. package/dist/lib/request-log/postgres-request-log-sink.d.ts +64 -0
  161. package/dist/lib/request-log/postgres-request-log-sink.d.ts.map +1 -0
  162. package/dist/lib/request-log/postgres-request-log-sink.js +168 -0
  163. package/dist/lib/request-log/postgres-request-log-sink.js.map +1 -0
  164. package/dist/schema.d.ts +6 -0
  165. package/dist/schema.d.ts.map +1 -0
  166. package/dist/schema.js +5 -0
  167. package/dist/schema.js.map +1 -0
  168. package/dist/testing/audit-log-record.factory.d.ts +13 -0
  169. package/dist/testing/audit-log-record.factory.d.ts.map +1 -0
  170. package/dist/testing/audit-log-record.factory.js +46 -0
  171. package/dist/testing/audit-log-record.factory.js.map +1 -0
  172. package/dist/testing/audit-log.factory.d.ts +14 -0
  173. package/dist/testing/audit-log.factory.d.ts.map +1 -0
  174. package/dist/testing/audit-log.factory.js +30 -0
  175. package/dist/testing/audit-log.factory.js.map +1 -0
  176. package/dist/testing/index.d.ts +4 -0
  177. package/dist/testing/index.d.ts.map +1 -0
  178. package/dist/testing/index.js +5 -0
  179. package/dist/testing/index.js.map +1 -0
  180. package/dist/testing/request-log.factory.d.ts +13 -0
  181. package/dist/testing/request-log.factory.d.ts.map +1 -0
  182. package/dist/testing/request-log.factory.js +40 -0
  183. package/dist/testing/request-log.factory.js.map +1 -0
  184. package/package.json +67 -4
  185. package/src/lib/database/migrations/20260910233455_audit.migration.ts +345 -0
@@ -0,0 +1,59 @@
1
+ import { boolean, check, index, integer, jsonb, pgTable, text, timestamp, } from "drizzle-orm/pg-core";
2
+ import { sql } from "drizzle-orm";
3
+ import { inet } from "@dads-framework/postgres";
4
+ /**
5
+ * One row per mutating request or denial, written off the request path
6
+ * (ADR-0112 slice B). Joins to `audit_log` by `requestId`: this table is a
7
+ * request's environment (IP, user agent, route, decision), `audit_log` is a
8
+ * row's history.
9
+ *
10
+ * No `audit_record` trigger is attached — see `AUDIT_TRIGGER_ALLOWLIST`.
11
+ * Rows here are pruned on a schedule; `audit_log` is never pruned.
12
+ */
13
+ export const requestLog = pgTable("request_log", {
14
+ requestId: text("request_id").primaryKey().notNull().$type(),
15
+ recordedAt: timestamp("recorded_at", { withTimezone: true })
16
+ .defaultNow()
17
+ .notNull(),
18
+ method: text("method").notNull(),
19
+ routeId: text("route_id"),
20
+ path: text("path").notNull(),
21
+ status: integer("status").notNull(),
22
+ durationMs: integer("duration_ms").notNull(),
23
+ actorKind: text("actor_kind").notNull(),
24
+ actorId: text("actor_id").notNull(),
25
+ authMethod: text("auth_method"),
26
+ accessKind: text("access_kind"),
27
+ permission: text("permission"),
28
+ resourceType: text("resource_type"),
29
+ resourceId: text("resource_id"),
30
+ decision: text("decision").notNull(),
31
+ errorCode: text("error_code"),
32
+ ip: inet("ip"),
33
+ forwardedFor: text("forwarded_for"),
34
+ userAgent: text("user_agent"),
35
+ clientHints: jsonb("client_hints").$type(),
36
+ acceptLanguage: text("accept_language"),
37
+ clientContext: jsonb("client_context").$type(),
38
+ apiRelease: text("api_release"),
39
+ dbQueryCount: integer("db_query_count"),
40
+ dbMs: integer("db_ms"),
41
+ authzCheckCount: integer("authz_check_count"),
42
+ authzMs: integer("authz_ms"),
43
+ instanceId: text("instance_id"),
44
+ exceptionType: text("exception_type"),
45
+ exceptionFingerprint: text("exception_fingerprint"),
46
+ traceId: text("trace_id"),
47
+ imported: boolean("imported").notNull().default(false),
48
+ authzCachedReadCount: integer("authz_cached_read_count"),
49
+ authzFullyConsistentReadCount: integer("authz_fully_consistent_read_count"),
50
+ authzLargestLookupCount: integer("authz_largest_lookup_count"),
51
+ }, (table) => [
52
+ index("request_log_actor_recorded_at_idx").on(table.actorKind, table.actorId, table.recordedAt),
53
+ index("request_log_exception_recorded_at_idx").on(table.exceptionFingerprint, table.recordedAt),
54
+ index("request_log_recorded_at_idx").on(table.recordedAt),
55
+ index("request_log_route_recorded_at_idx").on(table.routeId, table.recordedAt),
56
+ index("request_log_status_recorded_at_idx").on(table.status, table.recordedAt),
57
+ check("request_log_decision_check", sql `${table.decision} = ANY (ARRAY['allow'::text, 'denied'::text, 'not-found'::text, 'unauthenticated'::text, 'error'::text])`),
58
+ ]);
59
+ //# sourceMappingURL=request-log.tables.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request-log.tables.js","sourceRoot":"","sources":["../../../../src/lib/database/generated-tables/request-log.tables.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EACP,KAAK,EACL,KAAK,EACL,OAAO,EACP,KAAK,EACL,OAAO,EACP,IAAI,EACJ,SAAS,GACV,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAGlC,OAAO,EAAE,IAAI,EAAE,MAAM,0BA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ACZ,KAAK,CAAC,UAAU,CACjB;IACD,KAAK,CACH,4BAA4B,EAC5B,GAAG,CAAA,GAAG,KAAK,CAAC,QAAQ,0GAA0G,CAC/H;CACF,CACF,CAAC"}
@@ -0,0 +1,5 @@
1
+ import type { MigrationBuilder } from "node-pg-migrate";
2
+ /** The complete audit schema for a new database. Applications attach their own table triggers. */
3
+ export declare function up(pgm: MigrationBuilder): void;
4
+ export declare const down = false;
5
+ //# sourceMappingURL=20260910233455_audit.migration.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"20260910233455_audit.migration.d.ts","sourceRoot":"","sources":["../../../../src/lib/database/migrations/20260910233455_audit.migration.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAExD,kGAAkG;AAClG,wBAAgB,EAAE,CAAC,GAAG,EAAE,gBAAgB,GAAG,IAAI,CAO9C;AAED,eAAO,MAAM,IAAI,QAAQ,CAAC"}
@@ -0,0 +1,304 @@
1
+ /** The complete audit schema for a new database. Applications attach their own table triggers. */
2
+ export function up(pgm) {
3
+ createAuditLogTable(pgm);
4
+ pgm.sql(AUDIT_LOG_IMMUTABLE_SQL);
5
+ pgm.sql(AUDIT_RECORD_FUNCTION_SQL);
6
+ createRequestLog(pgm);
7
+ createClientDiagnostics(pgm);
8
+ createKnownFaults(pgm);
9
+ }
10
+ export const down = false;
11
+ function createAuditLogTable(pgm) {
12
+ pgm.createTable("audit_log", {
13
+ position: {
14
+ type: "bigint",
15
+ primaryKey: true,
16
+ sequenceGenerated: { precedence: "ALWAYS" },
17
+ },
18
+ transaction_id: {
19
+ type: "xid8",
20
+ notNull: true,
21
+ default: pgm.func("pg_current_xact_id()"),
22
+ },
23
+ recorded_at: {
24
+ type: "timestamptz",
25
+ notNull: true,
26
+ default: pgm.func("now()"),
27
+ },
28
+ actor_kind: { type: "text", notNull: true },
29
+ actor_id: { type: "text", notNull: true },
30
+ request_id: { type: "text", notNull: true },
31
+ reason: { type: "text" },
32
+ owner_partition_id: { type: "uuid" },
33
+ table_name: { type: "text", notNull: true },
34
+ scope_values: { type: "jsonb" },
35
+ row_key: { type: "jsonb", notNull: true },
36
+ user_id: { type: "text" },
37
+ client_id: { type: "text" },
38
+ client_name: { type: "text" },
39
+ operation: { type: "text", notNull: true },
40
+ changed_columns: { type: "text[]" },
41
+ row_before: { type: "jsonb" },
42
+ row_after: { type: "jsonb" },
43
+ });
44
+ pgm.addConstraint("audit_log", "audit_log_operation_check", "CHECK (operation IN ('insert', 'update', 'delete'))");
45
+ pgm.sql(AUDIT_LOG_INDEXES_SQL);
46
+ }
47
+ const AUDIT_LOG_IMMUTABLE_SQL = `
48
+ CREATE FUNCTION audit_log_immutable() RETURNS trigger
49
+ LANGUAGE plpgsql AS $$
50
+ BEGIN
51
+ RAISE EXCEPTION 'audit_log rows are immutable'
52
+ USING ERRCODE = 'insufficient_privilege';
53
+ END;
54
+ $$;
55
+
56
+ CREATE TRIGGER audit_log_immutable
57
+ BEFORE UPDATE OR DELETE ON audit_log
58
+ FOR EACH ROW EXECUTE FUNCTION audit_log_immutable();
59
+ `;
60
+ const AUDIT_LOG_INDEXES_SQL = `
61
+ CREATE INDEX audit_log_actor_position_idx ON audit_log (actor_kind, actor_id, position);
62
+ CREATE INDEX audit_log_partition_position_idx ON audit_log (owner_partition_id, position);
63
+ CREATE INDEX audit_log_request_idx ON audit_log (request_id);
64
+ CREATE INDEX audit_log_row_time_idx ON audit_log (table_name, row_key, recorded_at DESC, position DESC);
65
+ CREATE INDEX audit_log_table_position_idx ON audit_log (table_name, position);
66
+ CREATE INDEX audit_log_time_idx ON audit_log (recorded_at DESC, position DESC);
67
+ CREATE INDEX audit_log_user_time_idx ON audit_log (user_id, recorded_at DESC, position DESC)
68
+ WHERE user_id IS NOT NULL;
69
+ CREATE INDEX audit_log_scope_table_idx
70
+ ON audit_log USING gin (scope_values jsonb_path_ops, (ARRAY[table_name]));
71
+ `;
72
+ const AUDIT_RECORD_FUNCTION_SQL = `
73
+ CREATE FUNCTION public.audit_record() RETURNS trigger
74
+ LANGUAGE plpgsql AS $$
75
+ -- Arguments name key columns, an optional UUID owner column, and a scope map.
76
+ DECLARE
77
+ actor_setting text;
78
+ actor jsonb;
79
+ before_row jsonb;
80
+ after_row jsonb;
81
+ subject jsonb;
82
+ key_values jsonb;
83
+ changed text[];
84
+ owner_id uuid;
85
+ acting_user_id text;
86
+ scope_values jsonb;
87
+ BEGIN
88
+ actor_setting := nullif(current_setting('dads.audit_actor', true), '');
89
+ IF actor_setting IS NULL THEN
90
+ RAISE EXCEPTION 'Audit actor is required for %', TG_TABLE_NAME
91
+ USING ERRCODE = 'insufficient_privilege';
92
+ END IF;
93
+ actor := actor_setting::jsonb;
94
+ IF nullif(actor ->> 'kind', '') IS NULL
95
+ OR nullif(actor ->> 'requestId', '') IS NULL
96
+ OR actor ->> 'id' IS NULL THEN
97
+ RAISE EXCEPTION 'Audit actor identity is incomplete for %', TG_TABLE_NAME
98
+ USING ERRCODE = 'insufficient_privilege';
99
+ END IF;
100
+
101
+ IF TG_OP = 'INSERT' THEN
102
+ after_row := to_jsonb(NEW);
103
+ ELSIF TG_OP = 'UPDATE' THEN
104
+ before_row := to_jsonb(OLD);
105
+ after_row := to_jsonb(NEW);
106
+ ELSE
107
+ before_row := to_jsonb(OLD);
108
+ END IF;
109
+
110
+ subject := coalesce(after_row, before_row);
111
+
112
+ IF TG_ARGV[0] = '' THEN
113
+ key_values := '{}'::jsonb;
114
+ ELSE
115
+ SELECT coalesce(jsonb_object_agg(key_column, subject -> key_column), '{}'::jsonb)
116
+ INTO key_values
117
+ FROM unnest(string_to_array(TG_ARGV[0], ',')) AS key_column;
118
+ END IF;
119
+
120
+ IF TG_OP = 'UPDATE' THEN
121
+ SELECT array_agg(changes.key ORDER BY changes.key)
122
+ INTO changed
123
+ FROM jsonb_each(after_row) AS changes
124
+ WHERE before_row -> changes.key IS DISTINCT FROM changes.value;
125
+ END IF;
126
+
127
+ IF TG_ARGV[1] <> '' THEN
128
+ owner_id := (subject ->> TG_ARGV[1])::uuid;
129
+ END IF;
130
+
131
+ SELECT jsonb_object_agg(scope.dimension, subject ->> scope.column_name)
132
+ INTO scope_values
133
+ FROM jsonb_each_text(coalesce(TG_ARGV[2]::jsonb, '{}'::jsonb))
134
+ AS scope(dimension, column_name)
135
+ WHERE subject ->> scope.column_name IS NOT NULL
136
+ AND (
137
+ TG_OP <> 'UPDATE'
138
+ OR before_row -> scope.column_name IS NOT DISTINCT FROM after_row -> scope.column_name
139
+ );
140
+
141
+ IF actor ->> 'kind' = 'user' THEN
142
+ acting_user_id := nullif(actor ->> 'id', '');
143
+ END IF;
144
+
145
+ INSERT INTO public.audit_log (
146
+ actor_kind,
147
+ actor_id,
148
+ request_id,
149
+ reason,
150
+ owner_partition_id,
151
+ scope_values,
152
+ user_id,
153
+ client_id,
154
+ client_name,
155
+ table_name,
156
+ row_key,
157
+ operation,
158
+ changed_columns,
159
+ row_before,
160
+ row_after
161
+ ) VALUES (
162
+ actor ->> 'kind',
163
+ actor ->> 'id',
164
+ actor ->> 'requestId',
165
+ actor ->> 'reason',
166
+ owner_id,
167
+ scope_values,
168
+ acting_user_id,
169
+ nullif(actor ->> 'clientId', ''),
170
+ nullif(actor ->> 'clientName', ''),
171
+ TG_TABLE_NAME,
172
+ key_values,
173
+ lower(TG_OP),
174
+ changed,
175
+ before_row,
176
+ after_row
177
+ );
178
+
179
+ RETURN NULL;
180
+ END;
181
+ $$;
182
+ `;
183
+ function createRequestLog(pgm) {
184
+ pgm.createTable("request_log", {
185
+ request_id: { type: "text", primaryKey: true },
186
+ recorded_at: {
187
+ type: "timestamptz",
188
+ notNull: true,
189
+ default: pgm.func("now()"),
190
+ },
191
+ method: { type: "text", notNull: true },
192
+ route_id: { type: "text" },
193
+ path: { type: "text", notNull: true },
194
+ status: { type: "integer", notNull: true },
195
+ duration_ms: { type: "integer", notNull: true },
196
+ actor_kind: { type: "text", notNull: true },
197
+ actor_id: { type: "text", notNull: true },
198
+ auth_method: { type: "text" },
199
+ access_kind: { type: "text" },
200
+ permission: { type: "text" },
201
+ resource_type: { type: "text" },
202
+ resource_id: { type: "text" },
203
+ decision: { type: "text", notNull: true },
204
+ error_code: { type: "text" },
205
+ ip: { type: "inet" },
206
+ forwarded_for: { type: "text" },
207
+ user_agent: { type: "text" },
208
+ client_hints: { type: "jsonb" },
209
+ accept_language: { type: "text" },
210
+ client_context: { type: "jsonb" },
211
+ api_release: { type: "text" },
212
+ db_query_count: { type: "integer" },
213
+ db_ms: { type: "integer" },
214
+ authz_check_count: { type: "integer" },
215
+ authz_ms: { type: "integer" },
216
+ instance_id: { type: "text" },
217
+ authz_cached_read_count: { type: "integer" },
218
+ authz_fully_consistent_read_count: { type: "integer" },
219
+ authz_largest_lookup_count: { type: "integer" },
220
+ exception_type: { type: "text" },
221
+ exception_fingerprint: { type: "text" },
222
+ trace_id: { type: "text" },
223
+ imported: { type: "boolean", notNull: true, default: false },
224
+ });
225
+ pgm.addConstraint("request_log", "request_log_decision_check", "CHECK (decision IN ('allow', 'denied', 'not-found', 'unauthenticated', 'error'))");
226
+ pgm.createIndex("request_log", ["recorded_at"], {
227
+ name: "request_log_recorded_at_idx",
228
+ });
229
+ pgm.createIndex("request_log", ["actor_kind", "actor_id", "recorded_at"], {
230
+ name: "request_log_actor_recorded_at_idx",
231
+ });
232
+ pgm.createIndex("request_log", ["status", "recorded_at"], {
233
+ name: "request_log_status_recorded_at_idx",
234
+ });
235
+ pgm.createIndex("request_log", ["route_id", "recorded_at"], {
236
+ name: "request_log_route_recorded_at_idx",
237
+ });
238
+ pgm.createIndex("request_log", ["exception_fingerprint", "recorded_at"], {
239
+ name: "request_log_exception_recorded_at_idx",
240
+ });
241
+ }
242
+ function createClientDiagnostics(pgm) {
243
+ pgm.createTable("client_diagnostic_events", {
244
+ id: { type: "uuid", primaryKey: true },
245
+ recorded_at: {
246
+ type: "timestamptz",
247
+ notNull: true,
248
+ default: pgm.func("now()"),
249
+ },
250
+ occurred_at: { type: "timestamptz", notNull: true },
251
+ fingerprint: { type: "text", notNull: true },
252
+ kind: { type: "text", notNull: true },
253
+ severity: { type: "text", notNull: true },
254
+ name: { type: "text", notNull: true },
255
+ message: { type: "text", notNull: true },
256
+ frames: { type: "jsonb", notNull: true },
257
+ breadcrumbs: { type: "jsonb", notNull: true },
258
+ release: { type: "text", notNull: true },
259
+ client: { type: "text", notNull: true },
260
+ request_id: { type: "text" },
261
+ actor_kind: { type: "text", notNull: true },
262
+ actor_id: { type: "text", notNull: true },
263
+ context: { type: "jsonb" },
264
+ });
265
+ pgm.addConstraint("client_diagnostic_events", "client_diagnostic_events_severity_check", "CHECK (severity IN ('error', 'warning', 'info'))");
266
+ pgm.createIndex("client_diagnostic_events", ["fingerprint", "recorded_at"], {
267
+ name: "client_diagnostic_events_fingerprint_recorded_at_idx",
268
+ });
269
+ pgm.createIndex("client_diagnostic_events", ["recorded_at"], {
270
+ name: "client_diagnostic_events_recorded_at_idx",
271
+ });
272
+ pgm.createIndex("client_diagnostic_events", ["release", "recorded_at"], {
273
+ name: "client_diagnostic_events_release_recorded_at_idx",
274
+ });
275
+ pgm.createIndex("client_diagnostic_events", ["request_id"], {
276
+ name: "client_diagnostic_events_request_idx",
277
+ });
278
+ pgm.sql(`
279
+ COMMENT ON COLUMN client_diagnostic_events.message IS
280
+ 'The exception''s own text. Free text a page may have composed from user input, bounded by the contract on the way in and by the retention sweep on the way out — the same bound request_log puts on an address.';
281
+ `);
282
+ pgm.sql(`
283
+ COMMENT ON COLUMN client_diagnostic_events.occurred_at IS
284
+ 'The CLIENT''s clock at the moment of the fault, not the server''s. Untrusted: compare against recorded_at rather than ordering by it.';
285
+ `);
286
+ }
287
+ function createKnownFaults(pgm) {
288
+ pgm.createTable("known_fault_fingerprints", {
289
+ origin: { type: "text", notNull: true },
290
+ fingerprint: { type: "text", notNull: true },
291
+ first_alerted_at: {
292
+ type: "timestamptz",
293
+ notNull: true,
294
+ default: pgm.func("now()"),
295
+ },
296
+ });
297
+ pgm.addConstraint("known_fault_fingerprints", "known_fault_fingerprints_pkey", { primaryKey: ["origin", "fingerprint"] });
298
+ pgm.addConstraint("known_fault_fingerprints", "known_fault_fingerprints_origin_check", "CHECK (origin IN ('server', 'client'))");
299
+ pgm.sql(`
300
+ COMMENT ON TABLE known_fault_fingerprints IS
301
+ 'Fault types that have already been alerted on. Never pruned: it is what lets the sweep say "new" truthfully after the fault tables it watches have aged their rows out.';
302
+ `);
303
+ }
304
+ //# sourceMappingURL=20260910233455_audit.migration.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"20260910233455_audit.migration.js","sourceRoot":"","sources":["../../../../src/lib/database/migrations/20260910233455_audit.migration.ts"],"names":[],"mappings":"AAEA,kGAAkG;AAClG,MAAM,UAAU,EAAE,CAAC,GAAqB;IACtC,mBAAmB,CAAC,GAAG,CAAC,CAAC;IACzB,GAAG,CAAC,GAAG,CAAC,uBAAuB,CAAC,CAAC;IACjC,GAAG,CAAC,GAAG,CAAC,yBAAyB,CAAC,CAAC;IACnC,gBAAgB,CAAC,GAAG,CAAC,CAAC;IACtB,uBAAuB,CAAC,GAAG,CAAC,CAAC;IAC7B,iBAAiB,CAAC,GAAG,CAAC,CAAC;AACzB,CAAC;AAED,MAAM,CAAC,MAAM,IAAI,GAAG,KAAK,CAAC;AAE1B,SAAS,mBAAmB,CAAC,GAAqB;IAChD,GAAG,CAAC,WAAW,CAAC,WAAW,EAAE;QAC3B,QAAQ,EAAE;YACR,IAAI,EAAE,QAAQ;YACd,UAAU,EAAE,IAAI;YAChB,iBAAiB,EAAE,EAAE,UAAU,EAAE,QAAQ,EAAE;SAC5C;QACD,cAAc,EAAE;YACd,IAAI,EAAE,MAAM;YACZ,OAAO,EAAE,IAAI;YACb,OAAO,EAAE,GAAG,CAAC,IAAI,CAAC,sBAAsB,CAAC;SAC1C;QACD,WAAW,EAAE;YACX,IAAI,EAAE,aAAa;YACnB,OAAO,EAAE,IAAI;YACb,OAA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AiB,CAAC,GAAqB;IAC9C,GAAG,CAAC,WAAW,CAAC,0BAA0B,EAAE;QAC1C,MAAM,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE;QACvC,WAAW,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE;QAC5C,gBAAgB,EAAE;YAChB,IAAI,EAAE,aAAa;YACnB,OAAO,EAAE,IAAI;YACb,OAAO,EAAE,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC;SAC3B;KACF,CAAC,CAAC;IAEH,GAAG,CAAC,aAAa,CACf,0BAA0B,EAC1B,+BAA+B,EAC/B,EAAE,UAAU,EAAE,CAAC,QAAQ,EAAE,aAAa,CAAC,EAAE,CAC1C,CAAC;IAEF,GAAG,CAAC,aAAa,CACf,0BAA0B,EAC1B,uCAAuC,EACvC,wCAAwC,CACzC,CAAC;IAEF,GAAG,CAAC,GAAG,CAAC;;;GAGP,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,82 @@
1
+ import type { AuditLogPosition } from "../audit-log-position.ids.js";
2
+ import type { AuditScopeValues } from "../audit-row-json.js";
3
+ import type { auditLog } from "../generated-tables/audit-log.tables.js";
4
+ /** Full audit history or rows matching every configured application scope dimension. */
5
+ export type AuditLogScope = {
6
+ readonly kind: "application";
7
+ readonly values: AuditScopeValues;
8
+ } | {
9
+ readonly kind: "platform";
10
+ };
11
+ /** Narrowing within a scope. Every field is optional and they combine with AND. */
12
+ export type AuditLogRowFilter = {
13
+ /** Inclusive lower bound on `recorded_at`. */
14
+ readonly from?: Date;
15
+ /** Exclusive upper bound on `recorded_at`. */
16
+ readonly to?: Date;
17
+ /** The human behind the write (`user_id`). */
18
+ readonly userId?: string;
19
+ /** Stored `actor_kind` values; empty or absent means any. */
20
+ readonly actorKinds?: readonly string[];
21
+ /** Only these tables; empty or absent means any. */
22
+ readonly tableNames?: readonly string[];
23
+ /** Never these tables. */
24
+ readonly excludeTableNames?: readonly string[];
25
+ /**
26
+ * Leave out one request's rows: a request too large to fit one page is
27
+ * shown once, and the pages after it skip the rest of it.
28
+ */
29
+ readonly excludeRequestId?: string;
30
+ /**
31
+ * Leave out an update that changed nothing a reader is shown: for each
32
+ * table named, the columns an update must change at least one of to be
33
+ * listed. An update that touched only the others (`updated_at`, a sync
34
+ * marker) is left out; inserts, deletes and updates to tables not named
35
+ * are always kept.
36
+ */
37
+ readonly shownUpdateColumns?: Readonly<Record<string, readonly string[]>>;
38
+ /** The history of one record. */
39
+ readonly record?: {
40
+ readonly tableName: string;
41
+ readonly rowKey: Readonly<Record<string, unknown>>;
42
+ };
43
+ };
44
+ /**
45
+ * Where the previous page stopped: rows strictly older than this are next.
46
+ *
47
+ * `recordedAt` is the stored instant to the microsecond, as UTC text
48
+ * (`2026-10-22T14:03:11.123456Z`). A `Date` keeps milliseconds only, and rows
49
+ * of different transactions can share a millisecond, so a millisecond cursor
50
+ * would skip or repeat them; the text round-trips the stored value exactly.
51
+ */
52
+ export type AuditLogCursor = {
53
+ readonly recordedAt: string;
54
+ readonly position: AuditLogPosition;
55
+ };
56
+ /** A stored audit entry and the cursor required to resume its page. */
57
+ export type AuditLogRecord = Readonly<Pick<typeof auditLog.$inferSelect, "scopeValues" | "position" | "recordedAt" | "actorKind" | "actorId" | "clientId" | "clientName" | "requestId" | "reason" | "userId" | "tableName" | "rowKey" | "changedColumns" | "rowBefore" | "rowAfter">> & {
58
+ readonly operation: "insert" | "update" | "delete";
59
+ readonly cursor: AuditLogCursor;
60
+ };
61
+ /**
62
+ * No behavior lives in this file — it is not a wrapper or a pass-through.
63
+ * This abstract class is the port for reading `audit_log`: the type
64
+ * a caller holds AND the injection token composition binds.
65
+ * `DrizzleAuditLogReader` (database/readers/) implements it; a fake stands in
66
+ * for unit tests that never reach a database.
67
+ */
68
+ export declare abstract class AuditLogReader {
69
+ /**
70
+ * Rows in the scope, newest first by `(recorded_at, position)`, strictly
71
+ * older than `olderThan` when given, at most `limit` of them. Every scope
72
+ * and filter combination is served by one index seek; nothing counts.
73
+ */
74
+ abstract listRows(scope: AuditLogScope, filter: AuditLogRowFilter, olderThan: AuditLogCursor | null, limit: number): Promise<readonly AuditLogRecord[]>;
75
+ /** Every row one request wrote, oldest first. */
76
+ abstract rowsForRequest(requestId: string): Promise<readonly AuditLogRecord[]>;
77
+ /** Count distinct records changed by one request in the named scope. */
78
+ abstract countRecordsForRequest(scope: AuditLogScope, requestId: string, filter: Pick<AuditLogRowFilter, "shownUpdateColumns">): Promise<number>;
79
+ /** One row by its position, for a write that carried no request id. */
80
+ abstract rowAt(position: AuditLogPosition): Promise<AuditLogRecord | null>;
81
+ }
82
+ //# sourceMappingURL=audit-log-reader.interface.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"audit-log-reader.interface.d.ts","sourceRoot":"","sources":["../../../../src/lib/database/readers/audit-log-reader.interface.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AACrE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAC7D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,yCAAyC,CAAC;AAExE,wFAAwF;AACxF,MAAM,MAAM,aAAa,GACrB;IAAE,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,gBAAgB,CAAA;CAAE,GACnE;IAAE,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAA;CAAE,CAAC;AAElC,mFAAmF;AACnF,MAAM,MAAM,iBAAiB,GAAG;IAC9B,8CAA8C;IAC9C,QAAQ,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC;IACrB,8CAA8C;IAC9C,QAAQ,CAAC,EAAE,CAAC,EAAE,IAAI,CAAC;IACnB,8CAA8C;IAC9C,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,6DAA6D;IAC7D,QAAQ,CAAC,UAAU,C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QAAQ,CACf,KAAK,EAAE,aAAa,EACpB,MAAM,EAAE,iBAAiB,EACzB,SAAS,EAAE,cAAc,GAAG,IAAI,EAChC,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC,SAAS,cAAc,EAAE,CAAC,CAAC;IAEtC,iDAAiD;IACjD,QAAQ,CAAC,cAAc,CACrB,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,SAAS,cAAc,EAAE,CAAC,CAAC;IAEtC,wEAAwE;IACxE,QAAQ,CAAC,sBAAsB,CAC7B,KAAK,EAAE,aAAa,EACpB,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,IAAI,CAAC,iBAAiB,EAAE,oBAAoB,CAAC,GACpD,OAAO,CAAC,MAAM,CAAC,CAAC;IAEnB,uEAAuE;IACvE,QAAQ,CAAC,KAAK,CAAC,QAAQ,EAAE,gBAAgB,GAAG,OAAO,CAAC,cAAc,GAAG,IAAI,CAAC,CAAC;CAC5E"}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * No behavior lives in this file — it is not a wrapper or a pass-through.
3
+ * This abstract class is the port for reading `audit_log`: the type
4
+ * a caller holds AND the injection token composition binds.
5
+ * `DrizzleAuditLogReader` (database/readers/) implements it; a fake stands in
6
+ * for unit tests that never reach a database.
7
+ */
8
+ export class AuditLogReader {
9
+ }
10
+ //# sourceMappingURL=audit-log-reader.interface.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"audit-log-reader.interface.js","sourceRoot":"","sources":["../../../../src/lib/database/readers/audit-log-reader.interface.ts"],"names":[],"mappings":"AAiFA;;;;;;GAMG;AACH,MAAM,OAAgB,cAAc;CA2BnC"}
@@ -0,0 +1,16 @@
1
+ import type { DrizzleHandle } from "@dads-framework/postgres";
2
+ import type { AuditLogPosition } from "../audit-log-position.ids.js";
3
+ import type { AuditLogReader } from "./audit-log-reader.interface.js";
4
+ import { type AuditLogCursor, type AuditLogRecord, type AuditLogRowFilter, type AuditLogScope } from "./audit-log-reader.interface.js";
5
+ /** Keyset reads on the audit trail with exact stored cursor timestamps. */
6
+ export declare class DrizzleAuditLogReader implements AuditLogReader {
7
+ private readonly db;
8
+ constructor(db: DrizzleHandle);
9
+ listRows(scope: AuditLogScope, filter: AuditLogRowFilter, olderThan: AuditLogCursor | null, limit: number): Promise<readonly AuditLogRecord[]>;
10
+ /** Match scope and table through the combined GIN index before paging. */
11
+ private newestPerShownTable;
12
+ rowsForRequest(requestId: string): Promise<readonly AuditLogRecord[]>;
13
+ countRecordsForRequest(scope: AuditLogScope, requestId: string, filter: Pick<AuditLogRowFilter, "shownUpdateColumns">): Promise<number>;
14
+ rowAt(position: AuditLogPosition): Promise<AuditLogRecord | null>;
15
+ }
16
+ //# sourceMappingURL=drizzle-audit-log-reader.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"drizzle-audit-log-reader.d.ts","sourceRoot":"","sources":["../../../../src/lib/database/readers/drizzle-audit-log-reader.ts"],"names":[],"mappings":"AAeA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAE9D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAErE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iCAAiC,CAAC;AACtE,OAAO,EACL,KAAK,cAAc,EACnB,KAAK,cAAc,EACnB,KAAK,iBAAiB,EACtB,KAAK,aAAa,EACnB,MAAM,iCAAiC,CAAC;AAmDzC,2EAA2E;AAC3E,qBAAa,qBAAsB,YAAW,cAAc;IAC9C,OAAO,CAAC,QAAQ,CAAC,EAAE;IAA/B,YAA6B,EAAE,EAAE,aAAa,EAAI;IAE5C,QAAQ,CACZ,KAAK,EAAE,aAAa,EACpB,MAAM,EAAE,iBAAiB,EACzB,SAAS,EAAE,cAAc,GAAG,IAAI,EAChC,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC,SAAS,cAAc,EAAE,CAAC,CA0BpC;IAED,0EAA0E;IAC1E,OAAO,CAAC,mBAAmB;IAwCrB,cAAc,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,cAAc,EAAE,CAAC,CAO1E;IAEK,sBAAsB,CAC1B,KAAK,EAAE,aAAa,EACpB,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,IAAI,CAAC,iBAAiB,EAAE,oBAAoB,CAAC,GACpD,OAAO,CAAC,MAAM,CAAC,CAcjB;IAEK,KAAK,CAAC,QAAQ,EAAE,gBAAgB,GAAG,OAAO,CAAC,cAAc,GAAG,IAAI,CAAC,CAOtE;CACF"}