Skip to content

Open-source AI coworkers that each get a computer of their own: a browser, files and tools, with every action decided before it happens and recorded after. Bring any AG-UI agent.

Stop the tool-call repair recursing into itself (#63)

David McKayf8885b6d635c
diff --git a/app/src/lib/copilot/repair-history.ts b/app/src/lib/copilot/repair-history.ts
index 04ac6156..ad8f3e66 100644
--- a/app/src/lib/copilot/repair-history.ts
+++ b/app/src/lib/copilot/repair-history.ts
@@ -22,7 +22,12 @@ function isToolResult(message: Message): message is Message & ToolResult {
*/
export function repairUnansweredToolCalls(
messages: ReadonlyArray<Message>,
- newId: () => string = () => newId(),
+ /*
+ * Named so it cannot shadow the import it defaults to. A parameter called `newId` defaulting to
+ * `newId()` resolves to itself and recurses until the stack goes, and it does so only on the
+ * repair branch, which every test avoided by passing its own. Biome said so, as an unused import.
+ */
+ mintId: () => string = newId,
): ReadonlyArray<Message> {
const answered = new Set<string>();
for (const message of messages) {
@@ -47,7 +52,7 @@ export function repairUnansweredToolCalls(
// OpenAI requires the results to follow their calls, and some providers require the order to
// match the `tool_calls` array as well.
repaired.push({
- id: newId(),
+ id: mintId(),
role: "tool",
toolCallId: call.id,
content: UNANSWERED,
diff --git a/app/tests/repair-history.test.ts b/app/tests/repair-history.test.ts
index c697efe2..3ee972cf 100644
--- a/app/tests/repair-history.test.ts
+++ b/app/tests/repair-history.test.ts
@@ -123,3 +123,39 @@ describe("repairing a history before it is sent", () => {
expect(repairUnansweredToolCalls(messages, ids)).toBe(messages);
});
});
+
+/**
+ * The default id source, which every test above replaces with its own.
+ *
+ * That is the reason a self-recursive default survived review: the parameter exists so a test can
+ * make ids predictable, so no test ever ran the default, and the default only runs on the repair
+ * branch. This one calls it the way the only real caller does, with the argument omitted.
+ */
+describe("repairUnansweredToolCalls without an id source", () => {
+ test("repairs using its own ids rather than recursing", () => {
+ const messages: Message[] = [
+ { id: "a", role: "user", content: "go" },
+ {
+ id: "b",
+ role: "assistant",
+ content: "",
+ toolCalls: [
+ {
+ id: "call-1",
+ type: "function",
+ function: { name: "act", arguments: "{}" },
+ },
+ ],
+ },
+ ];
+
+ const repaired = repairUnansweredToolCalls(messages);
+
+ expect(repaired).toHaveLength(3);
+ const result = repaired[2] as Message & { toolCallId: string };
+ expect(result.role).toBe("tool");
+ expect(result.toolCallId).toBe("call-1");
+ // A real id, not an empty string and not the call's own id.
+ expect(result.id).toMatch(/^[0-9a-f-]{36}$/);
+ });
+});