refactor(database): split 5 god modules into domain-scoped files

Five files under `src/main/database/` exceeded 400 LOC by combining
multiple responsibilities. Split along natural fault lines. Zero
behavior change — each original `build*Methods` factory still returns
the same method surface, now via spread of smaller builders.

| Original                          | Split into                          | LOC each       |
|-----------------------------------|-------------------------------------|----------------|
| tasks-crud.ts (640)               | tasks-crud / task-sessions-crud /   | 250 / 252 /    |
|                                   | task-queries                        | 177            |
| agents-hierarchy.ts (487)         | agents-hierarchy / agent-permissions| 350 / 151      |
| company-settings.ts (476)         | company-settings / company-metrics  | 122 / 370      |
| documents.ts (426)                | documents / config-revisions        | 318 / 126      |
| runs.ts (413)                     | runs / run-logs / run-events /      | 245 / 96 /     |
|                                   | cost-entries                        | 82 / 52        |

AppDatabase call sites unchanged:
- db.tasks.saveTask, db.tasks.ensureAgentSession (task-sessions-crud),
  db.tasks.listAgentTasks (task-queries)
- db.agents.getChainOfCommand, db.agents.hasAgentPermission
- db.company.getCompanySettings, db.company.getStandupReport
  (company-metrics)
- db.documentsMod.saveDocument, db.documentsMod.recordConfigRevision
- db.runs.createRun, db.runs.appendRunLog (run-logs),
  db.runs.appendRunEvent (run-events), db.runs.recordCostEntry
  (cost-entries)

`database-base.ts` (449) kept as-is — thin facade where every
pass-through is one line; splitting adds mechanical files without
behavior benefit. Will revisit if it grows further.

Aggregator files (`tasks.ts`, `agents.ts`, `company.ts`) each import
and spread the new sub-builders. No renamed exports.
This commit is contained in:
ZacharyZhang-NY committed 2026-04-17 16:35:19 -04:00
1 parent f012f8e880
commit dbdec86cdd
16 files changed
+1329 -1172

No files matched your search

-390
View File
@@ -1,13 +1,10 @@
import { randomUUID } from "node:crypto";
import type { DatabaseSync } from "node:sqlite";
import type { TaskRecord, TaskSessionRecord } from "@shared/types";
import {
type ReadAll,
type Transaction,
ensureId,
normalizeOptionalId,
nowIso,
taskPriorityOrderSql,
} from "./helpers";
/**
@@ -168,360 +165,6 @@ export function buildTaskCrudMethods(
});
},
/**
* Returns actionable real work for an agent.
*/
listAgentTasks(
agentId: string,
companyId: string,
statuses: string[],
): TaskRecord[] {
const placeholders = statuses.map(() => "?").join(",");
return readAll<TaskRecord>(
`select id, company_id as companyId, project_id as projectId, goal_id as goalId, parent_id as parentId, title, description, assignee_agent_id as assigneeAgentId, workspace_id as workspaceId, status, priority, active_run_id as activeRunId, session_json as sessionJson, session_display_id as sessionDisplayId, cost_usd as costUsd, task_type as taskType, created_at as createdAt, updated_at as updatedAt
from tasks
where company_id = ? and assignee_agent_id = ? and status in (${placeholders})
order by
case status
when 'in_progress' then 0
when 'todo' then 1
when 'blocked' then 2
when 'backlog' then 3
else 4
end asc,
${taskPriorityOrderSql("priority")} desc,
created_at asc`,
companyId,
agentId,
...statuses,
);
},
/**
* Returns the slim task projection (id, title, status, priority) for a
* single assignee, ordered by created_at desc.
*/
listAssigneeTasksForStandup(
companyId: string,
agentId: string,
): Array<{ id: string; title: string; status: string; priority: string }> {
return readAll(
"select id, title, status, priority from tasks where company_id = ? and assignee_agent_id = ? order by created_at desc",
companyId,
agentId,
);
},
/**
* Returns the slim task projection for a set of assignees. Safe on
* an empty assignee list (returns []).
*/
listTasksForAssignees(
companyId: string,
assigneeIds: string[],
): Array<{
id: string;
title: string;
status: string;
priority: string;
assigneeAgentId: string;
}> {
if (assigneeIds.length === 0) return [];
const placeholders = assigneeIds.map(() => "?").join(",");
return readAll(
`select id, title, status, priority, assignee_agent_id as assigneeAgentId from tasks where company_id = ? and assignee_agent_id in (${placeholders}) order by created_at desc`,
companyId,
...assigneeIds,
);
},
/**
* Returns the slim task projection used by the me/standup aggregation.
* Ordered by created_at desc.
*/
listTasksForStandup(companyId: string): Array<{
id: string;
companyId: string;
title: string;
assigneeAgentId: string | null;
status: string;
priority: string;
}> {
return readAll(
"select id, company_id as companyId, title, assignee_agent_id as assigneeAgentId, status, priority from tasks where company_id = ? order by created_at desc",
companyId,
);
},
/**
* Returns tasks filtered by status.
*/
listTasksByStatuses(
companyId: string,
statuses: string[],
): TaskListByStatusesRow[] {
const placeholders = statuses.map(() => "?").join(", ");
return readAll<TaskListByStatusesRow & { requiresUserReview: number | boolean }>(
`select id, company_id as companyId, project_id as projectId, goal_id as goalId, parent_id as parentId, title, description, assignee_agent_id as assigneeAgentId, workspace_id as workspaceId, status, priority, active_run_id as activeRunId, session_json as sessionJson, session_display_id as sessionDisplayId, cost_usd as costUsd, requires_user_review as requiresUserReview, started_at as startedAt, completed_at as completedAt, cancelled_at as cancelledAt, created_at as createdAt, updated_at as updatedAt from tasks where company_id = ? and status in (${placeholders}) order by created_at desc`,
companyId,
...statuses,
).map((t) => ({
...t,
requiresUserReview: Boolean(t.requiresUserReview),
startedAt: t.startedAt ?? null,
completedAt: t.completedAt ?? null,
cancelledAt: t.cancelledAt ?? null,
})) as TaskListByStatusesRow[];
},
countActiveTasksForCompany(companyId: string): number {
const row = getDb()
.prepare(
"select count(*) as cnt from tasks where company_id = ? and status not in ('done', 'cancelled')",
)
.get(companyId) as { cnt: number };
return row.cnt;
},
listChildTasks(
parentId: string,
companyId: string,
): Array<{ id: string; status: string; assigneeAgentId: string | null }> {
return readAll<{ id: string; status: string; assigneeAgentId: string | null }>(
`select id, status, assignee_agent_id as assigneeAgentId from tasks where parent_id = ? and company_id = ?`,
parentId,
companyId,
);
},
updateTaskSession(
taskId: string,
sessionJson: Record<string, unknown> | null,
sessionDisplayId: string | null,
): void {
const ts = nowIso();
const sessionParamsJson = sessionJson ? JSON.stringify(sessionJson) : null;
const taskRow = getDb()
.prepare(
"select company_id as companyId, assignee_agent_id as assigneeAgentId, active_session_id as activeSessionId from tasks where id = ?",
)
.get(taskId) as
| { companyId: string; assigneeAgentId: string | null; activeSessionId: string | null }
| undefined;
if (!taskRow) throw new Error(`Task not found: ${taskId}`);
if (taskRow.activeSessionId) {
getDb()
.prepare(
"update task_sessions set session_params_json = ?, session_display_id = ?, updated_at = ? where id = ?",
)
.run(sessionParamsJson, sessionDisplayId, ts, taskRow.activeSessionId);
return;
}
if (sessionParamsJson === null && sessionDisplayId === null) return;
const agentId = taskRow.assigneeAgentId ?? "unassigned";
const agent = taskRow.assigneeAgentId
? (getDb()
.prepare("select connector_id as connectorId from agents where id = ?")
.get(taskRow.assigneeAgentId) as { connectorId: string } | undefined)
: undefined;
const adapterType = agent?.connectorId ?? "unknown";
const sessionId = randomUUID();
getDb()
.prepare(
`insert into task_sessions (id, company_id, task_id, agent_id, adapter_type, session_params_json, session_display_id, created_at, updated_at)
values (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
)
.run(
sessionId,
taskRow.companyId,
taskId,
agentId,
adapterType,
sessionParamsJson,
sessionDisplayId,
ts,
ts,
);
getDb()
.prepare("update tasks set active_session_id = ?, updated_at = ? where id = ?")
.run(sessionId, ts, taskId);
},
getTaskSession(taskId: string): {
sessionParams: Record<string, unknown> | null;
sessionDisplayId: string | null;
} {
const taskRow = getDb()
.prepare("select active_session_id as activeSessionId from tasks where id = ?")
.get(taskId) as { activeSessionId: string | null } | undefined;
if (!taskRow?.activeSessionId) {
return { sessionParams: null, sessionDisplayId: null };
}
const row = getDb()
.prepare(
"select session_params_json as sessionParamsJson, session_display_id as sessionDisplayId from task_sessions where id = ?",
)
.get(taskRow.activeSessionId) as
| { sessionParamsJson: string | null; sessionDisplayId: string | null }
| undefined;
let sessionParams: Record<string, unknown> | null = null;
if (row?.sessionParamsJson) {
sessionParams = JSON.parse(row.sessionParamsJson) as Record<string, unknown>;
}
return {
sessionParams,
sessionDisplayId: row?.sessionDisplayId ?? null,
};
},
getActiveTaskSession(taskId: string): TaskSessionRecord | null {
const task = getDb()
.prepare("SELECT active_session_id FROM tasks WHERE id = ?")
.get(taskId) as { active_session_id: string | null } | undefined;
if (!task?.active_session_id) return null;
return (
(getDb()
.prepare(
`SELECT id, company_id as companyId, task_id as taskId, agent_id as agentId,
adapter_type as adapterType, session_params_json as sessionParamsJson,
session_display_id as sessionDisplayId, last_run_id as lastRunId,
last_error as lastError, run_count as runCount,
total_input_tokens as totalInputTokens, total_output_tokens as totalOutputTokens,
rotated_at as rotatedAt, rotation_reason as rotationReason,
handoff_markdown as handoffMarkdown, created_at as createdAt, updated_at as updatedAt
FROM task_sessions WHERE id = ?`,
)
.get(task.active_session_id) as TaskSessionRecord | undefined) ?? null
);
},
incrementSessionUsage(
sessionId: string,
inputTokens: number,
outputTokens: number,
runId: string,
): void {
const ts = nowIso();
getDb()
.prepare(
`UPDATE task_sessions
SET run_count = run_count + 1,
total_input_tokens = total_input_tokens + ?,
total_output_tokens = total_output_tokens + ?,
last_run_id = ?,
updated_at = ?
WHERE id = ?`,
)
.run(inputTokens, outputTokens, runId, ts, sessionId);
},
archiveSession(sessionId: string, reason: string, handoffMarkdown: string): void {
const ts = nowIso();
getDb()
.prepare(
`UPDATE task_sessions SET rotated_at = ?, rotation_reason = ?, handoff_markdown = ?, updated_at = ? WHERE id = ?`,
)
.run(ts, reason, handoffMarkdown, ts, sessionId);
},
/**
* Returns the agent's active session id, creating one lazily on first
* call. Session is scoped to the agent (not to any particular task);
* switching tasks emits a user turn on the ongoing transcript rather
* than spawning a new session.
*
* Existing agents with pre-migration per-task sessions start fresh
* after this upgrade: their first run creates a new agent-scoped
* session. The old task_sessions + session_entries rows remain in the
* DB as historical records but are not loaded for new runs. This is a
* one-time transcript reset, accepted over a risky merge on a live
* production DB.
*/
ensureAgentSession(input: {
agentId: string;
companyId: string;
adapterType: string;
}): string {
const agentRow = getDb()
.prepare("SELECT session_id FROM agents WHERE id = ?")
.get(input.agentId) as { session_id: string | null } | undefined;
if (agentRow?.session_id) return agentRow.session_id;
const sessionId = randomUUID();
const ts = nowIso();
transaction(() => {
// task_id is left as empty string — the session is agent-scoped;
// the column persists for backwards-compatible schema but no
// longer keys the session to a particular task.
getDb()
.prepare(
`INSERT INTO task_sessions (id, company_id, task_id, agent_id, adapter_type,
session_params_json, session_display_id, run_count, total_input_tokens,
total_output_tokens, created_at, updated_at)
VALUES (?, ?, '', ?, ?, NULL, NULL, 0, 0, 0, ?, ?)`,
)
.run(sessionId, input.companyId, input.agentId, input.adapterType, ts, ts);
getDb()
.prepare("UPDATE agents SET session_id = ?, updated_at = ? WHERE id = ?")
.run(sessionId, ts, input.agentId);
});
return sessionId;
},
setAgentSessionId(agentId: string, sessionId: string): void {
const ts = nowIso();
getDb()
.prepare("UPDATE agents SET session_id = ?, updated_at = ? WHERE id = ?")
.run(sessionId, ts, agentId);
},
/**
* Point every task currently assigned to an agent at the agent's new
* active session. Called after session rotation so UI paths reading
* per-task active_session_id still see the agent's current session.
*/
updateTasksActiveSessionForAgent(agentId: string, sessionId: string): void {
const ts = nowIso();
getDb()
.prepare(
"UPDATE tasks SET active_session_id = ?, updated_at = ? WHERE assignee_agent_id = ?",
)
.run(sessionId, ts, agentId);
},
createNewSession(input: {
taskId: string;
companyId: string;
agentId: string;
adapterType: string;
sessionParamsJson: string | null;
sessionDisplayId: string | null;
}): string {
const sessionId = randomUUID();
const ts = nowIso();
getDb()
.prepare(
`INSERT INTO task_sessions (id, company_id, task_id, agent_id, adapter_type,
session_params_json, session_display_id, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
)
.run(
sessionId,
input.companyId,
input.taskId,
input.agentId,
input.adapterType,
input.sessionParamsJson,
input.sessionDisplayId,
ts,
ts,
);
getDb()
.prepare("UPDATE tasks SET active_session_id = ?, updated_at = ? WHERE id = ?")
.run(sessionId, ts, input.taskId);
return sessionId;
},
getRecentRunsForTask(
taskId: string,
limit: number,
@@ -591,39 +234,6 @@ export function buildTaskCrudMethods(
.run(ts, taskId);
},
/**
* Returns the set of distinct assignee agent ids on tasks in a project,
* excluding unassigned tasks. Used by message-dispatch to fan out wake
* targets for a project channel.
*/
listProjectTaskAssigneeIds(projectId: string, companyId: string): string[] {
const rows = readAll<{ assigneeAgentId: string | null }>(
"select distinct assignee_agent_id as assigneeAgentId from tasks where project_id = ? and company_id = ? and assignee_agent_id is not null",
projectId,
companyId,
);
return rows.map((r) => r.assigneeAgentId).filter((id): id is string => id !== null);
},
/**
* Returns assignee agent ids for a set of task ids scoped to a company.
* Used by task-quality-gates sweep to find reviewers of in-review tasks
* that have exceeded the review timeout.
*/
listReviewTasksForSweep(companyId: string): Array<{
id: string;
assigneeAgentId: string | null;
updatedAt: string;
title: string;
}> {
return readAll(
`SELECT id, assignee_agent_id as assigneeAgentId, updated_at as updatedAt, title
FROM tasks
WHERE company_id = ? AND status = 'in_review'`,
companyId,
);
},
/**
* Returns `task_type` for a single task id, or null when the task is
* missing. Used by checkpoint-engine to branch on task type without