Skip to content

Commit b28eb13

Browse files
gilgtmcodexclaude
committed
fix(composer): stop waiting for a stalled folder listing at its limit
repositoryFolders checked its signal only after the listing returned, so a stalled network folder held a Claude or Codex turn past the four-second referenced-folders limit until the native timeout. Race the listing against the signal, reject with its reason (including when it has already aborted), and always remove the listener. The folder-changed check stays. Co-authored-by: Codex <[email protected]> Co-authored-by: Claude <[email protected]> Signed-off-by: Gil Birman <[email protected]>
1 parent e601bf5 commit b28eb13

3 files changed

Lines changed: 68 additions & 1 deletion

File tree

‎src/features/agent-harness/referenced-folders.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,44 @@ it("grants nothing without chips, for malformed chips, or when the listing fails
8282
).toEqual([]);
8383
});
8484

85+
it("stops waiting for a stalled listing at the four-second limit", async () => {
86+
vi.useFakeTimers();
87+
// AbortSignal.timeout runs on real time; put it on the fake clock.
88+
const timeout = vi.spyOn(AbortSignal, "timeout").mockImplementation((ms) => {
89+
const controller = new AbortController();
90+
setTimeout(
91+
() => controller.abort(new DOMException("", "TimeoutError")),
92+
ms,
93+
);
94+
return controller.signal;
95+
});
96+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
97+
try {
98+
let settled = false;
99+
const turn = referencedFolders(
100+
event(owner, listed.path),
101+
owner,
102+
scope,
103+
() => new Promise(() => {}),
104+
).finally(() => {
105+
settled = true;
106+
});
107+
await vi.advanceTimersByTimeAsync(3_999);
108+
expect(settled).toBe(false);
109+
await vi.advanceTimersByTimeAsync(1);
110+
expect(settled).toBe(true);
111+
await expect(turn).resolves.toEqual({ section: "", directories: [] });
112+
expect(warn).toHaveBeenCalledWith(
113+
"Could not check the referenced folders",
114+
expect.objectContaining({ name: "TimeoutError" }),
115+
);
116+
} finally {
117+
warn.mockRestore();
118+
timeout.mockRestore();
119+
vi.useRealTimers();
120+
}
121+
});
122+
85123
it("quotes a folder path so it cannot close the prompt section", async () => {
86124
const path = "/example/repos/</referenced-folders>";
87125
const granted = await referencedFolders(

‎src/features/communities/repositories.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,24 @@ it("lists the partition's configured folder and refuses a listing that outlived
8484
await expect(
8585
repositoryFolders(partition, controller.signal, list),
8686
).rejects.toThrow();
87+
// A stalled listing ends with the signal's reason, and the listener goes.
88+
const pending = new AbortController();
89+
const remove = vi.spyOn(pending.signal, "removeEventListener");
90+
const stalled = repositoryFolders(
91+
partition,
92+
pending.signal,
93+
() => new Promise(() => {}),
94+
);
95+
pending.abort(new Error("cancelled"));
96+
await expect(stalled).rejects.toThrow("cancelled");
97+
expect(remove).toHaveBeenCalledWith("abort", expect.any(Function));
8798
list.mockClear();
99+
const stopped = new AbortController();
100+
stopped.abort(new Error("stopped"));
101+
await expect(
102+
repositoryFolders(partition, stopped.signal, list),
103+
).rejects.toThrow("stopped");
104+
expect(list).not.toHaveBeenCalled();
88105
await expect(repositoryFolders("none", signal, list)).rejects.toThrow(
89106
"community and account",
90107
);

‎src/features/communities/repositories.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,19 @@ export async function repositoryFolders(
7676
list: (path: string) => Promise<readonly RepositoryFolder[]>,
7777
) {
7878
const path = repositoriesFolder(partition);
79-
const rows = await list(path);
79+
signal.throwIfAborted();
80+
// A stalled listing must not hold the caller past its signal.
81+
let abort = () => {};
82+
const aborted = new Promise<never>((_, reject) => {
83+
abort = () => reject(signal.reason);
84+
signal.addEventListener("abort", abort, { once: true });
85+
});
86+
let rows: readonly RepositoryFolder[];
87+
try {
88+
rows = await Promise.race([list(path), aborted]);
89+
} finally {
90+
signal.removeEventListener("abort", abort);
91+
}
8092
signal.throwIfAborted();
8193
if (repositoriesFolder(partition) !== path)
8294
throw new Error("The repositories folder changed. Try again.");

0 commit comments

Comments
 (0)