Cotton code
70 lines · 4.2 KB · PythonRaw
#!/usr/bin/env python3# Inspect only the release walk's invented database files and report the data shapes its UI assertions need.import jsonimport refrom pathlib import Pathimport sqlite3import sysroot = Path(sys.argv[1]).resolve()allowed = (Path.home() / 'cotton-captures/release-walk').resolve()if allowed not in root.parents or not root.name.startswith('run-'):    sys.exit('fixture inspection requires an owned release-walk run')seed = json.loads((root / 'seed.json').read_text().strip().splitlines()[-1])path = next((root / 'household/files').glob('*/' + seed['workspaceId'] + '/life.db'))# The seeder checkpointed and exited; inspect BEFORE serve, with no WAL to omit.wal = Path(str(path) + '-wal')if wal.exists() and wal.stat().st_size:    sys.exit('fixture inspection requires the seeder checkpoint, before serving')db = sqlite3.connect('file:' + str(path) + '?mode=ro&immutable=1', uri=True)db.row_factory = sqlite3.Rowdef rows(sql):    return [dict(r) for r in db.execute(sql)]def has(sql):    return bool(db.execute(sql).fetchone())shapes = {    'direct chat with empty title': has("SELECT 1 FROM cotton_msg_threads WHERE kind='dm' AND coalesce(title,'')=''"),    'media-only message': has("SELECT 1 FROM cotton_msg_messages WHERE coalesce(body,'')='' AND media NOT IN ('[]','null','')"),    'empty thread': has('SELECT 1 FROM cotton_msg_threads t WHERE NOT EXISTS (SELECT 1 FROM cotton_msg_messages m WHERE m.thread_id=t.id)'),    'record tied to two projects': has('SELECT 1 FROM cotton_project_links GROUP BY record_kind,record_id HAVING count(DISTINCT project_id)>=2'),}source = root.parent / ('build-' + (root / 'sha').read_text().strip()) / 'src'source_text = (source / 'crates/cotton-core/src/rules/home_arrangement.rs').read_text()editorial = re.search(r'pub const HOME_TOOL_KEYS[^=]*= \[(.*?)\];', source_text, re.S)if not editorial:    sys.exit("cannot enumerate the cut commit tool keys")keys = re.findall(r'"([a-z_-]+)"', editorial.group(1))# The Tools screen does not show OFF_HOME_KEYS (Bin, Feedback, Storage, Everything).off_board = re.search(r'pub const OFF_HOME_KEYS[^=]*= \[(.*?)\];', source_text, re.S)if not off_board:    sys.exit("cannot enumerate the cut commit off-board tool keys")keys = [k for k in keys if k not in re.findall(r'"([a-z_-]+)"', off_board.group(1))]definitions = {t['id']: t for p in (source / 'crates/cotton-core/definitions').glob('*.json') for t in [json.loads(p.read_text())]}# A tool package carried in code arrives after this inspection: Disk's, which the serving headless's lane ledger installs.for p in (source / 'crates').glob('*/src/**/*_package/tables/*.json'):    table = json.loads(p.read_text())    definitions.setdefault(table['id'], table)for row in rows('SELECT definition FROM cotton_table_definitions'):    table = json.loads(row['definition'])    definitions[table['id']] = tabletables = list(definitions.values())settings = {t.get('picture', '').removeprefix('tool-') for t in tables if t.get('tile', {}).get('place') == 'settings'}arrangements = rows('SELECT hidden_tool_keys FROM cotton_home_arrangement')hidden = set(json.loads(arrangements[0]['hidden_tool_keys'])) if arrangements else set()# Seeded records can reveal hidden tools again; a fresh walk expects every ordinary tool.expected = [k for k in keys if k not in settings and k not in hidden]print(json.dumps({    'personal': seed['workspaceId'], 'shapes': shapes,    'projects': rows('SELECT slug,name FROM cotton_memory_projects ORDER BY slug'),    'tables': tables, 'expectedTools': expected,    'tools': [json.loads(r['definition']) for r in rows('SELECT definition FROM cotton_tool_definitions')],    'messages': rows("SELECT thread_id,body FROM cotton_msg_messages WHERE length(trim(coalesce(body,'')))>0"),    'chatCases': (        rows("SELECT 'untitled direct chat' AS shape,id FROM cotton_msg_threads WHERE kind='dm' AND coalesce(title,'')='' LIMIT 1")        + rows("SELECT 'empty thread' AS shape,id FROM cotton_msg_threads t WHERE NOT EXISTS (SELECT 1 FROM cotton_msg_messages m WHERE m.thread_id=t.id) LIMIT 1")        + rows("SELECT 'media-only message' AS shape,thread_id AS id FROM cotton_msg_messages WHERE coalesce(body,'')='' AND media NOT IN ('[]','null','') LIMIT 1")    ),}))