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

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 (140) hide show
  1. package/LICENSE +109 -0
  2. package/README.md +24 -2
  3. package/dist/blocks-DEELKGDC.js +233 -0
  4. package/dist/chunk-2ABSVMGB.js +13 -0
  5. package/dist/chunk-2S7375SC.js +802 -0
  6. package/dist/chunk-5KSXSK7Y.js +47 -0
  7. package/dist/chunk-6UWRT4VM.js +220 -0
  8. package/dist/chunk-E5JLTLRM.js +50 -0
  9. package/dist/chunk-G3ORKXVP.js +1178 -0
  10. package/dist/chunk-HDIOVFAZ.js +544 -0
  11. package/dist/chunk-KGHA67HG.js +765 -0
  12. package/dist/chunk-KOKYDJ7Z.js +1423 -0
  13. package/dist/chunk-KTXGEXTY.js +358 -0
  14. package/dist/chunk-LTQDLCP7.js +16486 -0
  15. package/dist/chunk-MAXLUQKB.js +1287 -0
  16. package/dist/chunk-STJZJIRA.js +11378 -0
  17. package/dist/chunk-X6I2LWB3.js +24319 -0
  18. package/dist/chunk-XDDBLYKW.js +29154 -0
  19. package/dist/chunk-ZRWF6QZS.js +2357 -0
  20. package/dist/cli-BRSACHSG.js +31393 -0
  21. package/dist/cli-DTMGNDXN.js +121 -0
  22. package/dist/cli.js +25 -0
  23. package/dist/client-repo-ZVZ6KW5S.js +55 -0
  24. package/dist/create.js +25 -0
  25. package/dist/fields-64BQJA62.js +48 -0
  26. package/dist/invoke-NHHOLLF6.js +26 -0
  27. package/dist/person-WMWRF2HV.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/migrations/0520_publish_requests.sql +20 -0
  111. package/dist/platform/sdk/automation-check.ts +534 -0
  112. package/dist/platform/sdk/automation.ts +743 -0
  113. package/dist/platform/sdk/behaviour.ts +215 -0
  114. package/dist/platform/sdk/capacity.ts +112 -0
  115. package/dist/platform/sdk/contract.ts +766 -0
  116. package/dist/platform/sdk/document.ts +788 -0
  117. package/dist/platform/sdk/field-types.ts +106 -0
  118. package/dist/platform/sdk/formula/ast.ts +72 -0
  119. package/dist/platform/sdk/formula/compile.ts +454 -0
  120. package/dist/platform/sdk/formula/index.ts +4 -0
  121. package/dist/platform/sdk/formula/parser.ts +417 -0
  122. package/dist/platform/sdk/index.ts +33 -0
  123. package/dist/platform/sdk/manifest.ts +1484 -0
  124. package/dist/platform/sdk/media.ts +46 -0
  125. package/dist/platform/sdk/permissions.ts +60 -0
  126. package/dist/platform/sdk/renamed.ts +63 -0
  127. package/dist/platform/sdk/rich-text-fixture.ts +68 -0
  128. package/dist/platform/sdk/rich-text.ts +173 -0
  129. package/dist/platform/sdk/runtime.ts +410 -0
  130. package/dist/platform/sdk/sellable.ts +396 -0
  131. package/dist/platform/sdk/slug.ts +25 -0
  132. package/dist/platform/sdk/states.ts +162 -0
  133. package/dist/platform/sdk/testing.ts +836 -0
  134. package/dist/products-2H5DR232.js +11 -0
  135. package/dist/run-4JNFLYXV.js +847 -0
  136. package/dist/src-G6OF5SKW.js +31 -0
  137. package/dist/src-J27WXNLY.js +270 -0
  138. package/dist/token-MOJUA2U6.js +70 -0
  139. package/dist/token-util-TENRN4V7.js +6 -0
  140. package/package.json +33 -4
@@ -0,0 +1,90 @@
1
+ -- Migration 0010: the approval queue (ADR 0022). A gate `review` outcome records a pending action
2
+ -- instead of failing. Pending actions group into change sets: one open set per MCP session by
3
+ -- default, or an explicit one (changeset.begin / changeset.commit). They expire after a
4
+ -- configurable period (default 7 days). Approval re-runs each action through the pipeline.
5
+ -- (0009 is intentionally unused: migration numbers were reserved per parallel stage.)
6
+
7
+ CREATE TABLE core_change_set (
8
+ id TEXT PRIMARY KEY NOT NULL,
9
+ -- Given by changeset.begin; default (per-session) sets have none.
10
+ title TEXT,
11
+ -- The MCP session (connected app id, cap_…) that queued it, or null for other surfaces.
12
+ session_id TEXT,
13
+ client_id TEXT,
14
+ client_name TEXT,
15
+ user_id TEXT NOT NULL,
16
+ actor TEXT NOT NULL CHECK (actor IN ('human', 'assistant', 'mcp', 'automation')),
17
+ origin TEXT NOT NULL CHECK (json_valid(origin)),
18
+ -- 1 when opened with changeset.begin.
19
+ explicit INTEGER NOT NULL DEFAULT 0 CHECK (explicit IN (0, 1)),
20
+ -- open: accepting new actions. committed: closed by changeset.commit, waiting for review.
21
+ -- closed: a human decided on it; new actions go to a fresh set.
22
+ state TEXT NOT NULL DEFAULT 'open' CHECK (state IN ('open', 'committed', 'closed')),
23
+ created_at TEXT NOT NULL,
24
+ updated_at TEXT NOT NULL,
25
+ committed_at TEXT,
26
+ closed_at TEXT
27
+ );
28
+ CREATE INDEX core_change_set_session_idx ON core_change_set (session_id, state, created_at);
29
+ CREATE INDEX core_change_set_state_idx ON core_change_set (state, created_at);
30
+ CREATE INDEX core_change_set_user_idx ON core_change_set (user_id, created_at);
31
+
32
+ -- One queued operation call. `input` is the caller's input as given (re-validated on apply).
33
+ -- ids are ULIDs, so `ORDER BY id` is queue order.
34
+ CREATE TABLE core_pending_action (
35
+ id TEXT PRIMARY KEY NOT NULL,
36
+ change_set_id TEXT NOT NULL REFERENCES core_change_set (id),
37
+ operation TEXT NOT NULL,
38
+ risk TEXT NOT NULL
39
+ CHECK (risk IN ('read', 'write', 'bulk-write', 'destructive', 'schema', 'extension', 'outbound')),
40
+ input TEXT NOT NULL CHECK (json_valid(input)),
41
+ reason TEXT NOT NULL,
42
+ actor TEXT NOT NULL CHECK (actor IN ('human', 'assistant', 'mcp', 'automation')),
43
+ user_id TEXT NOT NULL,
44
+ origin TEXT NOT NULL CHECK (json_valid(origin)),
45
+ -- The permissions the caller's token allowed (role ∩ scopes), or null for no restriction.
46
+ granted_permissions TEXT CHECK (granted_permissions IS NULL OR json_valid(granted_permissions)),
47
+ bulk_size INTEGER NOT NULL DEFAULT 1,
48
+ -- JSON `[{ object, id, action, diff }]`: the computed diff preview.
49
+ preview TEXT NOT NULL CHECK (json_valid(preview)),
50
+ -- JSON `{ output, records: [{ object, id, state, record }] }`: what the session's reads see.
51
+ simulated TEXT NOT NULL CHECK (json_valid(simulated)),
52
+ -- JSON `[{ object, id }]`: provisional ids (…_pending_…) this action creates, in order.
53
+ provisional TEXT NOT NULL DEFAULT '[]' CHECK (json_valid(provisional)),
54
+ -- JSON `[actionId]`: earlier pending actions this one needs applied first.
55
+ depends_on TEXT NOT NULL DEFAULT '[]' CHECK (json_valid(depends_on)),
56
+ status TEXT NOT NULL DEFAULT 'pending'
57
+ CHECK (status IN ('pending', 'applied', 'failed', 'skipped', 'rejected', 'expired')),
58
+ status_reason TEXT,
59
+ error TEXT CHECK (error IS NULL OR json_valid(error)),
60
+ -- JSON `{ "<provisional id>": "<real id>" }`, set when applied.
61
+ bindings TEXT CHECK (bindings IS NULL OR json_valid(bindings)),
62
+ result TEXT CHECK (result IS NULL OR json_valid(result)),
63
+ request_audit_id TEXT,
64
+ applied_audit_id TEXT,
65
+ decided_by TEXT,
66
+ decided_at TEXT,
67
+ created_at TEXT NOT NULL,
68
+ expires_at TEXT NOT NULL
69
+ );
70
+ CREATE INDEX core_pending_action_set_idx ON core_pending_action (change_set_id, id);
71
+ CREATE INDEX core_pending_action_status_idx ON core_pending_action (status, expires_at);
72
+
73
+ -- Records a pending action touches (real or provisional ids): dependencies and record badges.
74
+ CREATE TABLE core_pending_action_records (
75
+ action_id TEXT NOT NULL REFERENCES core_pending_action (id),
76
+ object_type TEXT NOT NULL,
77
+ record_id TEXT NOT NULL,
78
+ -- create | update | delete | restore | erase | merge | reference
79
+ action TEXT NOT NULL,
80
+ PRIMARY KEY (action_id, object_type, record_id)
81
+ );
82
+ CREATE INDEX core_pending_action_records_record_idx
83
+ ON core_pending_action_records (object_type, record_id);
84
+
85
+ -- Links audit rows to the pending action they queued (outcome review) or applied (outcome ok).
86
+ ALTER TABLE core_audit ADD COLUMN pending_action_id TEXT;
87
+ CREATE INDEX core_audit_pending_action_idx ON core_audit (pending_action_id)
88
+ WHERE pending_action_id IS NOT NULL;
89
+
90
+ UPDATE core_meta SET value = '10' WHERE key = 'schema_version';
@@ -0,0 +1,36 @@
1
+ -- Migration 0011: connected apps (ADR 0021, ADR 0032). One row per OAuth authorization a user
2
+ -- gave an MCP client. Tokens live in the OAuth provider's KV; this row is the authority on
3
+ -- whether the connection may still be used (revocation), what it may do (scopes) and when it was
4
+ -- last used. Its id is the MCP session id.
5
+
6
+ CREATE TABLE core_connected_app (
7
+ id TEXT PRIMARY KEY NOT NULL,
8
+ user_id TEXT NOT NULL REFERENCES core_user (id) ON DELETE CASCADE,
9
+ client_id TEXT NOT NULL,
10
+ client_name TEXT NOT NULL,
11
+ client_uri TEXT,
12
+ redirect_host TEXT,
13
+ -- JSON array of granted scopes: read, write, admin.
14
+ scopes TEXT NOT NULL CHECK (json_valid(scopes)),
15
+ -- The OAuth provider's grant id, bound when the authorization code is exchanged.
16
+ grant_id TEXT,
17
+ created_at TEXT NOT NULL,
18
+ last_used_at TEXT,
19
+ revoked_at TEXT,
20
+ revoked_by TEXT,
21
+ -- revoked (by a person) | replaced (the user authorized the same client again)
22
+ revoke_reason TEXT
23
+ );
24
+ CREATE INDEX core_connected_app_user_idx ON core_connected_app (user_id, created_at);
25
+ CREATE INDEX core_connected_app_grant_idx ON core_connected_app (grant_id);
26
+
27
+ -- Fixed one-minute windows per key (`<connected app id>:requests` / `:writes`).
28
+ CREATE TABLE core_rate_limit (
29
+ key TEXT NOT NULL,
30
+ window_start TEXT NOT NULL,
31
+ count INTEGER NOT NULL DEFAULT 0,
32
+ PRIMARY KEY (key, window_start)
33
+ );
34
+ CREATE INDEX core_rate_limit_window_idx ON core_rate_limit (window_start);
35
+
36
+ UPDATE core_meta SET value = '11' WHERE key = 'schema_version';
@@ -0,0 +1,373 @@
1
+ -- Migration 0020: extension runtime state, the `extension` actor, and base views for the schema
2
+ -- reconciler (stage 3, ADR 0023, ADR 0024, ADR 0025, ADR 0040).
3
+ --
4
+ -- This migration touches core tables only. The x_* tables and the crm_* views that join them are
5
+ -- owned by the reconciler (packages/core/src/extensions/reconciler.ts), never by migrations.
6
+
7
+ -- 1. The audit log accepts actor `extension` (calls made by extension code through
8
+ -- codemode:crm). SQLite cannot alter a CHECK constraint, so core_audit is rebuilt with the
9
+ -- same rows. Its children (core_audit_changes, core_audit_redactions) reference it by name;
10
+ -- foreign keys are deferred so the swap is checked once, at commit. The redaction trigger
11
+ -- that writes core_audit is dropped first and recreated after, so the rename sees a
12
+ -- consistent schema.
13
+ PRAGMA defer_foreign_keys = ON;
14
+
15
+ DROP TRIGGER core_audit_redactions_apply_audit;
16
+
17
+ CREATE TABLE core_audit_next (
18
+ id TEXT PRIMARY KEY NOT NULL,
19
+ at TEXT NOT NULL,
20
+ operation TEXT NOT NULL,
21
+ risk TEXT NOT NULL
22
+ CHECK (risk IN ('read', 'write', 'bulk-write', 'destructive', 'schema', 'extension', 'outbound')),
23
+ actor TEXT NOT NULL CHECK (actor IN ('human', 'assistant', 'mcp', 'automation', 'extension')),
24
+ user_id TEXT,
25
+ origin TEXT NOT NULL CHECK (json_valid(origin)),
26
+ input TEXT NOT NULL CHECK (json_valid(input)),
27
+ result TEXT CHECK (result IS NULL OR json_valid(result)),
28
+ gate_decision TEXT NOT NULL CHECK (gate_decision IN ('auto', 'review', 'deny')),
29
+ outcome TEXT NOT NULL CHECK (outcome IN ('ok', 'forbidden', 'denied', 'review', 'error')),
30
+ error TEXT CHECK (error IS NULL OR json_valid(error)),
31
+ -- Migration 0010: the pending action an audit row queued or applied.
32
+ pending_action_id TEXT
33
+ );
34
+
35
+ INSERT INTO core_audit_next
36
+ (id, at, operation, risk, actor, user_id, origin, input, result, gate_decision, outcome, error,
37
+ pending_action_id)
38
+ SELECT id, at, operation, risk, actor, user_id, origin, input, result, gate_decision, outcome, error,
39
+ pending_action_id
40
+ FROM core_audit;
41
+
42
+ DROP TABLE core_audit;
43
+ ALTER TABLE core_audit_next RENAME TO core_audit;
44
+
45
+ CREATE INDEX core_audit_at_idx ON core_audit (at, id);
46
+ CREATE INDEX core_audit_user_idx ON core_audit (user_id, at);
47
+ CREATE INDEX core_audit_operation_idx ON core_audit (operation, at);
48
+ CREATE INDEX core_audit_actor_idx ON core_audit (actor, at);
49
+ CREATE INDEX core_audit_pending_action_idx ON core_audit (pending_action_id)
50
+ WHERE pending_action_id IS NOT NULL;
51
+
52
+ -- The approval queue (migration 0010) accepts actor `extension` too: a gate `review` on an
53
+ -- extension's call queues a pending action like any other. Same rebuild, same deferred keys.
54
+ CREATE TABLE core_change_set_next (
55
+ id TEXT PRIMARY KEY NOT NULL,
56
+ title TEXT,
57
+ session_id TEXT,
58
+ client_id TEXT,
59
+ client_name TEXT,
60
+ user_id TEXT NOT NULL,
61
+ actor TEXT NOT NULL CHECK (actor IN ('human', 'assistant', 'mcp', 'automation', 'extension')),
62
+ origin TEXT NOT NULL CHECK (json_valid(origin)),
63
+ explicit INTEGER NOT NULL DEFAULT 0 CHECK (explicit IN (0, 1)),
64
+ state TEXT NOT NULL DEFAULT 'open' CHECK (state IN ('open', 'committed', 'closed')),
65
+ created_at TEXT NOT NULL,
66
+ updated_at TEXT NOT NULL,
67
+ committed_at TEXT,
68
+ closed_at TEXT
69
+ );
70
+ INSERT INTO core_change_set_next SELECT * FROM core_change_set;
71
+ DROP TABLE core_change_set;
72
+ ALTER TABLE core_change_set_next RENAME TO core_change_set;
73
+ CREATE INDEX core_change_set_session_idx ON core_change_set (session_id, state, created_at);
74
+ CREATE INDEX core_change_set_state_idx ON core_change_set (state, created_at);
75
+ CREATE INDEX core_change_set_user_idx ON core_change_set (user_id, created_at);
76
+
77
+ CREATE TABLE core_pending_action_next (
78
+ id TEXT PRIMARY KEY NOT NULL,
79
+ change_set_id TEXT NOT NULL REFERENCES core_change_set (id),
80
+ operation TEXT NOT NULL,
81
+ risk TEXT NOT NULL
82
+ CHECK (risk IN ('read', 'write', 'bulk-write', 'destructive', 'schema', 'extension', 'outbound')),
83
+ input TEXT NOT NULL CHECK (json_valid(input)),
84
+ reason TEXT NOT NULL,
85
+ actor TEXT NOT NULL CHECK (actor IN ('human', 'assistant', 'mcp', 'automation', 'extension')),
86
+ user_id TEXT NOT NULL,
87
+ origin TEXT NOT NULL CHECK (json_valid(origin)),
88
+ granted_permissions TEXT CHECK (granted_permissions IS NULL OR json_valid(granted_permissions)),
89
+ bulk_size INTEGER NOT NULL DEFAULT 1,
90
+ preview TEXT NOT NULL CHECK (json_valid(preview)),
91
+ simulated TEXT NOT NULL CHECK (json_valid(simulated)),
92
+ provisional TEXT NOT NULL DEFAULT '[]' CHECK (json_valid(provisional)),
93
+ depends_on TEXT NOT NULL DEFAULT '[]' CHECK (json_valid(depends_on)),
94
+ status TEXT NOT NULL DEFAULT 'pending'
95
+ CHECK (status IN ('pending', 'applied', 'failed', 'skipped', 'rejected', 'expired')),
96
+ status_reason TEXT,
97
+ error TEXT CHECK (error IS NULL OR json_valid(error)),
98
+ bindings TEXT CHECK (bindings IS NULL OR json_valid(bindings)),
99
+ result TEXT CHECK (result IS NULL OR json_valid(result)),
100
+ request_audit_id TEXT,
101
+ applied_audit_id TEXT,
102
+ decided_by TEXT,
103
+ decided_at TEXT,
104
+ created_at TEXT NOT NULL,
105
+ expires_at TEXT NOT NULL
106
+ );
107
+ INSERT INTO core_pending_action_next SELECT * FROM core_pending_action;
108
+ DROP TABLE core_pending_action;
109
+ ALTER TABLE core_pending_action_next RENAME TO core_pending_action;
110
+ CREATE INDEX core_pending_action_set_idx ON core_pending_action (change_set_id, id);
111
+ CREATE INDEX core_pending_action_status_idx ON core_pending_action (status, expires_at);
112
+
113
+ CREATE TRIGGER core_audit_no_update BEFORE UPDATE ON core_audit
114
+ WHEN NOT (
115
+ EXISTS (SELECT 1 FROM core_audit_redactions r WHERE r.audit_id = OLD.id)
116
+ AND NEW.id IS OLD.id
117
+ AND NEW.at IS OLD.at
118
+ AND NEW.operation IS OLD.operation
119
+ AND NEW.risk IS OLD.risk
120
+ AND NEW.actor IS OLD.actor
121
+ AND NEW.user_id IS OLD.user_id
122
+ AND NEW.origin IS OLD.origin
123
+ AND NEW.gate_decision IS OLD.gate_decision
124
+ AND NEW.outcome IS OLD.outcome
125
+ AND NEW.input IS '"[erased]"'
126
+ AND NEW.result IS (CASE WHEN OLD.result IS NULL THEN NULL ELSE '"[erased]"' END)
127
+ AND NEW.error IS (CASE WHEN OLD.error IS NULL THEN NULL
128
+ ELSE json_object('code', json_extract(OLD.error, '$.code'), 'message', '[erased]') END)
129
+ )
130
+ BEGIN
131
+ SELECT RAISE(ABORT, 'core_audit is append-only');
132
+ END;
133
+
134
+ CREATE TRIGGER core_audit_no_delete BEFORE DELETE ON core_audit
135
+ BEGIN
136
+ SELECT RAISE(ABORT, 'core_audit is append-only');
137
+ END;
138
+
139
+ CREATE TRIGGER core_audit_redactions_apply_audit AFTER INSERT ON core_audit_redactions
140
+ WHEN NEW.audit_id IS NOT NULL
141
+ BEGIN
142
+ UPDATE core_audit SET
143
+ input = '"[erased]"',
144
+ result = CASE WHEN result IS NULL THEN NULL ELSE '"[erased]"' END,
145
+ error = CASE WHEN error IS NULL THEN NULL
146
+ ELSE json_object('code', json_extract(error, '$.code'), 'message', '[erased]') END
147
+ WHERE id = NEW.audit_id;
148
+ END;
149
+
150
+ -- 2. Extension principals. Each installed extension acts as a core_user row of kind
151
+ -- `extension` (id `extension:<id>`), so created_by/owner columns and the audit log can name
152
+ -- it. They cannot sign in (no account), are never the first admin, and are not listed.
153
+ ALTER TABLE core_user ADD COLUMN kind TEXT NOT NULL DEFAULT 'human'
154
+ CHECK (kind IN ('human', 'extension'));
155
+
156
+ DROP TRIGGER core_user_first_admin;
157
+ CREATE TRIGGER core_user_first_admin
158
+ AFTER INSERT ON core_user
159
+ WHEN NEW.kind = 'human' AND (SELECT COUNT(*) FROM core_user WHERE kind = 'human') = 1
160
+ BEGIN
161
+ UPDATE core_user SET role = 'admin' WHERE id = NEW.id;
162
+ END;
163
+
164
+ DROP VIEW crm_users;
165
+ CREATE VIEW crm_users AS
166
+ SELECT u.id, u.name, u.email, u.role, u.image, u.created_at
167
+ FROM core_user u
168
+ WHERE u.kind = 'human';
169
+
170
+ -- 3. Base read views for core objects. crm_people, crm_organisations, crm_activities and
171
+ -- crm_tasks become `SELECT b.* FROM core_base_<x> b` plus LEFT JOINs to extension side
172
+ -- tables, regenerated by the reconciler. Core migrations change core_base_* (never the crm_*
173
+ -- views of these four objects); SQLite expands `b.*` when the view is used, so new core
174
+ -- columns appear in crm_* without a reconcile. query.sql cannot name core_base_* (core_
175
+ -- prefix), only the crm_* views over them.
176
+ -- The bodies are the 0007 definitions.
177
+ CREATE VIEW core_base_people AS
178
+ SELECT
179
+ person.id,
180
+ person.display_name,
181
+ person.given_name,
182
+ person.family_name,
183
+ (SELECT e.email FROM core_person_emails e
184
+ WHERE e.person_id = person.id AND e.is_primary = 1) AS primary_email,
185
+ (SELECT json_group_array(e.email) FROM
186
+ (SELECT email FROM core_person_emails WHERE person_id = person.id ORDER BY position) e) AS emails,
187
+ (SELECT ph.number FROM core_person_phones ph
188
+ WHERE ph.person_id = person.id AND ph.is_primary = 1) AS primary_phone,
189
+ aff.id AS primary_affiliation_id,
190
+ aff.title AS job_title,
191
+ aff.organisation_id AS organisation_id,
192
+ org.name AS organisation_name,
193
+ person.location,
194
+ person.timezone,
195
+ person.owner_id,
196
+ person.avatar_key,
197
+ (SELECT json_group_array(t.name) FROM
198
+ (SELECT tg.name FROM core_record_tags rt JOIN core_tags tg ON tg.id = rt.tag_id
199
+ WHERE rt.object_type = 'person' AND rt.record_id = person.id ORDER BY tg.name) t) AS tags,
200
+ person.created_at,
201
+ person.updated_at
202
+ FROM core_person person
203
+ LEFT JOIN core_affiliation aff
204
+ ON aff.person_id = person.id AND aff.is_primary = 1 AND aff.deleted_at IS NULL
205
+ LEFT JOIN core_organisation org
206
+ ON org.id = aff.organisation_id AND org.deleted_at IS NULL
207
+ WHERE person.deleted_at IS NULL;
208
+
209
+ CREATE VIEW core_base_organisations AS
210
+ SELECT
211
+ organisation.id,
212
+ organisation.name,
213
+ (SELECT d.domain FROM core_organisation_domains d
214
+ WHERE d.organisation_id = organisation.id AND d.is_primary = 1) AS primary_domain,
215
+ (SELECT json_group_array(d.domain) FROM
216
+ (SELECT domain FROM core_organisation_domains WHERE organisation_id = organisation.id
217
+ ORDER BY position) d) AS domains,
218
+ organisation.website,
219
+ organisation.industry,
220
+ organisation.parent_id,
221
+ parent.name AS parent_name,
222
+ organisation.owner_id,
223
+ organisation.avatar_key,
224
+ (SELECT COUNT(*) FROM core_affiliation a JOIN core_person p ON p.id = a.person_id
225
+ WHERE a.organisation_id = organisation.id AND a.is_current = 1
226
+ AND a.deleted_at IS NULL AND p.deleted_at IS NULL) AS current_people_count,
227
+ (SELECT json_group_array(t.name) FROM
228
+ (SELECT tg.name FROM core_record_tags rt JOIN core_tags tg ON tg.id = rt.tag_id
229
+ WHERE rt.object_type = 'organisation' AND rt.record_id = organisation.id
230
+ ORDER BY tg.name) t) AS tags,
231
+ organisation.created_at,
232
+ organisation.updated_at
233
+ FROM core_organisation organisation
234
+ LEFT JOIN core_organisation parent
235
+ ON parent.id = organisation.parent_id AND parent.deleted_at IS NULL
236
+ WHERE organisation.deleted_at IS NULL;
237
+
238
+ CREATE VIEW core_base_activities AS
239
+ SELECT
240
+ activity.id,
241
+ activity.type,
242
+ activity.subject,
243
+ activity.body,
244
+ activity.payload,
245
+ activity.occurred_at,
246
+ activity.author_user_id,
247
+ activity.author_actor,
248
+ activity.created_at,
249
+ activity.updated_at
250
+ FROM core_activity activity
251
+ WHERE activity.deleted_at IS NULL;
252
+
253
+ CREATE VIEW core_base_tasks AS
254
+ SELECT
255
+ task.id,
256
+ task.title,
257
+ task.notes,
258
+ task.due_at,
259
+ task.status,
260
+ task.completed_at,
261
+ task.assignee_id,
262
+ task.created_by,
263
+ task.created_at,
264
+ task.updated_at
265
+ FROM core_task task
266
+ WHERE task.deleted_at IS NULL;
267
+
268
+ DROP VIEW crm_people;
269
+ CREATE VIEW crm_people AS SELECT b.* FROM core_base_people b;
270
+ DROP VIEW crm_organisations;
271
+ CREATE VIEW crm_organisations AS SELECT b.* FROM core_base_organisations b;
272
+ DROP VIEW crm_activities;
273
+ CREATE VIEW crm_activities AS SELECT b.* FROM core_base_activities b;
274
+ DROP VIEW crm_tasks;
275
+ CREATE VIEW crm_tasks AS SELECT b.* FROM core_base_tasks b;
276
+
277
+ -- 4. Extension state (ADR 0025). One row per installed extension; its manifest is the JSON the
278
+ -- sandbox produced from its source at `commit_oid` in the extension workspace.
279
+ CREATE TABLE core_extensions (
280
+ id TEXT PRIMARY KEY NOT NULL,
281
+ source TEXT NOT NULL CHECK (source IN ('shipped', 'runtime')),
282
+ status TEXT NOT NULL CHECK (status IN ('active', 'disabled', 'quarantined')),
283
+ status_reason TEXT,
284
+ version TEXT NOT NULL,
285
+ commit_oid TEXT NOT NULL,
286
+ -- The last version that ran without failing, offered for rollback on quarantine.
287
+ last_good_commit TEXT,
288
+ manifest TEXT NOT NULL CHECK (json_valid(manifest)),
289
+ -- Consecutive failed runs; 3 quarantines the extension.
290
+ failure_count INTEGER NOT NULL DEFAULT 0,
291
+ -- The extension principal (core_user kind 'extension').
292
+ user_id TEXT NOT NULL REFERENCES core_user (id),
293
+ installed_at TEXT NOT NULL,
294
+ installed_by TEXT REFERENCES core_user (id) ON DELETE SET NULL,
295
+ updated_at TEXT NOT NULL
296
+ );
297
+
298
+ -- Every version ever installed (a commit in the workspace), for history and rollback.
299
+ CREATE TABLE core_extension_versions (
300
+ extension_id TEXT NOT NULL REFERENCES core_extensions (id) ON DELETE CASCADE,
301
+ commit_oid TEXT NOT NULL,
302
+ version TEXT NOT NULL,
303
+ manifest TEXT NOT NULL CHECK (json_valid(manifest)),
304
+ installed_at TEXT NOT NULL,
305
+ installed_by TEXT REFERENCES core_user (id) ON DELETE SET NULL,
306
+ PRIMARY KEY (extension_id, commit_oid)
307
+ );
308
+
309
+ -- Run history: one row per execution of extension code (tool calls, manifest evaluation).
310
+ CREATE TABLE core_extension_runs (
311
+ id TEXT PRIMARY KEY NOT NULL,
312
+ extension_id TEXT NOT NULL,
313
+ commit_oid TEXT,
314
+ kind TEXT NOT NULL CHECK (kind IN ('tool', 'manifest', 'automation')),
315
+ name TEXT,
316
+ actor TEXT,
317
+ user_id TEXT,
318
+ started_at TEXT NOT NULL,
319
+ duration_ms INTEGER NOT NULL,
320
+ outcome TEXT NOT NULL CHECK (outcome IN ('ok', 'error', 'timeout')),
321
+ error TEXT CHECK (error IS NULL OR json_valid(error)),
322
+ logs TEXT NOT NULL DEFAULT '[]' CHECK (json_valid(logs)),
323
+ operations TEXT NOT NULL DEFAULT '[]' CHECK (json_valid(operations))
324
+ );
325
+ CREATE INDEX core_extension_runs_ext_idx ON core_extension_runs (extension_id, started_at);
326
+
327
+ -- codemode:storage: per-extension key-value storage.
328
+ CREATE TABLE core_extension_kv (
329
+ extension_id TEXT NOT NULL,
330
+ key TEXT NOT NULL CHECK (length(key) BETWEEN 1 AND 512),
331
+ value TEXT NOT NULL CHECK (json_valid(value)),
332
+ updated_at TEXT NOT NULL,
333
+ PRIMARY KEY (extension_id, key)
334
+ );
335
+
336
+ -- Reconciler state (ADR 0024): the physical x_* tables and columns behind declared objects and
337
+ -- fields. Removed objects and fields are `hidden` (data kept) until dropped explicitly.
338
+ CREATE TABLE core_extension_objects (
339
+ name TEXT PRIMARY KEY NOT NULL,
340
+ extension_id TEXT NOT NULL,
341
+ local_name TEXT NOT NULL,
342
+ table_name TEXT NOT NULL UNIQUE,
343
+ status TEXT NOT NULL CHECK (status IN ('active', 'hidden')),
344
+ hidden_at TEXT,
345
+ definition TEXT NOT NULL CHECK (json_valid(definition)),
346
+ created_at TEXT NOT NULL,
347
+ updated_at TEXT NOT NULL
348
+ );
349
+
350
+ CREATE TABLE core_extension_fields (
351
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
352
+ extension_id TEXT NOT NULL,
353
+ -- Registry name of the object the field belongs to: `organisation`, `example.project`.
354
+ object TEXT NOT NULL,
355
+ -- Name as declared in the extension.
356
+ local_name TEXT NOT NULL,
357
+ -- Registry field name: the local name on the extension's own objects, `<ext>.<name>` elsewhere.
358
+ name TEXT NOT NULL,
359
+ table_name TEXT NOT NULL,
360
+ -- NULL for formula fields (computed in the crm_* view).
361
+ column_name TEXT,
362
+ type TEXT NOT NULL,
363
+ definition TEXT NOT NULL CHECK (json_valid(definition)),
364
+ status TEXT NOT NULL CHECK (status IN ('active', 'hidden')),
365
+ hidden_at TEXT,
366
+ created_at TEXT NOT NULL,
367
+ updated_at TEXT NOT NULL
368
+ );
369
+ CREATE UNIQUE INDEX core_extension_fields_name_uq ON core_extension_fields (object, name);
370
+ CREATE UNIQUE INDEX core_extension_fields_column_uq ON core_extension_fields (table_name, column_name)
371
+ WHERE column_name IS NOT NULL;
372
+
373
+ UPDATE core_meta SET value = '20' WHERE key = 'schema_version';
@@ -0,0 +1,29 @@
1
+ -- Migration 0030: extension proposals (stage 3b, ADR 0041).
2
+ --
3
+ -- `extension.propose` commits an extension's working copy and queues one pending action
4
+ -- (operation `extension.apply`, risk `extension`) in the caller's change set. What a reviewer
5
+ -- needs to judge it lives here, keyed by that pending action: the per-file git diff from the
6
+ -- installed version, the host-computed impact (schema changes, permissions requested or
7
+ -- widened, host modules, triggers), the check result and the AI-written summary (model role
8
+ -- `build`). Core tables only; written by trusted host code.
9
+ CREATE TABLE core_extension_proposals (
10
+ action_id TEXT PRIMARY KEY NOT NULL REFERENCES core_pending_action (id),
11
+ extension_id TEXT NOT NULL,
12
+ commit_oid TEXT NOT NULL,
13
+ -- The installed commit the proposal was diffed against (null: not installed yet).
14
+ base_commit TEXT,
15
+ source TEXT NOT NULL CHECK (source IN ('shipped', 'runtime')),
16
+ version TEXT NOT NULL,
17
+ message TEXT NOT NULL,
18
+ diff TEXT NOT NULL CHECK (json_valid(diff)),
19
+ impact TEXT NOT NULL CHECK (json_valid(impact)),
20
+ check_result TEXT NOT NULL CHECK (json_valid(check_result)),
21
+ -- The `build` model role's plain-English summary, or null when no AI is configured.
22
+ ai_summary TEXT,
23
+ ai_model TEXT,
24
+ created_by TEXT NOT NULL,
25
+ created_at TEXT NOT NULL
26
+ );
27
+ CREATE INDEX core_extension_proposals_ext_idx ON core_extension_proposals (extension_id, created_at);
28
+
29
+ UPDATE core_meta SET value = '30' WHERE key = 'schema_version';
@@ -0,0 +1,32 @@
1
+ -- Migration 0050: partial Trash indexes, so list pages stop scanning every live row
2
+ -- (docs/research/cloudflare-cost-audit.md).
3
+ --
4
+ -- The 0003/0004 indexes on deleted_at covered every row. D1 databases carry no ANALYZE statistics,
5
+ -- so the planner took `deleted_at IS NULL` (true for nearly every row) as selective. It walked
6
+ -- the whole table through that index, computed the crm_* views' correlated subqueries for every
7
+ -- row, then sorted. The display-name, name and occurred-at indexes went unused, and so did the
8
+ -- primary key for `id IN (...)` lookups.
9
+ --
10
+ -- A default 50-row person.list on 20k people read about 82k rows. search.global's hydration
11
+ -- read about 20k rows per query.
12
+ --
13
+ -- Trash queries always say `deleted_at IS NOT NULL` or `deleted_at < ?`, so partial indexes still
14
+ -- serve them, and the planner can no longer pick them for live-row reads.
15
+
16
+ DROP INDEX core_person_deleted_idx;
17
+ CREATE INDEX core_person_deleted_idx ON core_person (deleted_at) WHERE deleted_at IS NOT NULL;
18
+
19
+ DROP INDEX core_organisation_deleted_idx;
20
+ CREATE INDEX core_organisation_deleted_idx ON core_organisation (deleted_at)
21
+ WHERE deleted_at IS NOT NULL;
22
+
23
+ DROP INDEX core_activity_deleted_idx;
24
+ CREATE INDEX core_activity_deleted_idx ON core_activity (deleted_at) WHERE deleted_at IS NOT NULL;
25
+
26
+ DROP INDEX core_task_deleted_idx;
27
+ CREATE INDEX core_task_deleted_idx ON core_task (deleted_at) WHERE deleted_at IS NOT NULL;
28
+
29
+ -- task.list's default order (due date, then id) had no index, so every page sorted all tasks.
30
+ CREATE INDEX core_task_due_at_idx ON core_task (due_at, id);
31
+
32
+ UPDATE core_meta SET value = '50' WHERE key = 'schema_version';
@@ -0,0 +1,31 @@
1
+ -- Migration 0060: hourly read counts instead of one audit row per non-human read (ADR 0090,
2
+ -- docs/research/cloudflare-cost-audit.md finding F6, decision D3).
3
+ --
4
+ -- Before this, every successful read by an MCP client, extension, automation or the assistant
5
+ -- wrote a core_audit row: about 6 rows written (5 indexes) and ~1 KB stored per read. Successful
6
+ -- non-human reads are now counted here instead, one row per hour, actor, user, caller and
7
+ -- operation, upserted. Writes, refusals and failed reads still write full core_audit rows.
8
+ -- Human reads are still not recorded at all.
9
+ --
10
+ -- Rows hold ids and counts only (no inputs), so an erase (ADR 0019) has nothing to redact here.
11
+ -- They are not append-only: an upsert has to update the count. The audit retention purge
12
+ -- (migration 0061) deletes hours older than the retention window.
13
+ CREATE TABLE core_audit_read_counts (
14
+ -- The UTC hour, 'YYYY-MM-DDTHH:00:00.000Z'.
15
+ hour TEXT NOT NULL,
16
+ actor TEXT NOT NULL CHECK (actor IN ('human', 'assistant', 'mcp', 'automation', 'extension')),
17
+ user_id TEXT NOT NULL,
18
+ -- Who exactly, within the actor: the connected app (cap_…) for MCP, the extension id, the
19
+ -- automation id, else the MCP client id, else ''.
20
+ caller TEXT NOT NULL DEFAULT '',
21
+ operation TEXT NOT NULL,
22
+ count INTEGER NOT NULL DEFAULT 0,
23
+ -- How many of them saw the session's simulated pending changes (ADR 0032).
24
+ simulated INTEGER NOT NULL DEFAULT 0,
25
+ first_at TEXT NOT NULL,
26
+ last_at TEXT NOT NULL,
27
+ PRIMARY KEY (hour, actor, user_id, caller, operation)
28
+ ) WITHOUT ROWID;
29
+ CREATE INDEX core_audit_read_counts_user_idx ON core_audit_read_counts (user_id, hour);
30
+
31
+ UPDATE core_meta SET value = '60' WHERE key = 'schema_version';