GeezerVison is showing github.com — Fix Claude Process leak[MEMORY INTENSIVE], archiving, and stale claud… · MuneerAhmed03/t3code@f84bc80
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/package.json +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 20 | 20 | "start": "node dist/bin.mjs", | 21 | 21 | "prepare": "effect-language-service patch", | 22 | 22 | "typecheck": "tsc --noEmit", | 23 | | - "test": "vitest run" | | 23 | + "test": "vitest run", | | 24 | + "test:process-reaper": "vitest run src/server.test.ts src/provider/Layers/ClaudeAdapter.test.ts src/provider/Layers/ProviderSessionDirectory.test.ts src/provider/Layers/ProviderSessionReaper.test.ts src/provider/Layers/CodexAdapter.test.ts" | 24 | 25 | }, | 25 | 26 | "dependencies": { | 26 | 27 | "@anthropic-ai/claude-agent-sdk": "^0.2.111", | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/codexAppServerManager.test.ts +148Lines changed: 148 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -470,6 +470,154 @@ describe("startSession", () => { | 470 | 470 | manager.stopAll(); | 471 | 471 | } | 472 | 472 | }); | | 473 | + | | 474 | + it("disposes an existing session before starting a replacement for the same thread", async () => { | | 475 | + const manager = new CodexAppServerManager(); | | 476 | + const existingContext = { | | 477 | + session: { | | 478 | + provider: "codex", | | 479 | + status: "ready", | | 480 | + threadId: asThreadId("thread-1"), | | 481 | + runtimeMode: "full-access", | | 482 | + createdAt: "2026-02-10T00:00:00.000Z", | | 483 | + updatedAt: "2026-02-10T00:00:00.000Z", | | 484 | + }, | | 485 | + }; | | 486 | + | | 487 | + ( | | 488 | + manager as unknown as { | | 489 | + sessions: Map<ThreadId, typeof existingContext>; | | 490 | + } | | 491 | + ).sessions.set(asThreadId("thread-1"), existingContext); | | 492 | + | | 493 | + const disposeSession = vi | | 494 | + .spyOn( | | 495 | + manager as unknown as { | | 496 | + disposeSession: ( | | 497 | + context: typeof existingContext, | | 498 | + options?: { readonly emitLifecycleEvent?: boolean }, | | 499 | + ) => void; | | 500 | + }, | | 501 | + "disposeSession", | | 502 | + ) | | 503 | + .mockImplementation(() => {}); | | 504 | + const assertSupportedCodexCliVersion = vi | | 505 | + .spyOn( | | 506 | + manager as unknown as { | | 507 | + assertSupportedCodexCliVersion: (input: { | | 508 | + binaryPath: string; | | 509 | + cwd: string; | | 510 | + homePath?: string; | | 511 | + }) => void; | | 512 | + }, | | 513 | + "assertSupportedCodexCliVersion", | | 514 | + ) | | 515 | + .mockImplementation(() => {}); | | 516 | + const processCwd = vi.spyOn(process, "cwd").mockImplementation(() => { | | 517 | + throw new Error("cwd missing"); | | 518 | + }); | | 519 | + | | 520 | + try { | | 521 | + await expect( | | 522 | + manager.startSession({ | | 523 | + threadId: asThreadId("thread-1"), | | 524 | + provider: "codex", | | 525 | + binaryPath: "codex", | | 526 | + runtimeMode: "full-access", | | 527 | + }), | | 528 | + ).rejects.toThrow("cwd missing"); | | 529 | + | | 530 | + expect(disposeSession).toHaveBeenCalledWith(existingContext, { | | 531 | + emitLifecycleEvent: false, | | 532 | + }); | | 533 | + expect(assertSupportedCodexCliVersion).not.toHaveBeenCalled(); | | 534 | + } finally { | | 535 | + disposeSession.mockRestore(); | | 536 | + assertSupportedCodexCliVersion.mockRestore(); | | 537 | + processCwd.mockRestore(); | | 538 | + ( | | 539 | + manager as unknown as { | | 540 | + sessions: Map<ThreadId, typeof existingContext>; | | 541 | + } | | 542 | + ).sessions.clear(); | | 543 | + manager.stopAll(); | | 544 | + } | | 545 | + }); | | 546 | + | | 547 | + it("continues replacement start when existing session disposal fails", async () => { | | 548 | + const manager = new CodexAppServerManager(); | | 549 | + const existingContext = { | | 550 | + session: { | | 551 | + provider: "codex", | | 552 | + status: "ready", | | 553 | + threadId: asThreadId("thread-1"), | | 554 | + runtimeMode: "full-access", | | 555 | + createdAt: "2026-02-10T00:00:00.000Z", | | 556 | + updatedAt: "2026-02-10T00:00:00.000Z", | | 557 | + }, | | 558 | + }; | | 559 | + | | 560 | + ( | | 561 | + manager as unknown as { | | 562 | + sessions: Map<ThreadId, typeof existingContext>; | | 563 | + } | | 564 | + ).sessions.set(asThreadId("thread-1"), existingContext); | | 565 | + | | 566 | + const disposeSession = vi | | 567 | + .spyOn( | | 568 | + manager as unknown as { | | 569 | + disposeSession: ( | | 570 | + context: typeof existingContext, | | 571 | + options?: { readonly emitLifecycleEvent?: boolean }, | | 572 | + ) => void; | | 573 | + }, | | 574 | + "disposeSession", | | 575 | + ) | | 576 | + .mockImplementation(() => { | | 577 | + throw new Error("dispose failed"); | | 578 | + }); | | 579 | + const assertSupportedCodexCliVersion = vi | | 580 | + .spyOn( | | 581 | + manager as unknown as { | | 582 | + assertSupportedCodexCliVersion: (input: { | | 583 | + binaryPath: string; | | 584 | + cwd: string; | | 585 | + homePath?: string; | | 586 | + }) => void; | | 587 | + }, | | 588 | + "assertSupportedCodexCliVersion", | | 589 | + ) | | 590 | + .mockImplementation(() => {}); | | 591 | + const processCwd = vi.spyOn(process, "cwd").mockImplementation(() => { | | 592 | + throw new Error("cwd missing"); | | 593 | + }); | | 594 | + | | 595 | + try { | | 596 | + await expect( | | 597 | + manager.startSession({ | | 598 | + threadId: asThreadId("thread-1"), | | 599 | + provider: "codex", | | 600 | + binaryPath: "codex", | | 601 | + runtimeMode: "full-access", | | 602 | + }), | | 603 | + ).rejects.toThrow("cwd missing"); | | 604 | + | | 605 | + expect(disposeSession).toHaveBeenCalledWith(existingContext, { | | 606 | + emitLifecycleEvent: false, | | 607 | + }); | | 608 | + expect(assertSupportedCodexCliVersion).not.toHaveBeenCalled(); | | 609 | + } finally { | | 610 | + disposeSession.mockRestore(); | | 611 | + assertSupportedCodexCliVersion.mockRestore(); | | 612 | + processCwd.mockRestore(); | | 613 | + ( | | 614 | + manager as unknown as { | | 615 | + sessions: Map<ThreadId, typeof existingContext>; | | 616 | + } | | 617 | + ).sessions.clear(); | | 618 | + manager.stopAll(); | | 619 | + } | | 620 | + }); | 473 | 621 | }); | 474 | 622 |
| 475 | 623 | describe("sendTurn", () => { | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/codexAppServerManager.ts +36-2Lines changed: 36 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -450,6 +450,25 @@ export class CodexAppServerManager extends EventEmitter<CodexAppServerManagerEve | 450 | 450 | let context: CodexSessionContext | undefined; | 451 | 451 |
| 452 | 452 | try { | | 453 | + const existingContext = this.sessions.get(threadId); | | 454 | + if (existingContext) { | | 455 | + await Effect.logWarning("codex app-server replacing existing session", { | | 456 | + threadId, | | 457 | + existingStatus: existingContext.session.status, | | 458 | + }).pipe(this.runPromise); | | 459 | + try { | | 460 | + this.disposeSession(existingContext, { | | 461 | + emitLifecycleEvent: false, | | 462 | + }); | | 463 | + } catch (error) { | | 464 | + await Effect.logWarning("codex app-server failed to dispose existing session", { | | 465 | + threadId, | | 466 | + existingStatus: existingContext.session.status, | | 467 | + cause: error instanceof Error ? error.message : String(error), | | 468 | + }).pipe(this.runPromise); | | 469 | + } | | 470 | + } | | 471 | + | 453 | 472 | const resolvedCwd = input.cwd ?? process.cwd(); | 454 | 473 |
| 455 | 474 | const session: ProviderSession = { | @@ -901,6 +920,19 @@ export class CodexAppServerManager extends EventEmitter<CodexAppServerManagerEve | 901 | 920 | return; | 902 | 921 | } | 903 | 922 |
| | 923 | + this.disposeSession(context, { | | 924 | + emitLifecycleEvent: true, | | 925 | + }); | | 926 | + } | | 927 | + | | 928 | + private disposeSession( | | 929 | + context: CodexSessionContext, | | 930 | + options?: { readonly emitLifecycleEvent?: boolean }, | | 931 | + ): void { | | 932 | + if (context.stopping) { | | 933 | + return; | | 934 | + } | | 935 | + | 904 | 936 | context.stopping = true; | 905 | 937 |
| 906 | 938 | for (const pending of context.pending.values()) { | @@ -921,8 +953,10 @@ export class CodexAppServerManager extends EventEmitter<CodexAppServerManagerEve | 921 | 953 | status: "closed", | 922 | 954 | activeTurnId: undefined, | 923 | 955 | }); | 924 | | - this.emitLifecycleEvent(context, "session/closed", "Session stopped"); | 925 | | - this.sessions.delete(threadId); | | 956 | + if (options?.emitLifecycleEvent !== false) { | | 957 | + this.emitLifecycleEvent(context, "session/closed", "Session stopped"); | | 958 | + } | | 959 | + this.sessions.delete(context.session.threadId); | 926 | 960 | } | 927 | 961 |
| 928 | 962 | listSessions(): ProviderSession[] { | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/provider/Layers/ClaudeAdapter.test.ts +65Lines changed: 65 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1307,6 +1307,71 @@ describe("ClaudeAdapterLive", () => { | 1307 | 1307 | ); | 1308 | 1308 | }); | 1309 | 1309 |
| | 1310 | + it.effect("closes the previous session before replacing an existing thread session", () => { | | 1311 | + const queries: FakeClaudeQuery[] = []; | | 1312 | + const layer = makeClaudeAdapterLive({ | | 1313 | + createQuery: () => { | | 1314 | + const query = new FakeClaudeQuery(); | | 1315 | + queries.push(query); | | 1316 | + return query; | | 1317 | + }, | | 1318 | + }).pipe( | | 1319 | + Layer.provideMerge(ServerConfig.layerTest("/tmp/claude-adapter-test", "/tmp")), | | 1320 | + Layer.provideMerge(ServerSettingsService.layerTest()), | | 1321 | + Layer.provideMerge(NodeServices.layer), | | 1322 | + ); | | 1323 | + | | 1324 | + return Effect.gen(function* () { | | 1325 | + const adapter = yield* ClaudeAdapter; | | 1326 | + | | 1327 | + const runtimeEventsFiber = yield* Stream.take(adapter.streamEvents, 6).pipe( | | 1328 | + Stream.runCollect, | | 1329 | + Effect.forkChild, | | 1330 | + ); | | 1331 | + | | 1332 | + const firstSession = yield* adapter.startSession({ | | 1333 | + threadId: THREAD_ID, | | 1334 | + provider: "claudeAgent", | | 1335 | + runtimeMode: "full-access", | | 1336 | + }); | | 1337 | + | | 1338 | + const secondSession = yield* adapter.startSession({ | | 1339 | + threadId: THREAD_ID, | | 1340 | + provider: "claudeAgent", | | 1341 | + runtimeMode: "full-access", | | 1342 | + resumeCursor: firstSession.resumeCursor, | | 1343 | + }); | | 1344 | + | | 1345 | + const runtimeEvents = Array.from(yield* Fiber.join(runtimeEventsFiber)); | | 1346 | + const activeSessions = yield* adapter.listSessions(); | | 1347 | + | | 1348 | + assert.equal(queries.length, 2); | | 1349 | + assert.equal(queries[0]?.closeCalls, 1); | | 1350 | + assert.equal(queries[1]?.closeCalls, 0); | | 1351 | + assert.equal(yield* adapter.hasSession(THREAD_ID), true); | | 1352 | + assert.equal(activeSessions.length, 1); | | 1353 | + assert.deepEqual(activeSessions[0]?.resumeCursor, secondSession.resumeCursor); | | 1354 | + assert.deepEqual( | | 1355 | + runtimeEvents.map((event) => event.type), | | 1356 | + [ | | 1357 | + "session.started", | | 1358 | + "session.configured", | | 1359 | + "session.state.changed", | | 1360 | + "session.started", | | 1361 | + "session.configured", | | 1362 | + "session.state.changed", | | 1363 | + ], | | 1364 | + ); | | 1365 | + assert.equal( | | 1366 | + runtimeEvents.some((event) => event.type === "session.exited"), | | 1367 | + false, | | 1368 | + ); | | 1369 | + }).pipe( | | 1370 | + Effect.provideService(Random.Random, makeDeterministicRandomService()), | | 1371 | + Effect.provide(layer), | | 1372 | + ); | | 1373 | + }); | | 1374 | + | 1310 | 1375 | it.effect("stopSession does not throw into the SDK prompt consumer", () => { | 1311 | 1376 | // The SDK consumes user messages via `for await (... of prompt)`. | 1312 | 1377 | // Stopping a session must end that loop cleanly — not throw an error. | |
|