@composable-os/cli 0.0.0-stage → 0.1.0-beta.7

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 (139) hide show
  1. package/LICENSE +109 -0
  2. package/README.md +24 -2
  3. package/dist/blocks-HVTAQ5W2.js +233 -0
  4. package/dist/chunk-2ABSVMGB.js +13 -0
  5. package/dist/chunk-2P4AZTFL.js +358 -0
  6. package/dist/chunk-2S7375SC.js +802 -0
  7. package/dist/chunk-5KSXSK7Y.js +47 -0
  8. package/dist/chunk-6UWRT4VM.js +220 -0
  9. package/dist/chunk-BEKL3CA2.js +28975 -0
  10. package/dist/chunk-E5JLTLRM.js +50 -0
  11. package/dist/chunk-HDIOVFAZ.js +544 -0
  12. package/dist/chunk-KGHA67HG.js +765 -0
  13. package/dist/chunk-MAXLUQKB.js +1287 -0
  14. package/dist/chunk-MINQ46RU.js +16485 -0
  15. package/dist/chunk-STJZJIRA.js +11378 -0
  16. package/dist/chunk-VL62IMBN.js +1423 -0
  17. package/dist/chunk-X6I2LWB3.js +24319 -0
  18. package/dist/chunk-Y7PQ7NIL.js +1178 -0
  19. package/dist/chunk-ZRWF6QZS.js +2357 -0
  20. package/dist/cli-BRSACHSG.js +31393 -0
  21. package/dist/cli-XHC6QBQW.js +121 -0
  22. package/dist/cli.js +25 -0
  23. package/dist/client-repo-G55G4OLV.js +55 -0
  24. package/dist/create.js +25 -0
  25. package/dist/fields-64BQJA62.js +48 -0
  26. package/dist/invoke-5SR5ZWW7.js +26 -0
  27. package/dist/person-AZFEPZOE.js +58 -0
  28. package/dist/platform/migrations/0001_init.sql +59 -0
  29. package/dist/platform/migrations/0002_users_settings.sql +34 -0
  30. package/dist/platform/migrations/0003_people_organisations.sql +225 -0
  31. package/dist/platform/migrations/0004_activities_tasks.sql +88 -0
  32. package/dist/platform/migrations/0005_views_audit.sql +79 -0
  33. package/dist/platform/migrations/0006_search.sql +223 -0
  34. package/dist/platform/migrations/0007_crm_views.sql +141 -0
  35. package/dist/platform/migrations/0008_audit_redaction.sql +116 -0
  36. package/dist/platform/migrations/0010_approval_queue.sql +90 -0
  37. package/dist/platform/migrations/0011_connected_apps.sql +36 -0
  38. package/dist/platform/migrations/0020_extensions.sql +373 -0
  39. package/dist/platform/migrations/0030_extension_proposals.sql +29 -0
  40. package/dist/platform/migrations/0050_live_row_indexes.sql +32 -0
  41. package/dist/platform/migrations/0060_audit_read_counts.sql +31 -0
  42. package/dist/platform/migrations/0061_audit_retention.sql +95 -0
  43. package/dist/platform/migrations/0062_run_history_retention.sql +6 -0
  44. package/dist/platform/migrations/0063_pending_action_compaction.sql +18 -0
  45. package/dist/platform/migrations/0064_drop_rate_limit.sql +6 -0
  46. package/dist/platform/migrations/0065_storage_stats.sql +13 -0
  47. package/dist/platform/migrations/0070_drop_proposals_ext_idx.sql +9 -0
  48. package/dist/platform/migrations/0080_views_dashboards.sql +46 -0
  49. package/dist/platform/migrations/0100_stage_history.sql +54 -0
  50. package/dist/platform/migrations/0110_integrations.sql +103 -0
  51. package/dist/platform/migrations/0111_email_calendar_sync.sql +137 -0
  52. package/dist/platform/migrations/0112_sync_item_occurred_at.sql +8 -0
  53. package/dist/platform/migrations/0130_automations.sql +113 -0
  54. package/dist/platform/migrations/0131_notifications.sql +29 -0
  55. package/dist/platform/migrations/0132_automation_runtime.sql +14 -0
  56. package/dist/platform/migrations/0150_ai_summaries_embeddings.sql +48 -0
  57. package/dist/platform/migrations/0160_releases.sql +41 -0
  58. package/dist/platform/migrations/0161_setup_wizard.sql +30 -0
  59. package/dist/platform/migrations/0170_automation_connections.sql +32 -0
  60. package/dist/platform/migrations/0171_automation_outbox.sql +23 -0
  61. package/dist/platform/migrations/0172_notification_delivery.sql +46 -0
  62. package/dist/platform/migrations/0200_assistant_conversations.sql +25 -0
  63. package/dist/platform/migrations/0205_policy_adjudicator.sql +61 -0
  64. package/dist/platform/migrations/0210_daily_digest.sql +33 -0
  65. package/dist/platform/migrations/0220_entitlements.sql +13 -0
  66. package/dist/platform/migrations/0221_support_grants.sql +33 -0
  67. package/dist/platform/migrations/0250_judge.sql +11 -0
  68. package/dist/platform/migrations/0251_adjudication_judgement.sql +8 -0
  69. package/dist/platform/migrations/0252_judge_usage.sql +14 -0
  70. package/dist/platform/migrations/0255_summary_checks.sql +9 -0
  71. package/dist/platform/migrations/0256_outbound_check.sql +9 -0
  72. package/dist/platform/migrations/0260_ai_usage.sql +20 -0
  73. package/dist/platform/migrations/0270_assistant_conversation_refs.sql +29 -0
  74. package/dist/platform/migrations/0310_assignment_rules.sql +54 -0
  75. package/dist/platform/migrations/0320_duplicates.sql +41 -0
  76. package/dist/platform/migrations/0330_erase_completeness.sql +54 -0
  77. package/dist/platform/migrations/0350_assistant_thread.sql +38 -0
  78. package/dist/platform/migrations/0351_assistant_model_roles.sql +26 -0
  79. package/dist/platform/migrations/0352_assistant_memory.sql +74 -0
  80. package/dist/platform/migrations/0358_ai_cache_observation.sql +24 -0
  81. package/dist/platform/migrations/0359_ai_compaction_decision.sql +25 -0
  82. package/dist/platform/migrations/0360_ai_cache_estimate.sql +35 -0
  83. package/dist/platform/migrations/0361_voice_preferences.sql +27 -0
  84. package/dist/platform/migrations/0362_voice_usage.sql +17 -0
  85. package/dist/platform/migrations/0365_routines.sql +38 -0
  86. package/dist/platform/migrations/0370_approval_execution.sql +20 -0
  87. package/dist/platform/migrations/0371_agent_jobs.sql +65 -0
  88. package/dist/platform/migrations/0380_rec_views.sql +94 -0
  89. package/dist/platform/migrations/0400_customer_audit_actor.sql +142 -0
  90. package/dist/platform/migrations/0401_customer_accounts.sql +61 -0
  91. package/dist/platform/migrations/0402_erase_requests.sql +34 -0
  92. package/dist/platform/migrations/0410_idempotency_keys.sql +45 -0
  93. package/dist/platform/migrations/0411_device_sessions.sql +33 -0
  94. package/dist/platform/migrations/0420_transition_history.sql +100 -0
  95. package/dist/platform/migrations/0421_orders.sql +134 -0
  96. package/dist/platform/migrations/0422_order_payments.sql +109 -0
  97. package/dist/platform/migrations/0440_capacity.sql +100 -0
  98. package/dist/platform/migrations/0441_capacity_views.sql +40 -0
  99. package/dist/platform/migrations/0450_order_grants.sql +21 -0
  100. package/dist/platform/migrations/0451_pack_roles.sql +18 -0
  101. package/dist/platform/migrations/0460_content.sql +59 -0
  102. package/dist/platform/migrations/0461_media.sql +52 -0
  103. package/dist/platform/migrations/0470_publish_versions.sql +36 -0
  104. package/dist/platform/migrations/0471_permission_grants.sql +18 -0
  105. package/dist/platform/migrations/0490_staff_invites.sql +40 -0
  106. package/dist/platform/migrations/0491_sign_in_mail_kind.sql +10 -0
  107. package/dist/platform/migrations/0492_email_confirmation_and_sign_in_confirm.sql +30 -0
  108. package/dist/platform/migrations/0500_published_media_field.sql +25 -0
  109. package/dist/platform/migrations/0501_import_keys.sql +23 -0
  110. package/dist/platform/sdk/automation-check.ts +534 -0
  111. package/dist/platform/sdk/automation.ts +743 -0
  112. package/dist/platform/sdk/behaviour.ts +215 -0
  113. package/dist/platform/sdk/capacity.ts +112 -0
  114. package/dist/platform/sdk/contract.ts +766 -0
  115. package/dist/platform/sdk/document.ts +788 -0
  116. package/dist/platform/sdk/field-types.ts +106 -0
  117. package/dist/platform/sdk/formula/ast.ts +72 -0
  118. package/dist/platform/sdk/formula/compile.ts +454 -0
  119. package/dist/platform/sdk/formula/index.ts +4 -0
  120. package/dist/platform/sdk/formula/parser.ts +417 -0
  121. package/dist/platform/sdk/index.ts +33 -0
  122. package/dist/platform/sdk/manifest.ts +1484 -0
  123. package/dist/platform/sdk/media.ts +46 -0
  124. package/dist/platform/sdk/permissions.ts +60 -0
  125. package/dist/platform/sdk/renamed.ts +63 -0
  126. package/dist/platform/sdk/rich-text-fixture.ts +68 -0
  127. package/dist/platform/sdk/rich-text.ts +173 -0
  128. package/dist/platform/sdk/runtime.ts +410 -0
  129. package/dist/platform/sdk/sellable.ts +396 -0
  130. package/dist/platform/sdk/slug.ts +25 -0
  131. package/dist/platform/sdk/states.ts +162 -0
  132. package/dist/platform/sdk/testing.ts +836 -0
  133. package/dist/products-2H5DR232.js +11 -0
  134. package/dist/run-4JNFLYXV.js +847 -0
  135. package/dist/src-J27WXNLY.js +270 -0
  136. package/dist/src-SSVOBH6L.js +31 -0
  137. package/dist/token-MOJUA2U6.js +70 -0
  138. package/dist/token-util-TENRN4V7.js +6 -0
  139. package/package.json +33 -4
@@ -0,0 +1,88 @@
1
+ -- Migration 0004: Activities and Tasks, linked to records polymorphically (ADR 0015).
2
+ --
3
+ -- Links are (object_type, record_id) pairs, not foreign keys, so extension objects can be linked
4
+ -- later. object_type is the object's registry name: 'person', 'organisation', 'task', or an
5
+ -- extension object's name.
6
+
7
+ CREATE TABLE core_activity (
8
+ id TEXT PRIMARY KEY NOT NULL,
9
+ type TEXT NOT NULL CHECK (type IN ('note', 'email', 'call', 'meeting', 'system')),
10
+ -- Short headline: an email subject, meeting title, or a system event summary.
11
+ subject TEXT,
12
+ -- Plain-text or Markdown body (notes, email text, call notes). Indexed for search.
13
+ body TEXT,
14
+ -- Typed per `type` (validated by the operation layer), e.g. email headers, call duration,
15
+ -- or a system event's field changes.
16
+ payload TEXT NOT NULL DEFAULT '{}' CHECK (json_valid(payload)),
17
+ occurred_at TEXT NOT NULL,
18
+ -- The user the activity is attributed to (null for automation or system events).
19
+ author_user_id TEXT REFERENCES core_user (id) ON DELETE SET NULL,
20
+ -- Who caused it: human | assistant | mcp | automation | system.
21
+ author_actor TEXT NOT NULL DEFAULT 'human'
22
+ CHECK (author_actor IN ('human', 'assistant', 'mcp', 'automation', 'system')),
23
+ created_at TEXT NOT NULL,
24
+ updated_at TEXT NOT NULL,
25
+ deleted_at TEXT,
26
+ deleted_by TEXT REFERENCES core_user (id) ON DELETE SET NULL
27
+ );
28
+ CREATE INDEX core_activity_occurred_idx ON core_activity (occurred_at, id);
29
+ CREATE INDEX core_activity_type_idx ON core_activity (type);
30
+ CREATE INDEX core_activity_deleted_idx ON core_activity (deleted_at);
31
+
32
+ CREATE TABLE core_activity_links (
33
+ activity_id TEXT NOT NULL REFERENCES core_activity (id) ON DELETE CASCADE,
34
+ object_type TEXT NOT NULL,
35
+ record_id TEXT NOT NULL,
36
+ PRIMARY KEY (activity_id, object_type, record_id)
37
+ );
38
+ CREATE INDEX core_activity_links_record_idx ON core_activity_links (object_type, record_id, activity_id);
39
+
40
+ CREATE TABLE core_task (
41
+ id TEXT PRIMARY KEY NOT NULL,
42
+ title TEXT NOT NULL CHECK (title <> ''),
43
+ notes TEXT,
44
+ -- A date (`YYYY-MM-DD`) or an ISO timestamp.
45
+ due_at TEXT,
46
+ status TEXT NOT NULL DEFAULT 'open' CHECK (status IN ('open', 'done')),
47
+ completed_at TEXT,
48
+ completed_by TEXT REFERENCES core_user (id) ON DELETE SET NULL,
49
+ assignee_id TEXT REFERENCES core_user (id) ON DELETE SET NULL,
50
+ created_at TEXT NOT NULL,
51
+ created_by TEXT REFERENCES core_user (id) ON DELETE SET NULL,
52
+ updated_at TEXT NOT NULL,
53
+ deleted_at TEXT,
54
+ deleted_by TEXT REFERENCES core_user (id) ON DELETE SET NULL
55
+ );
56
+ CREATE INDEX core_task_assignee_idx ON core_task (assignee_id, status, due_at);
57
+ CREATE INDEX core_task_due_idx ON core_task (status, due_at);
58
+ CREATE INDEX core_task_deleted_idx ON core_task (deleted_at);
59
+
60
+ CREATE TABLE core_task_links (
61
+ task_id TEXT NOT NULL REFERENCES core_task (id) ON DELETE CASCADE,
62
+ object_type TEXT NOT NULL,
63
+ record_id TEXT NOT NULL,
64
+ PRIMARY KEY (task_id, object_type, record_id)
65
+ );
66
+ CREATE INDEX core_task_links_record_idx ON core_task_links (object_type, record_id, task_id);
67
+
68
+ -- Hard-deleting a record (purge or erase) removes links that point at it. The activities and
69
+ -- tasks themselves stay; the purge decides what to do with ones left unlinked.
70
+ CREATE TRIGGER core_person_purge_links AFTER DELETE ON core_person
71
+ BEGIN
72
+ DELETE FROM core_activity_links WHERE object_type = 'person' AND record_id = OLD.id;
73
+ DELETE FROM core_task_links WHERE object_type = 'person' AND record_id = OLD.id;
74
+ END;
75
+
76
+ CREATE TRIGGER core_organisation_purge_links AFTER DELETE ON core_organisation
77
+ BEGIN
78
+ DELETE FROM core_activity_links WHERE object_type = 'organisation' AND record_id = OLD.id;
79
+ DELETE FROM core_task_links WHERE object_type = 'organisation' AND record_id = OLD.id;
80
+ END;
81
+
82
+ CREATE TRIGGER core_task_purge_links AFTER DELETE ON core_task
83
+ BEGIN
84
+ DELETE FROM core_activity_links WHERE object_type = 'task' AND record_id = OLD.id;
85
+ DELETE FROM core_record_tags WHERE object_type = 'task' AND record_id = OLD.id;
86
+ END;
87
+
88
+ UPDATE core_meta SET value = '4' WHERE key = 'schema_version';
@@ -0,0 +1,79 @@
1
+ -- Migration 0005: saved views and the audit log (ADR 0016, ADR 0017, ADR 0018).
2
+
3
+ -- Saved views: a user's stored list configuration. `spec` is a `table`-type view spec
4
+ -- `{ "type": "table", "object", "filter", "sort", "columns" }` that stage 4 extends.
5
+ CREATE TABLE core_views (
6
+ id TEXT PRIMARY KEY NOT NULL,
7
+ user_id TEXT NOT NULL REFERENCES core_user (id) ON DELETE CASCADE,
8
+ object TEXT NOT NULL,
9
+ name TEXT NOT NULL CHECK (name <> ''),
10
+ spec TEXT NOT NULL CHECK (json_valid(spec)),
11
+ position INTEGER NOT NULL DEFAULT 0,
12
+ created_at TEXT NOT NULL,
13
+ updated_at TEXT NOT NULL
14
+ );
15
+ CREATE INDEX core_views_user_object_idx ON core_views (user_id, object, position);
16
+
17
+ -- Audit log: one row per operation invocation (successful writes, every denial, and reads by
18
+ -- non-human actors). Append-only: UPDATE and DELETE are refused by triggers.
19
+ CREATE TABLE core_audit (
20
+ id TEXT PRIMARY KEY NOT NULL,
21
+ at TEXT NOT NULL,
22
+ operation TEXT NOT NULL,
23
+ risk TEXT NOT NULL
24
+ CHECK (risk IN ('read', 'write', 'bulk-write', 'destructive', 'schema', 'extension', 'outbound')),
25
+ actor TEXT NOT NULL CHECK (actor IN ('human', 'assistant', 'mcp', 'automation')),
26
+ user_id TEXT,
27
+ -- JSON `{ surface: "ui" | "mcp" | "automation" | "system", clientId?, automationId? }`.
28
+ origin TEXT NOT NULL CHECK (json_valid(origin)),
29
+ -- Validated input with secrets redacted.
30
+ input TEXT NOT NULL CHECK (json_valid(input)),
31
+ -- A small summary of the output (ids, counts), not the full result.
32
+ result TEXT CHECK (result IS NULL OR json_valid(result)),
33
+ gate_decision TEXT NOT NULL CHECK (gate_decision IN ('auto', 'review', 'deny')),
34
+ -- ok: applied. forbidden: refused by role or actor rules. denied: refused by the gate.
35
+ -- review: routed to review (not yet supported in stage 1). error: failed and rolled back.
36
+ outcome TEXT NOT NULL CHECK (outcome IN ('ok', 'forbidden', 'denied', 'review', 'error')),
37
+ error TEXT CHECK (error IS NULL OR json_valid(error))
38
+ );
39
+ CREATE INDEX core_audit_at_idx ON core_audit (at, id);
40
+ CREATE INDEX core_audit_user_idx ON core_audit (user_id, at);
41
+ CREATE INDEX core_audit_operation_idx ON core_audit (operation, at);
42
+ CREATE INDEX core_audit_actor_idx ON core_audit (actor, at);
43
+
44
+ -- Field-level changes, one row per record an audited operation touched. The record History tab
45
+ -- reads these by (object_type, record_id).
46
+ CREATE TABLE core_audit_changes (
47
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
48
+ audit_id TEXT NOT NULL REFERENCES core_audit (id),
49
+ object_type TEXT NOT NULL,
50
+ record_id TEXT NOT NULL,
51
+ action TEXT NOT NULL CHECK (action IN ('create', 'update', 'delete', 'restore', 'erase', 'merge')),
52
+ -- JSON `{ "<field>": { "before": ..., "after": ... } }` for changed fields only.
53
+ diff TEXT NOT NULL CHECK (json_valid(diff)),
54
+ at TEXT NOT NULL
55
+ );
56
+ CREATE INDEX core_audit_changes_record_idx ON core_audit_changes (object_type, record_id, id);
57
+ CREATE INDEX core_audit_changes_audit_idx ON core_audit_changes (audit_id);
58
+
59
+ CREATE TRIGGER core_audit_no_update BEFORE UPDATE ON core_audit
60
+ BEGIN
61
+ SELECT RAISE(ABORT, 'core_audit is append-only');
62
+ END;
63
+
64
+ CREATE TRIGGER core_audit_no_delete BEFORE DELETE ON core_audit
65
+ BEGIN
66
+ SELECT RAISE(ABORT, 'core_audit is append-only');
67
+ END;
68
+
69
+ CREATE TRIGGER core_audit_changes_no_update BEFORE UPDATE ON core_audit_changes
70
+ BEGIN
71
+ SELECT RAISE(ABORT, 'core_audit_changes is append-only');
72
+ END;
73
+
74
+ CREATE TRIGGER core_audit_changes_no_delete BEFORE DELETE ON core_audit_changes
75
+ BEGIN
76
+ SELECT RAISE(ABORT, 'core_audit_changes is append-only');
77
+ END;
78
+
79
+ UPDATE core_meta SET value = '5' WHERE key = 'schema_version';
@@ -0,0 +1,223 @@
1
+ -- Migration 0006: full-text search (FTS5) over people, organisations, activities and tasks.
2
+ --
3
+ -- Two layers:
4
+ -- * core_search_docs: one plain row per live record (object_type, record_id, title, body).
5
+ -- Domain triggers below rebuild a record's row whenever it or its emails/domains change, and
6
+ -- drop it when the record is soft-deleted or purged.
7
+ -- * core_search: an external-content FTS5 index over core_search_docs, kept in step by the
8
+ -- standard external-content triggers. Query it with
9
+ -- SELECT d.object_type, d.record_id FROM core_search s
10
+ -- JOIN core_search_docs d ON d.id = s.rowid WHERE core_search MATCH ? ORDER BY rank
11
+ --
12
+ -- What is indexed: people (display, given and family names; all emails), organisations (name;
13
+ -- all domains), activities (subject, body) and tasks (title, notes). unicode61 splits emails and
14
+ -- domains on punctuation, so "ada", "example" and "com" are tokens; prefix indexes keep
15
+ -- type-ahead queries ("ad*") fast.
16
+
17
+ CREATE TABLE core_search_docs (
18
+ id INTEGER PRIMARY KEY,
19
+ object_type TEXT NOT NULL,
20
+ record_id TEXT NOT NULL,
21
+ title TEXT NOT NULL DEFAULT '',
22
+ body TEXT NOT NULL DEFAULT '',
23
+ UNIQUE (object_type, record_id)
24
+ );
25
+
26
+ CREATE VIRTUAL TABLE core_search USING fts5(
27
+ title,
28
+ body,
29
+ content = 'core_search_docs',
30
+ content_rowid = 'id',
31
+ tokenize = 'unicode61 remove_diacritics 2',
32
+ prefix = '2 3'
33
+ );
34
+
35
+ CREATE TRIGGER core_search_docs_ai AFTER INSERT ON core_search_docs
36
+ BEGIN
37
+ INSERT INTO core_search (rowid, title, body) VALUES (NEW.id, NEW.title, NEW.body);
38
+ END;
39
+
40
+ CREATE TRIGGER core_search_docs_ad AFTER DELETE ON core_search_docs
41
+ BEGIN
42
+ INSERT INTO core_search (core_search, rowid, title, body)
43
+ VALUES ('delete', OLD.id, OLD.title, OLD.body);
44
+ END;
45
+
46
+ CREATE TRIGGER core_search_docs_au AFTER UPDATE ON core_search_docs
47
+ BEGIN
48
+ INSERT INTO core_search (core_search, rowid, title, body)
49
+ VALUES ('delete', OLD.id, OLD.title, OLD.body);
50
+ INSERT INTO core_search (rowid, title, body) VALUES (NEW.id, NEW.title, NEW.body);
51
+ END;
52
+
53
+ -- person
54
+ CREATE TRIGGER core_person_search_ai AFTER INSERT ON core_person
55
+ BEGIN
56
+ DELETE FROM core_search_docs WHERE object_type = 'person' AND record_id = NEW.id;
57
+ INSERT INTO core_search_docs (object_type, record_id, title, body)
58
+ SELECT 'person', p.id,
59
+ trim(p.display_name || ' ' || coalesce(p.given_name, '') || ' ' || coalesce(p.family_name, '')),
60
+ coalesce((SELECT group_concat(e.email, ' ') FROM core_person_emails e WHERE e.person_id = p.id), '')
61
+ FROM core_person p WHERE p.id = NEW.id AND p.deleted_at IS NULL;
62
+ END;
63
+
64
+ CREATE TRIGGER core_person_search_au AFTER UPDATE OF display_name, given_name, family_name, deleted_at ON core_person
65
+ BEGIN
66
+ DELETE FROM core_search_docs WHERE object_type = 'person' AND record_id = NEW.id;
67
+ INSERT INTO core_search_docs (object_type, record_id, title, body)
68
+ SELECT 'person', p.id,
69
+ trim(p.display_name || ' ' || coalesce(p.given_name, '') || ' ' || coalesce(p.family_name, '')),
70
+ coalesce((SELECT group_concat(e.email, ' ') FROM core_person_emails e WHERE e.person_id = p.id), '')
71
+ FROM core_person p WHERE p.id = NEW.id AND p.deleted_at IS NULL;
72
+ END;
73
+
74
+ CREATE TRIGGER core_person_search_ad AFTER DELETE ON core_person
75
+ BEGIN
76
+ DELETE FROM core_search_docs WHERE object_type = 'person' AND record_id = OLD.id;
77
+ END;
78
+
79
+ -- organisation
80
+ CREATE TRIGGER core_organisation_search_ai AFTER INSERT ON core_organisation
81
+ BEGIN
82
+ DELETE FROM core_search_docs WHERE object_type = 'organisation' AND record_id = NEW.id;
83
+ INSERT INTO core_search_docs (object_type, record_id, title, body)
84
+ SELECT 'organisation', o.id, o.name,
85
+ coalesce((SELECT group_concat(d.domain, ' ') FROM core_organisation_domains d
86
+ WHERE d.organisation_id = o.id), '')
87
+ FROM core_organisation o WHERE o.id = NEW.id AND o.deleted_at IS NULL;
88
+ END;
89
+
90
+ CREATE TRIGGER core_organisation_search_au AFTER UPDATE OF name, deleted_at ON core_organisation
91
+ BEGIN
92
+ DELETE FROM core_search_docs WHERE object_type = 'organisation' AND record_id = NEW.id;
93
+ INSERT INTO core_search_docs (object_type, record_id, title, body)
94
+ SELECT 'organisation', o.id, o.name,
95
+ coalesce((SELECT group_concat(d.domain, ' ') FROM core_organisation_domains d
96
+ WHERE d.organisation_id = o.id), '')
97
+ FROM core_organisation o WHERE o.id = NEW.id AND o.deleted_at IS NULL;
98
+ END;
99
+
100
+ CREATE TRIGGER core_organisation_search_ad AFTER DELETE ON core_organisation
101
+ BEGIN
102
+ DELETE FROM core_search_docs WHERE object_type = 'organisation' AND record_id = OLD.id;
103
+ END;
104
+
105
+ -- activity
106
+ CREATE TRIGGER core_activity_search_ai AFTER INSERT ON core_activity
107
+ BEGIN
108
+ DELETE FROM core_search_docs WHERE object_type = 'activity' AND record_id = NEW.id;
109
+ INSERT INTO core_search_docs (object_type, record_id, title, body)
110
+ SELECT 'activity', a.id, coalesce(a.subject, ''), coalesce(a.body, '')
111
+ FROM core_activity a WHERE a.id = NEW.id AND a.deleted_at IS NULL;
112
+ END;
113
+
114
+ CREATE TRIGGER core_activity_search_au AFTER UPDATE OF subject, body, deleted_at ON core_activity
115
+ BEGIN
116
+ DELETE FROM core_search_docs WHERE object_type = 'activity' AND record_id = NEW.id;
117
+ INSERT INTO core_search_docs (object_type, record_id, title, body)
118
+ SELECT 'activity', a.id, coalesce(a.subject, ''), coalesce(a.body, '')
119
+ FROM core_activity a WHERE a.id = NEW.id AND a.deleted_at IS NULL;
120
+ END;
121
+
122
+ CREATE TRIGGER core_activity_search_ad AFTER DELETE ON core_activity
123
+ BEGIN
124
+ DELETE FROM core_search_docs WHERE object_type = 'activity' AND record_id = OLD.id;
125
+ END;
126
+
127
+ -- task
128
+ CREATE TRIGGER core_task_search_ai AFTER INSERT ON core_task
129
+ BEGIN
130
+ DELETE FROM core_search_docs WHERE object_type = 'task' AND record_id = NEW.id;
131
+ INSERT INTO core_search_docs (object_type, record_id, title, body)
132
+ SELECT 'task', t.id, t.title, coalesce(t.notes, '')
133
+ FROM core_task t WHERE t.id = NEW.id AND t.deleted_at IS NULL;
134
+ END;
135
+
136
+ CREATE TRIGGER core_task_search_au AFTER UPDATE OF title, notes, deleted_at ON core_task
137
+ BEGIN
138
+ DELETE FROM core_search_docs WHERE object_type = 'task' AND record_id = NEW.id;
139
+ INSERT INTO core_search_docs (object_type, record_id, title, body)
140
+ SELECT 'task', t.id, t.title, coalesce(t.notes, '')
141
+ FROM core_task t WHERE t.id = NEW.id AND t.deleted_at IS NULL;
142
+ END;
143
+
144
+ CREATE TRIGGER core_task_search_ad AFTER DELETE ON core_task
145
+ BEGIN
146
+ DELETE FROM core_search_docs WHERE object_type = 'task' AND record_id = OLD.id;
147
+ END;
148
+
149
+ -- person child rows
150
+ CREATE TRIGGER core_person_emails_search_ai AFTER INSERT ON core_person_emails
151
+ BEGIN
152
+ DELETE FROM core_search_docs WHERE object_type = 'person' AND record_id = NEW.person_id;
153
+ INSERT INTO core_search_docs (object_type, record_id, title, body)
154
+ SELECT 'person', p.id,
155
+ trim(p.display_name || ' ' || coalesce(p.given_name, '') || ' ' || coalesce(p.family_name, '')),
156
+ coalesce((SELECT group_concat(e.email, ' ') FROM core_person_emails e WHERE e.person_id = p.id), '')
157
+ FROM core_person p WHERE p.id = NEW.person_id AND p.deleted_at IS NULL;
158
+ END;
159
+
160
+ CREATE TRIGGER core_person_emails_search_au AFTER UPDATE ON core_person_emails
161
+ BEGIN
162
+ DELETE FROM core_search_docs WHERE object_type = 'person' AND record_id = OLD.person_id;
163
+ INSERT INTO core_search_docs (object_type, record_id, title, body)
164
+ SELECT 'person', p.id,
165
+ trim(p.display_name || ' ' || coalesce(p.given_name, '') || ' ' || coalesce(p.family_name, '')),
166
+ coalesce((SELECT group_concat(e.email, ' ') FROM core_person_emails e WHERE e.person_id = p.id), '')
167
+ FROM core_person p WHERE p.id = OLD.person_id AND p.deleted_at IS NULL;
168
+ DELETE FROM core_search_docs WHERE object_type = 'person' AND record_id = NEW.person_id;
169
+ INSERT INTO core_search_docs (object_type, record_id, title, body)
170
+ SELECT 'person', p.id,
171
+ trim(p.display_name || ' ' || coalesce(p.given_name, '') || ' ' || coalesce(p.family_name, '')),
172
+ coalesce((SELECT group_concat(e.email, ' ') FROM core_person_emails e WHERE e.person_id = p.id), '')
173
+ FROM core_person p WHERE p.id = NEW.person_id AND p.deleted_at IS NULL;
174
+ END;
175
+
176
+ CREATE TRIGGER core_person_emails_search_ad AFTER DELETE ON core_person_emails
177
+ BEGIN
178
+ DELETE FROM core_search_docs WHERE object_type = 'person' AND record_id = OLD.person_id;
179
+ INSERT INTO core_search_docs (object_type, record_id, title, body)
180
+ SELECT 'person', p.id,
181
+ trim(p.display_name || ' ' || coalesce(p.given_name, '') || ' ' || coalesce(p.family_name, '')),
182
+ coalesce((SELECT group_concat(e.email, ' ') FROM core_person_emails e WHERE e.person_id = p.id), '')
183
+ FROM core_person p WHERE p.id = OLD.person_id AND p.deleted_at IS NULL;
184
+ END;
185
+
186
+ -- organisation child rows
187
+ CREATE TRIGGER core_organisation_domains_search_ai AFTER INSERT ON core_organisation_domains
188
+ BEGIN
189
+ DELETE FROM core_search_docs WHERE object_type = 'organisation' AND record_id = NEW.organisation_id;
190
+ INSERT INTO core_search_docs (object_type, record_id, title, body)
191
+ SELECT 'organisation', o.id, o.name,
192
+ coalesce((SELECT group_concat(d.domain, ' ') FROM core_organisation_domains d
193
+ WHERE d.organisation_id = o.id), '')
194
+ FROM core_organisation o WHERE o.id = NEW.organisation_id AND o.deleted_at IS NULL;
195
+ END;
196
+
197
+ CREATE TRIGGER core_organisation_domains_search_au AFTER UPDATE ON core_organisation_domains
198
+ BEGIN
199
+ DELETE FROM core_search_docs WHERE object_type = 'organisation' AND record_id = OLD.organisation_id;
200
+ INSERT INTO core_search_docs (object_type, record_id, title, body)
201
+ SELECT 'organisation', o.id, o.name,
202
+ coalesce((SELECT group_concat(d.domain, ' ') FROM core_organisation_domains d
203
+ WHERE d.organisation_id = o.id), '')
204
+ FROM core_organisation o WHERE o.id = OLD.organisation_id AND o.deleted_at IS NULL;
205
+ DELETE FROM core_search_docs WHERE object_type = 'organisation' AND record_id = NEW.organisation_id;
206
+ INSERT INTO core_search_docs (object_type, record_id, title, body)
207
+ SELECT 'organisation', o.id, o.name,
208
+ coalesce((SELECT group_concat(d.domain, ' ') FROM core_organisation_domains d
209
+ WHERE d.organisation_id = o.id), '')
210
+ FROM core_organisation o WHERE o.id = NEW.organisation_id AND o.deleted_at IS NULL;
211
+ END;
212
+
213
+ CREATE TRIGGER core_organisation_domains_search_ad AFTER DELETE ON core_organisation_domains
214
+ BEGIN
215
+ DELETE FROM core_search_docs WHERE object_type = 'organisation' AND record_id = OLD.organisation_id;
216
+ INSERT INTO core_search_docs (object_type, record_id, title, body)
217
+ SELECT 'organisation', o.id, o.name,
218
+ coalesce((SELECT group_concat(d.domain, ' ') FROM core_organisation_domains d
219
+ WHERE d.organisation_id = o.id), '')
220
+ FROM core_organisation o WHERE o.id = OLD.organisation_id AND o.deleted_at IS NULL;
221
+ END;
222
+
223
+ UPDATE core_meta SET value = '6' WHERE key = 'schema_version';
@@ -0,0 +1,141 @@
1
+ -- Migration 0007: crm_* read views (ADR 0010, ADR 0015 amendment 2026-11-04).
2
+ --
3
+ -- D1/SQLite has no schemas, so the `crm.*` read surface is a set of views named `crm_<name>`
4
+ -- (`crm.people` is `crm_people`). They are what dashboard SQL, `query.sql` and list operations
5
+ -- read. Rules:
6
+ -- * Live rows only (deleted_at IS NULL).
7
+ -- * Friendly, stable column names; no secrets (crm_users has no auth columns).
8
+ -- * Each view selects from its core table aliased by the object name, so the stage-3 reconciler
9
+ -- can regenerate it with `LEFT JOIN x_<ext>_<object> USING (id)` for extension side tables
10
+ -- without changing core tables.
11
+ -- * Multi-valued fields are JSON arrays (emails, domains, tags).
12
+
13
+ CREATE VIEW crm_users AS
14
+ SELECT u.id, u.name, u.email, u.role, u.image, u.created_at
15
+ FROM core_user u;
16
+
17
+ CREATE VIEW crm_people AS
18
+ SELECT
19
+ person.id,
20
+ person.display_name,
21
+ person.given_name,
22
+ person.family_name,
23
+ (SELECT e.email FROM core_person_emails e
24
+ WHERE e.person_id = person.id AND e.is_primary = 1) AS primary_email,
25
+ (SELECT json_group_array(e.email) FROM
26
+ (SELECT email FROM core_person_emails WHERE person_id = person.id ORDER BY position) e) AS emails,
27
+ (SELECT ph.number FROM core_person_phones ph
28
+ WHERE ph.person_id = person.id AND ph.is_primary = 1) AS primary_phone,
29
+ aff.id AS primary_affiliation_id,
30
+ aff.title AS job_title,
31
+ aff.organisation_id AS organisation_id,
32
+ org.name AS organisation_name,
33
+ person.location,
34
+ person.timezone,
35
+ person.owner_id,
36
+ person.avatar_key,
37
+ (SELECT json_group_array(t.name) FROM
38
+ (SELECT tg.name FROM core_record_tags rt JOIN core_tags tg ON tg.id = rt.tag_id
39
+ WHERE rt.object_type = 'person' AND rt.record_id = person.id ORDER BY tg.name) t) AS tags,
40
+ person.created_at,
41
+ person.updated_at
42
+ FROM core_person person
43
+ LEFT JOIN core_affiliation aff
44
+ ON aff.person_id = person.id AND aff.is_primary = 1 AND aff.deleted_at IS NULL
45
+ LEFT JOIN core_organisation org
46
+ ON org.id = aff.organisation_id AND org.deleted_at IS NULL
47
+ WHERE person.deleted_at IS NULL;
48
+
49
+ CREATE VIEW crm_organisations AS
50
+ SELECT
51
+ organisation.id,
52
+ organisation.name,
53
+ (SELECT d.domain FROM core_organisation_domains d
54
+ WHERE d.organisation_id = organisation.id AND d.is_primary = 1) AS primary_domain,
55
+ (SELECT json_group_array(d.domain) FROM
56
+ (SELECT domain FROM core_organisation_domains WHERE organisation_id = organisation.id
57
+ ORDER BY position) d) AS domains,
58
+ organisation.website,
59
+ organisation.industry,
60
+ organisation.parent_id,
61
+ parent.name AS parent_name,
62
+ organisation.owner_id,
63
+ organisation.avatar_key,
64
+ (SELECT COUNT(*) FROM core_affiliation a JOIN core_person p ON p.id = a.person_id
65
+ WHERE a.organisation_id = organisation.id AND a.is_current = 1
66
+ AND a.deleted_at IS NULL AND p.deleted_at IS NULL) AS current_people_count,
67
+ (SELECT json_group_array(t.name) FROM
68
+ (SELECT tg.name FROM core_record_tags rt JOIN core_tags tg ON tg.id = rt.tag_id
69
+ WHERE rt.object_type = 'organisation' AND rt.record_id = organisation.id
70
+ ORDER BY tg.name) t) AS tags,
71
+ organisation.created_at,
72
+ organisation.updated_at
73
+ FROM core_organisation organisation
74
+ LEFT JOIN core_organisation parent
75
+ ON parent.id = organisation.parent_id AND parent.deleted_at IS NULL
76
+ WHERE organisation.deleted_at IS NULL;
77
+
78
+ CREATE VIEW crm_affiliations AS
79
+ SELECT
80
+ affiliation.id,
81
+ affiliation.person_id,
82
+ affiliation.organisation_id,
83
+ affiliation.title,
84
+ affiliation.role,
85
+ affiliation.started_on,
86
+ affiliation.ended_on,
87
+ affiliation.is_current,
88
+ affiliation.is_primary,
89
+ affiliation.created_at,
90
+ affiliation.updated_at
91
+ FROM core_affiliation affiliation
92
+ JOIN core_person p ON p.id = affiliation.person_id AND p.deleted_at IS NULL
93
+ JOIN core_organisation o ON o.id = affiliation.organisation_id AND o.deleted_at IS NULL
94
+ WHERE affiliation.deleted_at IS NULL;
95
+
96
+ CREATE VIEW crm_activities AS
97
+ SELECT
98
+ activity.id,
99
+ activity.type,
100
+ activity.subject,
101
+ activity.body,
102
+ activity.payload,
103
+ activity.occurred_at,
104
+ activity.author_user_id,
105
+ activity.author_actor,
106
+ activity.created_at,
107
+ activity.updated_at
108
+ FROM core_activity activity
109
+ WHERE activity.deleted_at IS NULL;
110
+
111
+ CREATE VIEW crm_activity_links AS
112
+ SELECT l.activity_id, l.object_type, l.record_id
113
+ FROM core_activity_links l
114
+ JOIN core_activity a ON a.id = l.activity_id AND a.deleted_at IS NULL;
115
+
116
+ CREATE VIEW crm_tasks AS
117
+ SELECT
118
+ task.id,
119
+ task.title,
120
+ task.notes,
121
+ task.due_at,
122
+ task.status,
123
+ task.completed_at,
124
+ task.assignee_id,
125
+ task.created_by,
126
+ task.created_at,
127
+ task.updated_at
128
+ FROM core_task task
129
+ WHERE task.deleted_at IS NULL;
130
+
131
+ CREATE VIEW crm_task_links AS
132
+ SELECT l.task_id, l.object_type, l.record_id
133
+ FROM core_task_links l
134
+ JOIN core_task t ON t.id = l.task_id AND t.deleted_at IS NULL;
135
+
136
+ CREATE VIEW crm_record_tags AS
137
+ SELECT rt.object_type, rt.record_id, tg.id AS tag_id, tg.name AS tag
138
+ FROM core_record_tags rt
139
+ JOIN core_tags tg ON tg.id = rt.tag_id;
140
+
141
+ UPDATE core_meta SET value = '7' WHERE key = 'schema_version';
@@ -0,0 +1,116 @@
1
+ -- Migration 0008: sanctioned redaction of personal data in the audit log (ADR 0019).
2
+ --
3
+ -- core_audit and core_audit_changes stay append-only: DELETE is always refused, and UPDATE is
4
+ -- refused unless it is exactly the canonical redaction of a row that has a marker in
5
+ -- core_audit_redactions. Markers are themselves append-only and say which erase removed what,
6
+ -- so a redaction can never be silent.
7
+ --
8
+ -- The only way to redact is to insert a marker; the AFTER INSERT triggers below perform the
9
+ -- canonical UPDATE. A direct UPDATE passes the guard only if it writes the same canonical values
10
+ -- to a marked row, so nothing but "[erased]" can ever be written over audit content.
11
+ --
12
+ -- Canonical redaction:
13
+ -- core_audit: input -> "[erased]"; result -> "[erased]" (if not null);
14
+ -- error -> {"code": <kept>, "message": "[erased]"} (if not null).
15
+ -- Who, when, which operation, risk, actor, origin, gate decision and
16
+ -- outcome are kept.
17
+ -- core_audit_changes: diff keeps its field names; every before/after value becomes
18
+ -- "[erased]". Record, action and time are kept.
19
+
20
+ CREATE TABLE core_audit_redactions (
21
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
22
+ -- Exactly one target: an audit row or a field-change row.
23
+ audit_id TEXT REFERENCES core_audit (id),
24
+ change_id INTEGER REFERENCES core_audit_changes (id),
25
+ -- The erased record (ids carry no personal data).
26
+ object_type TEXT NOT NULL,
27
+ record_id TEXT NOT NULL,
28
+ -- The erase operation that caused it, and who ran it.
29
+ operation TEXT NOT NULL,
30
+ user_id TEXT,
31
+ at TEXT NOT NULL,
32
+ CHECK ((audit_id IS NULL) <> (change_id IS NULL))
33
+ );
34
+ CREATE UNIQUE INDEX core_audit_redactions_audit_uq ON core_audit_redactions (audit_id)
35
+ WHERE audit_id IS NOT NULL;
36
+ CREATE UNIQUE INDEX core_audit_redactions_change_uq ON core_audit_redactions (change_id)
37
+ WHERE change_id IS NOT NULL;
38
+ CREATE INDEX core_audit_redactions_record_idx ON core_audit_redactions (object_type, record_id);
39
+
40
+ CREATE TRIGGER core_audit_redactions_no_update BEFORE UPDATE ON core_audit_redactions
41
+ BEGIN
42
+ SELECT RAISE(ABORT, 'core_audit_redactions is append-only');
43
+ END;
44
+
45
+ CREATE TRIGGER core_audit_redactions_no_delete BEFORE DELETE ON core_audit_redactions
46
+ BEGIN
47
+ SELECT RAISE(ABORT, 'core_audit_redactions is append-only');
48
+ END;
49
+
50
+ -- Guards: replace the unconditional UPDATE refusals from migration 0005. DELETE refusals stay.
51
+
52
+ DROP TRIGGER core_audit_no_update;
53
+ CREATE TRIGGER core_audit_no_update BEFORE UPDATE ON core_audit
54
+ WHEN NOT (
55
+ EXISTS (SELECT 1 FROM core_audit_redactions r WHERE r.audit_id = OLD.id)
56
+ AND NEW.id IS OLD.id
57
+ AND NEW.at IS OLD.at
58
+ AND NEW.operation IS OLD.operation
59
+ AND NEW.risk IS OLD.risk
60
+ AND NEW.actor IS OLD.actor
61
+ AND NEW.user_id IS OLD.user_id
62
+ AND NEW.origin IS OLD.origin
63
+ AND NEW.gate_decision IS OLD.gate_decision
64
+ AND NEW.outcome IS OLD.outcome
65
+ AND NEW.input IS '"[erased]"'
66
+ AND NEW.result IS (CASE WHEN OLD.result IS NULL THEN NULL ELSE '"[erased]"' END)
67
+ AND NEW.error IS (CASE WHEN OLD.error IS NULL THEN NULL
68
+ ELSE json_object('code', json_extract(OLD.error, '$.code'), 'message', '[erased]') END)
69
+ )
70
+ BEGIN
71
+ SELECT RAISE(ABORT, 'core_audit is append-only');
72
+ END;
73
+
74
+ DROP TRIGGER core_audit_changes_no_update;
75
+ CREATE TRIGGER core_audit_changes_no_update BEFORE UPDATE ON core_audit_changes
76
+ WHEN NOT (
77
+ EXISTS (SELECT 1 FROM core_audit_redactions r WHERE r.change_id = OLD.id)
78
+ AND NEW.id IS OLD.id
79
+ AND NEW.audit_id IS OLD.audit_id
80
+ AND NEW.object_type IS OLD.object_type
81
+ AND NEW.record_id IS OLD.record_id
82
+ AND NEW.action IS OLD.action
83
+ AND NEW.at IS OLD.at
84
+ AND NEW.diff IS (
85
+ SELECT coalesce(json_group_object(j.key, json_object('before', '[erased]', 'after', '[erased]')), '{}')
86
+ FROM json_each(OLD.diff) j
87
+ )
88
+ )
89
+ BEGIN
90
+ SELECT RAISE(ABORT, 'core_audit_changes is append-only');
91
+ END;
92
+
93
+ -- Inserting a marker applies the canonical redaction to its target.
94
+
95
+ CREATE TRIGGER core_audit_redactions_apply_audit AFTER INSERT ON core_audit_redactions
96
+ WHEN NEW.audit_id IS NOT NULL
97
+ BEGIN
98
+ UPDATE core_audit SET
99
+ input = '"[erased]"',
100
+ result = CASE WHEN result IS NULL THEN NULL ELSE '"[erased]"' END,
101
+ error = CASE WHEN error IS NULL THEN NULL
102
+ ELSE json_object('code', json_extract(error, '$.code'), 'message', '[erased]') END
103
+ WHERE id = NEW.audit_id;
104
+ END;
105
+
106
+ CREATE TRIGGER core_audit_redactions_apply_change AFTER INSERT ON core_audit_redactions
107
+ WHEN NEW.change_id IS NOT NULL
108
+ BEGIN
109
+ UPDATE core_audit_changes SET diff = (
110
+ SELECT coalesce(json_group_object(j.key, json_object('before', '[erased]', 'after', '[erased]')), '{}')
111
+ FROM json_each(core_audit_changes.diff) j
112
+ )
113
+ WHERE id = NEW.change_id;
114
+ END;
115
+
116
+ UPDATE core_meta SET value = '8' WHERE key = 'schema_version';