Khala Phase 1: D1 schema & data access layer #125
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Parent: #119
Objective
Create D1 migration and type-safe data access functions.
Tasks
1.1 D1 Migration
Create
packages/khala/migrations/0001_initial.sqlwith tables:agents(id TEXT PK, token_hash TEXT, created_at TEXT)threads(id TEXT PK, workflow TEXT, status TEXT, initiator TEXT, result TEXT, created_at, updated_at)messages(id INTEGER PK AUTOINCREMENT, thread_id, role, content, meta, step INTEGER, agent_id, created_at) + index on (thread_id, step)tasks(id TEXT PK, thread_id, role, instruction, status TEXT default open, claim_id, claimed_by, claimed_at, timeout_seconds INTEGER default 300, created_at) + index on (status, created_at)1.2 Types & Data Access
packages/khala/src/types.ts— row types for all tablespackages/khala/src/db.ts— query functions:createThread(db, workflow, initiator)appendMessage(db, threadId, role, content, meta, step, agentId)createTask(db, threadId, role, instruction, timeoutSeconds)claimTask(db, taskId, agentId)→{ ok, claimId }(atomic UPDATE WHERE status=open)completeTask(db, taskId, claimId)→ booleanexpireTimedOutTasks(db)→ countgetOpenTasks(db, limit)getThreadMessages(db, threadId, opts?)(filter by role, since, step, last)Use
ulidxfor ID generation. Add to package.json deps.Rules
typenotinterface,functionnotclass, no?:(useT | null)CLAUDE.mdfor full conventionsReferences
docs/plans/2026-04-25-khala-mvp.md(Task 1.1, 1.2)