create-safest-tools 0.6.0 → 0.7.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 (172) hide show
  1. package/README.md +3 -3
  2. package/package.json +2 -2
  3. package/src/config.mjs +2 -2
  4. package/src/update.mjs +10 -1
  5. package/template/README.md +2 -2
  6. package/template/console/README.md +2 -2
  7. package/template/console/analytics/AnalyticsWorkspace.tsx +49 -36
  8. package/template/console/analytics/controller.ts +10 -2
  9. package/template/console/analytics/types.ts +7 -0
  10. package/template/console/appeals/AppealFollowup.tsx +171 -0
  11. package/template/console/appeals/AppealWorkspace.tsx +33 -4
  12. package/template/console/appeals/RecordAppealDialog.tsx +42 -0
  13. package/template/console/appeals/appeal-corrections.css +29 -0
  14. package/template/console/appeals/controller.ts +40 -14
  15. package/template/console/appeals/events.ts +1 -0
  16. package/template/console/appeals/types.ts +6 -0
  17. package/template/console/appeals/useAppealDraftGuard.ts +12 -0
  18. package/template/console/assistant/AssistantWorkspace.tsx +28 -5
  19. package/template/console/command/CommandCentre.tsx +101 -64
  20. package/template/console/command/controller.ts +80 -70
  21. package/template/console/command/events.ts +1 -2
  22. package/template/console/command/presentation.ts +29 -0
  23. package/template/console/command/store.ts +4 -4
  24. package/template/console/command/types.ts +26 -21
  25. package/template/console/configuration/ConfigurationBuilders.tsx +101 -0
  26. package/template/console/configuration/ConfigurationDialog.tsx +28 -13
  27. package/template/console/configuration/ConfigurationWorkspace.tsx +21 -8
  28. package/template/console/configuration/types.ts +1 -1
  29. package/template/console/lib/priority.ts +13 -0
  30. package/template/console/operations/CaseEscalations.tsx +142 -0
  31. package/template/console/operations/OperationsWorkspace.tsx +2 -0
  32. package/template/console/operations/case-escalations.css +20 -0
  33. package/template/console/people/PeopleWorkspace.tsx +8 -4
  34. package/template/console/people/PersonAccessDialog.tsx +40 -0
  35. package/template/console/people/types.ts +1 -0
  36. package/template/console/profile/ProfileWorkspace.tsx +1 -1
  37. package/template/console/registry/IntegrationSetup.tsx +41 -0
  38. package/template/console/registry/IntegrationVerification.tsx +54 -0
  39. package/template/console/registry/RegistryDetailsDialog.tsx +2 -2
  40. package/template/console/registry/RegistryDialog.tsx +56 -27
  41. package/template/console/registry/RegistryWorkspace.tsx +10 -5
  42. package/template/console/registry/integration-verification.css +25 -0
  43. package/template/console/reports/CaseActions.tsx +81 -0
  44. package/template/console/reports/CaseContinuity.tsx +27 -0
  45. package/template/console/reports/CaseNotices.tsx +48 -0
  46. package/template/console/reports/CaseOwnership.tsx +78 -0
  47. package/template/console/reports/CaseReviewTasks.tsx +148 -0
  48. package/template/console/reports/EnrichmentFindings.tsx +148 -0
  49. package/template/console/reports/InvestigationBrief.tsx +29 -0
  50. package/template/console/reports/ReportDrawer.tsx +122 -67
  51. package/template/console/reports/ReportWorkspace.tsx +112 -23
  52. package/template/console/reports/controller.ts +125 -60
  53. package/template/console/reports/events.ts +9 -2
  54. package/template/console/reports/load-inbox.ts +17 -0
  55. package/template/console/reports/presence-presentation.ts +52 -0
  56. package/template/console/reports/private-drafts.ts +32 -0
  57. package/template/console/reports/store.ts +2 -2
  58. package/template/console/reports/types.ts +50 -2
  59. package/template/console/reports/use-editing-access.ts +13 -0
  60. package/template/console/settings/SettingsWorkspace.tsx +1 -1
  61. package/template/console/shell/WorkspaceShell.tsx +55 -29
  62. package/template/console/shell/WorkspaceUpdateNotice.tsx +48 -0
  63. package/template/console/shell/controller.ts +23 -16
  64. package/template/console/shell/navigation.ts +30 -39
  65. package/template/console/workflows/CreatePlaybookDialog.tsx +73 -0
  66. package/template/console/workflows/PlaybookExamples.tsx +160 -0
  67. package/template/console/workflows/PlaybookFields.tsx +109 -0
  68. package/template/console/workflows/PlaybookGuideEditor.tsx +125 -0
  69. package/template/console/workflows/PlaybookHistory.tsx +106 -0
  70. package/template/console/workflows/WorkflowDialogs.tsx +2 -1
  71. package/template/console/workflows/WorkflowStudio.tsx +108 -39
  72. package/template/console/workflows/WorkflowWorkspace.tsx +9 -7
  73. package/template/console/workflows/templates.ts +3 -3
  74. package/template/console/workflows/types.ts +11 -0
  75. package/template/migrations/0001_safest_resolve_baseline.sql +0 -1
  76. package/template/migrations/0002_playbook_publication_evidence.sql +36 -0
  77. package/template/migrations/0003_action_confirmation.sql +24 -0
  78. package/template/migrations/0004_notice_recovery.sql +22 -0
  79. package/template/migrations/0005_appeal_continuity.sql +17 -0
  80. package/template/migrations/0006_case_ownership.sql +23 -0
  81. package/template/migrations/0007_integration_verification.sql +21 -0
  82. package/template/migrations/0008_case_escalations.sql +135 -0
  83. package/template/migrations/0009_participant_continuation.sql +61 -0
  84. package/template/migrations/0010_appeal_corrections.sql +41 -0
  85. package/template/npm-shrinkwrap.json +8036 -0
  86. package/template/package.json +31 -22
  87. package/template/public/_headers +16 -1
  88. package/template/public/console/auth-shell.js +22151 -14847
  89. package/template/public/console/release.json +1 -0
  90. package/template/public/console/workspace-features.css +2 -0
  91. package/template/public/embed/embed.css +3 -0
  92. package/template/public/embed/embed.js +19 -1
  93. package/template/public/embed/index.html +2 -0
  94. package/template/public/index.html +9 -6
  95. package/template/public/participant.css +2 -0
  96. package/template/public/participant.html +13 -0
  97. package/template/public/participant.js +111 -0
  98. package/template/public/public-report.js +21 -1
  99. package/template/public/report/index.html +1 -1
  100. package/template/public/styles.css +599 -83
  101. package/template/public/workspace.css +124 -0
  102. package/template/scripts/reports-plan.mjs +1 -1
  103. package/template/shared/case-escalations.ts +31 -0
  104. package/template/shared/integration-progress.ts +27 -0
  105. package/template/shared/notice-inputs.ts +3 -0
  106. package/template/shared/playbook-resources.ts +14 -0
  107. package/template/shared/playbooks.ts +249 -0
  108. package/template/src/agent-executor.ts +25 -8
  109. package/template/src/ai-observability.ts +14 -13
  110. package/template/src/ai-registry-validation.ts +1 -1
  111. package/template/src/appeal-continuity.ts +75 -0
  112. package/template/src/appeal-correction-state.ts +46 -0
  113. package/template/src/appeal-corrections.ts +112 -0
  114. package/template/src/appeal-decisions.ts +85 -0
  115. package/template/src/assistant.ts +3 -4
  116. package/template/src/case-escalations.ts +223 -0
  117. package/template/src/case-investigator.ts +179 -0
  118. package/template/src/component-executor.ts +5 -4
  119. package/template/src/configuration-registry.ts +4 -1
  120. package/template/src/connection-oauth.ts +12 -3
  121. package/template/src/database-readiness.ts +14 -0
  122. package/template/src/human-tasks.ts +152 -55
  123. package/template/src/index.ts +191 -29
  124. package/template/src/installation-admin.ts +2 -1
  125. package/template/src/integration-progress.ts +45 -0
  126. package/template/src/integration-verification.ts +249 -0
  127. package/template/src/investigation-evidence.ts +95 -0
  128. package/template/src/investigator-fixture.ts +30 -0
  129. package/template/src/notice-delivery.ts +19 -0
  130. package/template/src/notice-recovery.ts +81 -0
  131. package/template/src/operator-scope.ts +31 -0
  132. package/template/src/participant-access.ts +163 -0
  133. package/template/src/participant-case.ts +87 -0
  134. package/template/src/participant-customer-access.ts +93 -0
  135. package/template/src/participant-recovery.ts +43 -0
  136. package/template/src/participant-routes.ts +67 -0
  137. package/template/src/platform-registry-validation.ts +25 -2
  138. package/template/src/platform-registry.ts +10 -7
  139. package/template/src/playbook-resources.ts +58 -0
  140. package/template/src/report-admin.ts +9 -2
  141. package/template/src/report-auth.ts +1 -28
  142. package/template/src/report-better-auth.ts +18 -6
  143. package/template/src/report-delivery.ts +149 -148
  144. package/template/src/report-email-inbound.ts +33 -22
  145. package/template/src/report-email-template.ts +5 -2
  146. package/template/src/report-email.ts +7 -30
  147. package/template/src/report-governance.ts +42 -45
  148. package/template/src/report-http.ts +37 -2
  149. package/template/src/report-operations.ts +30 -67
  150. package/template/src/report-operator-accounts.ts +7 -6
  151. package/template/src/report-ownership.ts +64 -0
  152. package/template/src/report-presence.ts +15 -9
  153. package/template/src/report-repository.ts +16 -7
  154. package/template/src/report-turnstile.ts +7 -3
  155. package/template/src/report-types.ts +1 -0
  156. package/template/src/report-workflow.ts +183 -249
  157. package/template/src/workflow-actions.ts +248 -156
  158. package/template/src/workflow-compiler.ts +63 -12
  159. package/template/src/workflow-dynamic-runtime.ts +20 -9
  160. package/template/src/workflow-effects.ts +117 -39
  161. package/template/src/workflow-event-transport.ts +16 -0
  162. package/template/src/workflow-expressions.ts +30 -0
  163. package/template/src/workflow-loader-code.ts +21 -0
  164. package/template/src/workflow-platform-types.ts +3 -0
  165. package/template/src/workflow-platform-validation.ts +23 -1
  166. package/template/src/workflow-repository.ts +131 -28
  167. package/template/src/workflow-runs.ts +61 -13
  168. package/template/src/workflow-simulator.ts +7 -4
  169. package/template/src/workspace-governance.ts +6 -4
  170. package/template/tsconfig.json +1 -0
  171. package/template/vite.console.config.ts +30 -2
  172. package/template/worker-configuration.d.ts +3 -2
@@ -0,0 +1,135 @@
1
+ CREATE TABLE case_escalation_policies (
2
+ scope_key TEXT PRIMARY KEY,
3
+ queue_id TEXT UNIQUE REFERENCES queue_definitions(id),
4
+ primary_operator_id TEXT REFERENCES operator_users(id),
5
+ backup_operator_id TEXT REFERENCES operator_users(id),
6
+ acknowledgement_minutes INTEGER NOT NULL DEFAULT 60 CHECK (acknowledgement_minutes BETWEEN 5 AND 10080),
7
+ follow_up_minutes INTEGER NOT NULL DEFAULT 240 CHECK (follow_up_minutes BETWEEN 5 AND 10080),
8
+ revision INTEGER NOT NULL DEFAULT 1,
9
+ updated_by TEXT NOT NULL,
10
+ updated_at TEXT NOT NULL
11
+ );
12
+ INSERT INTO case_escalation_policies (scope_key,updated_by,updated_at)
13
+ VALUES ('default','system',strftime('%Y-%m-%dT%H:%M:%fZ','now'));
14
+
15
+ -- Matches the application's effective-role and queue-scope union. A base owner
16
+ -- is global; otherwise active bindings replace base roles, as at sign-in.
17
+ CREATE VIEW case_escalation_operators AS
18
+ SELECT u.id, u.display_name, u.status,
19
+ CASE WHEN EXISTS (SELECT 1 FROM json_each(u.roles_json) WHERE value = 'owner')
20
+ OR NOT EXISTS (SELECT 1 FROM role_bindings b WHERE b.principal_type = 'operator_user' AND b.principal_id = u.id AND b.installation_id = 'default' AND b.revoked_at IS NULL)
21
+ THEN u.roles_json ELSE (SELECT json_group_array(b.role) FROM role_bindings b WHERE b.principal_type = 'operator_user' AND b.principal_id = u.id AND b.installation_id = 'default' AND b.revoked_at IS NULL) END AS roles_json,
22
+ CASE WHEN EXISTS (SELECT 1 FROM json_each(u.roles_json) WHERE value = 'owner')
23
+ OR NOT EXISTS (SELECT 1 FROM role_bindings b WHERE b.principal_type = 'operator_user' AND b.principal_id = u.id AND b.installation_id = 'default' AND b.revoked_at IS NULL)
24
+ OR EXISTS (SELECT 1 FROM role_bindings b WHERE b.principal_type = 'operator_user' AND b.principal_id = u.id AND b.installation_id = 'default' AND b.revoked_at IS NULL AND json_array_length(b.queue_scope_json) = 0)
25
+ THEN 'null' ELSE (SELECT json_group_array(DISTINCT j.value) FROM role_bindings b,json_each(b.queue_scope_json) j WHERE b.principal_type = 'operator_user' AND b.principal_id = u.id AND b.installation_id = 'default' AND b.revoked_at IS NULL) END AS queue_scope_json,
26
+ CASE WHEN EXISTS (SELECT 1 FROM json_each(u.roles_json) WHERE value = 'owner')
27
+ OR NOT EXISTS (SELECT 1 FROM role_bindings b WHERE b.principal_type = 'operator_user' AND b.principal_id = u.id AND b.installation_id = 'default' AND b.revoked_at IS NULL)
28
+ OR EXISTS (SELECT 1 FROM role_bindings b WHERE b.principal_type = 'operator_user' AND b.principal_id = u.id AND b.installation_id = 'default' AND b.revoked_at IS NULL AND b.field_scope_json = '{}')
29
+ THEN 1 ELSE CASE WHEN (SELECT COUNT(DISTINCT f.value) FROM role_bindings b,json_each(b.field_scope_json,'$.report') f WHERE b.principal_type = 'operator_user' AND b.principal_id = u.id AND b.installation_id = 'default' AND b.revoked_at IS NULL AND f.value IN ('id','publicReference','state','queueId')) = 4 THEN 1 ELSE 0 END END AS recovery_fields_allowed
30
+ FROM operator_users u;
31
+
32
+ CREATE TABLE case_escalations (
33
+ id TEXT PRIMARY KEY,
34
+ source_key TEXT NOT NULL UNIQUE,
35
+ source_fingerprint TEXT NOT NULL,
36
+ reason TEXT NOT NULL,
37
+ report_id TEXT REFERENCES reports(id) ON DELETE CASCADE,
38
+ queue_id TEXT REFERENCES queue_definitions(id),
39
+ report_queue_id TEXT REFERENCES queue_definitions(id),
40
+ source_id TEXT NOT NULL,
41
+ primary_operator_id TEXT,
42
+ backup_operator_id TEXT,
43
+ contact_warning TEXT,
44
+ state TEXT NOT NULL CHECK (state IN ('open','acknowledged','resolved')),
45
+ level INTEGER NOT NULL DEFAULT 0 CHECK (level IN (0,1)),
46
+ source_due_at TEXT NOT NULL,
47
+ response_due_at TEXT NOT NULL,
48
+ acknowledged_by TEXT,
49
+ acknowledged_at TEXT,
50
+ acknowledged_revision INTEGER,
51
+ created_at TEXT NOT NULL,
52
+ updated_at TEXT NOT NULL,
53
+ resolved_at TEXT,
54
+ revision INTEGER NOT NULL DEFAULT 1,
55
+ mutation_id TEXT
56
+ );
57
+ CREATE INDEX case_escalations_attention ON case_escalations(state,response_due_at,id);
58
+ CREATE TABLE case_escalation_scan_state (id TEXT PRIMARY KEY, cursor TEXT NOT NULL DEFAULT '');
59
+ INSERT INTO case_escalation_scan_state (id) VALUES ('default');
60
+
61
+ -- Source predicates remain authoritative. No escalation has a manual "resolved"
62
+ -- switch: fixing the case/task/delivery clears its predicate on reconciliation.
63
+ -- Later capabilities extend this projection without rewriting the base sources.
64
+ CREATE VIEW case_escalation_additional_sources AS
65
+ SELECT '' AS source_key,'' AS reason,NULL AS report_id,NULL AS queue_id,NULL AS report_queue_id,
66
+ '' AS source_id,'' AS source_fingerprint,NULL AS owner_id,'' AS due_at WHERE 0;
67
+ CREATE VIEW case_escalation_case_sources AS
68
+ SELECT 'unowned:' || r.id AS source_key, 'unowned' AS reason, r.id AS report_id,
69
+ r.queue_id, r.queue_id AS report_queue_id, r.id AS source_id,
70
+ 'unowned:' || COALESCE(r.queue_id,'') AS source_fingerprint, NULL AS owner_id,
71
+ strftime('%Y-%m-%dT%H:%M:%fZ',r.received_at,'+' || COALESCE(q.response_sla_minutes,1440) || ' minutes') AS due_at
72
+ FROM reports r LEFT JOIN review_claims c ON c.report_id = r.id
73
+ LEFT JOIN queue_versions q ON q.id = r.queue_version_id
74
+ WHERE r.state IN ('human_review','human_review_requested') AND c.report_id IS NULL
75
+ UNION ALL
76
+ SELECT 'owner_unavailable:' || r.id,'owner_unavailable',r.id,r.queue_id,r.queue_id,r.id,
77
+ c.reviewer_id || ':' || c.version,c.reviewer_id,c.claimed_at
78
+ FROM reports r JOIN review_claims c ON c.report_id = r.id
79
+ WHERE r.state IN ('human_review','human_review_requested','awaiting_reporter')
80
+ AND NOT EXISTS (SELECT 1 FROM case_escalation_operators u WHERE u.id = c.reviewer_id AND u.status = 'active'
81
+ AND EXISTS (SELECT 1 FROM json_each(u.roles_json) WHERE value IN ('owner','administrator','reviewer'))
82
+ AND (u.queue_scope_json = 'null' OR r.queue_id IN (SELECT value FROM json_each(u.queue_scope_json))))
83
+ UNION ALL
84
+ SELECT 'case_overdue:' || r.id,'case_overdue',r.id,r.queue_id,r.queue_id,r.id,
85
+ COALESCE(c.reviewer_id,'') || ':' || COALESCE(c.version,0),c.reviewer_id,
86
+ COALESCE(r.due_at,strftime('%Y-%m-%dT%H:%M:%fZ',r.received_at,'+' || COALESCE(q.resolution_sla_minutes,10080) || ' minutes'))
87
+ FROM reports r LEFT JOIN review_claims c ON c.report_id = r.id LEFT JOIN queue_versions q ON q.id = r.queue_version_id
88
+ WHERE r.state IN ('human_review','human_review_requested','awaiting_reporter','received','ai_review')
89
+ UNION ALL
90
+ SELECT 'task_overdue:' || t.id,'task_overdue',t.report_id,t.queue_id,r.queue_id,t.id,
91
+ COALESCE(t.assigned_operator_id,'') || ':' || t.state,t.assigned_operator_id,
92
+ COALESCE(t.escalation_at,t.due_at,strftime('%Y-%m-%dT%H:%M:%fZ',t.created_at,'+1440 minutes'))
93
+ FROM human_tasks t LEFT JOIN reports r ON r.id = t.report_id WHERE t.state IN ('open','claimed')
94
+ ;
95
+ CREATE VIEW case_escalation_delivery_sources (source_key,reason,report_id,queue_id,report_queue_id,source_id,source_fingerprint,owner_id,due_at) AS
96
+ SELECT 'action_failed:' || o.id,'action_failed',o.report_id,r.queue_id,r.queue_id,o.id,
97
+ o.status || ':' || o.attempt_count,NULL,o.updated_at
98
+ FROM decision_action_outbox o JOIN reports r ON r.id = o.report_id WHERE o.terminal = 1 AND o.status IN ('failed','rejected')
99
+ UNION ALL
100
+ SELECT 'proposal_failed:' || p.id,'proposal_failed',p.report_id,r.queue_id,r.queue_id,p.id,
101
+ p.state || ':' || p.delivery_cycle,NULL,p.updated_at
102
+ FROM action_proposals p JOIN reports r ON r.id = p.report_id WHERE p.appeal_id IS NULL AND p.delivery_terminal = 1 AND p.state IN ('failed','compensation_required')
103
+ UNION ALL
104
+ SELECT 'notice_failed:' || o.id,'notice_failed',n.report_id,r.queue_id,r.queue_id,o.id,
105
+ o.status || ':' || o.attempt_count,NULL,o.updated_at
106
+ FROM notification_outbox o JOIN notices n ON n.id = o.notice_id JOIN reports r ON r.id = n.report_id WHERE o.terminal = 1 AND o.status = 'failed'
107
+ UNION ALL
108
+ SELECT 'email_failed:' || e.id,'email_failed',m.report_id,r.queue_id,r.queue_id,e.id,
109
+ e.state || ':' || e.attempt_count,NULL,e.updated_at
110
+ FROM email_deliveries e JOIN case_messages m ON m.id = e.message_id JOIN reports r ON r.id = m.report_id
111
+ WHERE e.audience != 'operator' AND (e.state = 'bounced' OR (e.state = 'failed' AND e.next_attempt_at IS NULL))
112
+ ;
113
+ CREATE VIEW case_escalation_appeal_sources (source_key,reason,report_id,queue_id,report_queue_id,source_id,source_fingerprint,owner_id,due_at) AS
114
+ SELECT 'appeal_staffing:' || a.id,'appeal_staffing',a.report_id,a.queue_id,r.queue_id,a.id,
115
+ a.decision_id,NULL,a.submitted_at
116
+ FROM appeals a JOIN decisions d ON d.id = a.decision_id JOIN reports r ON r.id = a.report_id
117
+ WHERE a.state IN ('submitted','human_review') AND NOT EXISTS (
118
+ SELECT 1 FROM case_escalation_operators u WHERE u.status = 'active' AND u.id != d.maker_id
119
+ AND EXISTS (SELECT 1 FROM json_each(u.roles_json) WHERE value IN ('owner','administrator','appeal_reviewer'))
120
+ AND (u.queue_scope_json = 'null' OR (a.queue_id IN (SELECT value FROM json_each(u.queue_scope_json)) AND r.queue_id IN (SELECT value FROM json_each(u.queue_scope_json)))))
121
+ UNION ALL
122
+ SELECT 'appeal_overdue:' || a.id,'appeal_overdue',a.report_id,a.queue_id,r.queue_id,a.id,
123
+ COALESCE(c.reviewer_id,'') || ':' || COALESCE(c.version,0),c.reviewer_id,
124
+ strftime('%Y-%m-%dT%H:%M:%fZ',a.submitted_at,'+' || COALESCE(q.resolution_sla_minutes,10080) || ' minutes')
125
+ FROM appeals a JOIN reports r ON r.id = a.report_id LEFT JOIN appeal_claims c ON c.appeal_id = a.id
126
+ LEFT JOIN queue_definitions qd ON qd.id = a.queue_id LEFT JOIN queue_versions q ON q.id = qd.active_version_id
127
+ WHERE a.state IN ('submitted','human_review')
128
+ ;
129
+ -- D1 bounds the number of compound SELECT terms. Keep each source group and
130
+ -- the outer union small. LIMIT -1 prevents flattening groups into one compound.
131
+ CREATE VIEW case_escalation_sources AS
132
+ SELECT * FROM (SELECT * FROM case_escalation_case_sources LIMIT -1)
133
+ UNION ALL SELECT * FROM (SELECT * FROM case_escalation_delivery_sources LIMIT -1)
134
+ UNION ALL SELECT * FROM (SELECT * FROM case_escalation_appeal_sources LIMIT -1)
135
+ UNION ALL SELECT * FROM (SELECT * FROM case_escalation_additional_sources LIMIT -1);
@@ -0,0 +1,61 @@
1
+ -- Bearer secrets are never stored: links are deterministic HMACs over a random
2
+ -- grant ID, while sessions and CSRF proofs are stored only as SHA-256 digests.
3
+ CREATE TABLE participant_access_grants (
4
+ id TEXT PRIMARY KEY,
5
+ participant_id TEXT NOT NULL REFERENCES report_participants(id) ON DELETE CASCADE,
6
+ delivery_key TEXT NOT NULL UNIQUE,
7
+ identity_fingerprint TEXT NOT NULL,
8
+ token_hash TEXT NOT NULL UNIQUE,
9
+ created_at TEXT NOT NULL,
10
+ expires_at TEXT NOT NULL,
11
+ consumed_at TEXT,
12
+ revoked_at TEXT
13
+ );
14
+ CREATE TABLE participant_sessions (
15
+ token_hash TEXT PRIMARY KEY,
16
+ grant_id TEXT NOT NULL REFERENCES participant_access_grants(id) ON DELETE CASCADE,
17
+ csrf_hash TEXT NOT NULL,
18
+ created_at TEXT NOT NULL,
19
+ expires_at TEXT NOT NULL,
20
+ revoked_at TEXT
21
+ );
22
+ CREATE INDEX participant_access_participant ON participant_access_grants(participant_id);
23
+ CREATE TABLE participant_recovery_limits (
24
+ key_hash TEXT PRIMARY KEY,
25
+ next_allowed_at TEXT NOT NULL
26
+ );
27
+ CREATE TABLE participant_mutation_guards (
28
+ id TEXT PRIMARY KEY,
29
+ authorized INTEGER NOT NULL CONSTRAINT valid_participant_session CHECK (authorized = 1)
30
+ );
31
+ -- Limits count committed participant effects, so retries never spend capacity.
32
+ CREATE TABLE participant_rate_guards (
33
+ id TEXT PRIMARY KEY,
34
+ message_allowed INTEGER NOT NULL DEFAULT 1 CONSTRAINT participant_message_rate CHECK (message_allowed = 1),
35
+ appeal_allowed INTEGER NOT NULL DEFAULT 1 CONSTRAINT participant_appeal_rate CHECK (appeal_allowed = 1)
36
+ );
37
+ CREATE INDEX participant_message_history ON case_messages(report_id,audience,created_at,id);
38
+ CREATE INDEX participant_message_rate_window ON case_messages(report_id,audience,sender_id,created_at) WHERE direction='inbound';
39
+ CREATE INDEX participant_appeal_rate_window ON appeals(report_id,appellant_audience,submitted_at);
40
+ CREATE INDEX participant_sessions_expiry ON participant_sessions(expires_at);
41
+ CREATE INDEX participant_recovery_expiry ON participant_recovery_limits(next_allowed_at);
42
+
43
+ -- Restore missing signed-widget owners only where trusted retained identity is
44
+ -- unambiguous. Never recreate a redacted/removed identity or alter held records.
45
+ -- This repairs identity availability only: no notices or actions are sent.
46
+ INSERT OR IGNORE INTO report_participants(id,report_id,audience,reference,contact_mode,created_at)
47
+ SELECT 'migration0009-affected:' || r.id,r.id,'affected_user',t.owner_reference,'customer_notification',strftime('%Y-%m-%dT%H:%M:%fZ','now')
48
+ FROM reports r JOIN report_targets t ON t.report_id=r.id
49
+ WHERE r.source='signed_widget' AND t.sensitive_redacted_at IS NULL AND t.owner_reference IS NOT NULL AND trim(t.owner_reference)<>''
50
+ AND (SELECT COUNT(*) FROM report_targets all_targets WHERE all_targets.report_id=r.id)=1
51
+ AND EXISTS(SELECT 1 FROM report_participants reporter WHERE reporter.report_id=r.id AND reporter.audience='reporter'
52
+ AND reporter.reference_redacted_at IS NULL AND reporter.reference IS NOT NULL AND trim(reporter.reference)<>'')
53
+ AND NOT EXISTS(SELECT 1 FROM report_participants p WHERE p.report_id=r.id AND (p.audience='affected_user' OR p.reference_redacted_at IS NOT NULL))
54
+ AND NOT EXISTS(SELECT 1 FROM deletion_requests d WHERE d.report_id=r.id)
55
+ AND NOT EXISTS(SELECT 1 FROM legal_holds h WHERE h.report_id=r.id AND h.state='active');
56
+
57
+ INSERT OR IGNORE INTO audit_events(id,idempotency_key,action,actor_type,actor_id,target_type,target_id,details_json,created_at)
58
+ SELECT 'migration0009-affected-audit:' || p.report_id,'migration0009:affected-participant:' || p.report_id,
59
+ 'report.affected_participant_restored','system','migration:0009','report',p.report_id,
60
+ json_object('participantId',p.id,'audience','affected_user','source','retained_signed_widget_target'),p.created_at
61
+ FROM report_participants p WHERE p.id='migration0009-affected:' || p.report_id AND p.audience='affected_user';
@@ -0,0 +1,41 @@
1
+ -- One explicit corrective operation per original action and appeal verdict.
2
+ CREATE TABLE appeal_corrections (
3
+ id TEXT PRIMARY KEY,
4
+ appeal_decision_id TEXT NOT NULL REFERENCES appeal_decisions(id),
5
+ appeal_id TEXT NOT NULL REFERENCES appeals(id),
6
+ report_id TEXT NOT NULL REFERENCES reports(id),
7
+ integration_id TEXT NOT NULL,
8
+ source_key TEXT NOT NULL,
9
+ original_proposal_id TEXT REFERENCES action_proposals(id),
10
+ original_outbox_id TEXT REFERENCES decision_action_outbox(id),
11
+ original_operation_id TEXT NOT NULL,
12
+ original_target_json TEXT NOT NULL,
13
+ mode TEXT NOT NULL CHECK(mode IN ('manual','automated')),
14
+ proposal_id TEXT UNIQUE REFERENCES action_proposals(id),
15
+ assigned_operator_id TEXT NOT NULL REFERENCES operator_users(id),
16
+ due_at TEXT NOT NULL,
17
+ instructions TEXT NOT NULL,
18
+ approved_by TEXT NOT NULL,
19
+ request_digest TEXT NOT NULL,
20
+ idempotency_key TEXT NOT NULL UNIQUE,
21
+ verified_at TEXT,
22
+ verified_by TEXT,
23
+ evidence_reference TEXT,
24
+ evidence_note TEXT,
25
+ created_at TEXT NOT NULL,
26
+ updated_at TEXT NOT NULL,
27
+ UNIQUE(appeal_decision_id, source_key),
28
+ CHECK ((original_proposal_id IS NOT NULL) != (original_outbox_id IS NOT NULL)),
29
+ CHECK ((mode='automated' AND proposal_id IS NOT NULL) OR mode='manual')
30
+ );
31
+ CREATE INDEX appeal_corrections_due ON appeal_corrections(due_at,id) WHERE verified_at IS NULL;
32
+ CREATE INDEX appeal_corrections_report ON appeal_corrections(report_id,appeal_id);
33
+
34
+ DROP VIEW case_escalation_additional_sources;
35
+ CREATE VIEW case_escalation_additional_sources AS
36
+ SELECT 'correction_overdue:' || c.id AS source_key, 'correction_overdue' AS reason,
37
+ c.report_id, a.queue_id AS queue_id, r.queue_id AS report_queue_id, c.appeal_id AS source_id,
38
+ c.assigned_operator_id || ':' || c.mode || ':' || c.due_at AS source_fingerprint,
39
+ c.assigned_operator_id AS owner_id, CASE WHEN c.mode='automated' AND p.state='failed' AND p.delivery_terminal=1 THEN MIN(c.due_at,p.updated_at) ELSE c.due_at END AS due_at
40
+ FROM appeal_corrections c LEFT JOIN action_proposals p ON p.id=c.proposal_id JOIN appeals a ON a.id=c.appeal_id JOIN reports r ON r.id=c.report_id
41
+ WHERE c.verified_at IS NULL AND r.state='appealed';