Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 37 additions & 15 deletions apps/code/src/main/trpc/trpc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { initTRPC } from "@trpc/server";
import log from "electron-log/main";

const trpc = initTRPC.create({
isServer: true,
Expand All @@ -9,28 +10,49 @@ const CALL_RATE_THRESHOLD = 50;

const callCounts: Record<string, number[]> = {};

const callRateMonitor = trpc.middleware(async ({ path, next }) => {
if (process.env.NODE_ENV !== "development") {
return next();
}
const ipcTimingEnabled = process.env.IPC_TIMINGS === "true";
const ipcTimingBootMs = 15_000;
const bootTime = Date.now();

const now = Date.now();
if (!callCounts[path]) {
callCounts[path] = [];
}
const callRateMonitor = trpc.middleware(async ({ path, next, type }) => {
const shouldTime =
ipcTimingEnabled && Date.now() - bootTime < ipcTimingBootMs;
const t = shouldTime ? performance.now() : 0;

const timestamps = callCounts[path];
timestamps.push(now);
if (shouldTime) {
log.info(`[ipc-timing] >> ${type} ${path}`);
}

const cutoff = now - CALL_RATE_WINDOW_MS;
while (timestamps.length > 0 && timestamps[0] < cutoff) {
timestamps.shift();
if (process.env.NODE_ENV === "development") {
const now = Date.now();
if (!callCounts[path]) {
callCounts[path] = [];
}

const timestamps = callCounts[path];
timestamps.push(now);

const cutoff = now - CALL_RATE_WINDOW_MS;
while (timestamps.length > 0 && timestamps[0] < cutoff) {
timestamps.shift();
}

if (timestamps.length >= CALL_RATE_THRESHOLD) {
log.warn(
`[ipc-rate] ${type} ${path} called ${timestamps.length} times in ${CALL_RATE_WINDOW_MS}ms`,
);
}
}

if (timestamps.length >= CALL_RATE_THRESHOLD) {
const result = await next();

if (shouldTime) {
log.info(
`[ipc-timing] << ${type} ${path}: ${(performance.now() - t).toFixed(0)}ms`,
);
}

return next();
return result;
});

export const router = trpc.router;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ export function CommandCenterView() {
const { cells, summary } = useCommandCenterData();
const { markAsViewed } = useTaskViewed();

const visibleTaskIds = useMemo(
() => cells.map((c) => c.taskId).filter((id): id is string => id != null),
[cells],
);
const visibleTaskIdsKey = cells
.map((c) => c.taskId)
.filter(Boolean)
.join(",");

useEffect(() => {
for (const taskId of visibleTaskIds) {
if (!visibleTaskIdsKey) return;
for (const taskId of visibleTaskIdsKey.split(",")) {
markAsViewed(taskId);
}
}, [visibleTaskIds, markAsViewed]);
}, [visibleTaskIdsKey, markAsViewed]);

const headerContent = useMemo(
() => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useChatTitleGenerator } from "./useChatTitleGenerator";
const log = logger.scope("session-connection");

const connectingTasks = new Set<string>();
const activityRecorded = new Set<string>();

interface UseSessionConnectionOptions {
taskId: string;
Expand All @@ -37,14 +38,20 @@ export function useSessionConnection({
useEffect(() => {
const taskRunId = session?.taskRunId;
if (!taskRunId) return;
trpcClient.agent.recordActivity.mutate({ taskRunId }).catch(() => {});
if (!activityRecorded.has(taskRunId)) {
activityRecorded.add(taskRunId);
trpcClient.agent.recordActivity.mutate({ taskRunId }).catch(() => {});
}
const heartbeat = setInterval(
() => {
trpcClient.agent.recordActivity.mutate({ taskRunId }).catch(() => {});
},
5 * 60 * 1000,
);
return () => clearInterval(heartbeat);
return () => {
clearInterval(heartbeat);
activityRecorded.delete(taskRunId);
};
}, [session?.taskRunId]);

useEffect(() => {
Expand Down
Loading