Compare commits

..
Author SHA1 Message Date
Cursor AgentandJulius Marminge d6708e4efe Use Effect schemas in diagnostics parsing
Co-authored-by: Julius Marminge <[email protected]>
2026-05-18 16:04:04 +00:00
9 changed files with 202 additions and 158 deletions

No files matched your search

@@ -1,4 +1,4 @@
import { describe, expect, it } from "@effect/vitest";
import { assert, describe, it } from "@effect/vitest";
import * as DateTime from "effect/DateTime";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
@@ -41,7 +41,7 @@ describe("ProcessDiagnostics", () => {
].join("\n"),
);
expect(rows).toEqual([
assert.deepEqual(rows, [
{
pid: 10,
ppid: 1,
@@ -125,15 +125,21 @@ describe("ProcessDiagnostics", () => {
],
});
expect(diagnostics.serverPid).toBe(100);
expect(DateTime.formatIso(diagnostics.readAt)).toBe("2026-05-05T10:00:00.000Z");
expect(diagnostics.processCount).toBe(2);
expect(diagnostics.totalRssBytes).toBe(6_000);
expect(diagnostics.totalCpuPercent).toBe(4.75);
expect(diagnostics.processes.map((process) => process.pid)).toEqual([101, 102]);
expect(diagnostics.processes.map((process) => process.depth)).toEqual([0, 1]);
expect(Option.getOrNull(diagnostics.processes[0]!.pgid)).toBe(100);
expect(diagnostics.processes[0]?.childPids).toEqual([102]);
assert.equal(diagnostics.serverPid, 100);
assert.equal(DateTime.formatIso(diagnostics.readAt), "2026-05-05T10:00:00.000Z");
assert.equal(diagnostics.processCount, 2);
assert.equal(diagnostics.totalRssBytes, 6_000);
assert.equal(diagnostics.totalCpuPercent, 4.75);
assert.deepEqual(
diagnostics.processes.map((process) => process.pid),
[101, 102],
);
assert.deepEqual(
diagnostics.processes.map((process) => process.depth),
[0, 1],
);
assert.equal(Option.getOrNull(diagnostics.processes[0]!.pgid), 100);
assert.deepEqual(diagnostics.processes[0]?.childPids, [102]);
}),
);
@@ -176,7 +182,10 @@ describe("ProcessDiagnostics", () => {
],
});
expect(diagnostics.processes.map((process) => process.pid)).toEqual([101, 102, 103]);
assert.deepEqual(
diagnostics.processes.map((process) => process.pid),
[101, 102, 103],
);
}),
);
@@ -209,8 +218,11 @@ describe("ProcessDiagnostics", () => {
Effect.provide(layer),
);
expect(diagnostics.processes.map((process) => process.pid)).toEqual([4242]);
expect(commands).toEqual([
assert.deepEqual(
diagnostics.processes.map((process) => process.pid),
[4242],
);
assert.deepEqual(commands, [
{
command: "ps",
args: ["-axo", "pid=,ppid=,pgid=,stat=,pcpu=,rss=,etime=,command="],
@@ -219,6 +231,63 @@ describe("ProcessDiagnostics", () => {
}),
);
it.effect("decodes Windows process rows from schema-backed JSON", () =>
Effect.gen(function* () {
const commands: Array<{ readonly command: string; readonly args: ReadonlyArray<string> }> =
[];
const spawnerLayer = Layer.succeed(
ChildProcessSpawner.ChildProcessSpawner,
ChildProcessSpawner.make((command) => {
const childProcess = command as unknown as {
readonly command: string;
readonly args: ReadonlyArray<string>;
};
commands.push({ command: childProcess.command, args: childProcess.args });
return Effect.succeed(
mockHandle({
stdout: [
"[",
'{"ProcessId":4242,"ParentProcessId":1,"Name":"node.exe","CommandLine":"node server.js","Status":"Running","WorkingSetSize":2048,"PercentProcessorTime":12.5},',
'{"ProcessId":4243,"ParentProcessId":4242,"Name":"fallback.exe","CommandLine":"","Status":"","WorkingSetSize":-5,"PercentProcessorTime":-1},',
'{"ProcessId":0,"ParentProcessId":4242,"Name":"ignored.exe"}',
"]",
].join(""),
}),
);
}),
);
const rows = yield* ProcessDiagnostics.readProcessRows("win32").pipe(
Effect.provide(spawnerLayer),
);
assert.deepEqual(rows, [
{
pid: 4242,
ppid: 1,
pgid: null,
status: "Running",
cpuPercent: 12.5,
rssBytes: 2048,
elapsed: "",
command: "node server.js",
},
{
pid: 4243,
ppid: 4242,
pgid: null,
status: "Live",
cpuPercent: 0,
rssBytes: 0,
elapsed: "",
command: "fallback.exe",
},
]);
assert.equal(commands[0]?.command, "powershell.exe");
assert.match(commands[0]?.args.join(" ") ?? "", /Get-CimInstance Win32_Process/);
}),
);
it.effect("does not allow signaling the diagnostics query process", () =>
Effect.gen(function* () {
const spawnerLayer = Layer.succeed(
@@ -241,7 +310,7 @@ describe("ProcessDiagnostics", () => {
Effect.provide(layer),
);
expect(result).toEqual({
assert.deepEqual(result, {
pid: 4242,
signal: "SIGINT",
signaled: false,
@@ -26,7 +26,7 @@ export interface ProcessRow {
readonly command: string;
}
const PROCESS_QUERY_TIMEOUT_MS = 1_000;
const PROCESS_QUERY_TIMEOUT = Duration.seconds(1);
const POSIX_PROCESS_QUERY_COMMAND = "pid=,ppid=,pgid=,stat=,pcpu=,rss=,etime=,command=";
const PROCESS_QUERY_MAX_OUTPUT_BYTES = 2 * 1024 * 1024;
@@ -52,6 +52,24 @@ class ProcessDiagnosticsError extends Schema.TaggedErrorClass<ProcessDiagnostics
) {}
const isProcessDiagnosticsError = Schema.is(ProcessDiagnosticsError);
const WindowsProcessRecordJson = Schema.Struct({
ProcessId: Schema.optional(Schema.Number),
ParentProcessId: Schema.optional(Schema.Number),
Name: Schema.optional(Schema.String),
CommandLine: Schema.optional(Schema.String),
Status: Schema.optional(Schema.String),
WorkingSetSize: Schema.optional(Schema.Number),
PercentProcessorTime: Schema.optional(Schema.Number),
});
type WindowsProcessRecordJson = typeof WindowsProcessRecordJson.Type;
const WindowsProcessJson = Schema.Union([
WindowsProcessRecordJson,
Schema.Array(WindowsProcessRecordJson),
]);
const decodeWindowsProcessJson = Schema.decodeUnknownOption(
Schema.fromJsonString(WindowsProcessJson),
);
function toProcessDiagnosticsError(message: string, cause?: unknown): ProcessDiagnosticsError {
return new ProcessDiagnosticsError({
message,
@@ -74,6 +92,14 @@ function parseNumber(value: string): number | null {
return Number.isFinite(parsed) ? parsed : null;
}
function finiteNumberOption(value: number | undefined): Option.Option<number> {
return typeof value === "number" && Number.isFinite(value) ? Option.some(value) : Option.none();
}
function nonEmptyStringOption(value: string | undefined): Option.Option<string> {
return typeof value === "string" && value.trim().length > 0 ? Option.some(value) : Option.none();
}
export function parsePosixProcessRows(output: string): ReadonlyArray<ProcessRow> {
const rows: ProcessRow[] = [];
const rowPattern =
@@ -139,51 +165,52 @@ export function parsePosixProcessRows(output: string): ReadonlyArray<ProcessRow>
return rows;
}
function normalizeWindowsProcessRow(value: unknown): ProcessRow | null {
if (typeof value !== "object" || value === null) return null;
const record = value as Record<string, unknown>;
const pid = typeof record.ProcessId === "number" ? record.ProcessId : null;
const ppid = typeof record.ParentProcessId === "number" ? record.ParentProcessId : null;
const commandLine =
typeof record.CommandLine === "string" && record.CommandLine.trim().length > 0
? record.CommandLine
: typeof record.Name === "string"
? record.Name
: null;
const workingSet =
typeof record.WorkingSetSize === "number" && Number.isFinite(record.WorkingSetSize)
? Math.max(0, Math.round(record.WorkingSetSize))
: 0;
const cpuPercent =
typeof record.PercentProcessorTime === "number" && Number.isFinite(record.PercentProcessorTime)
? Math.max(0, record.PercentProcessorTime)
: 0;
function normalizeWindowsProcessRow(record: WindowsProcessRecordJson): Option.Option<ProcessRow> {
const pid = finiteNumberOption(record.ProcessId).pipe(Option.filter((value) => value > 0));
const ppid = finiteNumberOption(record.ParentProcessId).pipe(
Option.filter((value) => value >= 0),
);
const commandLine = nonEmptyStringOption(record.CommandLine).pipe(
Option.orElse(() => nonEmptyStringOption(record.Name)),
);
if (!pid || pid <= 0 || ppid === null || ppid < 0 || !commandLine) return null;
return {
pid,
ppid,
if (Option.isNone(pid) || Option.isNone(ppid) || Option.isNone(commandLine)) {
return Option.none();
}
const workingSet = finiteNumberOption(record.WorkingSetSize).pipe(
Option.map((value) => Math.max(0, Math.round(value))),
Option.getOrElse(() => 0),
);
const cpuPercent = finiteNumberOption(record.PercentProcessorTime).pipe(
Option.map((value) => Math.max(0, value)),
Option.getOrElse(() => 0),
);
const status = nonEmptyStringOption(record.Status).pipe(Option.getOrElse(() => "Live"));
return Option.some({
pid: pid.value,
ppid: ppid.value,
pgid: null,
status: typeof record.Status === "string" && record.Status.length > 0 ? record.Status : "Live",
status,
cpuPercent,
rssBytes: workingSet,
elapsed: "",
command: commandLine,
};
command: commandLine.value,
});
}
function parseWindowsProcessRows(output: string): ReadonlyArray<ProcessRow> {
if (output.trim().length === 0) return [];
try {
const parsed = JSON.parse(output) as unknown;
const records = Array.isArray(parsed) ? parsed : [parsed];
return records.flatMap((record) => {
const row = normalizeWindowsProcessRow(record);
return row ? [row] : [];
});
} catch {
const decoded = decodeWindowsProcessJson(output);
if (Option.isNone(decoded)) {
return [];
}
const records = Array.isArray(decoded.value) ? decoded.value : [decoded.value];
return records.flatMap((record) => {
const row = normalizeWindowsProcessRow(record);
return Option.isSome(row) ? [row.value] : [];
});
}
export function buildDescendantEntries(
@@ -309,7 +336,7 @@ const runProcess = Effect.fn("runProcess")(
(effect, input) =>
effect.pipe(
Effect.scoped,
Effect.timeoutOption(Duration.millis(PROCESS_QUERY_TIMEOUT_MS)),
Effect.timeoutOption(PROCESS_QUERY_TIMEOUT),
Effect.flatMap((result) =>
Option.match(result, {
onNone: () => Effect.fail(toProcessDiagnosticsError(`${input.errorMessage} timed out.`)),
@@ -14,6 +14,7 @@ import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as PlatformError from "effect/PlatformError";
import * as Schema from "effect/Schema";
interface TraceRecordLike {
readonly name?: unknown;
@@ -65,6 +66,8 @@ interface TraceDiagnosticsErrorSummary {
const DEFAULT_SLOW_SPAN_THRESHOLD_MS = 1_000;
const TOP_LIMIT = 10;
const RECENT_LIMIT = 20;
const decodeTraceRecordJson = Schema.decodeUnknownOption(Schema.fromJsonString(Schema.Unknown));
function toRotatedTracePaths(traceFilePath: string, maxFiles: number): ReadonlyArray<string> {
const backupCount = Math.max(0, Math.floor(maxFiles));
const backups = Array.from(
@@ -217,14 +220,13 @@ export function aggregateTraceDiagnostics(
for (const line of lines) {
if (line.trim().length === 0) continue;
let parsed: unknown;
try {
parsed = JSON.parse(line);
} catch {
const decoded = decodeTraceRecordJson(line);
if (Option.isNone(decoded)) {
parseErrorCount += 1;
continue;
}
const parsed = decoded.value;
if (!isRecordObject(parsed)) {
parseErrorCount += 1;
continue;
+39 -92
View File
@@ -121,8 +121,6 @@ import type { ChatComposerHandle } from "./chat/ChatComposer";
const EMPTY_BROWSE_ENTRIES: FilesystemBrowseResult["entries"] = [];
const BROWSE_STALE_TIME_MS = 30_000;
type CommandPaletteOpenIntent = ReturnType<typeof useCommandPaletteStore.getState>["openIntent"];
type BrowseEntry = FilesystemBrowseResult["entries"][number];
function getLocalFileManagerName(platform: string): string {
if (isMacPlatform(platform)) {
@@ -329,31 +327,11 @@ function errorMessage(error: unknown): string {
}
export function CommandPalette({ children }: { children: ReactNode }) {
const composerHandleRef = useRef<ChatComposerHandle | null>(null);
return (
<ComposerHandleContext value={composerHandleRef}>
{children}
<CommandPaletteController />
</ComposerHandleContext>
);
}
function CommandPaletteController() {
const open = useCommandPaletteStore((store) => store.open);
const setOpen = useCommandPaletteStore((store) => store.setOpen);
useCommandPaletteShortcut();
return (
<CommandDialog open={open} onOpenChange={setOpen}>
<CommandPaletteDialog />
</CommandDialog>
);
}
function useCommandPaletteShortcut() {
const toggleOpen = useCommandPaletteStore((store) => store.toggleOpen);
const keybindings = useServerKeybindings();
const composerHandleRef = useRef<ChatComposerHandle | null>(null);
const routeTarget = useParams({
strict: false,
select: (params) => resolveThreadRouteTarget(params),
@@ -384,12 +362,26 @@ function useCommandPaletteShortcut() {
window.addEventListener("keydown", onKeyDown);
return () => window.removeEventListener("keydown", onKeyDown);
}, [keybindings, terminalOpen, toggleOpen]);
return (
<ComposerHandleContext value={composerHandleRef}>
<CommandDialog open={open} onOpenChange={setOpen}>
{children}
<CommandPaletteDialog />
</CommandDialog>
</ComposerHandleContext>
);
}
function CommandPaletteDialog() {
const open = useCommandPaletteStore((store) => store.open);
const setOpen = useCommandPaletteStore((store) => store.setOpen);
useCloseCommandPaletteOnUnmount(setOpen);
useEffect(() => {
return () => {
setOpen(false);
};
}, [setOpen]);
if (!open) {
return null;
@@ -398,70 +390,6 @@ function CommandPaletteDialog() {
return <OpenCommandPaletteDialog />;
}
function useCloseCommandPaletteOnUnmount(setOpen: (open: boolean) => void) {
useEffect(() => {
return () => {
setOpen(false);
};
}, [setOpen]);
}
function usePrefetchBrowsePaths({
exactBrowseEntry,
filteredBrowseEntryCount,
highlightedBrowseEntry,
isBrowsing,
prefetchBrowsePath,
query,
}: {
readonly exactBrowseEntry: BrowseEntry | null;
readonly filteredBrowseEntryCount: number;
readonly highlightedBrowseEntry: BrowseEntry | null;
readonly isBrowsing: boolean;
readonly prefetchBrowsePath: (partialPath: string) => void;
readonly query: string;
}) {
// Prefetch the parent and the most likely next child so browse navigation
// stays warm without scanning every child directory in large trees.
useEffect(() => {
if (!isBrowsing || filteredBrowseEntryCount === 0) return;
if (canNavigateUp(query)) {
prefetchBrowsePath(getBrowseParentPath(query)!);
}
const nextChild = highlightedBrowseEntry ?? exactBrowseEntry;
if (nextChild) {
prefetchBrowsePath(appendBrowsePathSegment(query, nextChild.name));
}
}, [
exactBrowseEntry,
filteredBrowseEntryCount,
highlightedBrowseEntry,
isBrowsing,
prefetchBrowsePath,
query,
]);
}
function useOpenAddProjectIntent({
clearOpenIntent,
openAddProjectFlow,
openIntent,
}: {
readonly clearOpenIntent: () => void;
readonly openAddProjectFlow: () => void;
readonly openIntent: CommandPaletteOpenIntent;
}) {
useLayoutEffect(() => {
if (openIntent?.kind !== "add-project") {
return;
}
clearOpenIntent();
openAddProjectFlow();
}, [clearOpenIntent, openAddProjectFlow, openIntent]);
}
function OpenCommandPaletteDialog() {
const navigate = useNavigate();
const setOpen = useCommandPaletteStore((store) => store.setOpen);
@@ -659,14 +587,27 @@ function OpenCommandPaletteDialog() {
[browseEnvironmentId, currentProjectCwdForBrowse, fetchBrowseResult, queryClient],
);
usePrefetchBrowsePaths({
// Prefetch the parent and the most likely next child so browse navigation
// stays warm without scanning every child directory in large trees.
useEffect(() => {
if (!isBrowsing || filteredBrowseEntries.length === 0) return;
if (canNavigateUp(query)) {
prefetchBrowsePath(getBrowseParentPath(query)!);
}
const nextChild = highlightedBrowseEntry ?? exactBrowseEntry;
if (nextChild) {
prefetchBrowsePath(appendBrowsePathSegment(query, nextChild.name));
}
}, [
exactBrowseEntry,
filteredBrowseEntryCount: filteredBrowseEntries.length,
filteredBrowseEntries.length,
highlightedBrowseEntry,
isBrowsing,
prefetchBrowsePath,
query,
});
]);
const openProjectFromSearch = useMemo(
() => async (project: (typeof projects)[number]) => {
@@ -1030,7 +971,13 @@ function OpenCommandPaletteDialog() {
startAddProjectSourceSelection,
]);
useOpenAddProjectIntent({ clearOpenIntent, openAddProjectFlow, openIntent });
useLayoutEffect(() => {
if (openIntent?.kind !== "add-project") {
return;
}
clearOpenIntent();
openAddProjectFlow();
}, [clearOpenIntent, openAddProjectFlow, openIntent]);
const actionItems: Array<CommandPaletteActionItem | CommandPaletteSubmenuItem> = [];
+10 -11
View File
@@ -175,6 +175,7 @@ import {
import { sortThreads } from "../lib/threadSort";
import { SidebarUpdatePill } from "./sidebar/SidebarUpdatePill";
import { useCopyToClipboard } from "~/hooks/useCopyToClipboard";
import { CommandDialogTrigger } from "./ui/command";
import { readEnvironmentApi } from "../environmentApi";
import { useSettings, useUpdateSettings } from "~/hooks/useSettings";
import { useServerKeybindings } from "../rpc/serverState";
@@ -2618,22 +2619,20 @@ const SidebarProjectsContent = memo(function SidebarProjectsContent(
},
[updateSettings],
);
const setCommandPaletteOpen = useCommandPaletteStore((store) => store.setOpen);
const handleCommandPaletteClick = useCallback(() => {
setCommandPaletteOpen(true);
}, [setCommandPaletteOpen]);
return (
<SidebarContent className="gap-0">
<SidebarGroup className="px-2 pt-2 pb-1">
<SidebarMenu>
<SidebarMenuItem>
<SidebarMenuButton
size="sm"
className="gap-2 px-2 py-1.5 text-muted-foreground/70 hover:bg-accent hover:text-foreground focus-visible:ring-0"
data-testid="command-palette-trigger"
aria-haspopup="dialog"
onClick={handleCommandPaletteClick}
<CommandDialogTrigger
render={
<SidebarMenuButton
size="sm"
className="gap-2 px-2 py-1.5 text-muted-foreground/70 hover:bg-accent hover:text-foreground focus-visible:ring-0"
data-testid="command-palette-trigger"
/>
}
>
<SearchIcon className="size-3.5" />
<span className="flex-1 truncate text-left text-xs">Search</span>
@@ -2642,7 +2641,7 @@ const SidebarProjectsContent = memo(function SidebarProjectsContent(
{commandPaletteShortcutLabel}
</Kbd>
) : null}
</SidebarMenuButton>
</CommandDialogTrigger>
</SidebarMenuItem>
</SidebarMenu>
</SidebarGroup>
Binary file not shown.
Binary file not shown.

Before

Width:  |  Height:  |  Size: 84 KiB

Binary file not shown.
Binary file not shown.

Before

Width:  |  Height:  |  Size: 19 KiB