feat(#164): Add database schema for job tracking
Add Prisma schema for runner jobs, job steps, and job events to support the autonomous runner infrastructure (M4.2). Enums added: - RunnerJobStatus: PENDING, QUEUED, RUNNING, COMPLETED, FAILED, CANCELLED - JobStepPhase: SETUP, EXECUTION, VALIDATION, CLEANUP - JobStepType: COMMAND, AI_ACTION, GATE, ARTIFACT - JobStepStatus: PENDING, RUNNING, COMPLETED, FAILED, SKIPPED Models added: - RunnerJob: Top-level job tracking linked to workspace and agent_tasks - JobStep: Granular step tracking within jobs with phase organization - JobEvent: Immutable event sourcing audit log for jobs and steps Foreign key relationships: - runner_jobs → workspaces (workspace_id, CASCADE) - runner_jobs → agent_tasks (agent_task_id, SET NULL) - job_steps → runner_jobs (job_id, CASCADE) - job_events → runner_jobs (job_id, CASCADE) - job_events → job_steps (step_id, CASCADE) Indexes added for performance on workspace_id, status, priority, timestamp. Migration: 20260201205935_add_job_tracking Quality gates passed: typecheck, lint, build Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
-- CreateEnum
|
||||
CREATE TYPE "RunnerJobStatus" AS ENUM ('PENDING', 'QUEUED', 'RUNNING', 'COMPLETED', 'FAILED', 'CANCELLED');
|
||||
|
||||
-- CreateEnum
|
||||
CREATE TYPE "JobStepPhase" AS ENUM ('SETUP', 'EXECUTION', 'VALIDATION', 'CLEANUP');
|
||||
|
||||
-- CreateEnum
|
||||
CREATE TYPE "JobStepType" AS ENUM ('COMMAND', 'AI_ACTION', 'GATE', 'ARTIFACT');
|
||||
|
||||
-- CreateEnum
|
||||
CREATE TYPE "JobStepStatus" AS ENUM ('PENDING', 'RUNNING', 'COMPLETED', 'FAILED', 'SKIPPED');
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "runner_jobs" (
|
||||
"id" UUID NOT NULL,
|
||||
"workspace_id" UUID NOT NULL,
|
||||
"agent_task_id" UUID,
|
||||
"type" TEXT NOT NULL,
|
||||
"status" "RunnerJobStatus" NOT NULL DEFAULT 'PENDING',
|
||||
"priority" INTEGER NOT NULL,
|
||||
"progress_percent" INTEGER NOT NULL DEFAULT 0,
|
||||
"result" JSONB,
|
||||
"error" TEXT,
|
||||
"created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"started_at" TIMESTAMPTZ,
|
||||
"completed_at" TIMESTAMPTZ,
|
||||
|
||||
CONSTRAINT "runner_jobs_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "job_steps" (
|
||||
"id" UUID NOT NULL,
|
||||
"job_id" UUID NOT NULL,
|
||||
"ordinal" INTEGER NOT NULL,
|
||||
"phase" "JobStepPhase" NOT NULL,
|
||||
"name" TEXT NOT NULL,
|
||||
"type" "JobStepType" NOT NULL,
|
||||
"status" "JobStepStatus" NOT NULL DEFAULT 'PENDING',
|
||||
"output" TEXT,
|
||||
"tokens_input" INTEGER,
|
||||
"tokens_output" INTEGER,
|
||||
"started_at" TIMESTAMPTZ,
|
||||
"completed_at" TIMESTAMPTZ,
|
||||
"duration_ms" INTEGER,
|
||||
|
||||
CONSTRAINT "job_steps_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "job_events" (
|
||||
"id" UUID NOT NULL,
|
||||
"job_id" UUID NOT NULL,
|
||||
"step_id" UUID,
|
||||
"type" TEXT NOT NULL,
|
||||
"timestamp" TIMESTAMPTZ NOT NULL,
|
||||
"actor" TEXT NOT NULL,
|
||||
"payload" JSONB NOT NULL,
|
||||
|
||||
CONSTRAINT "job_events_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "runner_jobs_id_workspace_id_key" ON "runner_jobs"("id", "workspace_id");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "runner_jobs_workspace_id_idx" ON "runner_jobs"("workspace_id");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "runner_jobs_workspace_id_status_idx" ON "runner_jobs"("workspace_id", "status");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "runner_jobs_agent_task_id_idx" ON "runner_jobs"("agent_task_id");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "runner_jobs_priority_idx" ON "runner_jobs"("priority");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "job_steps_job_id_idx" ON "job_steps"("job_id");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "job_steps_job_id_ordinal_idx" ON "job_steps"("job_id", "ordinal");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "job_steps_status_idx" ON "job_steps"("status");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "job_events_job_id_idx" ON "job_events"("job_id");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "job_events_step_id_idx" ON "job_events"("step_id");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "job_events_timestamp_idx" ON "job_events"("timestamp");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "job_events_type_idx" ON "job_events"("type");
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "runner_jobs" ADD CONSTRAINT "runner_jobs_workspace_id_fkey" FOREIGN KEY ("workspace_id") REFERENCES "workspaces"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "runner_jobs" ADD CONSTRAINT "runner_jobs_agent_task_id_fkey" FOREIGN KEY ("agent_task_id") REFERENCES "agent_tasks"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "job_steps" ADD CONSTRAINT "job_steps_job_id_fkey" FOREIGN KEY ("job_id") REFERENCES "runner_jobs"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "job_events" ADD CONSTRAINT "job_events_job_id_fkey" FOREIGN KEY ("job_id") REFERENCES "runner_jobs"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "job_events" ADD CONSTRAINT "job_events_step_id_fkey" FOREIGN KEY ("step_id") REFERENCES "job_steps"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
@@ -135,6 +135,37 @@ enum FormalityLevel {
|
||||
VERY_FORMAL
|
||||
}
|
||||
|
||||
enum RunnerJobStatus {
|
||||
PENDING
|
||||
QUEUED
|
||||
RUNNING
|
||||
COMPLETED
|
||||
FAILED
|
||||
CANCELLED
|
||||
}
|
||||
|
||||
enum JobStepPhase {
|
||||
SETUP
|
||||
EXECUTION
|
||||
VALIDATION
|
||||
CLEANUP
|
||||
}
|
||||
|
||||
enum JobStepType {
|
||||
COMMAND
|
||||
AI_ACTION
|
||||
GATE
|
||||
ARTIFACT
|
||||
}
|
||||
|
||||
enum JobStepStatus {
|
||||
PENDING
|
||||
RUNNING
|
||||
COMPLETED
|
||||
FAILED
|
||||
SKIPPED
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// MODELS
|
||||
// ============================================
|
||||
@@ -216,6 +247,7 @@ model Workspace {
|
||||
personalities Personality[]
|
||||
llmSettings WorkspaceLlmSettings?
|
||||
qualityGates QualityGate[]
|
||||
runnerJobs RunnerJob[]
|
||||
|
||||
@@index([ownerId])
|
||||
@@map("workspaces")
|
||||
@@ -592,6 +624,7 @@ model AgentTask {
|
||||
workspace Workspace @relation(fields: [workspaceId], references: [id], onDelete: Cascade)
|
||||
createdBy User @relation("AgentTaskCreator", fields: [createdById], references: [id], onDelete: Cascade)
|
||||
createdById String @map("created_by_id") @db.Uuid
|
||||
runnerJobs RunnerJob[]
|
||||
|
||||
@@unique([id, workspaceId])
|
||||
@@index([workspaceId])
|
||||
@@ -1087,3 +1120,94 @@ model TokenBudget {
|
||||
@@index([suspiciousPattern])
|
||||
@@map("token_budgets")
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// RUNNER JOB TRACKING MODULE
|
||||
// ============================================
|
||||
|
||||
model RunnerJob {
|
||||
id String @id @default(uuid()) @db.Uuid
|
||||
workspaceId String @map("workspace_id") @db.Uuid
|
||||
agentTaskId String? @map("agent_task_id") @db.Uuid
|
||||
|
||||
// Job details
|
||||
type String // 'git-status', 'code-task', 'priority-calc'
|
||||
status RunnerJobStatus @default(PENDING)
|
||||
priority Int
|
||||
progressPercent Int @default(0) @map("progress_percent")
|
||||
|
||||
// Results
|
||||
result Json?
|
||||
error String? @db.Text
|
||||
|
||||
// Timing
|
||||
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
|
||||
startedAt DateTime? @map("started_at") @db.Timestamptz
|
||||
completedAt DateTime? @map("completed_at") @db.Timestamptz
|
||||
|
||||
// Relations
|
||||
workspace Workspace @relation(fields: [workspaceId], references: [id], onDelete: Cascade)
|
||||
agentTask AgentTask? @relation(fields: [agentTaskId], references: [id], onDelete: SetNull)
|
||||
steps JobStep[]
|
||||
events JobEvent[]
|
||||
|
||||
@@unique([id, workspaceId])
|
||||
@@index([workspaceId])
|
||||
@@index([workspaceId, status])
|
||||
@@index([agentTaskId])
|
||||
@@index([priority])
|
||||
@@map("runner_jobs")
|
||||
}
|
||||
|
||||
model JobStep {
|
||||
id String @id @default(uuid()) @db.Uuid
|
||||
jobId String @map("job_id") @db.Uuid
|
||||
|
||||
// Step details
|
||||
ordinal Int
|
||||
phase JobStepPhase
|
||||
name String
|
||||
type JobStepType
|
||||
status JobStepStatus @default(PENDING)
|
||||
|
||||
// Output and metrics
|
||||
output String? @db.Text
|
||||
tokensInput Int? @map("tokens_input")
|
||||
tokensOutput Int? @map("tokens_output")
|
||||
|
||||
// Timing
|
||||
startedAt DateTime? @map("started_at") @db.Timestamptz
|
||||
completedAt DateTime? @map("completed_at") @db.Timestamptz
|
||||
durationMs Int? @map("duration_ms")
|
||||
|
||||
// Relations
|
||||
job RunnerJob @relation(fields: [jobId], references: [id], onDelete: Cascade)
|
||||
events JobEvent[]
|
||||
|
||||
@@index([jobId])
|
||||
@@index([jobId, ordinal])
|
||||
@@index([status])
|
||||
@@map("job_steps")
|
||||
}
|
||||
|
||||
model JobEvent {
|
||||
id String @id @default(uuid()) @db.Uuid
|
||||
jobId String @map("job_id") @db.Uuid
|
||||
stepId String? @map("step_id") @db.Uuid
|
||||
|
||||
// Event details
|
||||
type String
|
||||
timestamp DateTime @db.Timestamptz
|
||||
actor String
|
||||
payload Json
|
||||
|
||||
// Relations
|
||||
job RunnerJob @relation(fields: [jobId], references: [id], onDelete: Cascade)
|
||||
step JobStep? @relation(fields: [stepId], references: [id], onDelete: Cascade)
|
||||
|
||||
@@index([jobId])
|
||||
@@index([stepId])
|
||||
@@index([timestamp])
|
||||
@@index([type])
|
||||
@@map("job_events")
|
||||
}
|
||||
|
||||
109
docs/scratchpads/164-database-schema-jobs.md
Normal file
109
docs/scratchpads/164-database-schema-jobs.md
Normal file
@@ -0,0 +1,109 @@
|
||||
# Issue #164: Database schema for job tracking
|
||||
|
||||
## Objective
|
||||
|
||||
Add Prisma schema for runner_jobs, job_steps, and job_events tables to support the autonomous runner infrastructure.
|
||||
|
||||
## Approach
|
||||
|
||||
1. Read existing schema.prisma to understand current conventions
|
||||
2. Add four enums: RunnerJobStatus, JobStepPhase, JobStepType, JobStepStatus
|
||||
3. Add three models: RunnerJob, JobStep, JobEvent
|
||||
4. Create and run migration
|
||||
5. Verify migration succeeds
|
||||
|
||||
## Schema Design
|
||||
|
||||
### Enums
|
||||
|
||||
- **RunnerJobStatus**: PENDING, QUEUED, RUNNING, COMPLETED, FAILED, CANCELLED
|
||||
- **JobStepPhase**: SETUP, EXECUTION, VALIDATION, CLEANUP
|
||||
- **JobStepType**: COMMAND, AI_ACTION, GATE, ARTIFACT
|
||||
- **JobStepStatus**: PENDING, RUNNING, COMPLETED, FAILED, SKIPPED
|
||||
|
||||
### Models
|
||||
|
||||
1. **RunnerJob** - Top-level job tracking
|
||||
- Links to workspace and optionally to agent_task
|
||||
- Tracks overall job status, progress, result
|
||||
- Timestamps: created_at, started_at, completed_at
|
||||
|
||||
2. **JobStep** - Granular step tracking
|
||||
- Child of RunnerJob
|
||||
- Phase-based organization (SETUP, EXECUTION, etc.)
|
||||
- Token tracking for AI operations
|
||||
- Duration tracking
|
||||
|
||||
3. **JobEvent** - Event sourcing audit log
|
||||
- Immutable event log for jobs and steps
|
||||
- Links to both job and optionally step
|
||||
- Actor tracking for accountability
|
||||
|
||||
## Progress
|
||||
|
||||
- [x] Read existing schema.prisma
|
||||
- [x] Read architecture document for schema requirements
|
||||
- [x] Add enums (RunnerJobStatus, JobStepPhase, JobStepType, JobStepStatus)
|
||||
- [x] Add RunnerJob model with workspace and agentTask relations
|
||||
- [x] Add JobStep model with job relation
|
||||
- [x] Add JobEvent model with job and step relations
|
||||
- [x] Add RunnerJob[] to Workspace and AgentTask relations
|
||||
- [x] Create migration (20260201205935_add_job_tracking)
|
||||
- [x] Test migration - all tables created successfully
|
||||
- [x] Run quality gates (typecheck, lint, build - all passed)
|
||||
- [x] Generate Prisma client
|
||||
- [ ] Commit changes
|
||||
|
||||
## Schema Observations from Existing Code
|
||||
|
||||
**Conventions Identified:**
|
||||
|
||||
- UUID primary keys with `@db.Uuid` annotation
|
||||
- snake_case for database column names via `@map`
|
||||
- snake_case for table names via `@@map`
|
||||
- Timestamps use `@db.Timestamptz` for timezone awareness
|
||||
- workspace_id on all workspace-scoped tables with cascading deletes
|
||||
- Composite unique constraints with `@@unique([id, workspaceId])`
|
||||
- Consistent indexing patterns: workspace_id, status, timestamps
|
||||
- Json fields for flexible metadata with `@default("{}")`
|
||||
- Optional foreign keys use `@db.Uuid` without NOT NULL
|
||||
- Relations use descriptive names in both directions
|
||||
|
||||
## Testing
|
||||
|
||||
Since this is a schema-only change, testing will verify:
|
||||
|
||||
- Migration runs successfully ✅
|
||||
- Foreign key constraints are valid ✅
|
||||
- Schema matches architecture document ✅
|
||||
|
||||
Verification performed:
|
||||
|
||||
1. Database tables created: runner_jobs, job_steps, job_events
|
||||
2. All enums created: RunnerJobStatus, JobStepPhase, JobStepType, JobStepStatus
|
||||
3. Foreign key relationships verified:
|
||||
- runner_jobs → workspaces (workspace_id)
|
||||
- runner_jobs → agent_tasks (agent_task_id, optional)
|
||||
- job_steps → runner_jobs (job_id)
|
||||
- job_events → runner_jobs (job_id)
|
||||
- job_events → job_steps (step_id, optional)
|
||||
4. Indexes created for performance:
|
||||
- workspace_id for workspace filtering
|
||||
- status for job querying
|
||||
- priority for job prioritization
|
||||
- timestamp for event ordering
|
||||
5. Quality gates passed:
|
||||
- TypeScript compilation ✅
|
||||
- ESLint checks ✅
|
||||
- NestJS build ✅
|
||||
- Prisma client generation ✅
|
||||
|
||||
## Notes
|
||||
|
||||
- Following existing patterns from schema.prisma
|
||||
- Using UUID for all primary keys (existing convention)
|
||||
- Using snake_case for table names (Prisma convention)
|
||||
- All workspace-scoped tables include workspace_id for RLS
|
||||
- Migration file created: 20260201205935_add_job_tracking
|
||||
- Database push successful, migration marked as applied
|
||||
- Schema format validated successfully
|
||||
Reference in New Issue
Block a user