gabriel / musehub public
0002_v2_domains.py python
306 lines 15.1 KB
7f1d07e8 feat: domains, MCP expansion, MIDI player, and production hardening (#8) Gabriel Cardona <cgcardona@gmail.com> 4d ago
1 """MuseHub V2 — Domain-agnostic paradigm shift.
2
3 Revision ID: 0002
4 Revises: 0001
5 Create Date: 2026-03-18
6
7 Adds the Muse domain plugin registry and makes all existing tables
8 domain-agnostic.
9
10 Changes:
11 NEW TABLES
12 - musehub_domains: Muse domain plugin registry (@author/slug namespace)
13 - musehub_domain_installs: User ↔ domain adoption tracking
14
15 MUSEHUB_REPOS
16 - ADD domain_id (nullable FK → musehub_domains)
17 - ADD domain_meta JSON (replaces key_signature + tempo_bpm)
18 - DROP key_signature
19 - DROP tempo_bpm
20
21 MUSEHUB_PR_COMMENTS
22 - ADD dimension_ref JSON (domain-agnostic replacement for music-specific fields)
23 - DROP target_type
24 - DROP target_track
25 - DROP target_beat_start
26 - DROP target_beat_end
27 - DROP target_note_pitch
28
29 MUSEHUB_ISSUE_COMMENTS
30 - RENAME musical_refs → state_refs
31
32 MUSEHUB_RENDER_JOBS
33 - RENAME midi_count → artifact_count
34 - RENAME mp3_object_ids → audio_object_ids
35 - RENAME image_object_ids → preview_object_ids
36
37 SEED DATA
38 - Insert @cgcardona/midi built-in domain (21-dimensional MIDI)
39 - Insert @cgcardona/code built-in domain (symbol-graph code)
40 """
41 from __future__ import annotations
42
43 import json
44 import hashlib
45
46 import sqlalchemy as sa
47 from alembic import op
48
49 revision = "0002"
50 down_revision = "0001"
51 branch_labels = None
52 depends_on = None
53
54
55 def _manifest_hash(capabilities: dict) -> str:
56 """Compute SHA-256 of the capabilities JSON (sorted keys)."""
57 blob = json.dumps(capabilities, sort_keys=True, separators=(",", ":")).encode()
58 return hashlib.sha256(blob).hexdigest()
59
60
61 # Capability manifests for the two built-in domains
62 _MIDI_CAPABILITIES = {
63 "dimensions": [
64 {"name": "harmony", "description": "Chord progressions and tonal centre analysis"},
65 {"name": "rhythm", "description": "Rhythmic density, groove, and syncopation"},
66 {"name": "melody", "description": "Melodic contour, range, and motif detection"},
67 {"name": "dynamics", "description": "Velocity curves and dynamic range"},
68 {"name": "structure", "description": "Section form and macro structure"},
69 {"name": "tempo", "description": "BPM and tempo map evolution"},
70 {"name": "key", "description": "Key signature detection and modulation"},
71 {"name": "meter", "description": "Time signature and metric complexity"},
72 {"name": "groove", "description": "Swing, feel, and micro-timing"},
73 {"name": "emotion", "description": "8-axis valence/arousal emotion map"},
74 {"name": "motifs", "description": "Recurring melodic and rhythmic motifs"},
75 {"name": "form", "description": "Formal structure (AABA, verse/chorus, etc.)"},
76 {"name": "pitch_bend", "description": "Pitch bend envelope per channel"},
77 {"name": "aftertouch", "description": "Polyphonic aftertouch per note"},
78 {"name": "modulation", "description": "CC1 modulation wheel data"},
79 {"name": "volume", "description": "CC7 channel volume envelopes"},
80 {"name": "pan", "description": "CC10 stereo panning"},
81 {"name": "expression", "description": "CC11 expression controller"},
82 {"name": "sustain", "description": "CC64 sustain pedal events"},
83 {"name": "reverb", "description": "CC91 reverb send levels"},
84 {"name": "chorus", "description": "CC93 chorus send levels"},
85 ],
86 "viewer_type": "piano_roll",
87 "artifact_types": ["audio/midi", "audio/mpeg", "image/webp"],
88 "merge_semantics": "ot",
89 "supported_commands": [
90 "muse analyze", "muse diff", "muse listen", "muse arrange",
91 "muse piano-roll", "muse groove-check", "muse emotion-diff",
92 ],
93 }
94
95 _CODE_CAPABILITIES = {
96 "dimensions": [
97 {"name": "symbols", "description": "Function and class symbol graph"},
98 {"name": "hotspots", "description": "Most frequently changed symbols"},
99 {"name": "coupling", "description": "Symbol-level coupling and cohesion"},
100 {"name": "complexity", "description": "Cyclomatic complexity per symbol"},
101 {"name": "churn", "description": "Commit frequency per file and symbol"},
102 {"name": "coverage", "description": "Test coverage by symbol"},
103 {"name": "dependencies", "description": "Import and dependency graph"},
104 {"name": "duplicates", "description": "Semantically duplicated code blocks"},
105 {"name": "refactors", "description": "Detected rename and move operations"},
106 {"name": "types", "description": "Type annotation completeness"},
107 ],
108 "viewer_type": "symbol_graph",
109 "artifact_types": [
110 "text/x-python", "text/typescript", "text/javascript",
111 "text/x-go", "text/x-rust", "text/x-java",
112 ],
113 "merge_semantics": "ot",
114 "supported_commands": [
115 "muse symbols", "muse hotspots", "muse coupling", "muse diff",
116 "muse query", "muse refactor",
117 ],
118 }
119
120
121 def upgrade() -> None:
122 # ── musehub_domains ───────────────────────────────────────────────────────
123 op.create_table(
124 "musehub_domains",
125 sa.Column("domain_id", sa.String(36), nullable=False),
126 sa.Column("author_user_id", sa.String(36), nullable=True),
127 sa.Column("author_slug", sa.String(64), nullable=False),
128 sa.Column("slug", sa.String(64), nullable=False),
129 sa.Column("display_name", sa.String(255), nullable=False),
130 sa.Column("description", sa.Text(), nullable=False, server_default=""),
131 sa.Column("version", sa.String(32), nullable=False, server_default="1.0.0"),
132 sa.Column("manifest_hash", sa.String(64), nullable=False, server_default=""),
133 sa.Column("capabilities", sa.JSON(), nullable=False),
134 sa.Column("viewer_type", sa.String(64), nullable=False, server_default="generic"),
135 sa.Column("install_count", sa.Integer(), nullable=False, server_default="0"),
136 sa.Column("is_verified", sa.Boolean(), nullable=False, server_default="false"),
137 sa.Column("is_deprecated", sa.Boolean(), nullable=False, server_default="false"),
138 sa.Column("created_at", sa.DateTime(timezone=True), nullable=False,
139 server_default=sa.text("CURRENT_TIMESTAMP")),
140 sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False,
141 server_default=sa.text("CURRENT_TIMESTAMP")),
142 sa.PrimaryKeyConstraint("domain_id"),
143 sa.UniqueConstraint("author_slug", "slug", name="uq_musehub_domains_author_slug"),
144 )
145 op.create_index("ix_musehub_domains_author_slug", "musehub_domains", ["author_slug"])
146 op.create_index("ix_musehub_domains_slug", "musehub_domains", ["slug"])
147 op.create_index("ix_musehub_domains_author_user_id", "musehub_domains", ["author_user_id"])
148
149 # ── musehub_domain_installs ───────────────────────────────────────────────
150 op.create_table(
151 "musehub_domain_installs",
152 sa.Column("install_id", sa.String(36), nullable=False),
153 sa.Column("user_id", sa.String(36), nullable=False),
154 sa.Column("domain_id", sa.String(36), nullable=False),
155 sa.Column("created_at", sa.DateTime(timezone=True), nullable=False,
156 server_default=sa.text("CURRENT_TIMESTAMP")),
157 sa.PrimaryKeyConstraint("install_id"),
158 sa.UniqueConstraint("user_id", "domain_id", name="uq_musehub_domain_installs"),
159 )
160 op.create_index("ix_musehub_domain_installs_user_id", "musehub_domain_installs", ["user_id"])
161 op.create_index("ix_musehub_domain_installs_domain_id", "musehub_domain_installs", ["domain_id"])
162
163 # ── musehub_repos: add domain_id + domain_meta, drop key_signature + tempo_bpm ──
164 op.add_column("musehub_repos",
165 sa.Column("domain_id", sa.String(36), nullable=True))
166 op.add_column("musehub_repos",
167 sa.Column("domain_meta", sa.JSON(), nullable=False, server_default="{}"))
168 op.create_index("ix_musehub_repos_domain_id", "musehub_repos", ["domain_id"])
169 op.drop_column("musehub_repos", "key_signature")
170 op.drop_column("musehub_repos", "tempo_bpm")
171
172 # ── musehub_pr_comments: add dimension_ref, drop music-specific fields ───
173 op.add_column("musehub_pr_comments",
174 sa.Column("dimension_ref", sa.JSON(), nullable=False, server_default="{}"))
175 op.drop_column("musehub_pr_comments", "target_type")
176 op.drop_column("musehub_pr_comments", "target_track")
177 op.drop_column("musehub_pr_comments", "target_beat_start")
178 op.drop_column("musehub_pr_comments", "target_beat_end")
179 op.drop_column("musehub_pr_comments", "target_note_pitch")
180
181 # ── musehub_issue_comments: rename musical_refs → state_refs ─────────────
182 op.add_column("musehub_issue_comments",
183 sa.Column("state_refs", sa.JSON(), nullable=False, server_default="[]"))
184 # Copy existing data to new column
185 op.execute(
186 "UPDATE musehub_issue_comments SET state_refs = musical_refs"
187 )
188 op.drop_column("musehub_issue_comments", "musical_refs")
189
190 # ── musehub_render_jobs: rename domain-specific column names ──────────────
191 op.add_column("musehub_render_jobs",
192 sa.Column("artifact_count", sa.Integer(), nullable=False, server_default="0"))
193 op.add_column("musehub_render_jobs",
194 sa.Column("audio_object_ids", sa.JSON(), nullable=False, server_default="[]"))
195 op.add_column("musehub_render_jobs",
196 sa.Column("preview_object_ids", sa.JSON(), nullable=False, server_default="[]"))
197 # Copy existing data
198 op.execute("UPDATE musehub_render_jobs SET artifact_count = midi_count")
199 op.execute("UPDATE musehub_render_jobs SET audio_object_ids = mp3_object_ids")
200 op.execute("UPDATE musehub_render_jobs SET preview_object_ids = image_object_ids")
201 op.drop_column("musehub_render_jobs", "midi_count")
202 op.drop_column("musehub_render_jobs", "mp3_object_ids")
203 op.drop_column("musehub_render_jobs", "image_object_ids")
204
205 # ── Seed built-in domains ─────────────────────────────────────────────────
206 # Use stable IDs so seed_v2.py and other tooling can reference them by
207 # known values without querying the DB. These are NOT real UUIDs but are
208 # valid VARCHAR(36) strings that match the DOMAIN_MIDI / DOMAIN_CODE
209 # constants in scripts/seed_v2.py.
210 _DOMAIN_MIDI_ID = "domain-midi-cgcardona-0001"
211 _DOMAIN_CODE_ID = "domain-code-cgcardona-0001"
212
213 midi_caps_json = json.dumps(_MIDI_CAPABILITIES)
214 code_caps_json = json.dumps(_CODE_CAPABILITIES)
215
216 op.execute(
217 sa.text(
218 "INSERT INTO musehub_domains "
219 "(domain_id, author_user_id, author_slug, slug, display_name, description, "
220 "version, manifest_hash, capabilities, viewer_type, install_count, "
221 "is_verified, is_deprecated, created_at, updated_at) "
222 "VALUES (:did, NULL, 'cgcardona', 'midi', 'MIDI', "
223 "'21-dimensional MIDI state space — notes, pitch bend, 11 CC controllers, "
224 "tempo map, time signatures, key signatures, and more. The reference "
225 "implementation of the MuseDomainPlugin protocol.', "
226 f"'1.0.0', :mhash, CAST('{midi_caps_json.replace(chr(39), chr(39)+chr(39))}' AS json), "
227 "'piano_roll', 0, true, false, now(), now()) "
228 "ON CONFLICT (author_slug, slug) DO NOTHING"
229 ).bindparams(
230 did=_DOMAIN_MIDI_ID,
231 mhash=_manifest_hash(_MIDI_CAPABILITIES),
232 )
233 )
234
235 op.execute(
236 sa.text(
237 "INSERT INTO musehub_domains "
238 "(domain_id, author_user_id, author_slug, slug, display_name, description, "
239 "version, manifest_hash, capabilities, viewer_type, install_count, "
240 "is_verified, is_deprecated, created_at, updated_at) "
241 "VALUES (:did, NULL, 'cgcardona', 'code', 'Code', "
242 "'Symbol-graph code state space — diff and merge at the level of named "
243 "functions, classes, and modules across Python, TypeScript, Go, Rust, "
244 "Java, C, C++, C#, Ruby, and Kotlin.', "
245 f"'1.0.0', :chash, CAST('{code_caps_json.replace(chr(39), chr(39)+chr(39))}' AS json), "
246 "'symbol_graph', 0, true, false, now(), now()) "
247 "ON CONFLICT (author_slug, slug) DO NOTHING"
248 ).bindparams(
249 did=_DOMAIN_CODE_ID,
250 chash=_manifest_hash(_CODE_CAPABILITIES),
251 )
252 )
253
254
255 def downgrade() -> None:
256 # Restore musehub_render_jobs original columns
257 op.add_column("musehub_render_jobs",
258 sa.Column("midi_count", sa.Integer(), nullable=False, server_default="0"))
259 op.add_column("musehub_render_jobs",
260 sa.Column("mp3_object_ids", sa.JSON(), nullable=False, server_default="[]"))
261 op.add_column("musehub_render_jobs",
262 sa.Column("image_object_ids", sa.JSON(), nullable=False, server_default="[]"))
263 op.execute("UPDATE musehub_render_jobs SET midi_count = artifact_count")
264 op.execute("UPDATE musehub_render_jobs SET mp3_object_ids = audio_object_ids")
265 op.execute("UPDATE musehub_render_jobs SET image_object_ids = preview_object_ids")
266 op.drop_column("musehub_render_jobs", "artifact_count")
267 op.drop_column("musehub_render_jobs", "audio_object_ids")
268 op.drop_column("musehub_render_jobs", "preview_object_ids")
269
270 # Restore musehub_issue_comments
271 op.add_column("musehub_issue_comments",
272 sa.Column("musical_refs", sa.JSON(), nullable=False, server_default="[]"))
273 op.execute("UPDATE musehub_issue_comments SET musical_refs = state_refs")
274 op.drop_column("musehub_issue_comments", "state_refs")
275
276 # Restore musehub_pr_comments
277 op.add_column("musehub_pr_comments",
278 sa.Column("target_type", sa.String(20), nullable=False, server_default="general"))
279 op.add_column("musehub_pr_comments",
280 sa.Column("target_track", sa.String(255), nullable=True))
281 op.add_column("musehub_pr_comments",
282 sa.Column("target_beat_start", sa.Float(), nullable=True))
283 op.add_column("musehub_pr_comments",
284 sa.Column("target_beat_end", sa.Float(), nullable=True))
285 op.add_column("musehub_pr_comments",
286 sa.Column("target_note_pitch", sa.Integer(), nullable=True))
287 op.drop_column("musehub_pr_comments", "dimension_ref")
288
289 # Restore musehub_repos
290 op.drop_index("ix_musehub_repos_domain_id", table_name="musehub_repos")
291 op.drop_column("musehub_repos", "domain_id")
292 op.drop_column("musehub_repos", "domain_meta")
293 op.add_column("musehub_repos",
294 sa.Column("key_signature", sa.String(50), nullable=True))
295 op.add_column("musehub_repos",
296 sa.Column("tempo_bpm", sa.Integer(), nullable=True))
297
298 # Drop new tables
299 op.drop_index("ix_musehub_domain_installs_domain_id", table_name="musehub_domain_installs")
300 op.drop_index("ix_musehub_domain_installs_user_id", table_name="musehub_domain_installs")
301 op.drop_table("musehub_domain_installs")
302
303 op.drop_index("ix_musehub_domains_author_user_id", table_name="musehub_domains")
304 op.drop_index("ix_musehub_domains_slug", table_name="musehub_domains")
305 op.drop_index("ix_musehub_domains_author_slug", table_name="musehub_domains")
306 op.drop_table("musehub_domains")