All files / src/db schema.ts

64.38% Statements 47/73
100% Branches 0/0
40.9% Functions 18/44
56.66% Lines 34/60

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538                                    24x                                 239x                             24x                           239x                                     24x                     239x                                               24x                           24x                                                                 239x                                                           24x                     239x                                       24x                           239x                     24x                                                     239x                                             24x                             239x       239x                             24x                       239x       239x                           24x                                         239x                                                           24x                         239x                                       24x                 239x                     24x                 239x       239x                                   24x             239x                     24x                       239x              
/**
 * Database schema for Codewalk District.
 *
 * Defines the three-level task hierarchy:
 * - Initiative: Top-level project
 * - Phase: Major milestone within initiative
 * - Task: Individual work item (can have parentTaskId for decomposition relationships)
 *
 * Plus a task_dependencies table for task dependency relationships.
 */
 
import { sqliteTable, text, integer, uniqueIndex, index } from 'drizzle-orm/sqlite-core';
import { relations, type InferInsertModel, type InferSelectModel } from 'drizzle-orm';
 
// ============================================================================
// INITIATIVES
// ============================================================================
 
export const initiatives = sqliteTable('initiatives', {
  id: text('id').primaryKey(),
  name: text('name').notNull(),
  status: text('status', { enum: ['active', 'completed', 'archived'] })
    .notNull()
    .default('active'),
  mergeRequiresApproval: integer('merge_requires_approval', { mode: 'boolean' })
    .notNull()
    .default(true),
  branch: text('branch'), // Auto-generated initiative branch (e.g., 'cw/user-auth')
  executionMode: text('execution_mode', { enum: ['yolo', 'review_per_phase'] })
    .notNull()
    .default('review_per_phase'),
  createdAt: integer('created_at', { mode: 'timestamp' }).notNull(),
  updatedAt: integer('updated_at', { mode: 'timestamp' }).notNull(),
});
 
export const initiativesRelations = relations(initiatives, ({ many }) => ({
  phases: many(phases),
  pages: many(pages),
  initiativeProjects: many(initiativeProjects),
  tasks: many(tasks),
  changeSets: many(changeSets),
}));
 
export type Initiative = InferSelectModel<typeof initiatives>;
export type NewInitiative = InferInsertModel<typeof initiatives>;
 
// ============================================================================
// PHASES
// ============================================================================
 
export const phases = sqliteTable('phases', {
  id: text('id').primaryKey(),
  initiativeId: text('initiative_id')
    .notNull()
    .references(() => initiatives.id, { onDelete: 'cascade' }),
  name: text('name').notNull(),
  content: text('content'),
  status: text('status', { enum: ['pending', 'approved', 'in_progress', 'completed', 'blocked', 'pending_review'] })
    .notNull()
    .default('pending'),
  createdAt: integer('created_at', { mode: 'timestamp' }).notNull(),
  updatedAt: integer('updated_at', { mode: 'timestamp' }).notNull(),
});
 
export const phasesRelations = relations(phases, ({ one, many }) => ({
  initiative: one(initiatives, {
    fields: [phases.initiativeId],
    references: [initiatives.id],
  }),
  tasks: many(tasks),
  // Dependencies: phases this phase depends on
  dependsOn: many(phaseDependencies, { relationName: 'dependentPhase' }),
  // Dependents: phases that depend on this phase
  dependents: many(phaseDependencies, { relationName: 'dependencyPhase' }),
}));
 
export type Phase = InferSelectModel<typeof phases>;
export type NewPhase = InferInsertModel<typeof phases>;
 
// ============================================================================
// PHASE DEPENDENCIES
// ============================================================================
 
export const phaseDependencies = sqliteTable('phase_dependencies', {
  id: text('id').primaryKey(),
  phaseId: text('phase_id')
    .notNull()
    .references(() => phases.id, { onDelete: 'cascade' }),
  dependsOnPhaseId: text('depends_on_phase_id')
    .notNull()
    .references(() => phases.id, { onDelete: 'cascade' }),
  createdAt: integer('created_at', { mode: 'timestamp' }).notNull(),
});
 
export const phaseDependenciesRelations = relations(phaseDependencies, ({ one }) => ({
  phase: one(phases, {
    fields: [phaseDependencies.phaseId],
    references: [phases.id],
    relationName: 'dependentPhase',
  }),
  dependsOnPhase: one(phases, {
    fields: [phaseDependencies.dependsOnPhaseId],
    references: [phases.id],
    relationName: 'dependencyPhase',
  }),
}));
 
export type PhaseDependency = InferSelectModel<typeof phaseDependencies>;
export type NewPhaseDependency = InferInsertModel<typeof phaseDependencies>;
 
// ============================================================================
// TASKS
// ============================================================================
 
/**
 * Task category enum values.
 * Defines what kind of work a task represents.
 */
export const TASK_CATEGORIES = [
  'execute',    // Standard execution task
  'research',   // Research/exploration task
  'discuss',    // Discussion/context gathering
  'plan',       // Plan initiative into phases
  'detail',     // Detail phase into tasks
  'refine',     // Refine/edit content
  'verify',     // Verification task
  'merge',      // Merge task
  'review',     // Review/approval task
] as const;
 
export type TaskCategory = (typeof TASK_CATEGORIES)[number];
 
export const tasks = sqliteTable('tasks', {
  id: text('id').primaryKey(),
  // Parent context - at least one should be set
  phaseId: text('phase_id').references(() => phases.id, { onDelete: 'cascade' }),
  initiativeId: text('initiative_id').references(() => initiatives.id, { onDelete: 'cascade' }),
  // Parent task for detail hierarchy (child tasks link to parent detail task)
  parentTaskId: text('parent_task_id').references((): ReturnType<typeof text> => tasks.id, { onDelete: 'cascade' }),
  name: text('name').notNull(),
  description: text('description'),
  type: text('type', {
    enum: ['auto', 'checkpoint:human-verify', 'checkpoint:decision', 'checkpoint:human-action'],
  })
    .notNull()
    .default('auto'),
  category: text('category', {
    enum: TASK_CATEGORIES,
  })
    .notNull()
    .default('execute'),
  priority: text('priority', { enum: ['low', 'medium', 'high'] })
    .notNull()
    .default('medium'),
  status: text('status', {
    enum: ['pending_approval', 'pending', 'in_progress', 'completed', 'blocked'],
  })
    .notNull()
    .default('pending'),
  requiresApproval: integer('requires_approval', { mode: 'boolean' }), // null = inherit from initiative
  order: integer('order').notNull().default(0),
  createdAt: integer('created_at', { mode: 'timestamp' }).notNull(),
  updatedAt: integer('updated_at', { mode: 'timestamp' }).notNull(),
});
 
export const tasksRelations = relations(tasks, ({ one, many }) => ({
  phase: one(phases, {
    fields: [tasks.phaseId],
    references: [phases.id],
  }),
  initiative: one(initiatives, {
    fields: [tasks.initiativeId],
    references: [initiatives.id],
  }),
  // Parent task (for detail hierarchy - child links to parent detail task)
  parentTask: one(tasks, {
    fields: [tasks.parentTaskId],
    references: [tasks.id],
    relationName: 'parentTask',
  }),
  // Child tasks (tasks created from decomposition of this task)
  childTasks: many(tasks, { relationName: 'parentTask' }),
  // Dependencies: tasks this task depends on
  dependsOn: many(taskDependencies, { relationName: 'dependentTask' }),
  // Dependents: tasks that depend on this task
  dependents: many(taskDependencies, { relationName: 'dependencyTask' }),
}));
 
export type Task = InferSelectModel<typeof tasks>;
export type NewTask = InferInsertModel<typeof tasks>;
 
// ============================================================================
// TASK DEPENDENCIES
// ============================================================================
 
export const taskDependencies = sqliteTable('task_dependencies', {
  id: text('id').primaryKey(),
  taskId: text('task_id')
    .notNull()
    .references(() => tasks.id, { onDelete: 'cascade' }),
  dependsOnTaskId: text('depends_on_task_id')
    .notNull()
    .references(() => tasks.id, { onDelete: 'cascade' }),
  createdAt: integer('created_at', { mode: 'timestamp' }).notNull(),
});
 
export const taskDependenciesRelations = relations(taskDependencies, ({ one }) => ({
  task: one(tasks, {
    fields: [taskDependencies.taskId],
    references: [tasks.id],
    relationName: 'dependentTask',
  }),
  dependsOnTask: one(tasks, {
    fields: [taskDependencies.dependsOnTaskId],
    references: [tasks.id],
    relationName: 'dependencyTask',
  }),
}));
 
export type TaskDependency = InferSelectModel<typeof taskDependencies>;
export type NewTaskDependency = InferInsertModel<typeof taskDependencies>;
 
// ============================================================================
// ACCOUNTS
// ============================================================================
 
export const accounts = sqliteTable('accounts', {
  id: text('id').primaryKey(),
  email: text('email').notNull(),
  provider: text('provider').notNull().default('claude'),
  configJson: text('config_json'),    // .claude.json content (JSON string)
  credentials: text('credentials'),    // .credentials.json content (JSON string)
  isExhausted: integer('is_exhausted', { mode: 'boolean' }).notNull().default(false),
  exhaustedUntil: integer('exhausted_until', { mode: 'timestamp' }),
  lastUsedAt: integer('last_used_at', { mode: 'timestamp' }),
  sortOrder: integer('sort_order').notNull().default(0),
  createdAt: integer('created_at', { mode: 'timestamp' }).notNull(),
  updatedAt: integer('updated_at', { mode: 'timestamp' }).notNull(),
});
 
export const accountsRelations = relations(accounts, ({ many }) => ({
  agents: many(agents),
}));
 
export type Account = InferSelectModel<typeof accounts>;
export type NewAccount = InferInsertModel<typeof accounts>;
 
// ============================================================================
// AGENTS
// ============================================================================
 
export const agents = sqliteTable('agents', {
  id: text('id').primaryKey(),
  name: text('name').notNull().unique(), // Human-readable alias (e.g., 'jolly-penguin')
  taskId: text('task_id').references(() => tasks.id, { onDelete: 'set null' }), // Task may be deleted
  initiativeId: text('initiative_id').references(() => initiatives.id, { onDelete: 'set null' }),
  sessionId: text('session_id'), // Claude CLI session ID for resumption (null until first run completes)
  worktreeId: text('worktree_id').notNull(), // Agent alias (deterministic path: agent-workdirs/<alias>/)
  provider: text('provider').notNull().default('claude'),
  accountId: text('account_id').references(() => accounts.id, { onDelete: 'set null' }),
  status: text('status', {
    enum: ['idle', 'running', 'waiting_for_input', 'stopped', 'crashed'],
  })
    .notNull()
    .default('idle'),
  mode: text('mode', { enum: ['execute', 'discuss', 'plan', 'detail', 'refine'] })
    .notNull()
    .default('execute'),
  pid: integer('pid'),
  exitCode: integer('exit_code'), // Process exit code for debugging crashes
  outputFilePath: text('output_file_path'),
  result: text('result'),
  pendingQuestions: text('pending_questions'),
  createdAt: integer('created_at', { mode: 'timestamp' }).notNull(),
  updatedAt: integer('updated_at', { mode: 'timestamp' }).notNull(),
  userDismissedAt: integer('user_dismissed_at', { mode: 'timestamp' }),
});
 
export const agentsRelations = relations(agents, ({ one, many }) => ({
  task: one(tasks, {
    fields: [agents.taskId],
    references: [tasks.id],
  }),
  initiative: one(initiatives, {
    fields: [agents.initiativeId],
    references: [initiatives.id],
  }),
  account: one(accounts, {
    fields: [agents.accountId],
    references: [accounts.id],
  }),
  changeSets: many(changeSets),
}));
 
export type Agent = InferSelectModel<typeof agents>;
export type NewAgent = InferInsertModel<typeof agents>;
 
// ============================================================================
// CHANGE SETS
// ============================================================================
 
export const changeSets = sqliteTable('change_sets', {
  id: text('id').primaryKey(),
  agentId: text('agent_id')
    .references(() => agents.id, { onDelete: 'set null' }),
  agentName: text('agent_name').notNull(),
  initiativeId: text('initiative_id')
    .notNull()
    .references(() => initiatives.id, { onDelete: 'cascade' }),
  mode: text('mode', { enum: ['plan', 'detail', 'refine'] }).notNull(),
  summary: text('summary'),
  status: text('status', { enum: ['applied', 'reverted'] })
    .notNull()
    .default('applied'),
  revertedAt: integer('reverted_at', { mode: 'timestamp' }),
  createdAt: integer('created_at', { mode: 'timestamp' }).notNull(),
}, (table) => [
  index('change_sets_initiative_id_idx').on(table.initiativeId),
]);
 
export const changeSetsRelations = relations(changeSets, ({ one, many }) => ({
  agent: one(agents, {
    fields: [changeSets.agentId],
    references: [agents.id],
  }),
  initiative: one(initiatives, {
    fields: [changeSets.initiativeId],
    references: [initiatives.id],
  }),
  entries: many(changeSetEntries),
}));
 
export type ChangeSet = InferSelectModel<typeof changeSets>;
export type NewChangeSet = InferInsertModel<typeof changeSets>;
 
export const changeSetEntries = sqliteTable('change_set_entries', {
  id: text('id').primaryKey(),
  changeSetId: text('change_set_id')
    .notNull()
    .references(() => changeSets.id, { onDelete: 'cascade' }),
  entityType: text('entity_type', { enum: ['page', 'phase', 'task', 'phase_dependency'] }).notNull(),
  entityId: text('entity_id').notNull(),
  action: text('action', { enum: ['create', 'update', 'delete'] }).notNull(),
  previousState: text('previous_state'), // JSON snapshot, null for creates
  newState: text('new_state'), // JSON snapshot, null for deletes
  sortOrder: integer('sort_order').notNull().default(0),
  createdAt: integer('created_at', { mode: 'timestamp' }).notNull(),
}, (table) => [
  index('change_set_entries_change_set_id_idx').on(table.changeSetId),
]);
 
export const changeSetEntriesRelations = relations(changeSetEntries, ({ one }) => ({
  changeSet: one(changeSets, {
    fields: [changeSetEntries.changeSetId],
    references: [changeSets.id],
  }),
}));
 
export type ChangeSetEntry = InferSelectModel<typeof changeSetEntries>;
export type NewChangeSetEntry = InferInsertModel<typeof changeSetEntries>;
 
// ============================================================================
// MESSAGES
// ============================================================================
 
export const messages = sqliteTable('messages', {
  id: text('id').primaryKey(),
  senderType: text('sender_type', { enum: ['agent', 'user'] }).notNull(),
  senderId: text('sender_id').references(() => agents.id, { onDelete: 'set null' }), // Agent ID if senderType='agent', null for user
  recipientType: text('recipient_type', { enum: ['agent', 'user'] }).notNull(),
  recipientId: text('recipient_id').references(() => agents.id, { onDelete: 'set null' }), // Agent ID if recipientType='agent', null for user
  type: text('type', { enum: ['question', 'info', 'error', 'response'] })
    .notNull()
    .default('info'),
  content: text('content').notNull(),
  requiresResponse: integer('requires_response', { mode: 'boolean' })
    .notNull()
    .default(false),
  status: text('status', { enum: ['pending', 'read', 'responded'] })
    .notNull()
    .default('pending'),
  parentMessageId: text('parent_message_id').references((): ReturnType<typeof text> => messages.id, { onDelete: 'set null' }), // Links response to original question
  createdAt: integer('created_at', { mode: 'timestamp' }).notNull(),
  updatedAt: integer('updated_at', { mode: 'timestamp' }).notNull(),
});
 
export const messagesRelations = relations(messages, ({ one, many }) => ({
  // Sender agent (optional - null for user senders)
  senderAgent: one(agents, {
    fields: [messages.senderId],
    references: [agents.id],
    relationName: 'senderAgent',
  }),
  // Recipient agent (optional - null for user recipients)
  recipientAgent: one(agents, {
    fields: [messages.recipientId],
    references: [agents.id],
    relationName: 'recipientAgent',
  }),
  // Parent message (for threading responses)
  parentMessage: one(messages, {
    fields: [messages.parentMessageId],
    references: [messages.id],
    relationName: 'parentMessage',
  }),
  // Child messages (replies to this message)
  childMessages: many(messages, { relationName: 'parentMessage' }),
}));
 
export type Message = InferSelectModel<typeof messages>;
export type NewMessage = InferInsertModel<typeof messages>;
 
// ============================================================================
// PAGES
// ============================================================================
 
export const pages = sqliteTable('pages', {
  id: text('id').primaryKey(),
  initiativeId: text('initiative_id')
    .notNull()
    .references(() => initiatives.id, { onDelete: 'cascade' }),
  parentPageId: text('parent_page_id').references((): ReturnType<typeof text> => pages.id, { onDelete: 'cascade' }),
  title: text('title').notNull(),
  content: text('content'), // JSON string from Tiptap
  sortOrder: integer('sort_order').notNull().default(0),
  createdAt: integer('created_at', { mode: 'timestamp' }).notNull(),
  updatedAt: integer('updated_at', { mode: 'timestamp' }).notNull(),
});
 
export const pagesRelations = relations(pages, ({ one, many }) => ({
  initiative: one(initiatives, {
    fields: [pages.initiativeId],
    references: [initiatives.id],
  }),
  parentPage: one(pages, {
    fields: [pages.parentPageId],
    references: [pages.id],
    relationName: 'parentPage',
  }),
  childPages: many(pages, { relationName: 'parentPage' }),
}));
 
export type Page = InferSelectModel<typeof pages>;
export type NewPage = InferInsertModel<typeof pages>;
 
// ============================================================================
// PROJECTS
// ============================================================================
 
export const projects = sqliteTable('projects', {
  id: text('id').primaryKey(),
  name: text('name').notNull().unique(),
  url: text('url').notNull().unique(),
  defaultBranch: text('default_branch').notNull().default('main'),
  createdAt: integer('created_at', { mode: 'timestamp' }).notNull(),
  updatedAt: integer('updated_at', { mode: 'timestamp' }).notNull(),
});
 
export const projectsRelations = relations(projects, ({ many }) => ({
  initiativeProjects: many(initiativeProjects),
}));
 
export type Project = InferSelectModel<typeof projects>;
export type NewProject = InferInsertModel<typeof projects>;
 
// ============================================================================
// INITIATIVE PROJECTS (junction)
// ============================================================================
 
export const initiativeProjects = sqliteTable('initiative_projects', {
  id: text('id').primaryKey(),
  initiativeId: text('initiative_id')
    .notNull()
    .references(() => initiatives.id, { onDelete: 'cascade' }),
  projectId: text('project_id')
    .notNull()
    .references(() => projects.id, { onDelete: 'cascade' }),
  createdAt: integer('created_at', { mode: 'timestamp' }).notNull(),
}, (table) => [
  uniqueIndex('initiative_project_unique').on(table.initiativeId, table.projectId),
]);
 
export const initiativeProjectsRelations = relations(initiativeProjects, ({ one }) => ({
  initiative: one(initiatives, {
    fields: [initiativeProjects.initiativeId],
    references: [initiatives.id],
  }),
  project: one(projects, {
    fields: [initiativeProjects.projectId],
    references: [projects.id],
  }),
}));
 
export type InitiativeProject = InferSelectModel<typeof initiativeProjects>;
export type NewInitiativeProject = InferInsertModel<typeof initiativeProjects>;
 
// ============================================================================
// AGENT LOG CHUNKS
// ============================================================================
 
export const agentLogChunks = sqliteTable('agent_log_chunks', {
  id: text('id').primaryKey(),
  agentId: text('agent_id').notNull(),            // NO FK — survives agent deletion
  agentName: text('agent_name').notNull(),         // Snapshot for display after deletion
  sessionNumber: integer('session_number').notNull().default(1),
  content: text('content').notNull(),              // Raw JSONL chunk from file
  createdAt: integer('created_at', { mode: 'timestamp' }).notNull(),
}, (table) => [
  index('agent_log_chunks_agent_id_idx').on(table.agentId),
]);
 
export type AgentLogChunk = InferSelectModel<typeof agentLogChunks>;
export type NewAgentLogChunk = InferInsertModel<typeof agentLogChunks>;
 
// ============================================================================
// CONVERSATIONS (inter-agent communication)
// ============================================================================
 
export const conversations = sqliteTable('conversations', {
  id: text('id').primaryKey(),
  fromAgentId: text('from_agent_id').notNull().references(() => agents.id, { onDelete: 'cascade' }),
  toAgentId: text('to_agent_id').notNull().references(() => agents.id, { onDelete: 'cascade' }),
  initiativeId: text('initiative_id').references(() => initiatives.id, { onDelete: 'set null' }),
  phaseId: text('phase_id').references(() => phases.id, { onDelete: 'set null' }),
  taskId: text('task_id').references(() => tasks.id, { onDelete: 'set null' }),
  question: text('question').notNull(),
  answer: text('answer'),
  status: text('status', { enum: ['pending', 'answered'] }).notNull().default('pending'),
  createdAt: integer('created_at', { mode: 'timestamp' }).notNull(),
  updatedAt: integer('updated_at', { mode: 'timestamp' }).notNull(),
}, (table) => [
  index('conversations_to_agent_status_idx').on(table.toAgentId, table.status),
  index('conversations_from_agent_idx').on(table.fromAgentId),
]);
 
export type Conversation = InferSelectModel<typeof conversations>;
export type NewConversation = InferInsertModel<typeof conversations>;