Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ function runtimeEventToActivities(
kind: "tool.updated",
summary: event.payload.title ?? "Tool updated",
payload: {
...(event.itemId ? { itemId: event.itemId } : {}),
itemType: event.payload.itemType,
...(event.payload.status ? { status: event.payload.status } : {}),
...(event.payload.detail ? { detail: truncateDetail(event.payload.detail) } : {}),
Expand All @@ -463,8 +464,9 @@ function runtimeEventToActivities(
kind: "tool.completed",
summary: event.payload.title ?? "Tool",
payload: {
...(event.itemId ? { itemId: event.itemId } : {}),
itemType: event.payload.itemType,
...(event.payload.detail ? { detail: truncateDetail(event.payload.detail) } : {}),
...(event.payload.detail ? { detail: event.payload.detail } : {}),
},
turnId: toTurnId(event.turnId) ?? null,
...maybeSequence,
Expand All @@ -484,8 +486,9 @@ function runtimeEventToActivities(
kind: "tool.started",
summary: `${event.payload.title ?? "Tool"} started`,
payload: {
...(event.itemId ? { itemId: event.itemId } : {}),
itemType: event.payload.itemType,
...(event.payload.detail ? { detail: truncateDetail(event.payload.detail) } : {}),
...(event.payload.detail ? { detail: event.payload.detail } : {}),
},
turnId: toTurnId(event.turnId) ?? null,
...maybeSequence,
Expand Down
10 changes: 7 additions & 3 deletions apps/server/src/provider/Layers/CodexAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,10 @@ function toCanonicalItemType(raw: unknown): CanonicalItemType {
return "unknown";
}

function itemTitle(itemType: CanonicalItemType): string | undefined {
function itemTitle(
itemType: CanonicalItemType,
started = false,
): string | undefined {
switch (itemType) {
case "assistant_message":
return "Assistant message";
Expand All @@ -224,7 +227,7 @@ function itemTitle(itemType: CanonicalItemType): string | undefined {
case "plan":
return "Plan";
case "command_execution":
return "Ran command";
return started ? "Running command" : "Ran command";
case "file_change":
return "File change";
case "mcp_tool_call":
Expand Down Expand Up @@ -562,14 +565,15 @@ function mapItemLifecycle(
: lifecycle === "item.completed"
? "completed"
: undefined;
const title = itemTitle(itemType, lifecycle === "item.started");

return {
...runtimeEventBase(event, canonicalThreadId),
type: lifecycle,
payload: {
itemType,
...(status ? { status } : {}),
...(itemTitle(itemType) ? { title: itemTitle(itemType) } : {}),
...(title ? { title } : {}),
...(detail ? { detail } : {}),
...(event.payload !== undefined ? { data: event.payload } : {}),
},
Expand Down
5 changes: 4 additions & 1 deletion apps/web/src/components/chat/MessagesTimeline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,10 @@ export const MessagesTimeline = memo(function MessagesTimeline({
)}
<div className="space-y-0.5">
{visibleEntries.map((workEntry) => (
<SimpleWorkEntryRow key={`work-row:${workEntry.id}`} workEntry={workEntry} />
<SimpleWorkEntryRow
key={`work-row:${workEntry.collapseKey ?? workEntry.id}`}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate React keys when same command runs twice

Medium Severity

Switching the React key from workEntry.id (always unique) to workEntry.collapseKey ?? workEntry.id can produce duplicate keys. When two invocations of the same command occur within one turn and lack a unique itemId, they share an identical content-derived collapseKey. After collapsing, both resulting entries retain that same collapseKey, causing React to receive duplicate keys, which can lead to incorrect component reuse or lost UI state.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 941b722. Configure here.

workEntry={workEntry}
/>
))}
</div>
</div>
Expand Down
68 changes: 57 additions & 11 deletions apps/web/src/session-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ export interface WorkLogEntry {
tone: "thinking" | "tool" | "info" | "error";
toolTitle?: string;
itemType?: ToolLifecycleItemType;
itemId?: string;
requestKind?: PendingApproval["requestKind"];
collapseKey?: string;
}

interface DerivedWorkLogEntry extends WorkLogEntry {
Expand Down Expand Up @@ -461,14 +463,23 @@ export function deriveWorkLogEntries(
const ordered = [...activities].toSorted(compareActivitiesByOrder);
const entries = ordered
.filter((activity) => (latestTurnId ? activity.turnId === latestTurnId : true))
.filter((activity) => activity.kind !== "tool.started")
.filter((activity) => {
if (activity.kind !== "tool.started") {
return true;
}
const payload =
activity.payload && typeof activity.payload === "object"
? (activity.payload as Record<string, unknown>)
: null;
return payload?.itemType === "command_execution";
})
.filter((activity) => activity.kind !== "task.started" && activity.kind !== "task.completed")
.filter((activity) => activity.kind !== "context-window.updated")
.filter((activity) => activity.summary !== "Checkpoint captured")
.filter((activity) => !isPlanBoundaryToolActivity(activity))
.map(toDerivedWorkLogEntry);
return collapseDerivedWorkLogEntries(entries).map(
({ activityKind: _activityKind, collapseKey: _collapseKey, ...entry }) => entry,
({ activityKind: _activityKind, ...entry }) => entry,
);
}

Expand Down Expand Up @@ -500,6 +511,7 @@ function toDerivedWorkLogEntry(activity: OrchestrationThreadActivity): DerivedWo
activityKind: activity.kind,
};
const itemType = extractWorkLogItemType(payload);
const itemId = extractWorkLogItemId(payload);
const requestKind = extractWorkLogRequestKind(payload);
if (payload && typeof payload.detail === "string" && payload.detail.length > 0) {
const detail = stripTrailingExitCode(payload.detail).output;
Expand All @@ -519,6 +531,9 @@ function toDerivedWorkLogEntry(activity: OrchestrationThreadActivity): DerivedWo
if (itemType) {
entry.itemType = itemType;
}
if (itemId) {
entry.itemId = itemId;
}
if (requestKind) {
entry.requestKind = requestKind;
}
Expand All @@ -533,13 +548,28 @@ function collapseDerivedWorkLogEntries(
entries: ReadonlyArray<DerivedWorkLogEntry>,
): DerivedWorkLogEntry[] {
const collapsed: DerivedWorkLogEntry[] = [];
const openLifecycleRowIndexByCollapseKey = new Map<string, number>();
for (const entry of entries) {
const previous = collapsed.at(-1);
if (previous && shouldCollapseToolLifecycleEntries(previous, entry)) {
collapsed[collapsed.length - 1] = mergeDerivedWorkLogEntries(previous, entry);
continue;
const collapseKey = entry.collapseKey;
if (collapseKey) {
const openIndex = openLifecycleRowIndexByCollapseKey.get(collapseKey);
if (openIndex !== undefined) {
const previous = collapsed[openIndex];
if (previous && shouldCollapseToolLifecycleEntries(previous, entry)) {
collapsed[openIndex] = mergeDerivedWorkLogEntries(previous, entry);
if (entry.activityKind === "tool.completed") {
openLifecycleRowIndexByCollapseKey.delete(collapseKey);
}
continue;
}
}
}

collapsed.push(entry);
if (collapseKey && (entry.activityKind === "tool.started" || entry.activityKind === "tool.updated")) {
openLifecycleRowIndexByCollapseKey.set(collapseKey, collapsed.length - 1);
continue;
}
}
return collapsed;
}
Expand All @@ -548,7 +578,11 @@ function shouldCollapseToolLifecycleEntries(
previous: DerivedWorkLogEntry,
next: DerivedWorkLogEntry,
): boolean {
if (previous.activityKind !== "tool.updated" && previous.activityKind !== "tool.completed") {
if (
previous.activityKind !== "tool.started" &&
previous.activityKind !== "tool.updated" &&
previous.activityKind !== "tool.completed"
) {
return false;
}
if (next.activityKind !== "tool.updated" && next.activityKind !== "tool.completed") {
Expand Down Expand Up @@ -596,16 +630,24 @@ function mergeChangedFiles(
}

function deriveToolLifecycleCollapseKey(entry: DerivedWorkLogEntry): string | undefined {
if (entry.activityKind !== "tool.updated" && entry.activityKind !== "tool.completed") {
if (
entry.activityKind !== "tool.started" &&
entry.activityKind !== "tool.updated" &&
entry.activityKind !== "tool.completed"
) {
return undefined;
}
const itemId = entry.itemId?.trim() ?? "";
if (itemId.length > 0) {
return itemId;
}
const normalizedLabel = normalizeCompactToolLabel(entry.toolTitle ?? entry.label);
const detail = entry.detail?.trim() ?? "";
const commandOrDetail = (entry.command ?? entry.detail)?.trim() ?? "";
const itemType = entry.itemType ?? "";
if (normalizedLabel.length === 0 && detail.length === 0 && itemType.length === 0) {
if (normalizedLabel.length === 0 && commandOrDetail.length === 0 && itemType.length === 0) {
return undefined;
}
return [itemType, normalizedLabel, detail].join("\u001f");
return [itemType, normalizedLabel, commandOrDetail].join("\u001f");
}

function normalizeCompactToolLabel(value: string): string {
Expand Down Expand Up @@ -698,6 +740,10 @@ function extractWorkLogItemType(
return undefined;
}

function extractWorkLogItemId(payload: Record<string, unknown> | null): string | undefined {
return typeof payload?.itemId === "string" && payload.itemId.length > 0 ? payload.itemId : undefined;
}

function extractWorkLogRequestKind(
payload: Record<string, unknown> | null,
): WorkLogEntry["requestKind"] | undefined {
Expand Down
Loading