← Files Codex SecurityARCHIVED FILE

shared/workbench-history-repairs.json

15.7 KB · Nov 24, 2026 · 00:14 UTC

↓ Download file

See the change to this file →

[
  [
    "WITH input AS (SELECT :history AS history),",
    "previews AS (",
    "    SELECT version, CASE",
    "        -- Prefer the original timestamp when a reader reopened the 52/53 repository preview.",
    "        WHEN version IN (64, 67) AND name = 'persist repository identities'",
    "            AND EXISTS (SELECT 1 FROM schema_migrations WHERE version = 52 AND name = receipt.name)",
    "            THEN 'DELETE FROM schema_migrations'",
    "        WHEN version IN (65, 68) AND name = 'stabilize Linux repository generations'",
    "            AND EXISTS (SELECT 1 FROM schema_migrations WHERE version = 53 AND name = receipt.name)",
    "            THEN 'DELETE FROM schema_migrations'",
    "        WHEN version IN (50, 52, 53, 54, 61, 63, 64) AND name = 'persist repository identities'",
    "            THEN 'UPDATE schema_migrations SET version = 67'",
    "        WHEN version IN (51, 53, 54, 55, 62, 64, 65) AND name = 'stabilize Linux repository generations'",
    "            THEN 'UPDATE schema_migrations SET version = 68'",
    "        WHEN version IN (56, 63, 65, 66) AND name = 'distinguish unvalidated workspace targets'",
    "            THEN 'UPDATE schema_migrations SET version = 69'",
    "        WHEN version IN (64, 66, 67) AND name = 'retain checkout roots for Git discovery'",
    "            THEN 'UPDATE schema_migrations SET version = 70'",
    "        WHEN version IN (67, 68) AND name = 'retain rejected workspace checkout boundaries'",
    "            THEN 'UPDATE schema_migrations SET version = 71'",
    "        WHEN version IN (48, 50, 63) AND name = 'persist scan log completion boundaries'",
    "            THEN 'UPDATE schema_migrations SET version = 64'",
    "        WHEN version IN (58, 59) AND name = 'record classification rubric provenance'",
    "            THEN 'UPDATE schema_migrations SET version = 60'",
    "        WHEN version IN (47, 48, 50, 52, 53, 54) AND name = 'freeze stopped scan checkpoint selections'",
    "            THEN 'UPDATE schema_migrations SET version = 57'",
    "        WHEN version IN (34, 40, 41, 42, 43, 44, 48) AND name = 'persist authorized source excerpt scopes'",
    "            THEN 'UPDATE schema_migrations SET version = 50'",
    "        WHEN version = 63 AND name = 'retain native investigation time allowances'",
    "            THEN 'DELETE FROM schema_migrations'",
    "    END AS repair FROM schema_migrations AS receipt",
    ")",
    "SELECT 'sql' AS operation, repair || ' WHERE version = ' || version AS value",
    "FROM previews WHERE repair IS NOT NULL",
    "-- Vacate checkpoint preview 50 before moving a lower source-scope receipt into it.",
    "ORDER BY version DESC;"
  ],
  [
    "-- mirror and early-moves: relocate numbers before checking profiles.",
    "WITH",
    "-- Keep the common :history binding even though these moves do not inspect it.",
    "input AS (SELECT :history AS history),",
    "mirror_names(version, name, destination) AS (",
    "    VALUES",
    "        (29, 'freeze stopped scan source digests', 31),",
    "        (30, 'separate deep scan publication failures', 32)",
    "),",
    "mirror AS (",
    "    SELECT EXISTS (",
    "        SELECT 1 FROM schema_migrations JOIN mirror_names USING (version, name)",
    "    ) AS present,",
    "    (SELECT COUNT(*) FROM schema_migrations WHERE version BETWEEN 29 AND 32) = 2",
    "        AND (SELECT COUNT(*) FROM schema_migrations",
    "            JOIN mirror_names USING (version, name)) = 2 AS valid",
    "),",
    "moves(position, version, destination, name) AS (",
    "    VALUES",
    "        (-2, 48, 67, 'persist repository identities'),",
    "        (-1, 49, 68, 'stabilize Linux repository generations'),",
    "        (0, 44, 68, 'stabilize Linux repository generations'),",
    "        (1, 45, 68, 'stabilize Linux repository generations'),",
    "        (2, 30, 67, 'persist repository identities'),",
    "        (3, 31, 67, 'persist repository identities'),",
    "        (4, 33, 67, 'persist repository identities'),",
    "        (5, 40, 67, 'persist repository identities'),",
    "        (6, 42, 67, 'persist repository identities'),",
    "        (7, 43, 67, 'persist repository identities'),",
    "        (8, 44, 67, 'persist repository identities'),",
    "        (9, 33, 40, 'index finding identity and comparison history'),",
    "        (10, 25, 26, 'persist scan completion warnings'),",
    "        (11, 12, 20, 'phase-specific scan progress')",
    ")",
    "SELECT 0 AS position, 'error' AS operation,",
    "        'The Codex Security database has an unsupported mirror migration history.' AS value",
    "    FROM mirror WHERE present AND NOT valid",
    "UNION ALL",
    "SELECT 32 - version, 'sql',",
    "        'UPDATE schema_migrations SET version = ' || destination ||",
    "        ' WHERE version = ' || version || ' AND name = ' || quote(name)",
    "    FROM mirror_names, mirror WHERE present",
    "UNION ALL",
    "-- early-moves",
    "SELECT 10 + position, 'sql', 'UPDATE schema_migrations SET version = ' || destination ||",
    "        ' WHERE version = ' || version || ' AND name = ' || quote(name)",
    "    FROM moves JOIN schema_migrations USING (version, name)",
    "ORDER BY position;"
  ],
  [
    "WITH history AS (",
    "    SELECT json_extract(value, '$.version') AS version,",
    "        json_extract(value, '$.name') AS name FROM json_each(:history)",
    ")",
    "SELECT 'error' AS operation,",
    "    'The Codex Security database has an unsupported source-scope migration history.' AS value",
    "FROM schema_migrations JOIN history USING (version)",
    "WHERE version IN (43, 50) AND schema_migrations.name != history.name;"
  ],
  [
    "-- A released reader can add its columns while a repository preview still owns the version.",
    "WITH released(version, table_name, column_name) AS (",
    "    VALUES (54, 'workspaces', 'deep_scan_config_json'),",
    "        (55, 'deep_scan_runs', 'pending_target_project_path'),",
    "        (58, 'deep_scan_runs', 'terminal_reviewed_surface_count'),",
    "        (59, 'deep_scan_runs', 'service_tier'),",
    "        (61, 'scans', 'launch_surface')",
    ")",
    "SELECT 'timestamp' AS operation,",
    "    'INSERT INTO schema_migrations (version, name, applied_at) VALUES (' ||",
    "        released.version || ', ' || quote(json_extract(history.value, '$.name')) || ', ?)' AS value",
    "FROM released JOIN json_each(:history) AS history",
    "    ON json_extract(history.value, '$.version') = released.version",
    "WHERE EXISTS (SELECT 1 FROM schema_migrations WHERE name = 'persist repository identities')",
    "    AND NOT EXISTS (SELECT 1 FROM schema_migrations WHERE version = released.version)",
    "    AND EXISTS (SELECT 1 FROM pragma_table_info(released.table_name) WHERE name = released.column_name);"
  ],
  [
    "-- A main-branch reader may have added discovery context under the old checkpoint number.",
    "SELECT 'timestamp' AS operation,",
    "    'INSERT INTO schema_migrations (version, name, applied_at) VALUES (47, ' ||",
    "    quote(json_extract(value, '$.name')) || ', ?)' AS value",
    "FROM json_each(:history) WHERE json_extract(value, '$.version') = 47",
    "    AND EXISTS (SELECT 1 FROM schema_migrations WHERE version = 57",
    "        AND name = 'freeze stopped scan checkpoint selections')",
    "    AND NOT EXISTS (SELECT 1 FROM schema_migrations WHERE version = 47)",
    "    AND EXISTS (SELECT 1 FROM pragma_table_info('deep_scan_runs')",
    "        WHERE name = 'discovery_user_context_json');"
  ],
  [
    "-- profiles: preserve legacy columns before the later repairs.",
    "WITH",
    "history AS (",
    "    SELECT json_extract(value, '$.version') AS version,",
    "        json_extract(value, '$.name') AS name",
    "    FROM json_each(:history)",
    "),",
    "profile_columns AS (",
    "    SELECT 'scans' AS table_name, name FROM pragma_table_info('scans')",
    "    UNION ALL",
    "    SELECT 'workspaces', name FROM pragma_table_info('workspaces')",
    "),",
    "profile_state AS (",
    "    SELECT",
    "        EXISTS (",
    "            SELECT 1 FROM schema_migrations",
    "            WHERE (version = 11 AND name = 'scan execution profiles')",
    "                OR (version = 12 AND name IN (",
    "                    'scan execution profiles', 'dynamic scan execution profiles'",
    "                ))",
    "        ) AS legacy_history,",
    "        EXISTS (",
    "            SELECT 1 FROM profile_columns",
    "            WHERE name = 'execution_model'",
    "                OR (table_name = 'workspaces' AND name = 'reasoning_effort')",
    "        ) AS legacy_columns,",
    "        (SELECT COUNT(*) FROM profile_columns",
    "            WHERE name IN ('execution_model', 'reasoning_effort')) = 4",
    "            AND NOT EXISTS (",
    "                SELECT 1 FROM profile_columns",
    "                WHERE name IN ('legacy_execution_model', 'legacy_reasoning_effort')",
    "            ) AS valid_columns,",
    "        NOT EXISTS (",
    "            SELECT 1 FROM schema_migrations",
    "            WHERE (version = 11 AND name NOT IN (",
    "                'deep scan orchestration state', 'scan execution profiles'",
    "            )) OR (version = 12 AND name NOT IN (",
    "                'scan continuation threads', 'scan execution profiles',",
    "                'dynamic scan execution profiles'",
    "            )) OR (version = 25 AND name NOT IN (",
    "                'dynamic scan execution profiles',",
    "                (SELECT name FROM history WHERE version = 25)",
    "            ))",
    "        ) AS valid_history",
    ")",
    "SELECT 0 AS position, 'sql' AS operation,",
    "    'UPDATE schema_migrations SET name = ' ||",
    "        quote((SELECT name FROM history WHERE version = 25)) || ' WHERE version = 25' AS value",
    "    WHERE EXISTS (SELECT 1 FROM schema_migrations",
    "        WHERE version = 25 AND name = 'dynamic scan execution profiles')",
    "UNION ALL",
    "SELECT 1, 'error',",
    "        'The Codex Security database has an unsupported execution-profile migration history.'",
    "    FROM profile_state",
    "    WHERE (legacy_history OR legacy_columns) AND (",
    "        NOT valid_history OR (legacy_columns AND NOT valid_columns)",
    "        OR (legacy_history AND NOT legacy_columns)",
    "    )",
    "UNION ALL",
    "-- Keep historical values and constraints for recovery while moving",
    "-- them out of the namespace used by independent scan settings.",
    "SELECT 2 + CASE table_name WHEN 'workspaces' THEN 0 ELSE 2 END",
    "        + CASE name WHEN 'execution_model' THEN 0 ELSE 1 END,",
    "        'sql', 'ALTER TABLE ' || table_name || ' RENAME COLUMN ' || name ||",
    "        ' TO legacy_' || name",
    "    FROM profile_columns, profile_state",
    "    WHERE (legacy_history OR legacy_columns)",
    "        AND name IN ('execution_model', 'reasoning_effort')",
    "UNION ALL",
    "SELECT 6, 'additive', 25 FROM profile_state",
    "    WHERE legacy_history OR legacy_columns",
    "UNION ALL",
    "SELECT 7, 'sql',",
    "        'UPDATE scans SET model = COALESCE(model, legacy_execution_model),",
    "            reasoning_effort = COALESCE(reasoning_effort, legacy_reasoning_effort)'",
    "    FROM profile_state WHERE legacy_history OR legacy_columns",
    "UNION ALL",
    "SELECT 8, 'sql',",
    "        'DELETE FROM schema_migrations WHERE (version = 11 AND name = ''scan execution profiles'')",
    "            OR (version = 12 AND name IN (''scan execution profiles'', ''dynamic scan execution profiles''))'",
    "    FROM profile_state WHERE legacy_history OR legacy_columns",
    "UNION ALL",
    "SELECT 9, 'timestamp',",
    "        'INSERT INTO schema_migrations (version, name, applied_at) VALUES (25, ' ||",
    "        quote((SELECT name FROM history WHERE version = 25)) || ', ?)'",
    "    FROM profile_state WHERE (legacy_history OR legacy_columns)",
    "        AND NOT EXISTS (SELECT 1 FROM schema_migrations WHERE version = 25)",
    "ORDER BY position;"
  ],
  [
    "-- late-moves, shadowed, and legacy: independent remaining histories.",
    "WITH",
    "history AS (",
    "    SELECT json_extract(value, '$.version') AS version,",
    "        json_extract(value, '$.name') AS name,",
    "        json_extract(value, '$.statements') AS statements",
    "    FROM json_each(:history)",
    "),",
    "shadow_names(version, name) AS (",
    "    VALUES",
    "        (18, 'scan target summaries'),",
    "        (19, 'structured scan guidance context'),",
    "        (19, 'idempotent scan lifecycle requests'),",
    "        (20, 'retain superseded scan lifecycle requests'),",
    "        (20, 'threat model publication receipts'),",
    "        (21, 'scan progress projection and activity'),",
    "        (21, 'deep coordinator manifest receipts'),",
    "        (22, 'dynamic scan execution profiles')",
    "),",
    "shadowed AS (",
    "    SELECT history.* FROM schema_migrations",
    "    JOIN shadow_names USING (version, name) JOIN history USING (version)",
    "),",
    "legacy_names(version, name) AS (",
    "    VALUES",
    "        (2, 'finding management schema'),",
    "        (3, 'scan handoff delivery claims'),",
    "        (4, 'finding remediation action claims'),",
    "        (5, 'scan target snapshot digests')",
    "),",
    "legacy AS (",
    "    SELECT EXISTS (",
    "        SELECT 1 FROM schema_migrations",
    "        WHERE version = 2 AND name = 'finding management schema'",
    "    ) AS present,",
    "    NOT EXISTS (",
    "        SELECT 1 FROM schema_migrations AS recorded",
    "        LEFT JOIN legacy_names USING (version, name)",
    "        WHERE recorded.version BETWEEN 2 AND 5 AND legacy_names.name IS NULL",
    "    ) AS valid",
    ")",
    "SELECT 0 AS position, 'sql' AS operation,",
    "    'UPDATE schema_migrations SET version = 21 WHERE version = 13 AND name = ''current scan preflight state''' AS value",
    "WHERE EXISTS (SELECT 1 FROM schema_migrations",
    "    WHERE version = 13 AND name = 'current scan preflight state')",
    "UNION ALL",
    "-- shadowed",
    "SELECT version * 10 + statements.key, 'sql',",
    "        CASE version WHEN 19 THEN replace(statements.value,",
    "            'CREATE TABLE ', 'CREATE TABLE IF NOT EXISTS ')",
    "            ELSE statements.value END",
    "    FROM shadowed, json_each(shadowed.statements) AS statements",
    "    WHERE version IN (18, 19)",
    "UNION ALL",
    "SELECT version * 10, 'additive', version",
    "    FROM shadowed WHERE version IN (20, 21, 22)",
    "UNION ALL",
    "SELECT version * 10 + 9, 'sql',",
    "        'UPDATE schema_migrations SET name = ' || quote(name) ||",
    "        ' WHERE version = ' || version",
    "    FROM shadowed",
    "UNION ALL",
    "-- legacy",
    "SELECT 300, 'error',",
    "        'The Codex Security database has an unsupported pre-release migration history.'",
    "    FROM legacy WHERE present AND NOT valid",
    "UNION ALL",
    "SELECT 301, 'sql', 'DELETE FROM schema_migrations WHERE version = 5'",
    "    FROM legacy WHERE present",
    "UNION ALL",
    "SELECT 306 - version, 'sql',",
    "        'UPDATE schema_migrations SET version = ' || (version + 1) ||",
    "        ' WHERE version = ' || version || ' AND name = ' || quote(name)",
    "    FROM legacy_names, legacy WHERE present AND version < 5",
    "UNION ALL",
    "SELECT 305, 'additive', 2 FROM legacy WHERE present",
    "UNION ALL",
    "SELECT 306, 'column', 'ALTER TABLE scans ADD COLUMN target_snapshot_digest TEXT;'",
    "    FROM legacy WHERE present",
    "UNION ALL",
    "SELECT 307, 'timestamp',",
    "        'INSERT INTO schema_migrations (version, name, applied_at) VALUES (2, ' ||",
    "        quote((SELECT name FROM history WHERE version = 2)) || ', ?)'",
    "    FROM legacy WHERE present",
    "ORDER BY position;"
  ]
]

SHA-256: 9ce97bb58f0803c9bf67b0bc11a56d015a0444b08fbf7f2ee3bb51ab5f87b755