sqlite-table-roles.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. export type LocalRuntimeSqliteTableRole =
  2. | 'schema_registry'
  3. | 'canonical_event_cursor'
  4. | 'active_projection'
  5. | 'compat_blob'
  6. | 'backfill_marker'
  7. | 'legacy_import_map'
  8. | 'runtime_coordination'
  9. | 'operational_state'
  10. | 'usage_accounting'
  11. | 'user_goal_state';
  12. export interface LocalRuntimeSqliteTableRoleEntry {
  13. table: string;
  14. role: LocalRuntimeSqliteTableRole;
  15. alias: string;
  16. description: string;
  17. }
  18. export interface LocalRuntimeSqliteTableRoleGroup {
  19. role: LocalRuntimeSqliteTableRole;
  20. label: string;
  21. description: string;
  22. tables: string[];
  23. }
  24. export interface LocalRuntimeSqliteTableRoleLegend {
  25. schemaVersion: 1;
  26. physicalRenamePolicy: 'no_rename_existing_tables';
  27. note: string;
  28. groups: LocalRuntimeSqliteTableRoleGroup[];
  29. tables: LocalRuntimeSqliteTableRoleEntry[];
  30. }
  31. const GROUPS: LocalRuntimeSqliteTableRoleGroup[] = [
  32. {
  33. role: 'schema_registry',
  34. label: 'Schema registry',
  35. description: 'Tracks applied SQLite schema migrations only.',
  36. tables: ['local_runtime_schema_migrations'],
  37. },
  38. {
  39. role: 'canonical_event_cursor',
  40. label: 'Canonical event cursor',
  41. description:
  42. 'Stores watermarks for append-only session ledger files under v2/sessions; it is not the event log body.',
  43. tables: ['local_runtime_ledger_watermarks'],
  44. },
  45. {
  46. role: 'active_projection',
  47. label: 'Active SQLite projection',
  48. description:
  49. 'Current query/index projection used by session lists, visible history, Pi history, and queued messages.',
  50. tables: [
  51. 'local_runtime_sessions',
  52. 'local_runtime_agents',
  53. 'local_runtime_message_rows',
  54. 'local_runtime_pi_history_rows',
  55. 'local_runtime_queue_items',
  56. 'local_runtime_session_projection_watermarks',
  57. ],
  58. },
  59. {
  60. role: 'compat_blob',
  61. label: 'Compatibility blob',
  62. description: 'Older local-runtime blob storage kept only as a migration/backfill source.',
  63. tables: ['local_runtime_messages', 'local_runtime_queues'],
  64. },
  65. {
  66. role: 'backfill_marker',
  67. label: 'Backfill marker',
  68. description:
  69. 'Per-session markers proving a compatibility blob was already converted into row storage.',
  70. tables: [
  71. 'local_runtime_message_row_migrations',
  72. 'local_runtime_pi_history_row_migrations',
  73. 'local_runtime_queue_row_migrations',
  74. ],
  75. },
  76. {
  77. role: 'legacy_import_map',
  78. label: 'Legacy import map',
  79. description:
  80. 'Legacy daemon/opencode mapping, status, source manifest, checksum, warning, and report data.',
  81. tables: [
  82. 'local_runtime_legacy_migrations',
  83. 'local_runtime_cron_legacy_imports',
  84. 'local_runtime_cron_data_migrations',
  85. ],
  86. },
  87. {
  88. role: 'runtime_coordination',
  89. label: 'Runtime coordination',
  90. description: 'Short-lived coordination state for active local-runtime owners.',
  91. tables: ['local_runtime_session_locks'],
  92. },
  93. {
  94. role: 'usage_accounting',
  95. label: 'Usage accounting',
  96. description: 'Token usage and turn-level tool file-change accounting.',
  97. tables: [
  98. 'local_runtime_token_usage',
  99. 'local_runtime_turn_diffs',
  100. 'local_runtime_turn_diff_journal',
  101. 'local_runtime_turn_diff_rewind_operations',
  102. 'local_runtime_turn_diff_retention_queue',
  103. ],
  104. },
  105. {
  106. role: 'operational_state',
  107. label: 'Operational state',
  108. description: 'Native runtime feature state unrelated to legacy migration naming.',
  109. tables: [
  110. 'local_runtime_preferences',
  111. 'local_runtime_communication_messages',
  112. 'local_runtime_crons',
  113. 'local_runtime_cron_session_history',
  114. 'questionnaire_requests',
  115. ],
  116. },
  117. {
  118. role: 'user_goal_state',
  119. label: 'User goal state',
  120. description: 'Thread goal state, one active/paused/blocked/completed goal per session.',
  121. tables: ['local_runtime_thread_goals'],
  122. },
  123. ];
  124. const TABLE_DESCRIPTIONS: Record<string, { alias: string; description: string }> = {
  125. local_runtime_schema_migrations: {
  126. alias: 'schema migration registry',
  127. description: 'Records applied local-runtime SQLite schema versions.',
  128. },
  129. local_runtime_sessions: {
  130. alias: 'session metadata projection',
  131. description: 'Stores LocalSessionRecord JSON for lists, lookup, and session tree projection.',
  132. },
  133. local_runtime_agents: {
  134. alias: 'agent metadata projection',
  135. description: 'Stores LocalAgentRecord JSON for local/manual/auto agent metadata.',
  136. },
  137. local_runtime_messages: {
  138. alias: 'legacy local-runtime message blob',
  139. description:
  140. 'Older display/pi history blob row. Current reads backfill into row tables and then clear it.',
  141. },
  142. local_runtime_queues: {
  143. alias: 'legacy local-runtime queue blob',
  144. description:
  145. 'Older queued-message blob row. Current reads backfill into local_runtime_queue_items.',
  146. },
  147. local_runtime_preferences: {
  148. alias: 'runtime preference key-value',
  149. description: 'Small runtime preference KV records stored as JSON values.',
  150. },
  151. local_runtime_message_rows: {
  152. alias: 'display history row projection',
  153. description: 'Current UI-visible message rows with stable order and session/msg_id uniqueness.',
  154. },
  155. local_runtime_message_row_migrations: {
  156. alias: 'display history backfill marker',
  157. description: 'Marks sessions whose old display message blob has been migrated to rows.',
  158. },
  159. local_runtime_pi_history_rows: {
  160. alias: 'Pi continuation history row projection',
  161. description:
  162. 'Current model-continuity history rows, intentionally distinct from full UI history.',
  163. },
  164. local_runtime_pi_history_row_migrations: {
  165. alias: 'Pi history backfill marker',
  166. description: 'Marks sessions whose old Pi history blob has been migrated to rows.',
  167. },
  168. local_runtime_queue_items: {
  169. alias: 'queued message row state',
  170. description: 'Current queued user/channel turn items with per-session item ids.',
  171. },
  172. local_runtime_queue_row_migrations: {
  173. alias: 'queue backfill marker',
  174. description: 'Marks sessions whose old queue blob has been migrated to queue item rows.',
  175. },
  176. local_runtime_legacy_migrations: {
  177. alias: 'legacy daemon import map and report',
  178. description:
  179. 'Maps legacy daemon/opencode sessions to local sessions and stores import status, counts, checksums, warnings, and reports.',
  180. },
  181. local_runtime_cron_legacy_imports: {
  182. alias: 'legacy .md cron import markers',
  183. description:
  184. 'Records which (agent, cron) pairs from pre-SQLite on-disk .md cron files were imported, so the startup migration never re-imports or resurrects a deleted cron.',
  185. },
  186. local_runtime_cron_data_migrations: {
  187. alias: 'cron data migration markers',
  188. description:
  189. 'Records one-shot cron data backfills such as purpose-tagged session history import into native cron history.',
  190. },
  191. local_runtime_session_locks: {
  192. alias: 'active session execution lock',
  193. description: 'Prevents multiple runtime owners from executing the same session simultaneously.',
  194. },
  195. local_runtime_ledger_watermarks: {
  196. alias: 'ledger append watermark',
  197. description:
  198. 'Tracks the last ledger seq/event id for v2/sessions/YYYY/MM/DD/<session>/ledger.jsonl, with dev-draft v2/chats fallback.',
  199. },
  200. local_runtime_session_projection_watermarks: {
  201. alias: 'SQLite projection replay watermark',
  202. description: 'Tracks how far SQLite session/message projections have replayed the ledger.',
  203. },
  204. local_runtime_token_usage: {
  205. alias: 'token usage accounting',
  206. description: 'Stores per-turn token and cost usage for session/agent/global summaries.',
  207. },
  208. local_runtime_turn_diffs: {
  209. alias: 'turn file-change records',
  210. description: 'Stores finalized per-turn tool file changes, undo snapshots, and revert state.',
  211. },
  212. local_runtime_turn_diff_journal: {
  213. alias: 'turn file-change journal',
  214. description: 'Stores pending turns and per-tool before/after snapshots before finalization.',
  215. },
  216. local_runtime_turn_diff_rewind_operations: {
  217. alias: 'turn file-change rewind operation',
  218. description: 'Stores immutable batch Undo plans and terminal receipts for Session Rewind.',
  219. },
  220. local_runtime_turn_diff_retention_queue: {
  221. alias: 'turn file-change retention queue',
  222. description:
  223. 'Indexes newly written terminal Turn Diff state for bounded time-ordered retention without scanning historical rows.',
  224. },
  225. local_runtime_communication_messages: {
  226. alias: 'session communication queue',
  227. description: 'Stores session-to-session communication records and dispatch status.',
  228. },
  229. local_runtime_crons: {
  230. alias: 'native cron config',
  231. description: 'Stores native cron task configuration by agent and cron name.',
  232. },
  233. local_runtime_cron_session_history: {
  234. alias: 'native cron session history',
  235. description: 'Stores sessions created by cron tasks.',
  236. },
  237. local_runtime_thread_goals: {
  238. alias: 'thread goal state',
  239. description: 'Stores codex-style thread goal objective and status by session.',
  240. },
  241. questionnaire_requests: {
  242. alias: 'questionnaire request lifecycle',
  243. description:
  244. 'Stores V2 ask_user questionnaire requests, reply payloads, injection recovery stamps, and terminal status.',
  245. },
  246. };
  247. const ROLE_BY_TABLE = new Map<string, LocalRuntimeSqliteTableRole>(
  248. GROUPS.flatMap((group) => group.tables.map((table) => [table, group.role] as const)),
  249. );
  250. export function getLocalRuntimeSqliteTableRoleLegend(): LocalRuntimeSqliteTableRoleLegend {
  251. return {
  252. schemaVersion: 1,
  253. physicalRenamePolicy: 'no_rename_existing_tables',
  254. note: 'Physical SQLite table names are kept stable for compatibility; use role/alias fields in diagnostics and docs to distinguish current projections from compatibility blobs.',
  255. groups: GROUPS.map((group) => ({ ...group, tables: [...group.tables] })),
  256. tables: Object.entries(TABLE_DESCRIPTIONS).map(([table, info]) => ({
  257. table,
  258. role: ROLE_BY_TABLE.get(table) ?? 'operational_state',
  259. alias: info.alias,
  260. description: info.description,
  261. })),
  262. };
  263. }