Skip to content

Commit 3697a84

Browse files
test: warn on polling loops in tests and make adapter test harnesses event-driven
Add t3code/no-polling-loop-in-tests (warn for now) for loops that spin on yieldNow, sleep, or timer/microtask ticks until a predicate holds. Fixed-N flushes stay allowed. The Claude and Codex adapter harnesses now publish to a PubSub whenever they record an event, continuation, or prompt. awaitTerminal, awaitContinuation, awaitSubagentStatus, and until re-check only when that state changes, replacing the 5000-yield awaitUntil budget. Settle windows that followed an offered SDK frame now use offerAndWait. OpenCode2 offer and turn waits now use Deferreds, and desktop preview waits use a change signal fed by state subscribers and the favicon mocks. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f45927f commit 3697a84

9 files changed

Lines changed: 995 additions & 756 deletions

File tree

‎apps/desktop/src/preview/Manager.test.ts‎

Lines changed: 126 additions & 52 deletions
Large diffs are not rendered by default.

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts‎

Lines changed: 320 additions & 440 deletions
Large diffs are not rendered by default.

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts‎

Lines changed: 205 additions & 253 deletions
Large diffs are not rendered by default.

‎apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4709,12 +4709,14 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
47094709
}),
47104710
]);
47114711
const turns: Array<OrchestrationV2ProviderTurn> = [];
4712+
const firstEnded = yield* Deferred.make<void>();
47124713
const bothEnded = yield* Deferred.make<void>();
47134714
yield* runtime.events.pipe(
47144715
Stream.tap((event) =>
47154716
Effect.gen(function* () {
47164717
if (event.type === "provider_turn.updated" && event.providerTurn.status !== "running") {
47174718
turns.push(event.providerTurn);
4719+
if (turns.length === 1) yield* Deferred.succeed(firstEnded, undefined);
47184720
if (turns.length === 2) yield* Deferred.succeed(bothEnded, undefined);
47194721
}
47204722
}),
@@ -4723,9 +4725,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
47234725
Effect.forkScoped,
47244726
);
47254727
yield* runtime.startTurn(turnInput(thread));
4726-
yield* Effect.gen(function* () {
4727-
while (turns.length === 0) yield* Effect.yieldNow;
4728-
});
4728+
yield* Deferred.await(firstEnded);
47294729
yield* runtime.startTurn(secondTurn(thread));
47304730
yield* Deferred.await(bothEnded);
47314731
const [first, second] = turns;
@@ -5021,6 +5021,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
50215021
const continued = (after: ReadonlyArray<ProviderReplayEntry>) =>
50225022
Effect.gen(function* () {
50235023
const offers: Array<ProviderContinuationRequests.ProviderContinuationRequest> = [];
5024+
const offered = yield* Deferred.make<void>();
50245025
const { runtime, thread } = yield* resumed([
50255026
...backgroundLaunch(CHILD),
50265027
event("session.execution.succeeded", { sessionID: SESSION }),
@@ -5055,7 +5056,10 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
50555056
...after,
50565057
]).pipe(
50575058
Effect.provideService(ProviderContinuationRequests.ProviderContinuationRequests, {
5058-
offer: (request) => Effect.sync(() => void offers.push(request)),
5059+
offer: (request) =>
5060+
Effect.sync(() => void offers.push(request)).pipe(
5061+
Effect.andThen(Deferred.succeed(offered, undefined)),
5062+
),
50595063
take: Effect.never,
50605064
}),
50615065
);
@@ -5074,9 +5078,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
50745078
Effect.forkScoped,
50755079
);
50765080
yield* runtime.startTurn(withLineage(thread));
5077-
yield* Effect.gen(function* () {
5078-
while (offers.length === 0) yield* Effect.yieldNow;
5079-
}).pipe(Effect.timeout("2 seconds"), Effect.orDie);
5081+
yield* Deferred.await(offered);
50805082
yield* runtime.startTurn(continuationTurn(thread, offers[0]!));
50815083
yield* Deferred.await(bothEnded);
50825084
const [first, second] = turns;
@@ -5661,6 +5663,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
56615663
const reportText = `<subagent sessionID="${CHILD}" state="completed" description="Sleep">\nCHILD_OK\n</subagent>`;
56625664
const steerId = `msg_t3_steer_${SESSION}:message:opencode2-adapter:steer`;
56635665
const offers: Array<ProviderContinuationRequests.ProviderContinuationRequest> = [];
5666+
const offered = yield* Deferred.make<void>();
56645667
const { runtime, thread } = yield* resumed([
56655668
...backgroundLaunch(CHILD),
56665669
event("session.execution.succeeded", { sessionID: SESSION }),
@@ -5709,7 +5712,10 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
57095712
event("session.execution.succeeded", { sessionID: SESSION }),
57105713
]).pipe(
57115714
Effect.provideService(ProviderContinuationRequests.ProviderContinuationRequests, {
5712-
offer: (request) => Effect.sync(() => void offers.push(request)),
5715+
offer: (request) =>
5716+
Effect.sync(() => void offers.push(request)).pipe(
5717+
Effect.andThen(Deferred.succeed(offered, undefined)),
5718+
),
57135719
take: Effect.never,
57145720
}),
57155721
);
@@ -5727,9 +5733,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
57275733
Effect.forkScoped,
57285734
);
57295735
yield* runtime.startTurn(withLineage(thread));
5730-
yield* Effect.gen(function* () {
5731-
while (offers.length === 0) yield* Effect.yieldNow;
5732-
}).pipe(Effect.timeout("2 seconds"), Effect.orDie);
5736+
yield* Deferred.await(offered);
57335737
const followUp = continuationTurn(thread, offers[0]!);
57345738
yield* runtime.startTurn(followUp);
57355739
const followUpTurnId = (yield* IdAllocator.IdAllocatorV2).derive.providerTurn({

‎apps/server/src/orchestration-v2/OpenCode2OrchestratorV2.live.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ const waitFor = Effect.fn("OpenCode2Live.waitFor")(function* (
223223
done: (projection: OrchestrationV2ThreadProjection) => boolean,
224224
) {
225225
const orchestrator = yield* Orchestrator.OrchestratorV2;
226+
// oxlint-disable-next-line t3code/no-polling-loop-in-tests -- gated live run against a real OpenCode server; wall-clock polling of the projection is the intended probe.
226227
for (let attempt = 0; attempt < STEP_WAIT_SECONDS * 2; attempt += 1) {
227228
const projection = yield* orchestrator.getThreadProjection(threadId);
228229
if (done(projection)) return projection;

‎oxlint-plugin-t3code/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import noMobileUniwindThemeEscapeHatches from "./rules/no-mobile-uniwind-theme-e
1414
import noMobilePullRequestGlyphLiterals from "./rules/no-mobile-pull-request-glyph-literals.ts";
1515
import noNativeTitleTooltip from "./rules/no-native-title-tooltip.ts";
1616
import noOutsetStateIndicators from "./rules/no-outset-state-indicators.ts";
17+
import noPollingLoopInTests from "./rules/no-polling-loop-in-tests.ts";
1718
import noRawMcpRegistration from "./rules/no-raw-mcp-registration.ts";
1819
import noRawResizeObserver from "./rules/no-raw-resize-observer.ts";
1920
import noRealTimeWaitInTests from "./rules/no-real-time-wait-in-tests.ts";
@@ -47,6 +48,7 @@ export default definePlugin({
4748
"no-mobile-pull-request-glyph-literals": noMobilePullRequestGlyphLiterals,
4849
"no-native-title-tooltip": noNativeTitleTooltip,
4950
"no-outset-state-indicators": noOutsetStateIndicators,
51+
"no-polling-loop-in-tests": noPollingLoopInTests,
5052
"no-raw-mcp-registration": noRawMcpRegistration,
5153
"no-raw-resize-observer": noRawResizeObserver,
5254
"no-real-time-wait-in-tests": noRealTimeWaitInTests,
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
import { assert, describe } from "@effect/vitest";
2+
3+
import { createOxlintRuleHarness } from "../test/utils.ts";
4+
5+
const rule = createOxlintRuleHarness("t3code/no-polling-loop-in-tests", {
6+
filename: "fixture.test.ts",
7+
});
8+
9+
describe("t3code/no-polling-loop-in-tests", () => {
10+
rule.valid(
11+
"allows a fixed-N flush with a literal bound",
12+
`
13+
import * as Effect from "effect/Effect";
14+
15+
export const flush = Effect.gen(function* () {
16+
for (let i = 0; i < 3; i++) yield* Effect.yieldNow;
17+
});
18+
`,
19+
);
20+
21+
rule.valid(
22+
"allows a loop that takes from a queue",
23+
`
24+
import * as Effect from "effect/Effect";
25+
import * as Queue from "effect/Queue";
26+
27+
export const until = (events: Queue.Queue<number>) =>
28+
Effect.gen(function* () {
29+
while (true) {
30+
const event = yield* Queue.take(events);
31+
if (event > 2) return event;
32+
}
33+
});
34+
`,
35+
);
36+
37+
rule.valid(
38+
"ignores waits inside nested callbacks",
39+
`
40+
import * as Effect from "effect/Effect";
41+
42+
export const run = (items: ReadonlyArray<number>, done: () => boolean) =>
43+
Effect.gen(function* () {
44+
for (let i = 0; i < items.length; i++) {
45+
if (done()) return;
46+
yield* Effect.forkChild(Effect.gen(function* () {
47+
yield* Effect.yieldNow;
48+
}));
49+
}
50+
});
51+
`,
52+
);
53+
54+
rule.invalid(
55+
"reports a budgeted yieldNow poll that dies after the loop",
56+
`
57+
import * as Effect from "effect/Effect";
58+
59+
export const awaitUntil = (predicate: () => boolean) =>
60+
Effect.gen(function* () {
61+
for (let attempt = 0; attempt < 5000; attempt++) {
62+
if (predicate()) return;
63+
yield* Effect.yieldNow;
64+
}
65+
return yield* Effect.die("Timed out");
66+
});
67+
`,
68+
(output) => {
69+
assert.match(output, /Await the `Deferred`/);
70+
},
71+
);
72+
73+
rule.invalid(
74+
"reports a while loop spinning on a predicate",
75+
`
76+
import * as Effect from "effect/Effect";
77+
78+
export const wait = (turns: Array<number>) =>
79+
Effect.gen(function* () {
80+
while (turns.length === 0) yield* Effect.yieldNow;
81+
});
82+
`,
83+
);
84+
85+
rule.invalid(
86+
"reports a sleep poll with a counter bound and an early return",
87+
`
88+
import * as Effect from "effect/Effect";
89+
90+
export const waitFor = (done: () => boolean) =>
91+
Effect.gen(function* () {
92+
for (let attempt = 0; attempt < 240; attempt += 1) {
93+
if (done()) return;
94+
yield* Effect.sleep("500 millis");
95+
}
96+
});
97+
`,
98+
);
99+
100+
rule.invalid(
101+
"reports a microtask poll keyed on the loop condition",
102+
`
103+
import * as Effect from "effect/Effect";
104+
105+
export const settle = function* (until: () => boolean) {
106+
for (let attempt = 0; attempt < 30 && !until(); attempt++) {
107+
yield* Effect.promise(() => Promise.resolve());
108+
}
109+
};
110+
`,
111+
);
112+
113+
rule.invalid(
114+
"reports an awaited setTimeout poll",
115+
`
116+
export const waitFor = async (done: () => boolean) => {
117+
do {
118+
await new Promise((resolve) => setTimeout(resolve, 10));
119+
} while (!done());
120+
};
121+
`,
122+
);
123+
124+
rule.invalid(
125+
"reports an awaited Promise.resolve poll that breaks",
126+
`
127+
export const waitFor = async (done: () => boolean) => {
128+
while (true) {
129+
if (done()) break;
130+
await Promise.resolve();
131+
}
132+
};
133+
`,
134+
);
135+
});
136+
137+
const productionRule = createOxlintRuleHarness("t3code/no-polling-loop-in-tests");
138+
139+
productionRule.valid(
140+
"ignores non-test files",
141+
`
142+
export const waitFor = async (done: () => boolean) => {
143+
while (!done()) await Promise.resolve();
144+
};
145+
`,
146+
);

0 commit comments

Comments
 (0)