Codemod db pass-through calls: messages + runs + approvals + company (C1 part 2/3)

Rewrite ~376 call sites for messages, runs, approvalsMod, and company
domains and delete the corresponding pass-through methods on
AppDatabase / AppDatabaseBase. Structural type references
(ReturnType<HandlerDeps["db"]["getRun"]>) and SweepStaleReviewsDeps-style
mocks are updated to route through the owning sub-module.
This commit is contained in:
ZacharyZhang-NY committed 2026-04-20 13:20:15 -04:00
1 parent 5e9b7acbc6
commit a69fbb053b
86 files changed
+393 -454

No files matched your search

@@ -14,7 +14,7 @@ describe("Step 4: Hire approval + decideApproval", () => {
const ceoId = createAgent({ name: "CEO", role: "CEO" });
const hireId = createAgent({ name: "CTO", role: "CTO", reportsTo: ceoId, status: "pending_approval" as "idle" });
const approvalId = shared.db.requestApproval({
const approvalId = shared.db.approvalsMod.requestApproval({
companyId: shared.companyId,
requestedByAgentId: ceoId,
type: "hire_agent",
@@ -23,7 +23,7 @@ describe("Step 4: Hire approval + decideApproval", () => {
payloadJson: JSON.stringify({ agentId: hireId }),
});
shared.db.decideApproval({
shared.db.approvalsMod.decideApproval({
approvalId,
state: "approved",
decisionNote: "Approved by board",
@@ -37,7 +37,7 @@ describe("Step 4: Hire approval + decideApproval", () => {
const ceoId = createAgent({ name: "CEO", role: "CEO" });
const hireId = createAgent({ name: "CTO", role: "CTO", reportsTo: ceoId, status: "pending_approval" as "idle" });
const approvalId = shared.db.requestApproval({
const approvalId = shared.db.approvalsMod.requestApproval({
companyId: shared.companyId,
requestedByAgentId: ceoId,
type: "hire_agent",
@@ -46,7 +46,7 @@ describe("Step 4: Hire approval + decideApproval", () => {
payloadJson: JSON.stringify({ agentId: hireId }),
});
shared.db.decideApproval({
shared.db.approvalsMod.decideApproval({
approvalId,
state: "rejected",
decisionNote: "Not needed",
@@ -59,7 +59,7 @@ describe("Step 4: Hire approval + decideApproval", () => {
it("getApprovalRecord includes payloadJson", () => {
const ceoId = createAgent({ name: "CEO", role: "CEO" });
const hireId = createAgent({ name: "CTO", role: "CTO", reportsTo: ceoId, status: "pending_approval" as "idle" });
const approvalId = shared.db.requestApproval({
const approvalId = shared.db.approvalsMod.requestApproval({
companyId: shared.companyId,
requestedByAgentId: ceoId,
type: "hire_agent",
@@ -68,7 +68,7 @@ describe("Step 4: Hire approval + decideApproval", () => {
payloadJson: JSON.stringify({ agentId: hireId, config: { connector: "openai-codex" } }),
});
const approval = shared.db.getApprovalRecord(approvalId);
const approval = shared.db.approvalsMod.getApprovalRecord(approvalId);
expect(approval.payloadJson).toBeTruthy();
const payload = JSON.parse(approval.payloadJson!);
expect(payload.agentId).toBe(hireId);
@@ -104,7 +104,7 @@ describe("Agent Messages", () => {
});
it("sends and retrieves direct messages", () => {
const msgId = shared.db.sendAgentMessage({
const msgId = shared.db.messages.sendAgentMessage({
companyId: shared.companyId,
fromAgentId: agentAId,
toAgentId: agentBId,
@@ -116,7 +116,7 @@ describe("Agent Messages", () => {
expect(msgId).toBeTruthy();
const messages = shared.db.listAgentMessages(shared.companyId);
const messages = shared.db.messages.listAgentMessages(shared.companyId);
expect(messages).toHaveLength(1);
expect(messages[0].subject).toBe("Need design review");
expect(messages[0].fromAgentId).toBe(agentAId);
@@ -125,7 +125,7 @@ describe("Agent Messages", () => {
});
it("marks messages as read", () => {
const msgId = shared.db.sendAgentMessage({
const msgId = shared.db.messages.sendAgentMessage({
companyId: shared.companyId,
fromAgentId: agentAId,
toAgentId: agentBId,
@@ -133,14 +133,14 @@ describe("Agent Messages", () => {
body: "Hello",
});
shared.db.markMessageReadForReader(msgId, shared.companyId);
const messages = shared.db.listAgentMessages(shared.companyId);
shared.db.messages.markMessageReadForReader(msgId, shared.companyId);
const messages = shared.db.messages.listAgentMessages(shared.companyId);
const updatedMessage = messages.find((message) => message.id === msgId);
expect(updatedMessage?.readAt).toBeTruthy();
});
it("tracks read state per agent without marking the sender unread", () => {
const msgId = shared.db.sendAgentMessage({
const msgId = shared.db.messages.sendAgentMessage({
companyId: shared.companyId,
fromAgentId: agentAId,
toAgentId: agentBId,
@@ -148,22 +148,22 @@ describe("Agent Messages", () => {
body: "Hello",
});
const senderView = shared.db.listAgentMessages(shared.companyId, { agentId: agentAId });
const senderView = shared.db.messages.listAgentMessages(shared.companyId, { agentId: agentAId });
expect(senderView).toHaveLength(1);
expect(senderView[0].id).toBe(msgId);
expect(senderView[0].readAt).toBeTruthy();
const recipientViewBefore = shared.db.listAgentMessages(shared.companyId, { agentId: agentBId, unreadOnly: true });
const recipientViewBefore = shared.db.messages.listAgentMessages(shared.companyId, { agentId: agentBId, unreadOnly: true });
expect(recipientViewBefore).toHaveLength(1);
expect(recipientViewBefore[0].id).toBe(msgId);
shared.db.markMessageReadForReader(msgId, shared.companyId, agentBId);
shared.db.messages.markMessageReadForReader(msgId, shared.companyId, agentBId);
const recipientViewAfter = shared.db.listAgentMessages(shared.companyId, { agentId: agentBId, unreadOnly: true });
const recipientViewAfter = shared.db.messages.listAgentMessages(shared.companyId, { agentId: agentBId, unreadOnly: true });
expect(recipientViewAfter).toHaveLength(0);
const thirdAgentId = createAgent({ name: "Agent C" });
const thirdView = shared.db.listAgentMessages(shared.companyId, { agentId: thirdAgentId });
const thirdView = shared.db.messages.listAgentMessages(shared.companyId, { agentId: thirdAgentId });
expect(thirdView).toHaveLength(0);
});
@@ -188,7 +188,7 @@ describe("Agent Messages", () => {
priority: "medium",
});
const deptMessageId = shared.db.sendAgentMessage({
const deptMessageId = shared.db.messages.sendAgentMessage({
companyId: shared.companyId,
fromAgentId: engineeringLeadId,
channel: "department",
@@ -196,7 +196,7 @@ describe("Agent Messages", () => {
subject: "Engineering update",
body: "Only engineering should see this",
});
const projectMessageId = shared.db.sendAgentMessage({
const projectMessageId = shared.db.messages.sendAgentMessage({
companyId: shared.companyId,
fromAgentId: engineeringLeadId,
channel: "project",
@@ -205,30 +205,30 @@ describe("Agent Messages", () => {
body: "Only project members should see this",
});
const engineeringView = shared.db.listAgentMessages(shared.companyId, { agentId: engineeringLeadId });
const engineeringView = shared.db.messages.listAgentMessages(shared.companyId, { agentId: engineeringLeadId });
expect(engineeringView.some((message) => message.id === deptMessageId)).toBe(true);
expect(engineeringView.some((message) => message.id === projectMessageId)).toBe(true);
const productView = shared.db.listAgentMessages(shared.companyId, { agentId: productLeadId });
const productView = shared.db.messages.listAgentMessages(shared.companyId, { agentId: productLeadId });
expect(productView.some((message) => message.id === deptMessageId)).toBe(false);
expect(productView.some((message) => message.id === projectMessageId)).toBe(false);
});
it("filters messages by channel", () => {
shared.db.sendAgentMessage({ companyId: shared.companyId, fromAgentId: agentAId, subject: "Dept update", body: "All good", channel: "department", channelTargetId: "engineering" });
shared.db.sendAgentMessage({ companyId: shared.companyId, fromAgentId: agentAId, toAgentId: agentBId, subject: "DM", body: "Hey", channel: "direct" });
shared.db.messages.sendAgentMessage({ companyId: shared.companyId, fromAgentId: agentAId, subject: "Dept update", body: "All good", channel: "department", channelTargetId: "engineering" });
shared.db.messages.sendAgentMessage({ companyId: shared.companyId, fromAgentId: agentAId, toAgentId: agentBId, subject: "DM", body: "Hey", channel: "direct" });
const deptMsgs = shared.db.listAgentMessages(shared.companyId, { channel: "department" });
const deptMsgs = shared.db.messages.listAgentMessages(shared.companyId, { channel: "department" });
expect(deptMsgs).toHaveLength(1);
expect(deptMsgs[0].subject).toBe("Dept update");
const directMsgs = shared.db.listAgentMessages(shared.companyId, { channel: "direct" });
const directMsgs = shared.db.messages.listAgentMessages(shared.companyId, { channel: "direct" });
expect(directMsgs).toHaveLength(1);
expect(directMsgs[0].subject).toBe("DM");
});
it("appears in snapshot", () => {
shared.db.sendAgentMessage({ companyId: shared.companyId, fromAgentId: agentAId, channel: "company", subject: "Snapshot test", body: "Hi" });
shared.db.messages.sendAgentMessage({ companyId: shared.companyId, fromAgentId: agentAId, channel: "company", subject: "Snapshot test", body: "Hi" });
const snapshot = shared.db.listSnapshot();
expect(snapshot.agentMessages.length).toBeGreaterThanOrEqual(1);
expect(snapshot.agentMessages[0].subject).toBe("Snapshot test");
@@ -23,7 +23,7 @@ describe("Step 7: Worker crash recovery (database layer)", () => {
status: "in_progress",
});
const run = shared.db.createRun({
const run = shared.db.runs.createRun({
companyId: shared.companyId,
taskId,
agentId,
@@ -31,7 +31,7 @@ describe("Step 7: Worker crash recovery (database layer)", () => {
connectorId: "openai-codex",
});
shared.db.markRunStatus(run.id, "running", "Started");
shared.db.runs.markRunStatus(run.id, "running", "Started");
// Verify it's running
let snapshot = shared.db.listSnapshot();
@@ -61,7 +61,7 @@ describe("Step 7: Worker crash recovery (database layer)", () => {
status: "in_progress",
});
const run = shared.db.createRun({
const run = shared.db.runs.createRun({
companyId: shared.companyId,
taskId,
agentId,
@@ -69,10 +69,10 @@ describe("Step 7: Worker crash recovery (database layer)", () => {
connectorId: "openai-codex",
});
shared.db.markRunStatus(run.id, "running", "Started");
shared.db.runs.markRunStatus(run.id, "running", "Started");
// Simulate crash recovery - mark as interrupted
shared.db.finishRun({
shared.db.runs.finishRun({
runId: run.id,
status: "interrupted",
summary: "Worker process crashed.",
@@ -31,7 +31,7 @@ describe("E2E: Full autonomous CEO cold start flow", () => {
const cmoId = createAgent({ name: "Aria", role: "CMO", reportsTo: ceoId, status: "pending_approval" as "idle" });
// 3. CEO submits org strategy approval
const orgApprovalId = shared.db.requestApproval({
const orgApprovalId = shared.db.approvalsMod.requestApproval({
companyId: shared.companyId,
requestedByAgentId: ceoId,
type: "approve_ceo_strategy",
@@ -64,7 +64,7 @@ describe("E2E: Full autonomous CEO cold start flow", () => {
expect(phase1Prompt).toContain("Awaiting Org Strategy Approval");
// 4. Board approves hires
const hireApprovalCTO = shared.db.requestApproval({
const hireApprovalCTO = shared.db.approvalsMod.requestApproval({
companyId: shared.companyId,
requestedByAgentId: ceoId,
type: "hire_agent",
@@ -72,9 +72,9 @@ describe("E2E: Full autonomous CEO cold start flow", () => {
impactSummary: "Test",
payloadJson: JSON.stringify({ agentId: ctoId }),
});
shared.db.decideApproval({ approvalId: hireApprovalCTO, state: "approved", decisionNote: "Approved" });
shared.db.approvalsMod.decideApproval({ approvalId: hireApprovalCTO, state: "approved", decisionNote: "Approved" });
const hireApprovalCMO = shared.db.requestApproval({
const hireApprovalCMO = shared.db.approvalsMod.requestApproval({
companyId: shared.companyId,
requestedByAgentId: ceoId,
type: "hire_agent",
@@ -82,14 +82,14 @@ describe("E2E: Full autonomous CEO cold start flow", () => {
impactSummary: "Test",
payloadJson: JSON.stringify({ agentId: cmoId }),
});
shared.db.decideApproval({ approvalId: hireApprovalCMO, state: "approved", decisionNote: "Approved" });
shared.db.approvalsMod.decideApproval({ approvalId: hireApprovalCMO, state: "approved", decisionNote: "Approved" });
// Agents now 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" });
shared.db.approvalsMod.decideApproval({ approvalId: orgApprovalId, state: "approved", decisionNote: "Go ahead" });
// 6. CEO now in Phase 2
const updatedCto = shared.db.entityCrud.getAgent(ctoId);
@@ -287,7 +287,7 @@ describe("Review Fix: Rejected approval wakes agent", () => {
it("decideApproval with rejected state still triggers wake (tested via DB)", () => {
const ceoId = createAgent({ name: "CEO", role: "CEO" });
const approvalId = shared.db.requestApproval({
const approvalId = shared.db.approvalsMod.requestApproval({
companyId: shared.companyId,
requestedByAgentId: ceoId,
type: "approve_ceo_strategy",
@@ -295,14 +295,14 @@ describe("Review Fix: Rejected approval wakes agent", () => {
impactSummary: "test",
});
shared.db.decideApproval({
shared.db.approvalsMod.decideApproval({
approvalId,
state: "rejected",
decisionNote: "Not ready",
});
// Verify approval state
const approval = shared.db.getApprovalRecord(approvalId);
const approval = shared.db.approvalsMod.getApprovalRecord(approvalId);
expect(approval.state).toBe("rejected");
// The IPC handler in index.ts now wakes on rejected too —
// we verify the DB layer works; the wakeAgentIfPossible call is in index.ts
@@ -22,7 +22,7 @@ describe("Step 6: Run failure escalation (database layer)", () => {
status: "in_progress",
});
const run = shared.db.createRun({
const run = shared.db.runs.createRun({
companyId: shared.companyId,
taskId,
agentId,
@@ -30,9 +30,9 @@ describe("Step 6: Run failure escalation (database layer)", () => {
connectorId: "openai-codex",
});
shared.db.markRunStatus(run.id, "running", "Started");
shared.db.runs.markRunStatus(run.id, "running", "Started");
const outcome = shared.db.finishRun({
const outcome = shared.db.runs.finishRun({
runId: run.id,
status: "failed",
summary: null,
@@ -54,7 +54,7 @@ export function installSharedSetup(): SharedHandle {
state.db = new AppDatabase(profileDir);
state.db.init();
state.companyId = state.db.saveCompany({
state.companyId = state.db.company.saveCompany({
name: "Autonomous Test Corp",
description: "Test company for autonomous chain",
status: "active",