feat(09-01): define granular event types, IPC channels, entity getters, and Zustand patch methods

- Add 5 granular event interfaces (TaskUpdated, RunUpdated, ApprovalUpdated, AgentUpdated, BudgetChanged) to DesktopEvent union
- Add 4 entity getter IPC channels (getTask, getAgent, getRun, getApproval) with Zod validation and company ownership checks
- Add getRun method to AppDatabase following existing getTask pattern
- Add 4 preload bridge methods for entity getters
- Add 8 patch/remove methods and refreshBadges to CompanySlice
- Add selective-ipc.test.ts with exhaustive union type tests
- Extend store.test.ts with 14 patch method tests

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
ZacharyZhang-NYandClaude Opus 4.6 committed 2026-03-27 19:33:33 -04:00
1 parent c1de05a7f1
commit 920a764856
8 files changed
+631 -5

No files matched your search

+95 -1
View File
@@ -1,10 +1,14 @@
import type { StateCreator } from "zustand";
import type { AppState } from "../store";
import type {
AgentRecord,
ApprovalRecord,
CompanyMetrics,
InboxItem,
ProfileSnapshot,
RunRecord,
StandupReport,
TaskRecord,
} from "@shared/types";
import type { SidebarBadges } from "@shared/contracts";
@@ -24,6 +28,15 @@ export interface CompanySlice {
setBadges: (badges: SidebarBadges | null) => void;
setError: (error: string | null) => void;
refreshSnapshot: () => Promise<void>;
patchTask: (task: TaskRecord) => void;
removeTask: (taskId: string) => void;
patchRun: (run: RunRecord) => void;
removeRun: (runId: string) => void;
patchAgent: (agent: AgentRecord) => void;
removeAgent: (agentId: string) => void;
patchApproval: (approval: ApprovalRecord) => void;
removeApproval: (approvalId: string) => void;
refreshBadges: () => Promise<void>;
}
export const createCompanySlice: StateCreator<
@@ -31,7 +44,7 @@ export const createCompanySlice: StateCreator<
[["zustand/immer", never]],
[],
CompanySlice
> = (set) => ({
> = (set, get) => ({
snapshot: null,
currentCompanyId: null,
companyMetrics: null,
@@ -85,4 +98,85 @@ export const createCompanySlice: StateCreator<
});
}
},
// --- Granular patch methods for selective IPC updates ---
patchTask: (task) =>
set((state) => {
if (!state.snapshot) return;
const idx = state.snapshot.tasks.findIndex((t) => t.id === task.id);
if (idx >= 0) {
state.snapshot.tasks[idx] = task;
} else {
state.snapshot.tasks.unshift(task);
}
}),
removeTask: (taskId) =>
set((state) => {
if (!state.snapshot) return;
state.snapshot.tasks = state.snapshot.tasks.filter((t) => t.id !== taskId);
}),
patchRun: (run) =>
set((state) => {
if (!state.snapshot) return;
const idx = state.snapshot.runs.findIndex((r) => r.id === run.id);
if (idx >= 0) {
state.snapshot.runs[idx] = run;
} else {
state.snapshot.runs.unshift(run);
}
}),
removeRun: (runId) =>
set((state) => {
if (!state.snapshot) return;
state.snapshot.runs = state.snapshot.runs.filter((r) => r.id !== runId);
}),
patchAgent: (agent) =>
set((state) => {
if (!state.snapshot) return;
const idx = state.snapshot.agents.findIndex((a) => a.id === agent.id);
if (idx >= 0) {
state.snapshot.agents[idx] = agent;
} else {
state.snapshot.agents.unshift(agent);
}
}),
removeAgent: (agentId) =>
set((state) => {
if (!state.snapshot) return;
state.snapshot.agents = state.snapshot.agents.filter((a) => a.id !== agentId);
}),
patchApproval: (approval) =>
set((state) => {
if (!state.snapshot) return;
const idx = state.snapshot.approvals.findIndex((a) => a.id === approval.id);
if (idx >= 0) {
state.snapshot.approvals[idx] = approval;
} else {
state.snapshot.approvals.unshift(approval);
}
}),
removeApproval: (approvalId) =>
set((state) => {
if (!state.snapshot) return;
state.snapshot.approvals = state.snapshot.approvals.filter((a) => a.id !== approvalId);
}),
refreshBadges: async () => {
const companyId = get().currentCompanyId;
if (!companyId) return;
try {
const result = await window.agentCompany.getSidebarBadges(companyId);
if (result.ok) {
set((state) => {
state.badges = result.data;
});
}
} catch {
/* ignore -- badges are non-critical */
}
},
});