refactor(db): group database domain modules

This commit is contained in:
ZacharyZhang-NY committed 2026-04-22 19:24:25 -04:00
1 parent e43a89096d
commit 6da0a58769
160 files changed
+2124 -1999

No files matched your search

@@ -51,7 +51,7 @@ export function retireDeletedArtifacts(
deleted: string[],
): ProjectionOutcome[] {
if (deleted.length === 0) return [];
const archived = db.documentsMod.archiveWorkProductsByExternalPaths({
const archived = db.core.documents.archiveWorkProductsByExternalPaths({
companyId: ctx.companyId,
workspaceId: ctx.workspaceId,
provider: "filesystem",
@@ -97,24 +97,24 @@ function upsertFilesystemArtifact(
const title = lastSlash >= 0 ? relativePath.slice(lastSlash + 1) : relativePath;
const provider = "filesystem";
const resolvedWorkspaceId =
ctx.workspaceId ?? db.workspacesMod.findWorkspaceIdByLocalPath(ctx.companyId, workspacePath);
const existing = db.documentsMod.findWorkProductByExternalId(
ctx.workspaceId ?? db.core.workspaces.findWorkspaceIdByLocalPath(ctx.companyId, workspacePath);
const existing = db.core.documents.findWorkProductByExternalId(
ctx.companyId,
provider,
relativePath,
resolvedWorkspaceId,
);
if (existing) {
const patch: Parameters<typeof db.documentsMod.updateWorkProduct>[2] = {
const patch: Parameters<typeof db.core.documents.updateWorkProduct>[2] = {
title,
status: "active",
lastTouchedByRunId: ctx.runId,
};
if (resolvedWorkspaceId) patch.workspaceId = resolvedWorkspaceId;
db.documentsMod.updateWorkProduct(existing.id, ctx.companyId, patch);
db.core.documents.updateWorkProduct(existing.id, ctx.companyId, patch);
return { kind: "updated", workProductId: existing.id, relativePath };
}
const workProductId = db.documentsMod.createWorkProduct({
const workProductId = db.core.documents.createWorkProduct({
companyId: ctx.companyId,
taskId: ctx.taskId,
workspaceId: resolvedWorkspaceId,
+1 -1
View File
@@ -45,7 +45,7 @@ export class CostWriter {
);
}
this.deps.db.runs.recordCostEntry({
this.deps.db.core.runs.recordCostEntry({
runId: input.runId,
companyId: input.companyId,
agentId: input.agentId,
+2 -2
View File
@@ -113,9 +113,9 @@ function checkOperationalApprovalBlock(
toolName: string,
): BeforeToolCallRpcResult {
try {
const blocking = deps.db.approvalsMod.getBlockingApproval(ctx.companyId, ctx.agentId, ctx.taskId);
const blocking = deps.db.core.approvals.getBlockingApproval(ctx.companyId, ctx.agentId, ctx.taskId);
if (!blocking) return { block: false, reason: null };
const companyMode = deps.db.company.getCompanyExecutionMode(ctx.companyId);
const companyMode = deps.db.core.company.getCompanyExecutionMode(ctx.companyId);
const cofounderCheckpointBlocks =
companyMode === "ai_cofounder" && isCofounderMandatoryApproval(blocking.type);
if (!isOperationalApproval(blocking.type) && !cofounderCheckpointBlocks) {
@@ -5,7 +5,7 @@
* `api-server/routes-approvals.ts`: validate → enforce the
* `approve_ceo_strategy` ordering gate → request approval → auto-
* approve or notify. Both the tool path and the HTTP path funnel
* through the same `db.approvalsMod.requestApproval` + `shouldAutoApprove`
* through the same `db.core.approvals.requestApproval` + `shouldAutoApprove`
* plumbing, so the board sees identical rows regardless of entry point.
*
* No fallbacks: malformed input throws UserInputError, DB failures
@@ -71,7 +71,7 @@ export function handleFileApprovalToolCall(
// otherwise re-open the exact loop the HTTP gate was written to
// close.
if (input.type === "approve_ceo_strategy") {
const companyHireApprovals = db.approvalsMod.listCompanyApprovalsByType(companyId, "hire_agent");
const companyHireApprovals = db.core.approvals.listCompanyApprovalsByType(companyId, "hire_agent");
if (companyHireApprovals.length === 0) {
throw new UserInputError({
code: "file_approval.strategy.before_hires",
@@ -79,7 +79,7 @@ export function handleFileApprovalToolCall(
"approve_ceo_strategy requires at least one hire_agent approval to exist first. File the intended hires, then the strategy.",
});
}
const existingStrategy = db.approvalsMod
const existingStrategy = db.core.approvals
.listCompanyApprovalsByType(companyId, "approve_ceo_strategy")
.find((a) => a.state === "pending" || a.state === "approved");
if (existingStrategy) {
@@ -111,7 +111,7 @@ export function handleFileApprovalToolCall(
let approvalId: string;
try {
approvalId = db.approvalsMod.requestApproval({
approvalId = db.core.approvals.requestApproval({
companyId,
type: input.type,
relatedTaskId: input.relatedTaskId ?? null,
@@ -131,7 +131,7 @@ export function handleFileApprovalToolCall(
}
try {
db.activityMod.addActivity({
db.core.activity.addActivity({
companyId,
actor: requesterId,
action: "approval.created",
@@ -137,7 +137,7 @@ export function handleHireAgentToolCall(
resolvedReportsTo &&
resolvedReportsTo !== requesterId &&
!canManageOrganization(db, companyId, requesterId) &&
!db.agents.getChainOfCommand(resolvedReportsTo).some((agent) => agent.id === requesterId)
!db.core.agents.getChainOfCommand(resolvedReportsTo).some((agent) => agent.id === requesterId)
) {
throw new UserInputError({
code: "hire_agent.routing.not_in_chain",
@@ -149,7 +149,7 @@ export function handleHireAgentToolCall(
const connectorRecord = (() => {
try {
return db.connectorsCrud.getConnector(input.connectorId);
return db.core.connectors.getConnector(input.connectorId);
} catch (error) {
throw new UserInputError({
code: "hire_agent.connector.missing",
@@ -167,7 +167,7 @@ export function handleHireAgentToolCall(
});
}
const existingAgents = db.agents.listExistingAgentsForHireCheck(companyId);
const existingAgents = db.core.agents.listExistingAgentsForHireCheck(companyId);
const duplicateByName = existingAgents.find(
(a) => a.name.toLowerCase() === input.name.toLowerCase(),
);
@@ -178,7 +178,7 @@ export function handleHireAgentToolCall(
});
}
const pendingHires = db.approvalsMod.listPendingHireApprovalPayloads(companyId);
const pendingHires = db.core.approvals.listPendingHireApprovalPayloads(companyId);
for (const pending of pendingHires) {
try {
const payload = pending.payloadJson ? JSON.parse(pending.payloadJson) as Record<string, unknown> : {};
@@ -199,7 +199,7 @@ export function handleHireAgentToolCall(
const managerLabel = resolvedReportsTo
? (() => {
try {
return db.entityCrud.getAgent(resolvedReportsTo).name;
return db.core.entity.getAgent(resolvedReportsTo).name;
} catch {
return resolvedReportsTo;
}
@@ -252,7 +252,7 @@ export function handleHireAgentToolCall(
}
try {
db.activityMod.addActivity({
db.core.activity.addActivity({
companyId,
actor: requesterId,
action: "agent.hire_requested",
+3 -3
View File
@@ -235,7 +235,7 @@ export class RuntimeBridge {
*/
private resolveAgentName(agentId: string): string {
try {
const name = this.deps.db.agents.findAgentName(agentId);
const name = this.deps.db.core.agents.findAgentName(agentId);
if (name && name.length > 0) return name;
} catch (error) {
this.deps.logger.warn(
@@ -434,11 +434,11 @@ export class RuntimeBridge {
// `?unreadOnly=true`, so agents posted status comments instead of
// replies. The explicit read endpoint is the sole signal.
const store = this.getOrCreateStore(payload.taskSessionId);
const inboxRecords = this.deps.db.messages.listAgentInboxUnread(payload.companyId, payload.agentId);
const inboxRecords = this.deps.db.core.messages.listAgentInboxUnread(payload.companyId, payload.agentId);
if (inboxRecords.length > 0) {
this.deps.db.withTransaction(() => {
for (const record of inboxRecords) {
const claimed = this.deps.db.inboxProjections.claimProjection(record.id, payload.taskSessionId);
const claimed = this.deps.db.projection.inbox.claimProjection(record.id, payload.taskSessionId);
if (!claimed) continue;
// `projectInboundMessage` allocates a monotonic pi-timestamp
// and indexes the correlation under `record.id`. Two rows
@@ -52,7 +52,7 @@ export class RuntimeThreadProjector {
try {
const body = extractTextBody(message.content);
const actor = this.resolveThreadEventActor(envelope, message);
this.deps.db.threads.addThreadEvent({
this.deps.db.core.threads.addThreadEvent({
threadId: thread.id,
actorId: actor.actorId,
actorType: actor.actorType,
@@ -91,7 +91,7 @@ export class RuntimeThreadProjector {
: record.senderKind === "system"
? "system"
: "agent";
this.deps.db.threads.addThreadEvent({
this.deps.db.core.threads.addThreadEvent({
threadId: thread.id,
actorId: record.fromAgentId,
actorType,
@@ -116,7 +116,7 @@ export class RuntimeThreadProjector {
private resolveThreadForRun(envelope: RuntimeThreadEnvelope) {
if (envelope.taskId) {
try {
const task = this.deps.db.entityCrud.getTask(envelope.taskId);
const task = this.deps.db.core.entity.getTask(envelope.taskId);
return ensureTaskThread(this.deps.db, envelope.taskId, envelope.companyId, task.title);
} catch (error) {
this.deps.logger.warn(
@@ -18,7 +18,7 @@ export class SqliteSessionStore {
) {}
loadMessages(): AgentMessage[] {
const rows = this.db.sessionEntries.listActive(this.taskSessionId);
const rows = this.db.projection.sessionEntries.listActive(this.taskSessionId);
const result: AgentMessage[] = [];
for (const row of rows) {
if (row.kind === "message") {
@@ -29,7 +29,7 @@ export class SqliteSessionStore {
}
appendMessage(message: AgentMessage): void {
this.db.sessionEntries.append({
this.db.projection.sessionEntries.append({
taskSessionId: this.taskSessionId,
kind: "message",
role: (message as { role?: string }).role ?? null,