Migrate all remaining pass-through call sites (social, skills, budget,
entityCrud, documents, knowledge, automation, wakeup, backup, threads,
settings, snapshot, connectors, workspaces, activity) to their owning
domain modules. `AppDatabase` now carries only three methods:
- createHireRequest (multi-module composite: agents + approvalsMod)
- rollbackConfigRevision (multi-module composite: documents + entityCrud)
- queryAll (raw-SQL escape hatch, @internal)
The vault/env-binding infrastructure previously passed argument-by-
argument into `entityCrud.saveSecret`/`resolveSecretEnv` now binds at
module construction, letting callers invoke `db.entityCrud.saveSecret({...})`
without carrying vault plumbing.
Structural types that narrowed AppDatabase to a bag of flat methods
(`ConnectorModelPolicyDeps` callers, `CapabilityMatchDb`, test mocks in
budget-enforcement / budget-incidents / task-quality-gates /
failure-escalation / task-quality-gates-sweep) are reshaped to the new
namespace. `connectorPolicyDepsFromDb` replaces the old pattern of
handing the whole AppDatabase to `validateConnectorModelSelection`.
A new ESLint rule in eslint.config.mjs blocks new top-level methods on
AppDatabase so the pass-through layer cannot silently grow back.
`AppDatabase.ts` dropped from 193 lines to 58; `AppDatabaseBase` lost
the 270-line trailing pass-through block.
210 lines
8.2 KiB
TypeScript
210 lines
8.2 KiB
TypeScript
import {
|
|
IPC_CHANNELS,
|
|
deleteEntitySchema,
|
|
goalInputSchema,
|
|
projectInputSchema,
|
|
taskInputSchema,
|
|
} from "@shared/contracts";
|
|
import { fail, ok, type RegisterCoreHandlersDependencies } from "./ipc-core-common";
|
|
import { validateTaskCreation, checkCapabilityMatch } from "./task-quality-gates";
|
|
|
|
export function registerCoreTaskGraphHandlers({
|
|
registerHandle,
|
|
db,
|
|
ensureCompanyExists,
|
|
findInvalidReference,
|
|
publishDomainChanged,
|
|
emitEvent,
|
|
wakeAgentIfPossible,
|
|
handleTaskCreated,
|
|
handleTaskStatusChange,
|
|
logger,
|
|
}: Pick<
|
|
RegisterCoreHandlersDependencies,
|
|
| "registerHandle"
|
|
| "db"
|
|
| "ensureCompanyExists"
|
|
| "findInvalidReference"
|
|
| "publishDomainChanged"
|
|
| "emitEvent"
|
|
| "wakeAgentIfPossible"
|
|
| "handleTaskCreated"
|
|
| "handleTaskStatusChange"
|
|
| "logger"
|
|
>) {
|
|
registerHandle(IPC_CHANNELS.saveGoal, async (_event, payload) => {
|
|
const parsed = goalInputSchema.parse(payload);
|
|
const companyError = ensureCompanyExists(parsed.companyId);
|
|
if (companyError) return companyError;
|
|
const invalidReference = findInvalidReference(parsed.companyId, [
|
|
{ id: parsed.parentId, table: "goals", label: "parent goal" },
|
|
{ id: parsed.ownerAgentId, table: "agents", label: "owner agent" },
|
|
]);
|
|
if (invalidReference) {
|
|
return fail("INVALID_COMPANY_REFERENCE", `The selected ${invalidReference.label} does not belong to the current company.`);
|
|
}
|
|
const id = db.entityCrud.saveGoal(parsed);
|
|
db.activityMod.addActivity({
|
|
companyId: parsed.companyId,
|
|
actor: "board",
|
|
action: parsed.id ? "goal.updated" : "goal.created",
|
|
entityType: "goal",
|
|
entityId: id,
|
|
detail: parsed.title,
|
|
});
|
|
publishDomainChanged();
|
|
if (parsed.status === "active" && parsed.ownerAgentId) {
|
|
wakeAgentIfPossible(parsed.ownerAgentId, parsed.companyId, "goal_activated");
|
|
}
|
|
return ok(id);
|
|
});
|
|
|
|
registerHandle(IPC_CHANNELS.saveProject, async (_event, payload) => {
|
|
const parsed = projectInputSchema.parse(payload);
|
|
const companyError = ensureCompanyExists(parsed.companyId);
|
|
if (companyError) return companyError;
|
|
const invalidReference = findInvalidReference(parsed.companyId, [
|
|
{ id: parsed.goalId, table: "goals", label: "goal" },
|
|
{ id: parsed.leadAgentId, table: "agents", label: "lead agent" },
|
|
]);
|
|
if (invalidReference) {
|
|
return fail("INVALID_COMPANY_REFERENCE", `The selected ${invalidReference.label} does not belong to the current company.`);
|
|
}
|
|
const id = db.entityCrud.saveProject(parsed);
|
|
db.activityMod.addActivity({
|
|
companyId: parsed.companyId,
|
|
actor: "board",
|
|
action: parsed.id ? "project.updated" : "project.created",
|
|
entityType: "project",
|
|
entityId: id,
|
|
detail: parsed.name,
|
|
});
|
|
if (parsed.leadAgentId) {
|
|
wakeAgentIfPossible(parsed.leadAgentId, parsed.companyId, "assignment");
|
|
}
|
|
publishDomainChanged();
|
|
return ok(id);
|
|
});
|
|
|
|
registerHandle(IPC_CHANNELS.saveTask, async (_event, payload) => {
|
|
const parsed = taskInputSchema.parse(payload);
|
|
const companyError = ensureCompanyExists(parsed.companyId);
|
|
if (companyError) return companyError;
|
|
const invalidReference = findInvalidReference(parsed.companyId, [
|
|
{ id: parsed.projectId, table: "projects", label: "project" },
|
|
{ id: parsed.goalId, table: "goals", label: "goal" },
|
|
{ id: parsed.parentId, table: "tasks", label: "parent task" },
|
|
{ id: parsed.assigneeAgentId, table: "agents", label: "assignee agent" },
|
|
{ id: parsed.workspaceId, table: "workspaces", label: "workspace" },
|
|
]);
|
|
if (invalidReference) {
|
|
return fail("INVALID_COMPANY_REFERENCE", `The selected ${invalidReference.label} does not belong to the current company.`);
|
|
}
|
|
const isNew = !parsed.id;
|
|
if (isNew) {
|
|
const validation = validateTaskCreation(parsed);
|
|
if (!validation.valid) {
|
|
return fail("TASK_VALIDATION_FAILED", validation.errors.join("; "));
|
|
}
|
|
}
|
|
let previousStatus: string | null = null;
|
|
let previousAssigneeAgentId: string | null = null;
|
|
if (!isNew) {
|
|
const taskId = parsed.id;
|
|
if (!taskId) return fail("INVALID_TASK_STATE", "Task ID required for updates.");
|
|
try {
|
|
const existingTask = db.entityCrud.getTask(taskId);
|
|
previousStatus = existingTask.status;
|
|
previousAssigneeAgentId = existingTask.assigneeAgentId;
|
|
} catch (err) {
|
|
logger.warn(`[ipc/task-graph] getTask failed for ${taskId}; treating as new task:`, err instanceof Error ? err.message : String(err));
|
|
previousStatus = null;
|
|
previousAssigneeAgentId = null;
|
|
}
|
|
}
|
|
let id: string;
|
|
try {
|
|
id = db.tasks.saveTask(parsed);
|
|
} catch (error) {
|
|
return fail("INVALID_TASK_STATE", error instanceof Error ? error.message : "Task could not be saved.");
|
|
}
|
|
db.activityMod.addActivity({
|
|
companyId: parsed.companyId,
|
|
actor: "board",
|
|
action: isNew ? "task.created" : "task.updated",
|
|
entityType: "task",
|
|
entityId: id,
|
|
detail: parsed.title,
|
|
});
|
|
if (isNew) {
|
|
handleTaskCreated(id, parsed.companyId);
|
|
} else {
|
|
if (previousStatus && previousStatus !== parsed.status) {
|
|
handleTaskStatusChange(id, parsed.companyId, previousStatus, parsed.status);
|
|
}
|
|
// User (board) manually advancing a flagged task to done: clear the review flag
|
|
if (parsed.status === "done") {
|
|
try {
|
|
db.tasks.setTaskRequiresUserReview(id, false);
|
|
} catch (error) {
|
|
logger.warn(
|
|
`[ipc-task] setTaskRequiresUserReview(false) failed for task ${id}: ${error instanceof Error ? error.message : String(error)}`,
|
|
);
|
|
}
|
|
}
|
|
// Wake new assignee when task is reassigned (matches api-server.ts behavior)
|
|
if (parsed.assigneeAgentId && parsed.assigneeAgentId !== previousAssigneeAgentId) {
|
|
wakeAgentIfPossible(parsed.assigneeAgentId, parsed.companyId, "assignment");
|
|
}
|
|
}
|
|
let capabilityWarning: string | undefined;
|
|
if (isNew && parsed.assigneeAgentId) {
|
|
try {
|
|
const match = checkCapabilityMatch(db, parsed.assigneeAgentId, parsed.description, parsed.companyId);
|
|
if (!match.matched) {
|
|
capabilityWarning = match.warning;
|
|
}
|
|
} catch (error) {
|
|
logger.warn(
|
|
`[ipc-task] capability check failed for new task ${id} (advisory, not blocking): ${error instanceof Error ? error.message : String(error)}`,
|
|
);
|
|
}
|
|
}
|
|
emitEvent({ type: "task-updated", taskId: id, action: isNew ? "created" : "updated" });
|
|
return ok({ id, capabilityWarning });
|
|
});
|
|
|
|
const deleteHandlers = [
|
|
{ channel: IPC_CHANNELS.deleteTask, table: "tasks", entity: "task", deleteFn: (id: string, companyId: string) => db.tasks.deleteTask(id, companyId) },
|
|
{ channel: IPC_CHANNELS.deleteGoal, table: "goals", entity: "goal", deleteFn: (id: string, companyId: string) => db.entityCrud.deleteGoal(id, companyId) },
|
|
{ channel: IPC_CHANNELS.deleteProject, table: "projects", entity: "project", deleteFn: (id: string, companyId: string) => db.entityCrud.deleteProject(id, companyId) },
|
|
{ channel: IPC_CHANNELS.deleteWorkspace, table: "workspaces", entity: "workspace", deleteFn: (id: string, companyId: string) => db.workspacesMod.deleteWorkspace(id, companyId) },
|
|
] as const;
|
|
|
|
for (const { channel, table, entity, deleteFn } of deleteHandlers) {
|
|
registerHandle(channel, async (_event, payload) => {
|
|
const parsed = deleteEntitySchema.parse(payload);
|
|
const companyError = ensureCompanyExists(parsed.companyId);
|
|
if (companyError) return companyError;
|
|
if (!db.settings.belongsToCompany(table, parsed.id, parsed.companyId)) {
|
|
return fail(`${entity.toUpperCase()}_NOT_FOUND`, `${entity.charAt(0).toUpperCase() + entity.slice(1)} not found in the current company.`);
|
|
}
|
|
deleteFn(parsed.id, parsed.companyId);
|
|
db.activityMod.addActivity({
|
|
companyId: parsed.companyId,
|
|
actor: "board",
|
|
action: `${entity}.deleted`,
|
|
entityType: entity,
|
|
entityId: parsed.id,
|
|
detail: "",
|
|
});
|
|
if (entity === "task") {
|
|
emitEvent({ type: "task-updated", taskId: parsed.id, action: "deleted" });
|
|
} else {
|
|
publishDomainChanged();
|
|
}
|
|
return ok(true);
|
|
});
|
|
}
|
|
}
|