Codemod db pass-through calls to domain modules: tasks + agents (C1 part 1/3)

Rewrite ~525 call sites from `db.<method>()` to `db.<module>.<method>()`
for tasks and agents domains (including `getAgent`/`findAgent`/`getTask`
which live on the `entityCrud` field). Delete the corresponding
pass-through methods on AppDatabaseBase. Structural types that narrowed
`db` to a bag of flat methods (CapabilityMatchDb, EscalationDeps,
SweepStaleReviewsDeps) are reshaped to match the new namespace. Internal
callback references inside database.ts/database-base.ts now invoke the
owning module directly (e.g. `this.agents.saveAgent`) rather than the
deleted pass-through.

The codemod lives at scripts/codemod/remove-appdatabase-passthrough.mjs
and filters by owning module + optional explicit method list. It is
idempotent and does not re-rewrite already-namespaced calls.
This commit is contained in:
ZacharyZhang-NY committed 2026-04-20 13:17:01 -04:00
1 parent 411294afa6
commit 5e9b7acbc6
95 files changed
+1123 -793

No files matched your search

@@ -29,7 +29,7 @@ describe("Step 4: Hire approval + decideApproval", () => {
decisionNote: "Approved by board",
});
const updatedAgent = shared.db.getAgent(hireId);
const updatedAgent = shared.db.entityCrud.getAgent(hireId);
expect(updatedAgent.status).toBe("idle");
});
@@ -52,7 +52,7 @@ describe("Step 4: Hire approval + decideApproval", () => {
decisionNote: "Not needed",
});
const updatedAgent = shared.db.getAgent(hireId);
const updatedAgent = shared.db.entityCrud.getAgent(hireId);
expect(updatedAgent.status).toBe("terminated");
});
@@ -89,13 +89,13 @@ describe("Agent Messages", () => {
let agentBId: string;
beforeEach(() => {
agentAId = shared.db.saveAgent({
agentAId = shared.db.agents.saveAgent({
companyId: shared.companyId, name: "Alice", role: "Engineer", title: "Senior Engineer",
department: "engineering", connectorId: "claude-pro", status: "active",
capabilities: "Backend development, API design, code review",
budgetMonthlyUsd: 100,
});
agentBId = shared.db.saveAgent({
agentBId = shared.db.agents.saveAgent({
companyId: shared.companyId, name: "Bob", role: "Designer", title: "UX Designer",
department: "design", connectorId: "claude-pro", status: "active",
capabilities: "UI design, wireframing, user research",
@@ -178,7 +178,7 @@ describe("Agent Messages", () => {
leadAgentId: engineeringLeadId,
status: "active",
});
shared.db.saveTask({
shared.db.tasks.saveTask({
companyId: shared.companyId,
title: "Project task",
description: "Do work",
@@ -240,13 +240,13 @@ describe("Auto Task Assignment", () => {
let frontendId: string;
beforeEach(() => {
backendId = shared.db.saveAgent({
backendId = shared.db.agents.saveAgent({
companyId: shared.companyId, name: "Backend Dev", role: "Backend Engineer", title: "Senior Backend",
department: "engineering", connectorId: "openai-codex", status: "active",
capabilities: "API development, database design, Node.js, PostgreSQL, backend testing",
budgetMonthlyUsd: 200,
});
frontendId = shared.db.saveAgent({
frontendId = shared.db.agents.saveAgent({
companyId: shared.companyId, name: "Frontend Dev", role: "Frontend Engineer", title: "Senior Frontend",
department: "engineering", connectorId: "openai-codex", status: "active",
capabilities: "React, TypeScript, CSS, Tailwind, responsive design, frontend testing, UI components",
@@ -255,61 +255,61 @@ describe("Auto Task Assignment", () => {
});
it("assigns to the best-matching agent by capability keywords", () => {
const taskId = shared.db.saveTask({
const taskId = shared.db.tasks.saveTask({
companyId: shared.companyId, title: "Build React component for user dashboard",
description: "Create a responsive React component with Tailwind CSS styling",
priority: "medium", status: "todo",
});
const assignedId = shared.db.autoAssignTask(taskId, shared.companyId);
const assignedId = shared.db.tasks.autoAssignTask(taskId, shared.companyId);
expect(assignedId).toBe(frontendId); // "React", "responsive", "Tailwind" match frontend
});
it("assigns backend task to backend agent", () => {
const taskId = shared.db.saveTask({
const taskId = shared.db.tasks.saveTask({
companyId: shared.companyId, title: "Design database schema for orders",
description: "PostgreSQL database design with API endpoints for order management",
priority: "high", status: "todo",
});
const assignedId = shared.db.autoAssignTask(taskId, shared.companyId);
const assignedId = shared.db.tasks.autoAssignTask(taskId, shared.companyId);
expect(assignedId).toBe(backendId); // "database", "PostgreSQL", "API" match backend
});
it("returns existing assignee if task already assigned", () => {
const taskId = shared.db.saveTask({
const taskId = shared.db.tasks.saveTask({
companyId: shared.companyId, title: "Already assigned task",
description: "This task has an assignee",
assigneeAgentId: frontendId,
priority: "medium", status: "todo",
});
const result = shared.db.autoAssignTask(taskId, shared.companyId);
const result = shared.db.tasks.autoAssignTask(taskId, shared.companyId);
expect(result).toBe(frontendId);
});
it("returns null if all agents have exhausted their budget", () => {
// Set positive budgets that are fully spent
shared.db.saveAgent({ id: backendId, companyId: shared.companyId, name: "Backend Dev", role: "Backend", title: "Backend", connectorId: "openai-codex", status: "active", capabilities: "backend", budgetMonthlyUsd: 100 });
shared.db.saveAgent({ id: frontendId, companyId: shared.companyId, name: "Frontend Dev", role: "Frontend", title: "Frontend", connectorId: "openai-codex", status: "active", capabilities: "frontend", budgetMonthlyUsd: 100 });
shared.db.agents.saveAgent({ id: backendId, companyId: shared.companyId, name: "Backend Dev", role: "Backend", title: "Backend", connectorId: "openai-codex", status: "active", capabilities: "backend", budgetMonthlyUsd: 100 });
shared.db.agents.saveAgent({ id: frontendId, companyId: shared.companyId, name: "Frontend Dev", role: "Frontend", title: "Frontend", connectorId: "openai-codex", status: "active", capabilities: "frontend", budgetMonthlyUsd: 100 });
// Exhaust budgets by recording spending via direct SQL (since finishRun would need a real run)
(shared.db as unknown as { db: { prepare: (sql: string) => { run: (...args: unknown[]) => void } } }).db.prepare("update agents set spent_monthly_usd = 100 where id = ?").run(backendId);
(shared.db as unknown as { db: { prepare: (sql: string) => { run: (...args: unknown[]) => void } } }).db.prepare("update agents set spent_monthly_usd = 100 where id = ?").run(frontendId);
const taskId = shared.db.saveTask({ companyId: shared.companyId, title: "No budget task", description: "Test", priority: "low", status: "todo" });
const result = shared.db.autoAssignTask(taskId, shared.companyId);
const taskId = shared.db.tasks.saveTask({ companyId: shared.companyId, title: "No budget task", description: "Test", priority: "low", status: "todo" });
const result = shared.db.tasks.autoAssignTask(taskId, shared.companyId);
expect(result).toBeNull();
});
it("penalizes agents with many active tasks", () => {
// Give backend agent 5 active tasks
for (let i = 0; i < 5; i++) {
shared.db.saveTask({ companyId: shared.companyId, title: `Backend task ${i}`, description: "", assigneeAgentId: backendId, priority: "medium", status: "in_progress" });
shared.db.tasks.saveTask({ companyId: shared.companyId, title: `Backend task ${i}`, description: "", assigneeAgentId: backendId, priority: "medium", status: "in_progress" });
}
// Generic task with no strong keyword match — should go to less busy frontend agent
const taskId = shared.db.saveTask({ companyId: shared.companyId, title: "Implement feature", description: "Build the feature", priority: "medium", status: "todo" });
const assignedId = shared.db.autoAssignTask(taskId, shared.companyId);
const taskId = shared.db.tasks.saveTask({ companyId: shared.companyId, title: "Implement feature", description: "Build the feature", priority: "medium", status: "todo" });
const assignedId = shared.db.tasks.autoAssignTask(taskId, shared.companyId);
expect(assignedId).toBe(frontendId); // Frontend has 0 active tasks vs backend's 5
});
});
@@ -40,7 +40,7 @@ describe("Step 2: CEO phased prompts", () => {
const ceoId = createAgent({ name: "CEO", role: "CEO" });
const hireId = createAgent({ name: "CTO", role: "CTO", reportsTo: ceoId, status: "pending_approval" as "idle" });
const hire = shared.db.getAgent(hireId);
const hire = shared.db.entityCrud.getAgent(hireId);
const ctx = buildCtx({
agentId: ceoId,
@@ -88,7 +88,7 @@ describe("Step 2: CEO phased prompts", () => {
it("Phase 2: shows normal executive instructions when active reports and no pending org approval", () => {
const ceoId = createAgent({ name: "CEO", role: "CEO" });
const ctoId = createAgent({ name: "CTO", role: "CTO", reportsTo: ceoId, status: "idle" });
const cto = shared.db.getAgent(ctoId);
const cto = shared.db.entityCrud.getAgent(ctoId);
const ctx = buildCtx({
agentId: ceoId,
@@ -109,7 +109,7 @@ describe("Step 2: CEO phased prompts", () => {
it("includes pending approvals section in prompt", () => {
const ceoId = createAgent({ name: "CEO", role: "CEO" });
const ctoId = createAgent({ name: "CTO", role: "CTO", reportsTo: ceoId, status: "idle" });
const cto = shared.db.getAgent(ctoId);
const cto = shared.db.entityCrud.getAgent(ctoId);
const ctx = buildCtx({
agentId: ceoId,
@@ -139,10 +139,10 @@ describe("Step 2: CEO phased prompts", () => {
it("Manager prompt includes credential escalation guidance", () => {
const ceoId = createAgent({ name: "CEO", role: "CEO" });
const ceo = shared.db.getAgent(ceoId);
const ceo = shared.db.entityCrud.getAgent(ceoId);
const managerId = createAgent({ name: "CTO", role: "CTO", reportsTo: ceoId });
const icId = createAgent({ name: "Dev", role: "Engineer", reportsTo: managerId });
const ic = shared.db.getAgent(icId);
const ic = shared.db.entityCrud.getAgent(icId);
const ctx = buildCtx({
agentId: managerId,
@@ -156,7 +156,7 @@ describe("Step 2: CEO phased prompts", () => {
it("IC prompt includes credential escalation guidance", () => {
const ceoId = createAgent({ name: "CEO", role: "CEO" });
const ceo = shared.db.getAgent(ceoId);
const ceo = shared.db.entityCrud.getAgent(ceoId);
const icId = createAgent({ name: "Dev", role: "Engineer", reportsTo: ceoId });
const ctx = buildCtx({
@@ -13,7 +13,7 @@ const { createAgent } = shared;
describe("Step 7: Worker crash recovery (database layer)", () => {
it("interruptStaleRuns clears running and queued runs on init", async () => {
const agentId = createAgent({ name: "Agent" });
const taskId = shared.db.saveTask({
const taskId = shared.db.tasks.saveTask({
companyId: shared.companyId,
title: "In-progress task",
description: "Running when crash happened",
@@ -51,7 +51,7 @@ describe("Step 7: Worker crash recovery (database layer)", () => {
it("finishRun can mark interrupted runs from crash recovery", () => {
const agentId = createAgent({ name: "Agent" });
const taskId = shared.db.saveTask({
const taskId = shared.db.tasks.saveTask({
companyId: shared.companyId,
title: "Crash recovery task",
description: "Test",
@@ -40,8 +40,8 @@ describe("E2E: Full autonomous CEO cold start flow", () => {
});
// CEO now in Phase 1
const ctoRecord = shared.db.getAgent(ctoId);
const cmoRecord = shared.db.getAgent(cmoId);
const ctoRecord = shared.db.entityCrud.getAgent(ctoId);
const cmoRecord = shared.db.entityCrud.getAgent(cmoId);
const phase1Ctx = buildCtx({
agentId: ceoId,
directReports: [ctoRecord, cmoRecord],
@@ -85,15 +85,15 @@ describe("E2E: Full autonomous CEO cold start flow", () => {
shared.db.decideApproval({ approvalId: hireApprovalCMO, state: "approved", decisionNote: "Approved" });
// Agents now idle
expect(shared.db.getAgent(ctoId).status).toBe("idle");
expect(shared.db.getAgent(cmoId).status).toBe("idle");
expect(shared.db.entityCrud.getAgent(ctoId).status).toBe("idle");
expect(shared.db.entityCrud.getAgent(cmoId).status).toBe("idle");
// 5. Board approves org strategy
shared.db.decideApproval({ approvalId: orgApprovalId, state: "approved", decisionNote: "Go ahead" });
// 6. CEO now in Phase 2
const updatedCto = shared.db.getAgent(ctoId);
const updatedCmo = shared.db.getAgent(cmoId);
const updatedCto = shared.db.entityCrud.getAgent(ctoId);
const updatedCmo = shared.db.entityCrud.getAgent(cmoId);
const phase2Ctx = buildCtx({
agentId: ceoId,
directReports: [updatedCto, updatedCmo],
@@ -106,7 +106,7 @@ describe("E2E: Full autonomous CEO cold start flow", () => {
expect(phase2Prompt).toContain("Delegation");
// 7. Verify hierarchical chain
const ctoChain = shared.db.getChainOfCommand(ctoId);
const ctoChain = shared.db.agents.getChainOfCommand(ctoId);
expect(ctoChain).toHaveLength(1);
expect(ctoChain[0].id).toBe(ceoId);
@@ -120,7 +120,7 @@ describe("E2E: Full autonomous CEO cold start flow", () => {
status: "in_progress",
});
const parentTaskId = shared.db.saveTask({
const parentTaskId = shared.db.tasks.saveTask({
companyId: shared.companyId,
title: "Build backend",
description: "Implement API",
@@ -133,7 +133,7 @@ describe("E2E: Full autonomous CEO cold start flow", () => {
// CTO creates subtask for IC (would be done via API)
const icId = createAgent({ name: "Dev", role: "Engineer", reportsTo: ctoId, status: "idle" });
const subtaskId = shared.db.saveTask({
const subtaskId = shared.db.tasks.saveTask({
companyId: shared.companyId,
title: "Implement auth",
description: "JWT auth",
@@ -146,7 +146,7 @@ describe("E2E: Full autonomous CEO cold start flow", () => {
});
// Verify parent-child relationship
const subtask = shared.db.getTask(subtaskId);
const subtask = shared.db.entityCrud.getTask(subtaskId);
expect(subtask.parentId).toBe(parentTaskId);
// 9. Wakeup request test: enqueue wake while IC is busy
@@ -159,7 +159,7 @@ describe("E2E: Full autonomous CEO cold start flow", () => {
const icPromptCtx = buildCtx({
agentId: icId,
task: subtask,
chainOfCommand: [shared.db.getAgent(ctoId), shared.db.getAgent(ceoId)],
chainOfCommand: [shared.db.entityCrud.getAgent(ctoId), shared.db.entityCrud.getAgent(ceoId)],
wakeReason: "assignment",
});
const icPrompt = buildTaskPrompt(icPromptCtx);
@@ -202,11 +202,11 @@ describe("E2E: Blocked escalation through org hierarchy", () => {
const icId = createAgent({ name: "Dev", role: "Engineer", reportsTo: ctoId });
// When IC marks task blocked, we need to find its manager
const chain = shared.db.getChainOfCommand(icId);
const chain = shared.db.agents.getChainOfCommand(icId);
expect(chain[0].id).toBe(ctoId); // immediate manager gets woken
// When CTO needs to escalate further
const ctoChain = shared.db.getChainOfCommand(ctoId);
const ctoChain = shared.db.agents.getChainOfCommand(ctoId);
expect(ctoChain[0].id).toBe(ceoId);
});
});
@@ -216,7 +216,7 @@ describe("Review Fix: CEO phase ordering edge case", () => {
const ceoId = createAgent({ name: "CEO", role: "CEO" });
// Hires submitted but not yet approved
const hireId = createAgent({ name: "CTO", role: "CTO", reportsTo: ceoId, status: "pending_approval" as "idle" });
const hire = shared.db.getAgent(hireId);
const hire = shared.db.entityCrud.getAgent(hireId);
// Org strategy already approved (not in pendingApprovals), but hires still pending
const ctx = buildCtx({
@@ -236,7 +236,7 @@ describe("Review Fix: CEO phase ordering edge case", () => {
it("moves to Phase 2 once hires are approved (no pending org approval)", () => {
const ceoId = createAgent({ name: "CEO", role: "CEO" });
const ctoId = createAgent({ name: "CTO", role: "CTO", reportsTo: ceoId, status: "idle" });
const cto = shared.db.getAgent(ctoId);
const cto = shared.db.entityCrud.getAgent(ctoId);
const ctx = buildCtx({
agentId: ceoId,
@@ -315,7 +315,7 @@ describe("Review Fix: Root task completion notification", () => {
const ctoId = createAgent({ name: "CTO", role: "CTO", reportsTo: ceoId });
// CTO has a root-level task (no parentId)
const taskId = shared.db.saveTask({
const taskId = shared.db.tasks.saveTask({
companyId: shared.companyId,
title: "Standalone task from CEO",
description: "No parent",
@@ -325,11 +325,11 @@ describe("Review Fix: Root task completion notification", () => {
status: "done",
});
const task = shared.db.getTask(taskId);
const task = shared.db.entityCrud.getTask(taskId);
expect(task.parentId).toBeNull();
// CTO's chain of command includes CEO — so api-server can wake CEO on completion
const chain = shared.db.getChainOfCommand(ctoId);
const chain = shared.db.agents.getChainOfCommand(ctoId);
expect(chain).toHaveLength(1);
expect(chain[0].id).toBe(ceoId);
});
@@ -31,9 +31,9 @@ describe("Step 1: CEO role detection", () => {
it("identifies manager when has reports and has chain of command", () => {
const ceoId = createAgent({ name: "CEO", role: "CEO" });
const managerId = createAgent({ name: "CTO", role: "CTO", reportsTo: ceoId });
const ceo = shared.db.getAgent(ceoId);
const ceo = shared.db.entityCrud.getAgent(ceoId);
const icId = createAgent({ name: "IC", role: "Engineer", reportsTo: managerId });
const ic = shared.db.getAgent(icId);
const ic = shared.db.entityCrud.getAgent(icId);
const ctx = buildCtx({
agentId: managerId,
@@ -49,7 +49,7 @@ describe("Step 1: CEO role detection", () => {
it("identifies IC when has chain of command and no direct reports", () => {
const ceoId = createAgent({ name: "CEO", role: "CEO" });
const ceo = shared.db.getAgent(ceoId);
const ceo = shared.db.entityCrud.getAgent(ceoId);
const icId = createAgent({ name: "Dev", role: "Engineer", reportsTo: ceoId });
const ctx = buildCtx({
@@ -12,7 +12,7 @@ const { createAgent } = shared;
describe("Step 6: Run failure escalation (database layer)", () => {
it("finishRun marks run as failed and returns outcome", () => {
const agentId = createAgent({ name: "IC", role: "Engineer" });
const taskId = shared.db.saveTask({
const taskId = shared.db.tasks.saveTask({
companyId: shared.companyId,
title: "Failing task",
description: "Will fail",
@@ -56,7 +56,7 @@ describe("Step 6: Run failure escalation (database layer)", () => {
const ctoId = createAgent({ name: "CTO", role: "CTO", reportsTo: ceoId });
const icId = createAgent({ name: "Dev", role: "Engineer", reportsTo: ctoId });
const chain = shared.db.getChainOfCommand(icId);
const chain = shared.db.agents.getChainOfCommand(icId);
// First in chain is the immediate manager (CTO)
expect(chain.length).toBeGreaterThan(0);
expect(chain[0].name).toBe("CTO");
@@ -30,7 +30,7 @@ export interface SharedHandle {
set db(value: AppDatabase);
get companyId(): string;
get workspaceId(): string;
createAgent(overrides?: Partial<Parameters<AppDatabase["saveAgent"]>[0]>): string;
createAgent(overrides?: Partial<Parameters<AppDatabase["agents"]["saveAgent"]>[0]>): string;
buildCtx(overrides?: Record<string, unknown>): Parameters<typeof buildSystemPrompt>[0];
tempDirs: string[];
}
@@ -80,8 +80,8 @@ export function installSharedSetup(): SharedHandle {
return state.db;
}
function createAgent(overrides: Partial<Parameters<AppDatabase["saveAgent"]>[0]> = {}): string {
return currentDb().saveAgent({
function createAgent(overrides: Partial<Parameters<AppDatabase["agents"]["saveAgent"]>[0]> = {}): string {
return currentDb().agents.saveAgent({
companyId: state.companyId,
name: "TestAgent",
role: "CEO",
@@ -98,7 +98,7 @@ export function installSharedSetup(): SharedHandle {
}
function buildCtx(overrides: Record<string, unknown> = {}): Parameters<typeof buildSystemPrompt>[0] {
const agent = currentDb().getAgent(overrides.agentId as string ?? createAgent());
const agent = currentDb().entityCrud.getAgent(overrides.agentId as string ?? createAgent());
return {
agent,
apiUrl: "http://127.0.0.1:3000",
@@ -15,7 +15,7 @@ describe("Step 5: Task status + comment wake propagation", () => {
const ctoId = createAgent({ name: "CTO", role: "CTO", reportsTo: ceoId });
const icId = createAgent({ name: "Dev", role: "Engineer", reportsTo: ctoId });
const chain = shared.db.getChainOfCommand(icId);
const chain = shared.db.agents.getChainOfCommand(icId);
expect(chain).toHaveLength(2);
expect(chain[0].id).toBe(ctoId); // immediate manager
expect(chain[1].id).toBe(ceoId); // top level
@@ -25,7 +25,7 @@ describe("Step 5: Task status + comment wake propagation", () => {
// Create a potential cycle (shouldn't happen in practice, but guard against it)
const agent1 = createAgent({ name: "Agent1" });
// agent1 reports to no one (CEO), so chain should be empty
const chain = shared.db.getChainOfCommand(agent1);
const chain = shared.db.agents.getChainOfCommand(agent1);
expect(chain).toHaveLength(0);
});
@@ -33,7 +33,7 @@ describe("Step 5: Task status + comment wake propagation", () => {
const ceoId = createAgent({ name: "CEO", role: "CEO" });
const ctoId = createAgent({ name: "CTO", role: "CTO", reportsTo: ceoId });
const parentTaskId = shared.db.saveTask({
const parentTaskId = shared.db.tasks.saveTask({
companyId: shared.companyId,
title: "Build feature X",
description: "Top-level task",
@@ -43,7 +43,7 @@ describe("Step 5: Task status + comment wake propagation", () => {
status: "in_progress",
});
const childTaskId = shared.db.saveTask({
const childTaskId = shared.db.tasks.saveTask({
companyId: shared.companyId,
title: "Implement module A",
description: "Subtask",
@@ -54,10 +54,10 @@ describe("Step 5: Task status + comment wake propagation", () => {
status: "todo",
});
const childTask = shared.db.getTask(childTaskId);
const childTask = shared.db.entityCrud.getTask(childTaskId);
expect(childTask.parentId).toBe(parentTaskId);
const parentTask = shared.db.getTask(parentTaskId);
const parentTask = shared.db.entityCrud.getTask(parentTaskId);
expect(parentTask.assigneeAgentId).toBe(ctoId);
});
});
@@ -14,7 +14,7 @@ describe("Step 8: Wake reason injection", () => {
it("includes wake reason section for non-timer triggers", () => {
const agentId = createAgent({ name: "CTO", role: "CTO" });
const ceoId = createAgent({ name: "CEO", role: "CEO" });
const ceo = shared.db.getAgent(ceoId);
const ceo = shared.db.entityCrud.getAgent(ceoId);
const ctx = buildCtx({
agentId,
@@ -92,7 +92,7 @@ describe("Step 8: Wake reason injection", () => {
it("includes wake reason before task description", () => {
const agentId = createAgent({ name: "Agent" });
const taskId = shared.db.saveTask({
const taskId = shared.db.tasks.saveTask({
companyId: shared.companyId,
title: "Do the thing",
description: "Test task",
@@ -101,7 +101,7 @@ describe("Step 8: Wake reason injection", () => {
priority: "medium",
status: "todo",
});
const task = shared.db.getTask(taskId);
const task = shared.db.entityCrud.getTask(taskId);
const ctx = buildCtx({
agentId,