Merge branch 'worktree-agent-a2f30682'
# Conflicts: # .planning/STATE.md # src/main/ipc-core-read-handlers.ts # src/shared/contracts.ts
This commit is contained in:
commit
039c1d117f
10 files changed
+906
-6
No files matched your search
@@ -1,3 +1,7 @@
|
||||
import type { DesktopEvent } from "@shared/types";
|
||||
|
||||
// --- Legacy simple batcher -- kept for backward compatibility ---
|
||||
|
||||
type BatchCallback = () => void;
|
||||
|
||||
export function createEventBatcher(callback: BatchCallback, windowMs = 50) {
|
||||
@@ -28,3 +32,94 @@ export function createEventBatcher(callback: BatchCallback, windowMs = 50) {
|
||||
|
||||
return { schedule, flush, dispose };
|
||||
}
|
||||
|
||||
// --- Enhanced batcher for granular events ---
|
||||
|
||||
export interface GranularEventEntry {
|
||||
type: string;
|
||||
entityId: string;
|
||||
action: string;
|
||||
}
|
||||
|
||||
const MAX_GRANULAR_EVENTS = 10;
|
||||
|
||||
export function entityKeyFromEvent(event: DesktopEvent): GranularEventEntry | null {
|
||||
switch (event.type) {
|
||||
case "task-updated":
|
||||
return { type: "task-updated", entityId: event.taskId, action: event.action };
|
||||
case "run-updated":
|
||||
return { type: "run-updated", entityId: event.runId, action: event.action };
|
||||
case "approval-updated":
|
||||
return { type: "approval-updated", entityId: event.approvalId, action: event.action };
|
||||
case "agent-updated":
|
||||
return { type: "agent-updated", entityId: event.agentId, action: event.action };
|
||||
case "budget-changed":
|
||||
return { type: "budget-changed", entityId: event.companyId, action: "changed" };
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function createDomainEventBatcher(
|
||||
onFullRefresh: () => void,
|
||||
onGranularBatch: (events: GranularEventEntry[]) => void,
|
||||
windowMs = 50,
|
||||
maxGranular = MAX_GRANULAR_EVENTS,
|
||||
) {
|
||||
let timer: ReturnType<typeof setTimeout> | null = null;
|
||||
let hasDomainChanged = false;
|
||||
const pending = new Map<string, GranularEventEntry>();
|
||||
|
||||
function push(event: DesktopEvent) {
|
||||
if (event.type === "domain-changed") {
|
||||
hasDomainChanged = true;
|
||||
} else {
|
||||
const entry = entityKeyFromEvent(event);
|
||||
if (entry) {
|
||||
const key = `${entry.type}:${entry.entityId}`;
|
||||
const existing = pending.get(key);
|
||||
// If any event in window has action "deleted", preserve that action
|
||||
if (existing && existing.action === "deleted" && entry.action !== "deleted") {
|
||||
// Keep the delete -- it takes priority
|
||||
} else {
|
||||
pending.set(key, entry);
|
||||
}
|
||||
} else {
|
||||
// Unknown event type -- treat as domain-changed for safety (per D-05)
|
||||
hasDomainChanged = true;
|
||||
}
|
||||
}
|
||||
if (timer === null) {
|
||||
timer = setTimeout(flushInternal, windowMs);
|
||||
}
|
||||
}
|
||||
|
||||
function flushInternal() {
|
||||
timer = null;
|
||||
if (hasDomainChanged || pending.size > maxGranular) {
|
||||
onFullRefresh();
|
||||
} else if (pending.size > 0) {
|
||||
onGranularBatch([...pending.values()]);
|
||||
}
|
||||
hasDomainChanged = false;
|
||||
pending.clear();
|
||||
}
|
||||
|
||||
function flush() {
|
||||
if (timer !== null) {
|
||||
clearTimeout(timer);
|
||||
flushInternal();
|
||||
}
|
||||
}
|
||||
|
||||
function dispose() {
|
||||
if (timer !== null) {
|
||||
clearTimeout(timer);
|
||||
timer = null;
|
||||
}
|
||||
hasDomainChanged = false;
|
||||
pending.clear();
|
||||
}
|
||||
|
||||
return { push, flush, dispose };
|
||||
}
|
||||
@@ -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 */
|
||||
}
|
||||
},
|
||||
});
|
||||
Reference in new issue
Block a user