🪡 fix: Leave Child-Thread Runs Out of the Active Chat List (#18167)

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.
This commit is contained in:
Danny Avila 2026-11-14 15:01:48 -05:00 • committed by GitHub
parent d603bfff0f
commit 929f05daec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 158 additions and 14 deletions

View file

@ -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<string[]> {
return this.jobStore.getActiveJobIdsByUser(userId, tenantId);
return this.jobStore.getActiveJobIdsByUser(userId, tenantId, isListedChatJob);
}
/** Returns every generation whose provider can still mutate user-owned data,

View file

@ -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;

View file

@ -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);
});
});

View file

@ -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<string[]> {
return this.getJobIdsByUser(userId, tenantId, false);
async getActiveJobIdsByUser(
userId: string,
tenantId?: string,
accept?: ActiveJobFilter,
): Promise<string[]> {
return this.getJobIdsByUser(userId, tenantId, false, accept);
}
async getCleanupBlockingJobIdsByUser(userId: string, tenantId?: string): Promise<string[]> {
@ -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 &&

View file

@ -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<string[]> {
return this.getJobIdsByUser(userId, tenantId, false);
async getActiveJobIdsByUser(
userId: string,
tenantId?: string,
accept?: ActiveJobFilter,
): Promise<string[]> {
return this.getJobIdsByUser(userId, tenantId, false, accept);
}
async getCleanupBlockingJobIdsByUser(userId: string, tenantId?: string): Promise<string[]> {
@ -4039,6 +4044,7 @@ export class RedisJobStore implements IJobStoreV2 {
userId: string,
tenantId: string | undefined,
includeUndrained: boolean,
accept?: ActiveJobFilter,
): Promise<string[]> {
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);
}

View file

@ -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<number>;
getJobCountByStatus(status: JobStatus): Promise<number>;
destroy(): Promise<void>;
getActiveJobIdsByUser(userId: string, tenantId?: string): Promise<string[]>;
getActiveJobIdsByUser(
userId: string,
tenantId?: string,
accept?: ActiveJobFilter,
): Promise<string[]>;
/** 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<string[]>;
getActiveJobIdsByUser(
userId: string,
tenantId?: string,
accept?: ActiveJobFilter,
): Promise<string[]>;
// ===== Content State Methods =====
// These methods manage volatile content state tied to each job.

View file

@ -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<SerializableJobData, 'agentEventBindingId'>): boolean {
return job.agentEventBindingId != null && job.agentEventBindingId !== '';
}
/** A job a client can show as a running chat. */
export function isListedChatJob(job: Pick<SerializableJobData, 'agentEventBindingId'>): boolean {
return !isChildThreadJob(job);
}
/** The acknowledgement carries the reply ID already reserved for this generation. */
export function buildGenerationStartedResponse(input: {
streamId: string;