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
65 changes: 57 additions & 8 deletions src/CodexAcpServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2382,14 +2382,26 @@ export class CodexAcpServer {
}
}

function mergeHistoryUpdates(
export function mergeHistoryUpdates(
responseItemFallbackUpdates: UpdateSessionEvent[],
threadUpdates: UpdateSessionEvent[],
): UpdateSessionEvent[] {
const merged: UpdateSessionEvent[] = [];
const seen = new Set<string>();
const fallbackIndicesByKey = new Map<string, number[]>();
const fallbackIndicesByContentKey = new Map<string, number[]>();
let fallbackIndex = 0;

for (let index = 0; index < responseItemFallbackUpdates.length; index += 1) {
const update = responseItemFallbackUpdates[index]!;
indexHistoryUpdate(fallbackIndicesByKey, historyUpdateKey(update), index);
indexHistoryUpdate(
fallbackIndicesByContentKey,
historyUpdateContentKey(update),
index,
);
}

const pushUpdate = (update: UpdateSessionEvent) => {
const key = historyUpdateKey(update);
if (key && seen.has(key)) {
Expand All @@ -2408,13 +2420,15 @@ function mergeHistoryUpdates(
return;
}

const matchIndex = responseItemFallbackUpdates.findIndex((update, index) => (
index >= fallbackIndex
&& (
(targetKey !== null && historyUpdateKey(update) === targetKey)
|| (targetContentKey !== null && historyUpdateContentKey(update) === targetContentKey)
)
));
const keyMatchIndex = nextHistoryUpdateIndex(
fallbackIndicesByKey.get(targetKey ?? ""),
fallbackIndex,
);
const contentMatchIndex = nextHistoryUpdateIndex(
fallbackIndicesByContentKey.get(targetContentKey ?? ""),
fallbackIndex,
);
const matchIndex = minHistoryUpdateIndex(keyMatchIndex, contentMatchIndex);
if (matchIndex === -1) {
return;
}
Expand All @@ -2439,6 +2453,41 @@ function mergeHistoryUpdates(
return merged;
}

function indexHistoryUpdate(
target: Map<string, number[]>,
key: string | null,
index: number,
): void {
if (key === null) return;
const indices = target.get(key);
if (indices) {
indices.push(index);
} else {
target.set(key, [index]);
}
}

function nextHistoryUpdateIndex(indices: number[] | undefined, minimum: number): number {
if (!indices || indices.length === 0) return -1;
let low = 0;
let high = indices.length;
while (low < high) {
const middle = low + Math.floor((high - low) / 2);
if (indices[middle]! < minimum) {
low = middle + 1;
} else {
high = middle;
}
}
return low < indices.length ? indices[low]! : -1;
}

function minHistoryUpdateIndex(left: number, right: number): number {
if (left === -1) return right;
if (right === -1) return left;
return Math.min(left, right);
}

function historyUpdateKey(update: UpdateSessionEvent): string | null {
switch (update.sessionUpdate) {
case "user_message_chunk":
Expand Down
49 changes: 49 additions & 0 deletions src/__tests__/HistoryUpdateMerge.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {describe, expect, it} from "vitest";
import type {UpdateSessionEvent} from "../ACPSessionConnection";
import {mergeHistoryUpdates} from "../CodexAcpServer";

describe("mergeHistoryUpdates", () => {
it("uses the earliest content or exact-key match", () => {
const earlierContentMatch = message("fallback-id", "same content", "fallback-content");
const laterExactMatch = message("target-id", "same content", "fallback-exact");
const target = message("target-id", "same content", "thread");

expect(mergeHistoryUpdates(
[earlierContentMatch, laterExactMatch],
[target],
)).toEqual([target]);
});

it("does not revisit matches before the advancing fallback cursor", () => {
const targetA = message("message-a", "alpha", "thread-a");
const between = thought("thought-between", "keep between matches");
const targetB = message("message-b", "beta", "thread-b");

expect(mergeHistoryUpdates(
[
message("message-a", "alpha", "fallback-a"),
between,
message("message-b", "beta", "fallback-b"),
message("message-a", "alpha", "fallback-a-late"),
],
[targetA, targetB],
)).toEqual([targetA, between, targetB]);
});
});

function message(messageId: string, text: string, source: string): UpdateSessionEvent {
return {
sessionUpdate: "agent_message_chunk",
messageId,
content: {type: "text", text},
_meta: {source},
};
}

function thought(messageId: string, text: string): UpdateSessionEvent {
return {
sessionUpdate: "agent_thought_chunk",
messageId,
content: {type: "text", text},
};
}