Skip to content

Commit 5f19376

Browse files
t3dotggclaude
andcommitted
perf(server): trim idle relay polling and reaper reads
The agent awareness relay checked the T3 Connect link every 5 s forever while the environment was unlinked or publishing was disabled. The retry now backs off from 5 s to 60 s. It still polls, because `t3 connect` can write the link secrets from another process. The provider session reaper read and decoded every binding row, JSON included, every 5 min and then skipped the stopped ones. Stopped rows stay for their resume cursors, so long-lived installs hold thousands. listBindings now takes `excludeStopped`, which filters them in SQL. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent a21b42c commit 5f19376

7 files changed

Lines changed: 130 additions & 17 deletions

File tree

‎apps/server/src/persistence/ProviderSessionRuntime.ts‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,14 @@ export class ProviderSessionRuntimeRepository extends Context.Service<
102102
>;
103103

104104
/**
105-
* List all provider runtime rows.
105+
* List provider runtime rows.
106106
*
107-
* Returned in ascending last-seen order.
107+
* Returned in ascending last-seen order. `excludeStopped` filters stopped
108+
* rows in SQL. Long-lived installs keep thousands for their resume cursors.
108109
*/
109-
readonly list: () => Effect.Effect<
110+
readonly list: (options?: {
111+
readonly excludeStopped?: boolean;
112+
}) => Effect.Effect<
110113
ReadonlyArray<ProviderSessionRuntime>,
111114
ProviderSessionRuntimeRepositoryError
112115
>;
@@ -336,9 +339,9 @@ export const make = Effect.gen(function* () {
336339
});
337340

338341
const listRuntimeRows = SqlSchema.findAll({
339-
Request: Schema.Void,
342+
Request: Schema.Struct({ excludeStopped: Schema.Boolean }),
340343
Result: ProviderSessionRuntimeRawDbRowSchema,
341-
execute: () =>
344+
execute: ({ excludeStopped }) =>
342345
sql`
343346
SELECT
344347
thread_id AS "threadId",
@@ -351,6 +354,7 @@ export const make = Effect.gen(function* () {
351354
resume_cursor_json AS "resumeCursor",
352355
runtime_payload_json AS "runtimePayload"
353356
FROM provider_session_runtime
357+
${excludeStopped ? sql`WHERE status != 'stopped'` : sql``}
354358
ORDER BY last_seen_at ASC, thread_id ASC
355359
`,
356360
});
@@ -414,8 +418,8 @@ export const make = Effect.gen(function* () {
414418
),
415419
);
416420

417-
const list: ProviderSessionRuntimeRepository["Service"]["list"] = () =>
418-
listRuntimeRows(undefined).pipe(
421+
const list: ProviderSessionRuntimeRepository["Service"]["list"] = (options) =>
422+
listRuntimeRows({ excludeStopped: options?.excludeStopped === true }).pipe(
419423
Effect.mapError(
420424
toPersistenceSqlOrDecodeError(
421425
"ProviderSessionRuntimeRepository.list:query",

‎apps/server/src/provider/Layers/ProviderSessionDirectory.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,41 @@ it.layer(makeDirectoryLayer(SqlitePersistenceMemory))("ProviderSessionDirectoryL
372372
}),
373373
);
374374

375+
it.effect("lists only bindings that are not stopped when asked", () =>
376+
Effect.gen(function* () {
377+
const directory = yield* ProviderSessionDirectory;
378+
const runtimeRepository = yield* ProviderSessionRuntime.ProviderSessionRuntimeRepository;
379+
const statuses = ["running", "starting", "error", "stopped"] as const;
380+
const threadIds = new Set<string>();
381+
382+
for (const status of statuses) {
383+
const threadId = ThreadId.make(`thread-exclude-stopped-${status}`);
384+
threadIds.add(threadId);
385+
yield* runtimeRepository.upsert({
386+
threadId,
387+
providerName: "codex",
388+
providerInstanceId: ProviderInstanceId.make("codex"),
389+
adapterKey: "codex",
390+
runtimeMode: "full-access",
391+
status,
392+
lastSeenAt: "2026-04-14T12:00:00.000Z",
393+
resumeCursor: null,
394+
runtimePayload: null,
395+
});
396+
}
397+
398+
const liveStatuses = (yield* directory.listBindings({ excludeStopped: true }))
399+
.filter((binding) => threadIds.has(binding.threadId))
400+
.map((binding) => binding.status);
401+
const allStatuses = (yield* directory.listBindings())
402+
.filter((binding) => threadIds.has(binding.threadId))
403+
.map((binding) => binding.status);
404+
405+
assert.deepEqual(liveStatuses.toSorted(), ["error", "running", "starting"]);
406+
assert.deepEqual(allStatuses.toSorted(), ["error", "running", "starting", "stopped"]);
407+
}),
408+
);
409+
375410
it.effect(
376411
"resets adapterKey to the new provider when provider changes without an explicit adapter key",
377412
() =>

‎apps/server/src/provider/Layers/ProviderSessionDirectory.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ const makeProviderSessionDirectory = Effect.gen(function* () {
182182
Effect.map((rows) => rows.map((row) => row.threadId)),
183183
);
184184

185-
const listBindings: ProviderSessionDirectoryShape["listBindings"] = () =>
186-
repository.list().pipe(
185+
const listBindings: ProviderSessionDirectoryShape["listBindings"] = (options) =>
186+
repository.list(options).pipe(
187187
Effect.mapError(toPersistenceError("ProviderSessionDirectory.listBindings:list")),
188188
Effect.flatMap((rows) =>
189189
Effect.forEach(

‎apps/server/src/provider/Layers/ProviderSessionReaper.ts‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,13 @@ const makeProviderSessionReaper = (options?: ProviderSessionReaperLiveOptions) =
3535
const sweepIntervalMs = Math.max(1, options?.sweepIntervalMs ?? DEFAULT_SWEEP_INTERVAL_MS);
3636

3737
const sweep = Effect.gen(function* () {
38-
const bindings = yield* directory.listBindings();
38+
// Stopped rows stay for their resume cursors and far outnumber live
39+
// ones, so the query skips them.
40+
const bindings = yield* directory.listBindings({ excludeStopped: true });
3941
const now = yield* Clock.currentTimeMillis;
4042
let reapedCount = 0;
4143

4244
for (const binding of bindings) {
43-
if (binding.status === "stopped") {
44-
continue;
45-
}
46-
4745
const lastSeenMs = Date.parse(binding.lastSeenAt);
4846
if (Number.isNaN(lastSeenMs)) {
4947
yield* Effect.logWarning("provider.session.reaper.invalid-last-seen", {
@@ -122,7 +120,7 @@ const makeProviderSessionReaper = (options?: ProviderSessionReaperLiveOptions) =
122120
if (reapedCount > 0) {
123121
yield* Effect.logInfo("provider.session.reaper.sweep-complete", {
124122
reapedCount,
125-
totalBindings: bindings.length,
123+
liveBindings: bindings.length,
126124
});
127125
}
128126
});

‎apps/server/src/provider/Services/ProviderSessionDirectory.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ export interface ProviderSessionDirectoryShape {
7070
ProviderSessionDirectoryPersistenceError
7171
>;
7272

73-
readonly listBindings: () => Effect.Effect<
73+
/** `excludeStopped` skips stopped rows in the query, not after decoding. */
74+
readonly listBindings: (options?: {
75+
readonly excludeStopped?: boolean;
76+
}) => Effect.Effect<
7477
ReadonlyArray<ProviderRuntimeBindingWithMetadata>,
7578
ProviderSessionDirectoryPersistenceError
7679
>;

‎apps/server/src/relay/AgentAwarenessRelay.test.ts‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import * as Option from "effect/Option";
2828
import * as Queue from "effect/Queue";
2929
import * as Stream from "effect/Stream";
3030
import * as Tracer from "effect/Tracer";
31+
import * as TestClock from "effect/testing/TestClock";
3132

3233
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
3334
import * as ServerEnvironment from "../environment/ServerEnvironment.ts";
@@ -664,6 +665,72 @@ describe.sequential("signRelayAgentActivityPublishProof", () => {
664665
),
665666
);
666667

668+
it.effect("checks an unlinked environment once a minute and still catches up once linked", () =>
669+
Effect.scoped(
670+
Effect.gen(function* () {
671+
const secrets = makeMemorySecretStore();
672+
let publishSettingReads = 0;
673+
let shellSnapshotReads = 0;
674+
const countingStore = {
675+
...secrets.store,
676+
get: (name: string) =>
677+
Effect.suspend(() => {
678+
if (name === PUBLISH_AGENT_ACTIVITY_SECRET) publishSettingReads += 1;
679+
return secrets.store.get(name);
680+
}),
681+
} satisfies ServerSecretStore.ServerSecretStore["Service"];
682+
683+
const layer = Layer.mergeAll(
684+
Layer.succeed(ServerSecretStore.ServerSecretStore, countingStore),
685+
Layer.succeed(ServerEnvironment.ServerEnvironment, {
686+
getEnvironmentId: Effect.succeed("env-1" as EnvironmentId),
687+
getDescriptor: Effect.die("unused descriptor"),
688+
}),
689+
Layer.succeed(OrchestrationEngineService, {
690+
streamDomainEvents: Stream.never,
691+
} as unknown as OrchestrationEngineShape),
692+
Layer.succeed(ProjectionSnapshotQuery, {
693+
getShellSnapshot: () =>
694+
Effect.sync(() => {
695+
shellSnapshotReads += 1;
696+
return {
697+
snapshotSequence: 1,
698+
projects: [],
699+
threads: [],
700+
updatedAt: "2026-05-25T00:00:00.000Z",
701+
} satisfies OrchestrationShellSnapshot;
702+
}),
703+
} as unknown as ProjectionSnapshotQueryShape),
704+
);
705+
706+
yield* Effect.gen(function* () {
707+
const relay = yield* AgentAwarenessRelay.AgentAwarenessRelay;
708+
yield* relay.start();
709+
710+
// Get past the backoff ramp, then count checks in a steady window.
711+
yield* TestClock.adjust("10 minutes");
712+
const readsBeforeWindow = publishSettingReads;
713+
yield* TestClock.adjust("10 minutes");
714+
expect(publishSettingReads - readsBeforeWindow).toBe(10);
715+
expect(shellSnapshotReads).toBe(0);
716+
717+
yield* secrets.setString(RELAY_URL_SECRET, "https://relay.example.test");
718+
yield* secrets.setString(RELAY_ENVIRONMENT_CREDENTIAL_SECRET, "relay-credential");
719+
yield* secrets.setString(PUBLISH_AGENT_ACTIVITY_SECRET, "true");
720+
yield* TestClock.adjust("1 minute");
721+
expect(shellSnapshotReads).toBe(1);
722+
}).pipe(
723+
Effect.provide(
724+
AgentAwarenessRelay.layer.pipe(
725+
Layer.provide(layer),
726+
Layer.provideMerge(NodeServices.layer),
727+
),
728+
),
729+
);
730+
}),
731+
),
732+
);
733+
667734
it.effect("publishes agent activity to the relay transport URL, not the relay issuer", () =>
668735
Effect.scoped(
669736
Effect.gen(function* () {

‎apps/server/src/relay/AgentAwarenessRelay.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,8 +566,13 @@ export const make = Effect.gen(function* () {
566566
return true;
567567
});
568568

569+
// Publishes the catch-up snapshot of active threads once the environment is
570+
// linked and publishing is enabled. Many environments never link, so the
571+
// retry backs off from 5 s to 60 s. It polls because `t3 connect` can write
572+
// the secrets from another process.
569573
const publishActiveThreadsOnceWhenConfigured = (logEnabledWhenReady: boolean) =>
570574
Effect.gen(function* () {
575+
let retryDelayMs = 5_000;
571576
while (!(yield* Ref.get(activeSnapshotPublishedRef))) {
572577
const published = yield* publishActiveThreadsUnsafe.pipe(Effect.orElseSucceed(() => false));
573578
if (published) {
@@ -580,7 +585,8 @@ export const make = Effect.gen(function* () {
580585
}
581586
return;
582587
}
583-
yield* Effect.sleep("5 seconds");
588+
yield* Effect.sleep(retryDelayMs);
589+
retryDelayMs = Math.min(retryDelayMs * 2, 60_000);
584590
}
585591
});
586592

0 commit comments

Comments
 (0)