The best way to control Claude Code, Codex and more from any computer or phone
perf(server): idle Grok sessions on Linux stop polling /proc every 25 ms (#24502)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: ayagmar <62888618+ayagmar@users.noreply.github.com> Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: maria-rcks <254055478+maria-rcks@users.noreply.github.com>
Victor Vogel committed
Oct 30, 2026 at 16:27 UTC
91a9196d6449e0d1dffe43ae27e802ee7dfaab6a
2 files changed
+111
-8
apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts
+100
@@ -10,6 +10,7 @@ import { AcpRegistrySettings } from "@t3tools/provider-acp-registry/settings";
10
import { AcpRegistryCatalog } from "@t3tools/provider-acp-registry/server/AcpRegistrySupport";
11
import { decodeAcpReplayTranscript, makeAcpReplayRuntime } from "./AcpAdapterV2.testkit.ts";
12
// @effect-diagnostics nodeBuiltinImport:off
13
+import * as NodeFS from "node:fs";
14
import * as NodePath from "node:path";
15
16
import * as NodeServices from "@effect/platform-node/NodeServices";
@@ -3506,6 +3507,105 @@ describe("AcpAdapterV2", () => {
3507
}).pipe(Effect.provide(layerTest), Effect.scoped),
3508
);
3509
3510
+ it.live("does not poll the process ledger while a cgroup lease owns teardown", () =>
3511
+ Effect.gen(function* () {
3512
+ if ((yield* HostProcess.Platform) !== "linux") return;
3513
+ const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
3514
+ const fileSystem = yield* FileSystem.FileSystem;
3515
+ const path = yield* Path.Path;
3516
+ const selfInvocation = yield* resolveSelfInvocation();
3517
+ const mockAgentPath = yield* path.fromFileUrl(
3518
+ new URL("../../../scripts/acp-mock-agent.ts", import.meta.url),
3519
+ );
3520
+ // A directory stands in for cgroupfs: the join wrapper appends the agent
3521
+ // PID to cgroup.procs and stays in this process's cgroup, which is the
3522
+ // cgroup the lease reports.
3523
+ const leasePath = yield* fileSystem.makeTempDirectoryScoped();
3524
+ const procsPath = path.join(leasePath, "cgroup.procs");
3525
+ yield* fileSystem.writeFileString(procsPath, "");
3526
+ const relativePath = AcpSessionRuntime.parseUnifiedCgroupPath(
3527
+ NodeFS.readFileSync("/proc/self/cgroup", "utf8"),
3528
+ );
3529
+ // No unified cgroup on this host means no lease is possible, so there is
3530
+ // nothing for this test to exercise.
3531
+ if (relativePath === undefined) return;
3532
+ const processTree = AcpSessionRuntime.makePosixProcessTreeController("linux");
3533
+ const leasePids = () =>
3534
+ NodeFS.readFileSync(procsPath, "utf8").split(/\s+/).filter(Boolean).map(Number);
3535
+ const isLive = (pid: number) => {
3536
+ const identity = processTree.identity(pid);
3537
+ return identity !== undefined && !AcpSessionRuntime.posixProcessIsZombie(identity);
3538
+ };
3539
+ let leaseExists = true;
3540
+ const linuxCgroupController: AcpSessionRuntime.AcpLinuxCgroupController = {
3541
+ create: () => ({
3542
+ contains: (pid) => leasePids().includes(pid),
3543
+ exists: () => leaseExists,
3544
+ path: leasePath,
3545
+ relativePath,
3546
+ kill: () => {
3547
+ for (const pid of leasePids().filter(isLive)) process.kill(pid, "SIGKILL");
3548
+ },
3549
+ populated: () => leasePids().some(isLive),
3550
+ remove: () => {
3551
+ leaseExists = false;
3552
+ },
3553
+ }),
3554
+ };
3555
+ let childListReads = 0;
3556
+ const posixProcessTreeController: AcpSessionRuntime.AcpPosixProcessTreeController = {
3557
+ ...processTree,
3558
+ childPidsOf: (pid) => {
3559
+ childListReads += 1;
3560
+ return processTree.childPidsOf(pid);
3561
+ },
3562
+ };
3563
+ const instanceId = ProviderInstanceId.make("acp-test-cgroup-ledger-idle");
3564
+ const adapter = yield* makeAcpAdapterV2({
3565
+ instanceId,
3566
+ flavor: {
3567
+ driver: ACP_TEST_DRIVER,
3568
+ capabilities: AcpProviderCapabilitiesV2,
3569
+ makeRuntime: makeMockRuntime({
3570
+ childProcessSpawner,
3571
+ linuxCgroupController,
3572
+ posixProcessTreeController,
3573
+ mockAgentPath,
3574
+ ownDescendantProcessGroups: true,
3575
+ ownDetachedProcessGroup: true,
3576
+ }),
3577
+ },
3578
+ selfInvocation,
3579
+ });
3580
+ const threadId = ThreadId.make("thread-acp-cgroup-ledger-idle");
3581
+ const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
3582
+ runtimeMode: "full-access",
3583
+ interactionMode: "default",
3584
+ cwd: process.cwd(),
3585
+ });
3586
+ const modelSelection = { instanceId, model: "default" } as const;
3587
+ const sessionScope = yield* Scope.fork(yield* Scope.Scope);
3588
+ const runtime = yield* adapter
3589
+ .openSession({
3590
+ threadId,
3591
+ providerSessionId: ProviderSessionId.make("provider-session-acp-cgroup-ledger-idle"),
3592
+ modelSelection,
3593
+ runtimePolicy,
3594
+ })
3595
+ .pipe(Effect.provideService(Scope.Scope, sessionScope));
3596
+ yield* runtime.ensureThread({ threadId, modelSelection, runtimePolicy });
3597
+ // The agent's children are listed once, to capture the root it may
3598
+ // signal at teardown, and never again while the session is open.
3599
+ assert.equal(childListReads, 1);
3600
+
3601
+ const agentPid = leasePids()[0];
3602
+ assert.isDefined(agentPid);
3603
+ yield* Scope.close(sessionScope, Exit.void);
3604
+ assert.isFalse(isLive(agentPid!), "closing the session must still tear the agent down");
3605
+ assert.isFalse(leaseExists, "closing the session must remove the lease");
3606
+ }).pipe(Effect.provide(layerTest), Effect.scoped),
3607
+ );
3608
+
3609
it.effect("negotiates and executes optional native session forks through the ACP runtime", () =>
3610
Effect.gen(function* () {
3611
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
packages/provider-acp/src/server/AcpSessionRuntime.ts
+11
-8
@@ -1504,14 +1504,17 @@ export const make = Effect.fn("AcpSessionRuntime.make")(function* (
1504
catch: (cause) =>
1505
new EffectAcpErrors.AcpSpawnError({ command: options.spawn.command, cause }),
1506
});
1507
- yield* observePosixOwnershipLedgerContinuously({
1508
- childQueues: posixOwnershipChildQueues,
1509
- controller: posixController,
1510
- frontier: posixOwnershipFrontier,
1511
- ledger: posixOwnershipLedger,
1512
- root: posixOwnershipRoot,
1513
- rootPid: Number(child.pid),
1514
- }).pipe(Effect.forkIn(runtimeScope));
1507
+ // Cgroup teardown needs only the root identity captured above.
1508
+ if (linuxCgroupLease === undefined) {
1509
+ yield* observePosixOwnershipLedgerContinuously({
1510
+ childQueues: posixOwnershipChildQueues,
1511
+ controller: posixController,
1512
+ frontier: posixOwnershipFrontier,
1513
+ ledger: posixOwnershipLedger,
1514
+ root: posixOwnershipRoot,
1515
+ rootPid: Number(child.pid),
1516
+ }).pipe(Effect.forkIn(runtimeScope));
1517
+ }
1518
}
1519
1520
const signalOwnedProcessGroup = (signal: NodeJS.Signals) =>