The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 10:45 UTC
fix(server): skip history reads for ineligible idle compaction (#23929)
Co-authored-by: azogz <11478150+azogz@users.noreply.github.com> Co-authored-by: maria-rcks <maria@kuuro.net>
azogz committed
Oct 30, 2026 at 17:00 UTC
f0ec7398d7741a2696586fc50c7cd41b0992d48f
2 files changed
+162
apps/server/src/orchestration-v2/IdleCompaction.test.ts
+148
@@ -1,6 +1,9 @@
1
import { describe, expect, it } from "@effect/vitest";
2
import {
3
+ DEFAULT_SERVER_SETTINGS,
4
+ EventId,
5
MessageId,
6
+ type OrchestrationV2ThreadProjection,
7
NodeId,
8
ProjectId,
9
ProviderDriverKind,
@@ -11,6 +14,17 @@ import {
14
ThreadId,
15
} from "@t3tools/contracts";
16
import * as DateTime from "effect/DateTime";
17
+import * as Deferred from "effect/Deferred";
18
+import * as Effect from "effect/Effect";
19
+import * as Layer from "effect/Layer";
20
+import * as Stream from "effect/Stream";
21
+import * as Tracer from "effect/Tracer";
22
+import * as TestClock from "effect/testing/TestClock";
23
+
24
+import * as ServerSettings from "../serverSettings.ts";
25
+import * as IdleCompactionService from "./IdleCompactionService.ts";
26
+import * as Orchestrator from "./Orchestrator.ts";
27
+import * as ProjectionStore from "./ProjectionStore.ts";
28
29
import { idleCompactionWindow, type IdleCompactionRecords } from "./IdleCompaction.ts";
30
@@ -154,3 +168,137 @@ describe("idleCompactionWindow", () => {
168
expect(idleCompactionWindow(archived, NOW_MS)).toBeNull();
169
});
170
});
171
+
172
+describe("IdleCompactionService", () => {
173
+ it.effect.each([
174
+ { mode: "onReturn", reads: 0 },
175
+ { mode: "off", reads: 0 },
176
+ { mode: "beforeCacheExpires", reads: 1 },
177
+ { mode: "beforeCacheExpires", override: "off", reads: 0 },
178
+ { mode: "onReturn", override: "beforeCacheExpires", reads: 1 },
179
+ { mode: "beforeCacheExpires", projectDisabled: true, reads: 0 },
180
+ { mode: "beforeCacheExpires", enabled: false, reads: 0 },
181
+ { mode: "beforeCacheExpires", driver: "codex", instance: "codex", reads: 0 },
182
+ { mode: "beforeCacheExpires", driver: "codex", instance: "custom-codex", reads: 0 },
183
+ { mode: "beforeCacheExpires", driver: "claudeAgent", instance: "custom-claude", reads: 1 },
184
+ { mode: "beforeCacheExpires", unconfigured: true, reads: 1 },
185
+ ] as const)(
186
+ "loads history only for eligible idle compaction: $mode/$override/$enabled/$projectDisabled/$instance/$unconfigured",
187
+ (scenario) => {
188
+ const test = {
189
+ override: "default",
190
+ enabled: true,
191
+ projectDisabled: false,
192
+ ...scenario,
193
+ } as const;
194
+ return Effect.scoped(
195
+ Effect.gen(function* () {
196
+ yield* TestClock.setTime(NOW_MS);
197
+ const selectedInstance = ProviderInstanceId.make(
198
+ "instance" in test ? test.instance : "claudeAgent",
199
+ );
200
+ const driver = ProviderDriverKind.make("driver" in test ? test.driver : "claudeAgent");
201
+ const base = records({ cacheExpiresInMs: 2 * MINUTE_MS });
202
+ const data: OrchestrationV2ThreadProjection = {
203
+ ...base,
204
+ thread: {
205
+ ...base.thread,
206
+ providerInstanceId: selectedInstance,
207
+ modelSelection: { ...base.thread.modelSelection, instanceId: selectedInstance },
208
+ },
209
+ runs: base.runs.map((run) => ({
210
+ ...run,
211
+ providerInstanceId: selectedInstance,
212
+ modelSelection: { ...run.modelSelection, instanceId: selectedInstance },
213
+ })),
214
+ providerThreads: base.providerThreads.map((thread) => ({
215
+ ...thread,
216
+ driver,
217
+ providerInstanceId: selectedInstance,
218
+ })),
219
+ attempts: [],
220
+ nodes: [],
221
+ subagents: [],
222
+ providerSessions: [],
223
+ plans: [],
224
+ checkpointScopes: [],
225
+ checkpoints: [],
226
+ contextHandoffs: [],
227
+ visibleTurnItems: [],
228
+ updatedAt: at(),
229
+ };
230
+ let historyReads = 0;
231
+ let dispatches = 0;
232
+ const done = yield* Deferred.make<void>();
233
+ const tracer = Tracer.make({
234
+ span(options) {
235
+ const span = new Tracer.NativeSpan(options);
236
+ const end = span.end.bind(span);
237
+ span.end = (...args) => {
238
+ end(...args);
239
+ if (span.name === "IdleCompactionService.compactWhenDue")
240
+ Deferred.doneUnsafe(done, Effect.void);
241
+ };
242
+ return span;
243
+ },
244
+ });
245
+ const dependencies = Layer.mergeAll(
246
+ Layer.mock(Orchestrator.OrchestratorV2)({
247
+ streamDomainEvents: Stream.make({
248
+ id: EventId.make("idle-completed"),
249
+ type: "run.updated",
250
+ threadId,
251
+ runId,
252
+ occurredAt: at(),
253
+ payload: data.runs[0]!,
254
+ }),
255
+ dispatch: () =>
256
+ Effect.sync(() => {
257
+ dispatches++;
258
+ return { sequence: 1, storedEvents: [] };
259
+ }),
260
+ }),
261
+ Layer.mock(ProjectionStore.ProjectionStoreV2)({
262
+ getThread: () => Effect.succeed(data.thread),
263
+ getThreadRecords: () =>
264
+ Effect.sync(() => {
265
+ historyReads++;
266
+ return data;
267
+ }),
268
+ }),
269
+ Layer.mock(ServerSettings.ServerSettingsService)({
270
+ getSettings: Effect.succeed({
271
+ ...DEFAULT_SERVER_SETTINGS,
272
+ idleCompaction: test.mode,
273
+ providerInstances:
274
+ "unconfigured" in test
275
+ ? {}
276
+ : {
277
+ [selectedInstance]: {
278
+ driver,
279
+ enabled: test.enabled,
280
+ config: { idleCompaction: test.override },
281
+ },
282
+ },
283
+ projectSettingsOverrides: test.projectDisabled
284
+ ? {
285
+ [data.thread.projectId]: {
286
+ providerInstances: { [selectedInstance]: { enabled: false } },
287
+ },
288
+ }
289
+ : {},
290
+ }),
291
+ }),
292
+ );
293
+ yield* Effect.gen(function* () {
294
+ const service = yield* IdleCompactionService.make;
295
+ yield* service.start();
296
+ yield* Deferred.await(done);
297
+ }).pipe(Effect.provide(dependencies), Effect.withTracer(tracer));
298
+ expect(historyReads).toBe(test.reads);
299
+ expect(dispatches).toBe(test.reads);
300
+ }),
301
+ );
302
+ },
303
+ );
304
+});
apps/server/src/orchestration-v2/IdleCompactionService.ts
+14
@@ -35,6 +35,20 @@ export const make = Effect.gen(function* () {
35
// Loops once after the wait: the thread may have moved on.
36
while (true) {
37
const current = yield* settings.getSettings;
38
+ const thread = yield* projections.getThread(threadId);
39
+ const claudeInstance = Object.hasOwn(current.providerInstances, thread.providerInstanceId)
40
+ ? current.providerInstances[thread.providerInstanceId]?.driver === "claudeAgent"
41
+ : thread.providerInstanceId === "claudeAgent";
42
+ if (
43
+ !claudeInstance ||
44
+ resolveIdleCompaction(current, thread.providerInstanceId) !== "beforeCacheExpires" ||
45
+ !resolveProjectProviderInstanceEnabled(
46
+ current,
47
+ thread.projectId,
48
+ thread.providerInstanceId,
49
+ )
50
+ )
51
+ return;
52
const records = yield* projections.getThreadRecords(
53
threadId,
54
[