Skip to content

Commit 81dd072

Browse files
fix(client-runtime): keep polling linked threads on servers that do not push their changes
Older servers never signal link removal, deletion or settlement changes, so dropping the 10s poll left their linked-thread counts stale. Servers now advertise `linkedThreadRefreshes`; clients poll only servers without it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent d987b6a commit 81dd072

4 files changed

Lines changed: 61 additions & 2 deletions

File tree

‎apps/server/src/environment/ServerEnvironment.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ export const make = Effect.gen(function* () {
227227
fileAttachments: { maxUploadBytes: PROVIDER_SEND_TURN_MAX_FILE_BYTES },
228228
pullRequests: true,
229229
pullRequestChecks: true,
230+
linkedThreadRefreshes: true,
230231
sideChats: true,
231232
vcsBranchStatus: true,
232233
inlineMessageContext: true,

‎packages/client-runtime/src/state/pullRequests.test.ts‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1842,6 +1842,44 @@ it.effect("shares one refresh stream between independently created pull request
18421842
),
18431843
);
18441844

1845+
it.effect.each([
1846+
{ linkedThreadRefreshes: undefined, polledReads: 1 },
1847+
{ linkedThreadRefreshes: true, polledReads: 0 },
1848+
])(
1849+
"polls linked threads only on servers without linked-thread refreshes ($linkedThreadRefreshes)",
1850+
({ linkedThreadRefreshes, polledReads }) =>
1851+
Effect.scoped(
1852+
Effect.gen(function* () {
1853+
let reads = 0;
1854+
const client = {
1855+
[WS_METHODS.pullRequestsSubscribeRefreshes]: () => Stream.never,
1856+
[WS_METHODS.pullRequestsLinkedThreads]: () =>
1857+
Effect.sync(() => {
1858+
reads++;
1859+
return { threads: [] };
1860+
}),
1861+
} as unknown as WsRpcProtocolClient;
1862+
vi.useFakeTimers();
1863+
yield* Effect.addFinalizer(() => Effect.sync(() => vi.useRealTimers()));
1864+
const { atoms, registry } = yield* makeTestRuntime(client);
1865+
registry.set(initialConfigs(TARGET.environmentId), {
1866+
environment: { capabilities: { linkedThreadRefreshes } },
1867+
});
1868+
const linked = atoms.linkedThreads({
1869+
environmentId: TARGET.environmentId,
1870+
input: { projectId: ProjectId.make("project-1"), repository: "acme/web", number: 1 },
1871+
});
1872+
const unmount = registry.mount(linked);
1873+
yield* Effect.addFinalizer(() => Effect.sync(unmount));
1874+
yield* AtomRegistry.getResult(registry, linked);
1875+
expect(reads).toBe(1);
1876+
1877+
yield* Effect.promise(() => vi.advanceTimersByTimeAsync(10_000));
1878+
expect(reads).toBe(1 + polledReads);
1879+
}),
1880+
),
1881+
);
1882+
18451883
it.effect("shares close, reopen, and merge with an untouched client's mounted PR readers", () =>
18461884
Effect.scoped(
18471885
Effect.gen(function* () {
@@ -2171,8 +2209,12 @@ it.effect("updates cached labels after successful edits without rereading the ho
21712209
);
21722210

21732211
vi.mock("./session.ts", () => ({
2174-
createEnvironmentSessionAtoms: () => ({ sessionStateAtom: grantedSessions }),
2212+
createEnvironmentSessionAtoms: () => ({
2213+
sessionStateAtom: grantedSessions,
2214+
initialConfigValueAtom: initialConfigs,
2215+
}),
21752216
}));
2217+
const initialConfigs = Atom.family((_id: EnvironmentId) => Atom.make<unknown>(null));
21762218
const grantedSessions = Atom.family((_id: EnvironmentId) =>
21772219
Atom.make<AsyncResult.AsyncResult<AuthSessionState>>(
21782220
AsyncResult.success({

‎packages/client-runtime/src/state/pullRequests.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import * as EnvironmentSupervisor from "../connection/supervisor.ts";
4949
import { isStalePullRequestReply } from "../pullRequestReply.ts";
5050
import { parsePullRequestQuery } from "../pullRequestQuery.ts";
5151
import { reuseUnchanged } from "./serverConfigProjection.ts";
52+
import { createEnvironmentSessionAtoms } from "./session.ts";
5253

5354
export {
5455
type PullRequestDiffLoadError,
@@ -433,6 +434,19 @@ export function createPullRequestEnvironmentAtoms<R, E>(
433434
onAnswer: PullRequestAnswerHandlers = {},
434435
) {
435436
const refreshes = createPullRequestRefreshAtomFamily(runtime);
437+
const sessions = createEnvironmentSessionAtoms(runtime);
438+
// Servers without `linkedThreadRefreshes` never signal link changes, so poll them instead.
439+
const linkedThreadsRefreshes = Atom.family((environmentId: EnvironmentId) =>
440+
Atom.make((get): unknown => {
441+
const signal = get(refreshes({ environmentId, input: {} }));
442+
const config = get(sessions.initialConfigValueAtom(environmentId));
443+
if (config?.environment.capabilities.linkedThreadRefreshes === true) return signal;
444+
// @effect-diagnostics-next-line globalTimers:off - Mirrors `Atom.withRefresh`, outside an Effect runtime.
445+
const handle = setTimeout(() => get.refreshSelf(), 10_000);
446+
get.addFinalizer(() => clearTimeout(handle));
447+
return {};
448+
}),
449+
);
436450
const commandScheduler = createAtomCommandScheduler();
437451
const routedRequest = createPullRequestRouter();
438452
const serialPerEnvironment = {
@@ -709,7 +723,7 @@ export function createPullRequestEnvironmentAtoms<R, E>(
709723
persistence: persistQuery(PullRequestLinkedThreadsResult),
710724
tag: WS_METHODS.pullRequestsLinkedThreads,
711725
staleTimeMs: 0,
712-
refreshTrigger: ({ environmentId }) => refreshes({ environmentId, input: {} }),
726+
refreshTrigger: ({ environmentId }) => linkedThreadsRefreshes(environmentId),
713727
}),
714728
// For callers that open a thread from the answer: a persisted one reads as final before
715729
// the connection is known, and a kept one can predate the thread.

‎packages/contracts/src/environment.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ export const ExecutionEnvironmentCapabilities = Schema.Struct({
201201
*/
202202
shellThreadFamilies: Schema.optionalKey(Schema.Boolean),
203203
shellOlderSettled: Schema.optionalKey(Schema.Boolean),
204+
/** Publishes a pull request refresh when linked-thread answers change; older servers need polling. */
205+
linkedThreadRefreshes: Schema.optionalKey(Schema.Boolean),
204206
});
205207
export type ExecutionEnvironmentCapabilities = typeof ExecutionEnvironmentCapabilities.Type;
206208

0 commit comments

Comments
 (0)