From 929f05daecfd50d5cabaa2c5b335a13e32c5b414 Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Sat, 14 Nov 2026 15:01:48 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=AA=A1=20fix:=20Leave=20Child-Thread=20Ru?= =?UTF-8?q?ns=20Out=20of=20the=20Active=20Chat=20List=20(#18167)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An event-bound subagent child runs as a generation job keyed by its child thread id, so GET /chat/active returned that id beside the user's chats. The sidebar has no row for it, looks the conversation up, gets 404, and asks again every two seconds for as long as the child runs. The manager's active-job read now takes a filter and leaves child-thread jobs out. The job still answers its own status and stream reads, and owner cleanup still counts it. --- .../api/src/stream/GenerationJobManager.ts | 9 +-- .../RedisJobStore.stream_integration.spec.ts | 32 +++++++++ .../stream/__tests__/childThreadJobs.spec.ts | 68 +++++++++++++++++++ .../implementations/InMemoryJobStore.ts | 14 +++- .../stream/implementations/RedisJobStore.ts | 17 +++-- .../api/src/stream/interfaces/IJobStore.ts | 16 ++++- packages/api/src/stream/metadata.ts | 16 ++++- 7 files changed, 158 insertions(+), 14 deletions(-) create mode 100644 packages/api/src/stream/__tests__/childThreadJobs.spec.ts diff --git a/packages/api/src/stream/GenerationJobManager.ts b/packages/api/src/stream/GenerationJobManager.ts index 83dca741c9..d3a4198674 100644 --- a/packages/api/src/stream/GenerationJobManager.ts +++ b/packages/api/src/stream/GenerationJobManager.ts @@ -98,7 +98,7 @@ import { toClientPendingAction } from '~/agents/hitl/policy'; import { markAbortedCompactionContent } from '~/agents/compaction'; import { ApprovalLifecycle, pausePersistenceActionId } from './ApprovalLifecycle'; import { projectPendingMCPOAuthPrompts } from '~/mcp/oauth/resume'; -import { sanitizeJobMetadata } from './metadata'; +import { sanitizeJobMetadata, isListedChatJob } from './metadata'; /** Terminal error surfaced to a client still attached when its approval window lapses. */ const APPROVAL_EXPIRED_ERROR = 'Approval expired before a decision was made'; @@ -9936,15 +9936,16 @@ class GenerationJobManagerClass { } /** - * Get active job IDs for a user. - * Returns conversation IDs of running jobs belonging to the user. + * The user's running chats: the stream ids a client lists as conversations. + * A job whose stream is a subagent child thread is left out. It still answers its own + * status and stream reads, and cleanup enumeration still counts it. * Performs self-healing cleanup of stale entries. * * @param userId - The user ID to query * @returns Array of conversation IDs with active jobs */ async getActiveJobIdsForUser(userId: string, tenantId?: string): Promise { - return this.jobStore.getActiveJobIdsByUser(userId, tenantId); + return this.jobStore.getActiveJobIdsByUser(userId, tenantId, isListedChatJob); } /** Returns every generation whose provider can still mutate user-owned data, diff --git a/packages/api/src/stream/__tests__/RedisJobStore.stream_integration.spec.ts b/packages/api/src/stream/__tests__/RedisJobStore.stream_integration.spec.ts index 2a9e7bf488..2bfcb85b50 100644 --- a/packages/api/src/stream/__tests__/RedisJobStore.stream_integration.spec.ts +++ b/packages/api/src/stream/__tests__/RedisJobStore.stream_integration.spec.ts @@ -1247,6 +1247,38 @@ describe('RedisJobStore Integration Tests', () => { await store.destroy(); }); + test('should apply the active filter without hiding the job from cleanup', async () => { + if (!ioredisClient) { + return; + } + + const { RedisJobStore } = await import('../implementations/RedisJobStore'); + const { isListedChatJob } = await import('../metadata'); + const store = new RedisJobStore(ioredisClient); + await store.initialize(); + + const userId = `child-thread-user-${Date.now()}`; + const chatId = `child-thread-chat-${Date.now()}`; + const childId = `child-thread-child-${Date.now()}`; + try { + await store.createJob(chatId, userId, chatId); + await store.createJob(childId, userId, childId, undefined, { + agentEventBindingId: 'binding-1', + }); + + expect((await store.getActiveJobIdsByUser(userId)).sort()).toEqual( + [chatId, childId].sort(), + ); + expect(await store.getActiveJobIdsByUser(userId, undefined, isListedChatJob)).toEqual([ + chatId, + ]); + expect(await store.getCleanupBlockingJobIdsByUser(userId)).toContain(childId); + expect((await store.getJob(childId))?.agentEventBindingId).toBe('binding-1'); + } finally { + await store.destroy(); + } + }); + test('should return resumed requires_action jobs to the running index', async () => { if (!ioredisClient) { return; diff --git a/packages/api/src/stream/__tests__/childThreadJobs.spec.ts b/packages/api/src/stream/__tests__/childThreadJobs.spec.ts new file mode 100644 index 0000000000..4a6fb89bec --- /dev/null +++ b/packages/api/src/stream/__tests__/childThreadJobs.spec.ts @@ -0,0 +1,68 @@ +import { InMemoryEventTransport } from '~/stream/implementations/InMemoryEventTransport'; +import { InMemoryJobStore } from '~/stream/implementations/InMemoryJobStore'; +import { GenerationJobManagerClass } from '~/stream/GenerationJobManager'; +import { isChildThreadJob, isListedChatJob } from '~/stream/metadata'; + +jest.spyOn(console, 'log').mockImplementation(); + +describe('child-thread jobs in the active chat list', () => { + const userId = 'owner'; + const chatId = 'chat-conversation'; + const childId = 'child-thread-conversation'; + let manager: GenerationJobManagerClass; + let jobStore: InMemoryJobStore; + + beforeEach(async () => { + jobStore = new InMemoryJobStore({ ttlAfterComplete: 60000 }); + manager = new GenerationJobManagerClass(); + manager.configure({ + jobStore, + eventTransport: new InMemoryEventTransport(), + isRedis: false, + cleanupOnComplete: false, + }); + manager.initialize(); + await manager.createJob(chatId, userId, chatId); + await manager.createJob(childId, userId, childId, { + initialMetadata: { agentEventBindingId: 'binding-1' }, + }); + }); + + afterEach(async () => { + await manager.destroy(); + }); + + it('leaves a running child thread out of the active chat list', async () => { + await expect(manager.getActiveJobIdsForUser(userId)).resolves.toEqual([chatId]); + }); + + it('keeps the child thread in the unfiltered store read', async () => { + const active = await jobStore.getActiveJobIdsByUser(userId); + expect(active.sort()).toEqual([chatId, childId].sort()); + }); + + it('still serves the child thread job to its own status and stream reads', async () => { + const job = await manager.getJob(childId); + expect(job).toMatchObject({ status: 'running' }); + expect(job?.metadata.userId).toBe(userId); + }); + + it('still counts the child thread for owner cleanup', async () => { + const blocking = await manager.getCleanupBlockingJobIdsForUser(userId); + expect(blocking).toContain(childId); + expect(blocking).toContain(chatId); + }); + + it('lists nothing when only a child thread is running', async () => { + await manager.completeJob(chatId); + await expect(manager.getActiveJobIdsForUser(userId)).resolves.toEqual([]); + }); + + it('reads the child-thread marker from the event binding', () => { + expect(isChildThreadJob({ agentEventBindingId: 'binding-1' })).toBe(true); + expect(isChildThreadJob({ agentEventBindingId: '' })).toBe(false); + expect(isChildThreadJob({})).toBe(false); + expect(isListedChatJob({})).toBe(true); + expect(isListedChatJob({ agentEventBindingId: 'binding-1' })).toBe(false); + }); +}); diff --git a/packages/api/src/stream/implementations/InMemoryJobStore.ts b/packages/api/src/stream/implementations/InMemoryJobStore.ts index d2d2d5cdbb..da09b0ac1d 100644 --- a/packages/api/src/stream/implementations/InMemoryJobStore.ts +++ b/packages/api/src/stream/implementations/InMemoryJobStore.ts @@ -25,6 +25,7 @@ import type { ParkedSteerClaim, ScheduleCleanupScope, ScheduleProviderOwner, + ActiveJobFilter, } from '~/stream/interfaces/IJobStore'; import type { EarlyBufferOverflowState } from '../../types/earlyBufferRecovery'; import type { RecoveredSteerPayload } from '~/stream/SteerRecovery'; @@ -1476,8 +1477,12 @@ export class InMemoryJobStore implements IJobStoreV2 { * Returns conversation IDs of running jobs belonging to the user. * Also performs self-healing cleanup: removes stale entries for jobs that no longer exist. */ - async getActiveJobIdsByUser(userId: string, tenantId?: string): Promise { - return this.getJobIdsByUser(userId, tenantId, false); + async getActiveJobIdsByUser( + userId: string, + tenantId?: string, + accept?: ActiveJobFilter, + ): Promise { + return this.getJobIdsByUser(userId, tenantId, false, accept); } async getCleanupBlockingJobIdsByUser(userId: string, tenantId?: string): Promise { @@ -1501,6 +1506,7 @@ export class InMemoryJobStore implements IJobStoreV2 { userId: string, tenantId: string | undefined, includeUndrained: boolean, + accept?: ActiveJobFilter, ): string[] { const userKey = tenantId ? `${tenantId}:${userId}` : userId; const trackedIds = this.userJobMap.get(userKey); @@ -1544,7 +1550,9 @@ export class InMemoryJobStore implements IJobStoreV2 { ) { continue; } - activeIds.push(streamId); + if (accept == null || accept(job)) { + activeIds.push(streamId); + } } else if ( job.providerDrained !== false && job.terminalPersistencePending !== true && diff --git a/packages/api/src/stream/implementations/RedisJobStore.ts b/packages/api/src/stream/implementations/RedisJobStore.ts index 5cfbf06590..37a61a9f22 100644 --- a/packages/api/src/stream/implementations/RedisJobStore.ts +++ b/packages/api/src/stream/implementations/RedisJobStore.ts @@ -35,6 +35,7 @@ import type { ParkedSteerClaim, ScheduleCleanupScope, ScheduleProviderOwner, + ActiveJobFilter, } from '~/stream/interfaces/IJobStore'; import type { EarlyBufferOverflowState } from '../../types/earlyBufferRecovery'; import type { ResolvedAskUserQuestion } from '~/agents/hitl/resume'; @@ -4009,8 +4010,12 @@ export class RedisJobStore implements IJobStoreV2 { * @param userId - The user ID to query * @returns Array of conversation IDs with active jobs */ - async getActiveJobIdsByUser(userId: string, tenantId?: string): Promise { - return this.getJobIdsByUser(userId, tenantId, false); + async getActiveJobIdsByUser( + userId: string, + tenantId?: string, + accept?: ActiveJobFilter, + ): Promise { + return this.getJobIdsByUser(userId, tenantId, false, accept); } async getCleanupBlockingJobIdsByUser(userId: string, tenantId?: string): Promise { @@ -4039,6 +4044,7 @@ export class RedisJobStore implements IJobStoreV2 { userId: string, tenantId: string | undefined, includeUndrained: boolean, + accept?: ActiveJobFilter, ): Promise { const userJobsKey = KEYS.userJobs(userId, tenantId); const trackedIds = await this.redis.smembers(userJobsKey); @@ -4083,7 +4089,9 @@ export class RedisJobStore implements IJobStoreV2 { ) { continue; } - activeIds.push(streamId); + if (accept == null || accept(job)) { + activeIds.push(streamId); + } } else { // Self-heal from durable state instead of a raw SREM, which could remove // a replacement's membership after the read. @@ -4115,7 +4123,8 @@ export class RedisJobStore implements IJobStoreV2 { currentJob.terminalPersistencePending === true || currentJob.terminalHostActionPending === true) ) - ) + ) && + (accept == null || accept(currentJob)) ) { activeIds.push(streamId); } diff --git a/packages/api/src/stream/interfaces/IJobStore.ts b/packages/api/src/stream/interfaces/IJobStore.ts index c8b5bf2717..b5768db551 100644 --- a/packages/api/src/stream/interfaces/IJobStore.ts +++ b/packages/api/src/stream/interfaces/IJobStore.ts @@ -411,6 +411,9 @@ export interface SerializableJobData { steersClosed?: boolean; } +/** Narrows an owner's active jobs, such as to the ones a client lists as chats. */ +export type ActiveJobFilter = (job: SerializableJobData) => boolean; + /** Exact active hash replaced by one atomic job creation. Built-in stores keep * this as non-enumerable transaction metadata; Redis also retains a private * receipt in the replacement hash so a committed create with a lost reply can @@ -1078,7 +1081,11 @@ export interface IJobStore { getJobCount(): Promise; getJobCountByStatus(status: JobStatus): Promise; destroy(): Promise; - getActiveJobIdsByUser(userId: string, tenantId?: string): Promise; + getActiveJobIdsByUser( + userId: string, + tenantId?: string, + accept?: ActiveJobFilter, + ): Promise; /** Complete owner cleanup query, including terminal host work and legacy-index recovery. * Managers retain the global-index fallback for older third-party stores. */ @@ -1380,9 +1387,14 @@ export interface IJobStoreV2 extends IJobStore { * Also performs self-healing cleanup of stale entries. * * @param userId - The user ID to query + * @param accept - Leaves out the active jobs it returns false for * @returns Array of conversation IDs with active jobs */ - getActiveJobIdsByUser(userId: string, tenantId?: string): Promise; + getActiveJobIdsByUser( + userId: string, + tenantId?: string, + accept?: ActiveJobFilter, + ): Promise; // ===== Content State Methods ===== // These methods manage volatile content state tied to each job. diff --git a/packages/api/src/stream/metadata.ts b/packages/api/src/stream/metadata.ts index b4a185c636..34e0d206cd 100644 --- a/packages/api/src/stream/metadata.ts +++ b/packages/api/src/stream/metadata.ts @@ -2,7 +2,7 @@ import { scheduleMCPOutcomeSchema, isScheduleMCPAuthorizationFailure, } from 'librechat-data-provider'; -import type { JobMetadataPatch } from './interfaces/IJobStore'; +import type { JobMetadataPatch, SerializableJobData } from './interfaces/IJobStore'; import type { GenerationJobMetadata } from '~/types'; import { parseScheduleMCPCompletion } from '~/schedules/authorization/continuation'; @@ -13,6 +13,20 @@ export function parseTerminalPersistenceOwner(value: unknown): 'external' | unde return value; } +/** + * A job whose stream is a subagent child thread. An event binding always targets a reserved + * child thread, so its id on the job marks the stream as one. Child threads are not listed + * conversations: a client has no row to mark for them and no record to load. + */ +export function isChildThreadJob(job: Pick): boolean { + return job.agentEventBindingId != null && job.agentEventBindingId !== ''; +} + +/** A job a client can show as a running chat. */ +export function isListedChatJob(job: Pick): boolean { + return !isChildThreadJob(job); +} + /** The acknowledgement carries the reply ID already reserved for this generation. */ export function buildGenerationStartedResponse(input: { streamId: string;