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

+50 -64
View File
@@ -14,11 +14,15 @@ describe("sweepStaleReviews", () => {
function makeSweepDeps(overrides: Partial<SweepStaleReviewsDeps> = {}): SweepStaleReviewsDeps {
return {
db: {
listReviewTasksForSweep: vi.fn().mockReturnValue([]),
tasks: {
listReviewTasksForSweep: vi.fn().mockReturnValue([]),
advanceTaskStatus: vi.fn().mockReturnValue({ previousStatus: "in_review" }),
},
agents: {
getManagerForAgent: vi.fn().mockReturnValue(null),
getChainOfCommand: vi.fn().mockReturnValue([]),
},
findPendingApprovalForTask: vi.fn().mockReturnValue(null),
getManagerForAgent: vi.fn().mockReturnValue(null),
getChainOfCommand: vi.fn().mockReturnValue([]),
advanceTaskStatus: vi.fn().mockReturnValue({ previousStatus: "in_review" }),
} as unknown as AppDatabase,
getCompanyExecutionMode: vi.fn().mockReturnValue("ai_ceo"),
dispatchAgentMessage: vi.fn().mockReturnValue({ ok: true }),
@@ -34,20 +38,27 @@ describe("sweepStaleReviews", () => {
};
}
it("escalates to manager's manager when task in_review for 35 minutes", () => {
const deps = makeSweepDeps();
const dbMock = deps.db as unknown as {
type SweepDbMock = {
tasks: {
listReviewTasksForSweep: ReturnType<typeof vi.fn>;
findPendingApprovalForTask: ReturnType<typeof vi.fn>;
advanceTaskStatus: ReturnType<typeof vi.fn>;
};
agents: {
getManagerForAgent: ReturnType<typeof vi.fn>;
getChainOfCommand: ReturnType<typeof vi.fn>;
};
findPendingApprovalForTask: ReturnType<typeof vi.fn>;
};
dbMock.listReviewTasksForSweep.mockReturnValueOnce([
it("escalates to manager's manager when task in_review for 35 minutes", () => {
const deps = makeSweepDeps();
const dbMock = deps.db as unknown as SweepDbMock;
dbMock.tasks.listReviewTasksForSweep.mockReturnValueOnce([
{ id: "task-1", assigneeAgentId: "agent-1", updatedAt: THIRTY_FIVE_MIN_AGO, title: "Build login" },
]);
dbMock.getManagerForAgent.mockReturnValueOnce({ id: "manager-1", name: "Manager One", companyId: "company-1" });
dbMock.getChainOfCommand.mockReturnValueOnce([{ id: "higher-manager-1", name: "Higher Manager" }]);
dbMock.agents.getManagerForAgent.mockReturnValueOnce({ id: "manager-1", name: "Manager One", companyId: "company-1" });
dbMock.agents.getChainOfCommand.mockReturnValueOnce([{ id: "higher-manager-1", name: "Higher Manager" }]);
const count = sweepStaleReviews("company-1", deps, NOW);
expect(count).toBe(1);
@@ -66,9 +77,9 @@ describe("sweepStaleReviews", () => {
it("does not escalate when task in_review for only 25 minutes", () => {
const deps = makeSweepDeps();
const dbMock = deps.db as unknown as { listReviewTasksForSweep: ReturnType<typeof vi.fn> };
const dbMock = deps.db as unknown as SweepDbMock;
dbMock.listReviewTasksForSweep.mockReturnValueOnce([
dbMock.tasks.listReviewTasksForSweep.mockReturnValueOnce([
{ id: "task-1", assigneeAgentId: "agent-1", updatedAt: TWENTY_FIVE_MIN_AGO, title: "Build login" },
]);
@@ -80,19 +91,14 @@ describe("sweepStaleReviews", () => {
it("notifies the human when the reviewer has no higher manager", () => {
const deps = makeSweepDeps();
const dbMock = deps.db as unknown as {
listReviewTasksForSweep: ReturnType<typeof vi.fn>;
findPendingApprovalForTask: ReturnType<typeof vi.fn>;
getManagerForAgent: ReturnType<typeof vi.fn>;
getChainOfCommand: ReturnType<typeof vi.fn>;
};
const dbMock = deps.db as unknown as SweepDbMock;
dbMock.listReviewTasksForSweep.mockReturnValueOnce([
dbMock.tasks.listReviewTasksForSweep.mockReturnValueOnce([
{ id: "task-1", assigneeAgentId: "agent-1", updatedAt: THIRTY_FIVE_MIN_AGO, title: "Build login" },
]);
dbMock.findPendingApprovalForTask.mockReturnValueOnce(null);
dbMock.getManagerForAgent.mockReturnValueOnce({ id: "ceo-1", name: "CEO", companyId: "company-1" });
dbMock.getChainOfCommand.mockReturnValueOnce([]);
dbMock.agents.getManagerForAgent.mockReturnValueOnce({ id: "ceo-1", name: "CEO", companyId: "company-1" });
dbMock.agents.getChainOfCommand.mockReturnValueOnce([]);
const count = sweepStaleReviews("company-1", deps, NOW);
expect(count).toBe(1);
@@ -109,18 +115,13 @@ describe("sweepStaleReviews", () => {
it("auto-approves pending approval linked to a stale review task", () => {
const deps = makeSweepDeps();
const dbMock = deps.db as unknown as {
listReviewTasksForSweep: ReturnType<typeof vi.fn>;
findPendingApprovalForTask: ReturnType<typeof vi.fn>;
getManagerForAgent: ReturnType<typeof vi.fn>;
getChainOfCommand: ReturnType<typeof vi.fn>;
};
const dbMock = deps.db as unknown as SweepDbMock;
dbMock.listReviewTasksForSweep.mockReturnValueOnce([
dbMock.tasks.listReviewTasksForSweep.mockReturnValueOnce([
{ id: "task-1", assigneeAgentId: "agent-1", updatedAt: THIRTY_FIVE_MIN_AGO, title: "Build login" },
]);
dbMock.getManagerForAgent.mockReturnValueOnce({ id: "manager-1", name: "Manager", companyId: "company-1" });
dbMock.getChainOfCommand.mockReturnValueOnce([{ id: "higher-1", name: "Higher" }]);
dbMock.agents.getManagerForAgent.mockReturnValueOnce({ id: "manager-1", name: "Manager", companyId: "company-1" });
dbMock.agents.getChainOfCommand.mockReturnValueOnce([{ id: "higher-1", name: "Higher" }]);
dbMock.findPendingApprovalForTask.mockReturnValueOnce({ id: "approval-1", type: "deliverable_review" });
sweepStaleReviews("company-1", deps, NOW);
@@ -139,18 +140,13 @@ describe("sweepStaleReviews", () => {
it("swallows resolveApprovalDecision races gracefully", () => {
const deps = makeSweepDeps();
const dbMock = deps.db as unknown as {
listReviewTasksForSweep: ReturnType<typeof vi.fn>;
findPendingApprovalForTask: ReturnType<typeof vi.fn>;
getManagerForAgent: ReturnType<typeof vi.fn>;
getChainOfCommand: ReturnType<typeof vi.fn>;
};
const dbMock = deps.db as unknown as SweepDbMock;
dbMock.listReviewTasksForSweep.mockReturnValueOnce([
dbMock.tasks.listReviewTasksForSweep.mockReturnValueOnce([
{ id: "task-1", assigneeAgentId: "agent-1", updatedAt: THIRTY_FIVE_MIN_AGO, title: "Build login" },
]);
dbMock.getManagerForAgent.mockReturnValueOnce({ id: "manager-1", name: "Manager", companyId: "company-1" });
dbMock.getChainOfCommand.mockReturnValueOnce([{ id: "higher-1", name: "Higher" }]);
dbMock.agents.getManagerForAgent.mockReturnValueOnce({ id: "manager-1", name: "Manager", companyId: "company-1" });
dbMock.agents.getChainOfCommand.mockReturnValueOnce([{ id: "higher-1", name: "Higher" }]);
dbMock.findPendingApprovalForTask.mockReturnValueOnce({ id: "approval-1", type: "deliverable_review" });
(deps.resolveApprovalDecision as ReturnType<typeof vi.fn>).mockImplementationOnce(() => {
throw new Error("Approval already decided");
@@ -161,24 +157,18 @@ describe("sweepStaleReviews", () => {
it("advances task status to done after escalation", () => {
const deps = makeSweepDeps();
const dbMock = deps.db as unknown as {
listReviewTasksForSweep: ReturnType<typeof vi.fn>;
findPendingApprovalForTask: ReturnType<typeof vi.fn>;
getManagerForAgent: ReturnType<typeof vi.fn>;
getChainOfCommand: ReturnType<typeof vi.fn>;
advanceTaskStatus: ReturnType<typeof vi.fn>;
};
const dbMock = deps.db as unknown as SweepDbMock;
dbMock.listReviewTasksForSweep.mockReturnValueOnce([
dbMock.tasks.listReviewTasksForSweep.mockReturnValueOnce([
{ id: "task-1", assigneeAgentId: "agent-1", updatedAt: THIRTY_FIVE_MIN_AGO, title: "Build login" },
]);
dbMock.getManagerForAgent.mockReturnValueOnce({ id: "manager-1", name: "Manager", companyId: "company-1" });
dbMock.getChainOfCommand.mockReturnValueOnce([{ id: "higher-1", name: "Higher" }]);
dbMock.agents.getManagerForAgent.mockReturnValueOnce({ id: "manager-1", name: "Manager", companyId: "company-1" });
dbMock.agents.getChainOfCommand.mockReturnValueOnce([{ id: "higher-1", name: "Higher" }]);
dbMock.findPendingApprovalForTask.mockReturnValueOnce(null);
sweepStaleReviews("company-1", deps, NOW);
expect(dbMock.advanceTaskStatus).toHaveBeenCalledWith(
expect(dbMock.tasks.advanceTaskStatus).toHaveBeenCalledWith(
"task-1",
"done",
"Auto-advanced: review timeout exceeded 30 minutes.",
@@ -188,9 +178,9 @@ describe("sweepStaleReviews", () => {
it("skips tasks without an assignee", () => {
const deps = makeSweepDeps();
const dbMock = deps.db as unknown as { listReviewTasksForSweep: ReturnType<typeof vi.fn> };
const dbMock = deps.db as unknown as SweepDbMock;
dbMock.listReviewTasksForSweep.mockReturnValueOnce([
dbMock.tasks.listReviewTasksForSweep.mockReturnValueOnce([
{ id: "task-1", assigneeAgentId: null, updatedAt: THIRTY_FIVE_MIN_AGO, title: "Orphan task" },
]);
@@ -208,9 +198,9 @@ describe("sweepStaleReviews", () => {
const deps = makeSweepDeps({
getCompanyExecutionMode: vi.fn().mockReturnValue("ai_cofounder"),
});
const dbMock = deps.db as unknown as { listReviewTasksForSweep: ReturnType<typeof vi.fn> };
const dbMock = deps.db as unknown as SweepDbMock;
dbMock.listReviewTasksForSweep.mockReturnValueOnce([
dbMock.tasks.listReviewTasksForSweep.mockReturnValueOnce([
{ id: "task-1", assigneeAgentId: "agent-1", updatedAt: TWENTY_FIVE_MIN_AGO, title: "Review me" },
]);
@@ -219,24 +209,20 @@ describe("sweepStaleReviews", () => {
expect(deps.notify).not.toHaveBeenCalled();
expect(deps.resolveApprovalDecision).not.toHaveBeenCalled();
expect(deps.dispatchAgentMessage).not.toHaveBeenCalled();
expect(deps.db.advanceTaskStatus).not.toHaveBeenCalled();
expect(dbMock.tasks.advanceTaskStatus).not.toHaveBeenCalled();
});
it("sends a critical cofounder review reminder without auto-approving", () => {
const deps = makeSweepDeps({
getCompanyExecutionMode: vi.fn().mockReturnValue("ai_cofounder"),
});
const dbMock = deps.db as unknown as {
listReviewTasksForSweep: ReturnType<typeof vi.fn>;
findPendingApprovalForTask: ReturnType<typeof vi.fn>;
getManagerForAgent: ReturnType<typeof vi.fn>;
};
const dbMock = deps.db as unknown as SweepDbMock;
dbMock.listReviewTasksForSweep.mockReturnValueOnce([
dbMock.tasks.listReviewTasksForSweep.mockReturnValueOnce([
{ id: "task-1", assigneeAgentId: "agent-1", updatedAt: THIRTY_FIVE_MIN_AGO, title: "Review me" },
]);
dbMock.findPendingApprovalForTask.mockReturnValueOnce({ id: "approval-1", type: "deliverable_review" });
dbMock.getManagerForAgent.mockReturnValueOnce({ id: "manager-1", name: "Manager", companyId: "company-1" });
dbMock.agents.getManagerForAgent.mockReturnValueOnce({ id: "manager-1", name: "Manager", companyId: "company-1" });
const count = sweepStaleReviews("company-1", deps, NOW);
expect(count).toBe(0);
@@ -250,6 +236,6 @@ describe("sweepStaleReviews", () => {
);
expect(deps.resolveApprovalDecision).not.toHaveBeenCalled();
expect(deps.dispatchAgentMessage).not.toHaveBeenCalled();
expect(deps.db.advanceTaskStatus).not.toHaveBeenCalled();
expect(dbMock.tasks.advanceTaskStatus).not.toHaveBeenCalled();
});
});