diff --git a/.github/workflows/channel-gateway.yml b/.github/workflows/channel-gateway.yml index f6c4e08c9a..5f4c190ec9 100644 --- a/.github/workflows/channel-gateway.yml +++ b/.github/workflows/channel-gateway.yml @@ -80,8 +80,9 @@ jobs: const { createHmac } = require('node:crypto'); const { setTimeout: delay } = require('node:timers/promises'); const bundle = { + delivery: { maxDeliveryParts: 100 }, channel: { channelId: 'fixture', tenantId: 'fixture', agentId: 'agent_fixture', createdBy: 'owner', type: 'slack', status: 'active', revision: 'fixture', slack: { teamId: 'T1', appId: 'A1', botUserId: 'U1' } }, - gateway: { version: 1, transport: 'webhook', nativeSessions: false, lifecycleReactions: false, signatureToleranceSeconds: 300 }, + gateway: { version: 1, transport: 'webhook', nativeSessions: false, lifecycleReactions: false, signatureToleranceSeconds: 300, socketReconnectDelayMs: 5000 }, credentials: { botToken: 'fixture', signingSecret: 'fixture-signing' }, agent: { id: 'agent_fixture' }, }; const backend = http.createServer((req, res) => { @@ -115,7 +116,8 @@ jobs: child.kill('SIGTERM'); if (await exited !== 0) throw new Error('Gateway did not exit cleanly'); console.log('Standalone gateway became healthy and stopped cleanly.'); - } catch { + } catch (error) { + console.error(error.message); child.kill('SIGKILL'); process.exitCode = 1; } finally { clearTimeout(deadline); backend.closeAllConnections(); backend.close(); diff --git a/api/server/services/Channels.js b/api/server/services/Channels.js index 83d568c511..6d4c603d26 100644 --- a/api/server/services/Channels.js +++ b/api/server/services/Channels.js @@ -44,7 +44,6 @@ const handlers = createChannelHandlers({ serviceToken: () => process.env.CHANNEL_SERVICE_TOKEN, config: policy.requestConfig, getAgent: (id) => db.getAgent({ id }), - deleteApiKey: db.deleteAgentApiKey, verifySlackToken: verifySlackChannelToken, logError: (metadata) => logger.error('[Channels] Operation failed', metadata), }); diff --git a/client/src/components/SidePanel/Agents/__tests__/Channels.spec.tsx b/client/src/components/SidePanel/Agents/__tests__/Channels.spec.tsx index 5c94dc90e1..c53e8e59c3 100644 --- a/client/src/components/SidePanel/Agents/__tests__/Channels.spec.tsx +++ b/client/src/components/SidePanel/Agents/__tests__/Channels.spec.tsx @@ -22,6 +22,7 @@ jest.mock('librechat-data-provider', () => ({ }, })); const pending: AgentChannel = { + revision: 'revision-1', channelId: 'channel-1', agentId: 'agent-1', createdBy: 'admin-1', @@ -344,8 +345,8 @@ test('mode/delete confirmations replace setup and discard secrets; mode requires ), ); }); -test.each(['active', 'pending_install', 'disabled'] as const)( - 'legacy %s connection has no mode control', +test.each(['active', 'pending_install'] as const)( + '%s connection has no mode control', async (status) => { const user = userEvent.setup(); jest.mocked(dataService.listAgentChannels).mockResolvedValue([{ ...pending, status }]); diff --git a/packages/api/src/admin/config.dotted.integration.spec.ts b/packages/api/src/admin/config.dotted.integration.spec.ts index 26f1d6c749..60b064d5f5 100644 --- a/packages/api/src/admin/config.dotted.integration.spec.ts +++ b/packages/api/src/admin/config.dotted.integration.spec.ts @@ -28,23 +28,29 @@ const ARN = 'arn:aws:bedrock:us-east-1:123456789012:inference-profile/us.anthropic.claude-sonnet-4-6'; const DOCX = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'; const API_KEY = 'sk-gateway-literal-0123456789'; +const LIFECYCLE_TIMEOUT_MS = 180_000; /** * The request path of `api/server/index.js` and `routes/admin/config.js`: app-level sanitizer, * authentication, the unsafe-key guard, then the real handlers over the real config methods. */ beforeAll(async () => { - jest.resetModules(); - const dataSchemas = await import('@librechat/data-schemas'); + const [dataSchemas, handlersModule, sanitizerModule, server] = await Promise.all([ + import('@librechat/data-schemas'), + import('./config'), + import('~/middleware/sanitize'), + MongoMemoryServer.create(), + ]); + mongoServer = server; ({ decryptV3, mergeConfigOverrides, logger } = dataSchemas); jest.spyOn(dataSchemas.logger, 'warn').mockReturnValue(dataSchemas.logger); jest.spyOn(dataSchemas.logger, 'info').mockReturnValue(dataSchemas.logger); - const { createAdminConfigHandlers } = await import('./config'); - const { createRequestSanitizer, rejectUnsafeConfigKeys } = await import('~/middleware/sanitize'); + const { createAdminConfigHandlers } = handlersModule; + const { createRequestSanitizer, rejectUnsafeConfigKeys } = sanitizerModule; - mongoServer = await MongoMemoryServer.create(); await mongoose.connect(mongoServer.getUri()); dataSchemas.createModels(mongoose); + await mongoose.models.Config.init(); const methods = dataSchemas.createMethods(mongoose); const handlers = createAdminConfigHandlers({ listAllConfigs: methods.listAllConfigs, @@ -84,12 +90,12 @@ beforeAll(async () => { }); app.use(createRequestSanitizer(mongoSanitize)); app.use('/api/admin/config', router); -}); +}, LIFECYCLE_TIMEOUT_MS); afterAll(async () => { await mongoose.disconnect(); - await mongoServer.stop(); -}); + await mongoServer?.stop(); +}, LIFECYCLE_TIMEOUT_MS); let principalCounter = 0; function nextRole(): string { diff --git a/packages/api/src/admin/config.integration.spec.ts b/packages/api/src/admin/config.integration.spec.ts index 4c71688401..3b30182d3b 100644 --- a/packages/api/src/admin/config.integration.spec.ts +++ b/packages/api/src/admin/config.integration.spec.ts @@ -92,21 +92,28 @@ async function readStored(principalId: string): Promise { return doc as StoredConfig | null; } +const LIFECYCLE_TIMEOUT_MS = 180_000; + beforeAll(async () => { - jest.resetModules(); - const dataSchemas = await import('@librechat/data-schemas'); + const [dataSchemas, handlersModule, capabilitiesModule, server] = await Promise.all([ + import('@librechat/data-schemas'), + import('./config'), + import('~/middleware/capabilities'), + MongoMemoryServer.create(), + ]); + mongoServer = server; jest.spyOn(dataSchemas.logger, 'error').mockReturnValue(dataSchemas.logger); jest.spyOn(dataSchemas.logger, 'warn').mockReturnValue(dataSchemas.logger); jest.spyOn(dataSchemas.logger, 'info').mockReturnValue(dataSchemas.logger); ({ mergeConfigOverrides } = dataSchemas); - ({ createAdminConfigHandlers } = await import('./config')); - ({ generateCapabilityCheck } = await import('~/middleware/capabilities')); + ({ createAdminConfigHandlers } = handlersModule); + ({ generateCapabilityCheck } = capabilitiesModule); - mongoServer = await MongoMemoryServer.create(); await mongoose.connect(mongoServer.getUri()); dataSchemas.createModels(mongoose); + await mongoose.models.Config.init(); methods = dataSchemas.createMethods(mongoose); -}); +}, LIFECYCLE_TIMEOUT_MS); afterEach(async () => { await Promise.all([ @@ -117,8 +124,8 @@ afterEach(async () => { afterAll(async () => { await mongoose.disconnect(); - await mongoServer.stop(); -}); + await mongoServer?.stop(); +}, LIFECYCLE_TIMEOUT_MS); describe('admin config writes against a real Config collection', () => { describe('top-level sections outside configSchema', () => { diff --git a/packages/api/src/admin/resolved.integration.spec.ts b/packages/api/src/admin/resolved.integration.spec.ts index b31eb10be4..6b8c553757 100644 --- a/packages/api/src/admin/resolved.integration.spec.ts +++ b/packages/api/src/admin/resolved.integration.spec.ts @@ -165,19 +165,28 @@ async function createGroup(name: string, memberIds: string[]): Promise { return group._id.toString(); } +const LIFECYCLE_TIMEOUT_MS = 180_000; + beforeAll(async () => { - jest.resetModules(); - dataSchemas = await import('@librechat/data-schemas'); + const [schemas, readHandlersModule, handlersModule, serviceModule, server] = await Promise.all([ + import('@librechat/data-schemas'), + import('./resolved'), + import('./config'), + import('~/app/service'), + MongoMemoryServer.create(), + ]); + dataSchemas = schemas; + mongoServer = server; jest.spyOn(dataSchemas.logger, 'warn').mockReturnValue(dataSchemas.logger); jest.spyOn(dataSchemas.logger, 'info').mockReturnValue(dataSchemas.logger); jest.spyOn(dataSchemas.logger, 'error').mockReturnValue(dataSchemas.logger); - ({ createAdminConfigReadHandlers } = await import('./resolved')); - ({ createAdminConfigHandlers } = await import('./config')); - const { createAppConfigService } = await import('~/app/service'); + ({ createAdminConfigReadHandlers } = readHandlersModule); + ({ createAdminConfigHandlers } = handlersModule); + const { createAppConfigService } = serviceModule; - mongoServer = await MongoMemoryServer.create(); await mongoose.connect(mongoServer.getUri()); dataSchemas.createModels(mongoose); + await mongoose.models.Config.init(); methods = dataSchemas.createMethods(mongoose); const appConfig: AppConfig = await dataSchemas.AppService({ config: structuredClone(YAML) }); @@ -197,7 +206,7 @@ beforeAll(async () => { getApplicableConfigs: methods.getApplicableConfigs, getUserPrincipals: methods.getUserPrincipals, }); -}); +}, LIFECYCLE_TIMEOUT_MS); afterEach(async () => { await Promise.all( @@ -208,8 +217,8 @@ afterEach(async () => { afterAll(async () => { await mongoose.disconnect(); - await mongoServer.stop(); -}); + await mongoServer?.stop(); +}, LIFECYCLE_TIMEOUT_MS); describe('GET /resolved/:principalType/:principalId', () => { it('resolves a profile over librechat.yaml and Base, in merge order and librechat.yaml shape', async () => { diff --git a/packages/api/src/admin/secrets.integration.spec.ts b/packages/api/src/admin/secrets.integration.spec.ts index 8633ecda91..48914109b9 100644 --- a/packages/api/src/admin/secrets.integration.spec.ts +++ b/packages/api/src/admin/secrets.integration.spec.ts @@ -261,9 +261,16 @@ function nextPrincipalId(): string { return `admin-${principalCounter}`; } +const LIFECYCLE_TIMEOUT_MS = 180_000; + beforeAll(async () => { - jest.resetModules(); - const dataSchemas = await import('@librechat/data-schemas'); + const [dataSchemas, handlersModule, secretsModule, server] = await Promise.all([ + import('@librechat/data-schemas'), + import('./config'), + import('./secrets'), + MongoMemoryServer.create(), + ]); + mongoServer = server; ({ decryptV3, loadOCRConfig, @@ -275,19 +282,17 @@ beforeAll(async () => { jest.spyOn(dataSchemas.logger, 'info').mockReturnValue(dataSchemas.logger); jest.spyOn(dataSchemas.logger, 'debug').mockReturnValue(dataSchemas.logger); - ({ createAdminConfigHandlers } = await import('./config')); - ({ getSecretPreview, encryptConfigSecretFields, UNPREVIEWED_SECRET_MASK } = await import( - './secrets' - )); + ({ createAdminConfigHandlers } = handlersModule); + ({ getSecretPreview, encryptConfigSecretFields, UNPREVIEWED_SECRET_MASK } = secretsModule); - mongoServer = await MongoMemoryServer.create(); await mongoose.connect(mongoServer.getUri()); dataSchemas.createModels(mongoose); + await mongoose.models.Config.init(); configMethods = dataSchemas.createMethods(mongoose); ({ patchConfigFields } = configMethods); handlers = createRealHandlers(); -}); +}, LIFECYCLE_TIMEOUT_MS); /** Handlers over the real Config collection; `getAppConfig` supplies a librechat.yaml base. */ function createRealHandlers( @@ -310,8 +315,8 @@ function createRealHandlers( afterAll(async () => { await mongoose.disconnect(); - await mongoServer.stop(); -}); + await mongoServer?.stop(); +}, LIFECYCLE_TIMEOUT_MS); describe('config secret registry — real handlers against a real Config collection', () => { describe.each(SECRET_FIELD_CASES)( diff --git a/packages/api/src/agents/responses/background.spec.ts b/packages/api/src/agents/responses/background.spec.ts index 9dfd9feeaf..324eae3524 100644 --- a/packages/api/src/agents/responses/background.spec.ts +++ b/packages/api/src/agents/responses/background.spec.ts @@ -469,12 +469,17 @@ it.each(['queued', 'pending', 'completed'] as const)( record = running.record; expect( ( - await storage.stageTerminal(owner, record.responseId, record.revision, { - generationCreatedAt: 10, - providerExecutionId: 'provider', - status: 'completed', - responseJSON: JSON.stringify(response), - }) + await storage.stageTerminal( + owner, + record.responseId, + { execution: (await storage.read(owner, record.responseId))! }, + { + generationCreatedAt: 10, + providerExecutionId: 'provider', + status: 'completed', + responseJSON: JSON.stringify(response), + }, + ) ).ok, ).toBe(true); } diff --git a/packages/api/src/agents/responses/decision.spec.ts b/packages/api/src/agents/responses/decision.spec.ts index 5759edcd1c..71f30c424c 100644 --- a/packages/api/src/agents/responses/decision.spec.ts +++ b/packages/api/src/agents/responses/decision.spec.ts @@ -111,7 +111,7 @@ async function setup( const result = await storage.publishReview( owner, responseId, - 1, + { execution: (await storage.read(owner, responseId))! }, execution.createdAt, { actionId: action.actionId, diff --git a/packages/api/src/agents/responses/foreground.spec.ts b/packages/api/src/agents/responses/foreground.spec.ts index 3f4373bccc..bccea65b29 100644 --- a/packages/api/src/agents/responses/foreground.spec.ts +++ b/packages/api/src/agents/responses/foreground.spec.ts @@ -7,7 +7,7 @@ import { ToolMessage } from '@librechat/agents/langchain/messages'; import { createModels, createMethods, tenantStorage, runAsSystem } from '@librechat/data-schemas'; import type { Response } from './types'; import { - createForegroundResponseTerminal, + createResponseTerminal, projectStoredResponseMessage, saveForegroundResponseInputs, requireResponseStorageWrite, @@ -331,7 +331,7 @@ it.each([null, undefined, { message: 'database unavailable' }, { conversationId: async function settleTerminal( terminal: Pick< - ReturnType, + ReturnType, 'beforeSettle' | 'afterSettle' | 'onSettlementError' >, outcome: 'complete' | 'error' | 'aborted' = 'complete', @@ -381,7 +381,7 @@ it.each([true, false])( async (requestedStore) => { const response = result(); const context = createResponseContext({ model: 'agent', input: '' }, response.id); - const terminal = createForegroundResponseTerminal({ context, requestedStore, logger }); + const terminal = createResponseTerminal({ context, requestedStore, logger }); const publish = jest.fn(); const saveResult = jest.fn(async () => ({ conversationId, messageId: response.id })); terminal.stage({ @@ -404,7 +404,7 @@ it.each([true, false])( it('retains provider output but reports store false on a failed optional save', async () => { const response = result(); const context = createResponseContext({ model: 'agent', input: '' }, response.id); - const terminal = createForegroundResponseTerminal({ context, requestedStore: true, logger }); + const terminal = createResponseTerminal({ context, requestedStore: true, logger }); const publish = jest.fn(); terminal.stage({ fail: jest.fn(), @@ -422,7 +422,7 @@ it.each(['error', 'cancelled'])( 'does not save or publish a staged success after %s', async (outcome) => { const response = result(); - const terminal = createForegroundResponseTerminal({ + const terminal = createResponseTerminal({ context: createResponseContext({ model: 'agent', input: '' }, response.id), requestedStore: true, logger, @@ -438,7 +438,7 @@ it.each(['error', 'cancelled'])( it('cannot restage a completed foreground result and repeat persistence', async () => { const response = result(); - const terminal = createForegroundResponseTerminal({ + const terminal = createResponseTerminal({ context: createResponseContext({ model: 'agent', input: '' }, response.id), requestedStore: true, logger, @@ -455,7 +455,7 @@ it('cannot restage a completed foreground result and repeat persistence', async it('fails closed if a caller skips the tracked-write phase', async () => { const response = result(); - const terminal = createForegroundResponseTerminal({ + const terminal = createResponseTerminal({ context: createResponseContext({ model: 'agent', input: '' }, response.id), requestedStore: true, logger, @@ -489,7 +489,7 @@ it('keeps foreground storage inside the account-erasure cleanup barrier', () => releaseSave = resolve; }); const response = result(); - const terminal = createForegroundResponseTerminal({ + const terminal = createResponseTerminal({ context: createResponseContext({ model: 'agent', input: '' }, response.id), requestedStore: true, logger, diff --git a/packages/api/src/agents/responses/foreground.ts b/packages/api/src/agents/responses/foreground.ts index 29821f9c90..6fb4bbcdde 100644 --- a/packages/api/src/agents/responses/foreground.ts +++ b/packages/api/src/agents/responses/foreground.ts @@ -413,10 +413,6 @@ export function createResponseTerminal< }; } -/** Compatibility name for existing foreground callers. */ -export const createForegroundResponseTerminal: typeof createResponseTerminal = - createResponseTerminal; - interface CompletionRequest extends Omit { config?: AppConfig; diff --git a/packages/api/src/agents/responses/host.spec.ts b/packages/api/src/agents/responses/host.spec.ts index 5304f413f7..3f362558d5 100644 --- a/packages/api/src/agents/responses/host.spec.ts +++ b/packages/api/src/agents/responses/host.spec.ts @@ -11,7 +11,7 @@ import { tenantStorage, runAsSystem, AGENT_TRIGGER_WORKER_CAPABILITY_RESPONSES_RESUME_V1, - AGENT_TRIGGER_WORKER_CAPABILITY_RESPONSES_V2, + AGENT_TRIGGER_WORKER_CAPABILITY_RESPONSES_V1, } from '@librechat/data-schemas'; import type { AppConfig, IUser, ResponseStorage } from '@librechat/data-schemas'; import type { RequestHandler } from 'express'; @@ -30,14 +30,12 @@ import { import { createAgentTriggerService, createAgentTriggerProducerBinding } from '../triggers/service'; import { InMemoryEventTransport } from '~/stream/implementations/InMemoryEventTransport'; import { InMemoryJobStore } from '~/stream/implementations/InMemoryJobStore'; -import { prepareAgentTriggerDelivery } from '../triggers/delivery'; import { createCheckpointNamespace } from '~/stream/checkpoints'; import { createRequireApiKeyAuth } from '~/apiKeys/middleware'; import { buildPendingBackgroundResponse } from './background'; import { mintBackgroundResponseWorkerToken } from './worker'; import { enrollAgentExecution } from '../remote/lifecycle'; import { createResponseExecutionRuntime } from './runtime'; -import { createBackgroundResponseEnvelope } from './queue'; import { buildResponseInputChain } from './persistence'; import { createBackgroundResponsesHost } from './host'; import { getSafeErrorMetadata } from '~/utils/errors'; @@ -430,7 +428,7 @@ async function ownerPause(keyId: string) { const published = await storage.publishReview( owner, id, - 1, + { execution: (await storage.read(owner, id))! }, execution.createdAt, { actionId: action.actionId, @@ -570,46 +568,7 @@ it('reuses canonical decision receipts and source preflight for a trusted native }); }); -it('drains the original V2 delivery after interactive worker activation without rewriting its identity or capability', async () => { - let deliveryKey = ''; - const f = await fixture( - undefined, - { - validate: async () => true, - admitResume: async () => ({ ok: false, code: 'response_resume_unavailable' }), - }, - '127.0.0.1', - async (phase, _client, _key, credentialId) => { - if (phase !== 'queue-starting') return; - const record = await reserveUnstarted(credentialId, '7aa6f6cc-0eef-4df4-9103-4205ed190804'); - const prepared = prepareAgentTriggerDelivery(createBackgroundResponseEnvelope(record), { - requiredWorkerCapability: AGENT_TRIGGER_WORKER_CAPABILITY_RESPONSES_V2, - }); - deliveryKey = prepared.deliveryKey; - const attached = await scoped(() => - storage.attachDelivery(record, record.responseId, deliveryKey), - ); - expect(attached.ok).toBe(true); - await runAsSystem(() => db.enqueueAgentTriggerDelivery(prepared)); - }, - ); - await f.readUntil('resp_delayed_worker', 'completed'); - expect(f.inputs).toHaveLength(1); - await f.stopQueue(); - const deliveries = await runAsSystem(() => mongoose.models.AgentTriggerDelivery.find({}).lean()); - expect(deliveries).toHaveLength(1); - expect(deliveries[0]).toMatchObject({ - deliveryKey, - requiredWorkerCapability: AGENT_TRIGGER_WORKER_CAPABILITY_RESPONSES_V2, - }); - const dispatcher = f.host.sourceDispatchers.get('responses')!; - expect(dispatcher.workerCapability).toBe(AGENT_TRIGGER_WORKER_CAPABILITY_RESPONSES_RESUME_V1); - expect(dispatcher.compatibleWorkerCapabilities).toEqual([ - AGENT_TRIGGER_WORKER_CAPABILITY_RESPONSES_V2, - ]); -}); - -it('registers only resume-capable initial/resume workers and dispatches saved intent through signed Responses ingress', async () => { +it('registers distinct initial/resume workers and dispatches saved intent through signed Responses ingress', async () => { const admitResume = jest.fn< ReturnType['admitResume']>, Parameters['admitResume']> @@ -617,9 +576,12 @@ it('registers only resume-capable initial/resume workers and dispatches saved in const validate = jest.fn(async () => true); const f = await fixture(undefined, { validate, admitResume }); expect([...f.host.sourceDispatchers.keys()].sort()).toEqual(['responses', 'responses.resume']); - for (const dispatcher of f.host.sourceDispatchers.values()) { - expect(dispatcher.workerCapability).toBe(AGENT_TRIGGER_WORKER_CAPABILITY_RESPONSES_RESUME_V1); - } + expect(f.host.sourceDispatchers.get('responses')?.workerCapability).toBe( + AGENT_TRIGGER_WORKER_CAPABILITY_RESPONSES_V1, + ); + expect(f.host.sourceDispatchers.get('responses.resume')?.workerCapability).toBe( + AGENT_TRIGGER_WORKER_CAPABILITY_RESPONSES_RESUME_V1, + ); const { id, action, owner } = await ownerPause(f.credential.id); const receipt = await f.client .post(`/api/agents/responses/${id}/review`) @@ -715,7 +677,7 @@ it('does not publish a completed response while its provider has durably paused const published = await storage.publishReview( record, record.responseId, - record.revision, + { execution: (await storage.read(record, record.responseId))! }, execution.createdAt, { actionId: action.actionId, diff --git a/packages/api/src/agents/responses/host.ts b/packages/api/src/agents/responses/host.ts index bf7fa0e797..991e31a34a 100644 --- a/packages/api/src/agents/responses/host.ts +++ b/packages/api/src/agents/responses/host.ts @@ -1,9 +1,4 @@ -import { - AGENT_TRIGGER_WORKER_CAPABILITY_RESPONSES_RESUME_V1, - AGENT_TRIGGER_WORKER_CAPABILITY_RESPONSES_V2, - createChatExpirationDate, - createFallbackRetentionDate, -} from '@librechat/data-schemas'; +import { createChatExpirationDate, createFallbackRetentionDate } from '@librechat/data-schemas'; import type { IUser, ResponseOwner, @@ -148,12 +143,6 @@ export function createBackgroundResponsesHost( enqueue: deps.enqueue, authorize: (record) => authorize(record, record.agentId, record.sourceKeyId), execute: transport, - compatibleWorkerCapabilities: deps.interactive - ? [AGENT_TRIGGER_WORKER_CAPABILITY_RESPONSES_V2] - : undefined, - workerCapability: deps.interactive - ? AGENT_TRIGGER_WORKER_CAPABILITY_RESPONSES_RESUME_V1 - : undefined, }); const resumeQueue = deps.interactive ? createResponseResumeQueue({ diff --git a/packages/api/src/agents/responses/outbox.spec.ts b/packages/api/src/agents/responses/outbox.spec.ts index 243a413ee9..4ece54e5d1 100644 --- a/packages/api/src/agents/responses/outbox.spec.ts +++ b/packages/api/src/agents/responses/outbox.spec.ts @@ -185,7 +185,7 @@ it('retires denied or cancelled work and never executes expired or closed scopes expect(f.onError).not.toHaveBeenCalled(); }); -it('leaves unready publication untouched and repairs legacy background reservations only', async () => { +it('leaves unready publication untouched and ignores records without a dispatch token', async () => { await reserve('legacy', owner, false); await reserve('foreground', owner, false, false); const f = fixture(); @@ -194,7 +194,7 @@ it('leaves unready publication untouched and repairs legacy background reservati expect(f.contexts).toEqual([]); f.setReady(true); await f.outbox.recoverPending(5, { found: false }); - expect([...f.published.values()]).toEqual(['legacy']); + expect([...f.published.values()]).toEqual([]); expect( (await scoped(owner, () => storage.read(owner, 'foreground')))?.dispatchOutbox, ).toBeUndefined(); @@ -215,3 +215,119 @@ it('fails closed on unknown future dispatch formats', async () => { ), ).toBe(false); }); + +it('settles a consumed resume after another pause without enqueueing it again', async () => { + await scoped(owner, async () => { + const record = await reserve('pause-before-ack'); + const started = await storage.publish(owner, record.responseId, record.revision, { + status: 'in_progress', + generationCreatedAt: 10, + responseJSON: '{}', + }); + if (!started.ok) throw new Error('Fixture start failed'); + const review = { + actionId: 'first', + providerExecutionId: 'provider-one', + expiresAt: 1000, + partialResponseJSON: '{}', + }; + const paused = await storage.publishReview( + owner, + record.responseId, + { execution: started.record }, + 10, + review, + '{}', + ); + expect(paused.ok).toBe(true); + expect( + ( + await storage.reserveResume( + owner, + record.responseId, + { + generationCreatedAt: 10, + intentId: 'resume-one', + actionId: 'first', + previousProviderExecutionId: 'provider-one', + providerExecutionId: 'provider-two', + fingerprint: 'approved', + resolutionJSON: '{}', + }, + 100, + ) + ).ok, + ).toBe(true); + const admitted = await storage.admitResume( + owner, + record.responseId, + 'resume-one', + 10, + 'provider-two', + '{}', + 101, + ); + if (!admitted.ok) throw new Error('Fixture resume admission failed'); + const nextReview = { ...review, actionId: 'second', providerExecutionId: 'provider-two' }; + const pausedAgain = await storage.publishReview( + owner, + record.responseId, + { execution: admitted.record }, + 10, + nextReview, + '{}', + ); + if (!pausedAgain.ok) throw new Error('Fixture second pause failed'); + expect(pausedAgain.record.resume).toBeUndefined(); + expect(pausedAgain.record.dispatchOutbox).toEqual({ + kind: 'resume', + id: 'resume-one', + state: 'pending', + }); + const f = fixture(); + const resumeEnqueue = jest.fn(); + const outbox = createResponseDispatchOutbox({ ...f.deps, resume: { enqueue: resumeEnqueue } }); + expect(await outbox.recoverPending(10, { found: false })).toBe(1); + expect(resumeEnqueue).not.toHaveBeenCalled(); + expect(f.enqueue).not.toHaveBeenCalled(); + const settled = (await storage.read(owner, record.responseId))!; + expect(settled).toMatchObject({ + revision: pausedAgain.record.revision, + status: 'requires_action', + review: nextReview, + dispatchOutbox: { kind: 'resume', id: 'resume-one', state: 'settled' }, + }); + expect( + await storage.settleDispatch(owner, record.responseId, { kind: 'resume', id: 'resume-one' }), + ).toBe(true); + const activity = { found: false }; + expect(await outbox.recoverPending(10, activity)).toBe(0); + expect(activity.found).toBe(false); + expect( + ( + await storage.reserveResume( + owner, + record.responseId, + { + generationCreatedAt: 10, + intentId: 'resume-two', + actionId: 'second', + previousProviderExecutionId: 'provider-two', + providerExecutionId: 'provider-three', + fingerprint: 'approved-two', + resolutionJSON: '{}', + }, + 102, + ) + ).ok, + ).toBe(true); + expect( + await storage.settleDispatch(owner, record.responseId, { kind: 'resume', id: 'resume-one' }), + ).toBe(false); + expect((await storage.read(owner, record.responseId))?.dispatchOutbox).toEqual({ + kind: 'resume', + id: 'resume-two', + state: 'pending', + }); + }); +}); diff --git a/packages/api/src/agents/responses/outbox.ts b/packages/api/src/agents/responses/outbox.ts index acc63714f4..bcfe6afc5f 100644 --- a/packages/api/src/agents/responses/outbox.ts +++ b/packages/api/src/agents/responses/outbox.ts @@ -82,35 +82,19 @@ export function createResponseDispatchOutbox(deps: { const record = await deps.storage.read(owner, owner.responseId); if (!record) return; if ( - record.dispatchOutbox && - (!['create', 'resume'].includes(record.dispatchOutbox.kind) || - !['pending', 'settled'].includes(record.dispatchOutbox.state)) + !record.dispatchOutbox || + !['create', 'resume'].includes(record.dispatchOutbox.kind) || + !['pending', 'settled'].includes(record.dispatchOutbox.state) ) return; - const kind = - record.resume?.state === 'reserved' - ? 'resume' - : (record.dispatchOutbox?.kind ?? 'create'); - if (!['create', 'resume'].includes(kind) || (kind === 'resume' && !deps.resume)) return; + const kind = record.dispatchOutbox.kind; + if (kind === 'resume' && !deps.resume) return; const tokenId = kind === 'create' ? record.responseId - : (record.resume?.intentId ?? record.dispatchOutbox?.id); - if ( - record.dispatchOutbox?.state === 'settled' && - record.dispatchOutbox.kind === kind && - record.dispatchOutbox.id === tokenId - ) + : (record.resume?.intentId ?? record.dispatchOutbox.id); + if (record.dispatchOutbox.id !== tokenId || record.dispatchOutbox.state === 'settled') return; - if (kind === 'create' && !record.dispatchOutbox) { - const payload = JSON.parse(record.requestJSON) as { - background?: boolean; - store?: boolean; - stream?: boolean; - }; - if (payload.background !== true || payload.store === false || payload.stream === true) - return; - } if ( !terminal(record) && (record.cancellationRequestedAt != null || !(await deps.authorize(record))) diff --git a/packages/api/src/agents/responses/preflight.spec.ts b/packages/api/src/agents/responses/preflight.spec.ts index cff3541d3d..a8709bed91 100644 --- a/packages/api/src/agents/responses/preflight.spec.ts +++ b/packages/api/src/agents/responses/preflight.spec.ts @@ -197,7 +197,7 @@ async function fixture( const published = await storage.publishReview( owner, responseId, - 1, + { execution: (await storage.read(owner, responseId))! }, execution.createdAt, { actionId: action.actionId, diff --git a/packages/api/src/agents/responses/queue.spec.ts b/packages/api/src/agents/responses/queue.spec.ts index 0f74d47ff6..bb65eb546d 100644 --- a/packages/api/src/agents/responses/queue.spec.ts +++ b/packages/api/src/agents/responses/queue.spec.ts @@ -132,7 +132,7 @@ it('executes queued Responses through its registered runtime, never through chat }); expect(fetcher).not.toHaveBeenCalled(); const delivery = await service.getDelivery(attached.record.deliveryKey!); - expect(delivery?.requiredWorkerCapability).toBe('responses_background_v2'); + expect(delivery?.requiredWorkerCapability).toBe('responses_background_v1'); expect(JSON.stringify(delivery?.envelope)).not.toContain('original-api-key'); expect(JSON.stringify(delivery?.envelope)).not.toContain('Original input'); })); @@ -164,7 +164,7 @@ it('does not let a queue worker without the Responses capability claim the respo ); if (!attached.ok) throw new Error('Attach failed'); await queue.enqueue(attached.record); - for (const workerCapabilities of [[], ['responses_background_v1']]) { + for (const workerCapabilities of [[], ['unrelated_worker_v1']]) { const claimed = await runAsSystem(async () => methods.claimNextAgentTriggerDelivery({ workerId: 'old-worker', @@ -213,7 +213,7 @@ async function reserveDecision(): Promise { const paused = await storage.publishReview( owner, 'resp_queue', - 1, + { execution: (await storage.read(owner, 'resp_queue'))! }, 100, { actionId: 'action-one', @@ -389,7 +389,7 @@ it('rejects an old delivery after a newer provider segment has paused for a diff await storage.publishReview( owner, record.responseId, - admitted.record.revision, + { execution: (await storage.read(owner, record.responseId))! }, 100, { actionId: 'action-two', diff --git a/packages/api/src/agents/responses/queue.ts b/packages/api/src/agents/responses/queue.ts index 0165a21471..846ed1ddfa 100644 --- a/packages/api/src/agents/responses/queue.ts +++ b/packages/api/src/agents/responses/queue.ts @@ -1,5 +1,5 @@ import { - AGENT_TRIGGER_WORKER_CAPABILITY_RESPONSES_V2, + AGENT_TRIGGER_WORKER_CAPABILITY_RESPONSES_V1, AGENT_TRIGGER_WORKER_CAPABILITY_RESPONSES_RESUME_V1, } from '@librechat/data-schemas'; import type { ResponseStorage, StoredResponseRecord } from '@librechat/data-schemas'; @@ -145,8 +145,6 @@ export interface BackgroundResponseQueueDependencies { storage: ResponseStorage; enqueue: AgentTriggerService['enqueue']; authorize: (record: StoredResponseRecord) => Promise; - workerCapability?: string; - compatibleWorkerCapabilities?: readonly string[]; /** Execute the persisted Responses payload with its exact stable identity. * Admission MUST be idempotent: a repeated delivery attaches rather than starting a second run. * The runtime also owns cancellation-before-start and already terminal reconciliation. */ @@ -215,8 +213,7 @@ export function createBackgroundResponseQueue( [ BACKGROUND_RESPONSE_SOURCE, { - workerCapability: deps.workerCapability ?? AGENT_TRIGGER_WORKER_CAPABILITY_RESPONSES_V2, - compatibleWorkerCapabilities: deps.compatibleWorkerCapabilities, + workerCapability: AGENT_TRIGGER_WORKER_CAPABILITY_RESPONSES_V1, dispatch, }, ], @@ -226,8 +223,7 @@ export function createBackgroundResponseQueue( throw new Error('Response queue identity changed'); const receipt = await deps.enqueue(createBackgroundResponseEnvelope(record), { orderingKey: `responses:${responseTenantId(record.tenantId) ?? '_'}:${record.userId}:${record.conversationId}`, - requiredWorkerCapability: - deps.workerCapability ?? AGENT_TRIGGER_WORKER_CAPABILITY_RESPONSES_V2, + requiredWorkerCapability: AGENT_TRIGGER_WORKER_CAPABILITY_RESPONSES_V1, }); if (receipt.deliveryKey !== record.deliveryKey) throw new Error('Response queue receipt identity changed'); diff --git a/packages/api/src/agents/responses/recovery.spec.ts b/packages/api/src/agents/responses/recovery.spec.ts index b7c5a92d60..b94aa17f80 100644 --- a/packages/api/src/agents/responses/recovery.spec.ts +++ b/packages/api/src/agents/responses/recovery.spec.ts @@ -325,7 +325,14 @@ it('does not replace another terminal receipt after a staging conflict', () => const { record, receipt } = await setup(); const competing = { ...receipt, ownerId: 'another-worker' }; expect( - (await storage.stageTerminal(owner, record.responseId, record.revision, competing)).ok, + ( + await storage.stageTerminal( + owner, + record.responseId, + { execution: (await storage.read(owner, record.responseId))! }, + competing, + ) + ).ok, ).toBe(true); const stage = jest.spyOn(storage, 'stageTerminal'); const cleanup = jest.fn(async () => {}); @@ -371,7 +378,7 @@ it.each(['provider', 'another-provider'])( const reviewed = await storage.publishReview( owner, record.responseId, - record.revision, + { execution: (await storage.read(owner, record.responseId))! }, receipt.generationCreatedAt, { actionId: 'new-action', @@ -405,7 +412,7 @@ it.each([false, true])( const reviewed = await storage.publishReview( owner, record.responseId, - record.revision, + { execution: (await storage.read(owner, record.responseId))! }, receipt.generationCreatedAt, { actionId: 'action', @@ -463,7 +470,12 @@ it.each([false, true])( it('lets authorized polling recover a saved result without queueing or observing execution', () => scoped(async () => { const { record, receipt } = await setup(); - await storage.stageTerminal(owner, record.responseId, record.revision, receipt); + await storage.stageTerminal( + owner, + record.responseId, + { execution: (await storage.read(owner, record.responseId))! }, + receipt, + ); const completion = createResponseCompletion({ confirmExecution: async () => null, releaseExecution: async () => {}, @@ -497,7 +509,12 @@ it('lets authorized polling recover a saved result without queueing or observing it('does not let a late Stop overwrite the saved result or require a transient job to survive', () => scoped(async () => { const { record, receipt } = await setup(); - await storage.stageTerminal(owner, record.responseId, record.revision, receipt); + await storage.stageTerminal( + owner, + record.responseId, + { execution: (await storage.read(owner, record.responseId))! }, + receipt, + ); const completion = createResponseCompletion({ confirmExecution: async () => null, releaseExecution: async () => {}, @@ -527,7 +544,12 @@ it('does not let a late Stop overwrite the saved result or require a transient j it('does not recover or disclose a saved reply after source authorization is revoked', () => scoped(async () => { const { record, receipt } = await setup(); - await storage.stageTerminal(owner, record.responseId, record.revision, receipt); + await storage.stageTerminal( + owner, + record.responseId, + { execution: (await storage.read(owner, record.responseId))! }, + receipt, + ); const recoverTerminal = jest.fn(async () => { throw new Error('Unauthorized recovery'); }); @@ -555,7 +577,12 @@ it('does not recover or disclose a saved reply after source authorization is rev it('converges concurrent finishers on one immutable canonical result', () => scoped(async () => { const { record, receipt } = await setup(); - const staged = await storage.stageTerminal(owner, record.responseId, record.revision, receipt); + const staged = await storage.stageTerminal( + owner, + record.responseId, + { execution: (await storage.read(owner, record.responseId))! }, + receipt, + ); if (!staged.ok) throw new Error('Stage failed'); const completion = createResponseCompletion({ confirmExecution: async () => null, @@ -635,7 +662,12 @@ it.each(['owner', 'conversation'] as const)( (kind) => scoped(async () => { const { record, receipt } = await setup(); - await storage.stageTerminal(owner, record.responseId, record.revision, receipt); + await storage.stageTerminal( + owner, + record.responseId, + { execution: (await storage.read(owner, record.responseId))! }, + receipt, + ); const completion = createResponseCompletion({ confirmExecution: async () => null, releaseExecution: async () => {}, diff --git a/packages/api/src/agents/responses/resume.spec.ts b/packages/api/src/agents/responses/resume.spec.ts index 6fb19a934c..71b00f299b 100644 --- a/packages/api/src/agents/responses/resume.spec.ts +++ b/packages/api/src/agents/responses/resume.spec.ts @@ -99,7 +99,7 @@ async function pausedDecision(): Promise { const result = await storage.publishReview( owner, responseId, - 1, + { execution: (await storage.read(owner, responseId))! }, execution.createdAt, { actionId: action.actionId, diff --git a/packages/api/src/agents/responses/turnProjection.ts b/packages/api/src/agents/responses/turnProjection.ts index 0021866f06..3f739e08a6 100644 --- a/packages/api/src/agents/responses/turnProjection.ts +++ b/packages/api/src/agents/responses/turnProjection.ts @@ -15,7 +15,7 @@ export function projectCanonicalResponseTurn( record: StoredResponseRecord, receipt: ResponseTerminalReceipt, ): ResponseTurnWrites | undefined { - if (!receipt.turnJSON) return undefined; // Legacy already-drained receipts have no history intent. + if (!receipt.turnJSON) return undefined; // Failures before turn preparation have no history to materialize. const intent = JSON.parse(receipt.turnJSON) as ResponseTurnIntent; if ( intent.version !== 1 || diff --git a/packages/api/src/agents/responses/worker.runtime.spec.ts b/packages/api/src/agents/responses/worker.runtime.spec.ts index 30b6d7d29e..8f8f37cd7d 100644 --- a/packages/api/src/agents/responses/worker.runtime.spec.ts +++ b/packages/api/src/agents/responses/worker.runtime.spec.ts @@ -524,7 +524,7 @@ describe('background Responses execution handoff', () => { const result = await storage.publishReview( owner, record.responseId, - 1, + { execution: (await storage.read(owner, record.responseId))! }, execution.createdAt, { actionId: action.actionId, diff --git a/packages/api/src/agents/triggers/service.ts b/packages/api/src/agents/triggers/service.ts index 602de54375..a9af46c300 100644 --- a/packages/api/src/agents/triggers/service.ts +++ b/packages/api/src/agents/triggers/service.ts @@ -75,8 +75,6 @@ export type AgentTriggerCompletionExpedite = export interface AgentTriggerSourceDispatcher { workerCapability: string; - /** Older persisted delivery formats this source can execute without rewriting their fences. */ - compatibleWorkerCapabilities?: readonly string[]; dispatch: AgentTriggerExecutionHost['dispatch']; /** Bounded source publication recovery. The canonical queue owns scheduling. */ recoverPending?: (limit: number, activity: { found: boolean }) => Promise; @@ -742,7 +740,6 @@ export function createAgentTriggerService(deps: AgentTriggerServiceDeps = {}): A () => purgeRecoveryLoop?.wake(), [...(deps.sourceDispatchers?.values() ?? [])].flatMap((source) => [ source.workerCapability, - ...(source.compatibleWorkerCapabilities ?? []), ]), ), dispatch: dispatchForActivePrincipal, diff --git a/packages/api/src/channels/authority.spec.ts b/packages/api/src/channels/authority.spec.ts index 86b00ee34a..7083b32e36 100644 --- a/packages/api/src/channels/authority.spec.ts +++ b/packages/api/src/channels/authority.spec.ts @@ -162,7 +162,6 @@ beforeEach(async () => { serviceToken: () => token, config: async () => ({ enabled: true }), getAgent: async (id) => ({ id }), - deleteApiKey: async () => true, verifySlackToken: async () => null, logError: jest.fn(), }); diff --git a/packages/api/src/channels/authority.ts b/packages/api/src/channels/authority.ts index 33a78c441c..34c54f8e29 100644 --- a/packages/api/src/channels/authority.ts +++ b/packages/api/src/channels/authority.ts @@ -42,7 +42,7 @@ export function channelExternalIdentity( return { tenantId: channel.tenantId || '_', channelId: channel.channelId, - channelRevision: channel.revision ?? channel.channelId, + channelRevision: channel.revision, teamId: channel.slack?.teamId ?? '', externalUserId, agentId: channel.agentId, @@ -67,7 +67,7 @@ export function createChannelAuthority(deps: ChannelAuthorityDependencies): Chan (channel.tenantId || '_') === identity.tenantId && channel.agentId === identity.agentId && channel.slack?.teamId === identity.teamId && - (channel.revision ?? channel.channelId) === identity.channelRevision + channel.revision === identity.channelRevision ? channel : null; } @@ -115,7 +115,7 @@ export function createChannelAuthority(deps: ChannelAuthorityDependencies): Chan if (!linked.ok) return null; return { userId: linked.value.userId, - channelRevision: channel.revision ?? channel.channelId, + channelRevision: channel.revision, linkId: linked.value.linkId, linkRevision: linked.value.revision, }; diff --git a/packages/api/src/channels/cleanup.spec.ts b/packages/api/src/channels/cleanup.spec.ts index fb43816e57..2f2bef2468 100644 --- a/packages/api/src/channels/cleanup.spec.ts +++ b/packages/api/src/channels/cleanup.spec.ts @@ -217,17 +217,16 @@ it('defers deletion while cleanup is unknown and completes after restart and exa }); }); -it('recovers deleting parents with no remaining runs but never bypasses a failed legacy key revocation', async () => { - await scope(() => db.updateAgentChannel('channel', 'user-a', { apiKeyId: 'legacy-key-id' })); +it('recovers deleting parents with no remaining runs after a storage outage', async () => { await scope(() => db.beginAgentChannelDeletion('channel', 'user-a')); - deps.deleteApiKey = async () => { - throw new Error('key store unavailable'); - }; + const deletion = jest + .spyOn(deps.channels, 'deleteAgentChannel') + .mockRejectedValueOnce(new Error('channel store unavailable')); await runtime.initialize(); expect(await scope(() => db.getAgentChannelMetadata('channel'))).toMatchObject({ status: 'deleting', }); - deps.deleteApiKey = async () => true; + deletion.mockRestore(); await restart(); expect(await scope(() => db.getAgentChannelMetadata('channel'))).toBeNull(); }); diff --git a/packages/api/src/channels/cleanup.ts b/packages/api/src/channels/cleanup.ts index 6f8af343bc..3d0e4025aa 100644 --- a/packages/api/src/channels/cleanup.ts +++ b/packages/api/src/channels/cleanup.ts @@ -108,8 +108,6 @@ export function createChannelCleanupRuntime(deps: ChannelCleanupDependencies) { try { const channel = await deps.channels.getAgentChannelMetadata(target.channelId); if (channel?.status !== 'deleting') return; - if (channel.apiKeyId) - await deps.deleteApiKey(channel.apiKeyId, channel.createdBy); const result = await deps.channels.deleteAgentChannel( channel.channelId, channel.createdBy, diff --git a/packages/api/src/channels/execution.spec.ts b/packages/api/src/channels/execution.spec.ts index 98a2112ee3..8895b0c0cd 100644 --- a/packages/api/src/channels/execution.spec.ts +++ b/packages/api/src/channels/execution.spec.ts @@ -118,7 +118,6 @@ describe('mounted service-token advance cannot execute an unbound run', () => { serviceToken: () => token, config: async () => ({ enabled: true }), getAgent: async () => ({ id: fixture.agentId }), - deleteApiKey: async () => true, verifySlackToken: async () => null, logError: jest.fn(), }); diff --git a/packages/api/src/channels/feedback.spec.ts b/packages/api/src/channels/feedback.spec.ts index 300ac6e6f7..a5e5a676b9 100644 --- a/packages/api/src/channels/feedback.spec.ts +++ b/packages/api/src/channels/feedback.spec.ts @@ -448,24 +448,12 @@ it( }), ); -it.each(['current', 'legacy'] as const)( +it.each(['current'] as const)( 'keeps distinct skin-tone slots and canonical aliases (%s)', - (mode) => + (_mode) => tenantStorage.run({ tenantId: actor.tenantId }, async () => { await service.record(actor, event('tone-two', 100, { reaction: 'thumbsup::skin-tone-2' })); await service.record(actor, event('tone-three', 200, { reaction: 'thumbsup::skin-tone-3' })); - if (mode === 'legacy') { - const record = ( - await deps.storage.pendingChannelFeedbackExports(actor.tenantId, clock, 10) - )[0]; - const { reaction: _identity, ...collapsed } = record.votes.find( - (vote) => vote.eventId === 'tone-three', - )!; - await mongoose.models.ChannelFeedback.updateOne( - { tenantId: actor.tenantId, key: record.key }, - { $set: { votes: [collapsed] } }, - ); - } expect( await service.record( actor, diff --git a/packages/api/src/channels/feedback.ts b/packages/api/src/channels/feedback.ts index a88dd874a8..3ee0170fa9 100644 --- a/packages/api/src/channels/feedback.ts +++ b/packages/api/src/channels/feedback.ts @@ -148,19 +148,11 @@ export function createChannelFeedbackService(deps: ChannelFeedbackDependencies) retryAtMs: 0, }); for (let attempt = 0; attempt < deps.maxConflictRetries; attempt++) { - const legacy = ledger.votes.some((vote) => !vote.reaction); - const votes = legacy - ? await storage.rebuildVotes( - record.tenantId, - record.ledgerKey, - ledger.votes.map((vote) => vote.eventId), - ) - : ledger.votes; - if (!votes) return failure('changed'); + const votes = ledger.votes; if (votes.length > CHANNEL_FEEDBACK_VOTE_SLOTS_HARD_MAX) return failure('capacity'); const next = projectChannelReaction(votes, record.event, record.rating); if (next.votes.length > CHANNEL_FEEDBACK_VOTE_SLOTS_HARD_MAX) return failure('capacity'); - if (!next.changed && !legacy) { + if (!next.changed) { await storage.markChannelFeedbackEvent(record); return { ok: true, value: response(ledger) }; } @@ -279,18 +271,8 @@ export function createChannelFeedbackService(deps: ChannelFeedbackDependencies) if (admitted.digest !== eventDigest) return failure('event_conflict'); return project(admitted); }, - /** Journal maintenance is independent of admission, recovery and provider export. */ - async compact(tenantId: string, limit: number): Promise { - return deps.storage.compactChannelFeedbackEvents( - tenantId, - limit, - Date.now(), - deps.exportRetryMs, - ); - }, /** Recover accepted receipts after a process exits between admission and projection. */ async recover(tenantId: string, limit: number): Promise { - await deps.storage.repairChannelFeedbackRetention(tenantId, limit); const events = await deps.storage.pendingChannelFeedbackEvents(tenantId, limit, now()); let processed = 0; for (const event of events) { diff --git a/packages/api/src/channels/feedbackHttp.spec.ts b/packages/api/src/channels/feedbackHttp.spec.ts index c6a741bcb0..ba74f76b49 100644 --- a/packages/api/src/channels/feedbackHttp.spec.ts +++ b/packages/api/src/channels/feedbackHttp.spec.ts @@ -182,7 +182,6 @@ beforeEach(async () => { serviceToken: () => serviceToken, config: async () => ({ enabled: true }), getAgent: async () => ({ id: 'agent-a' }), - deleteApiKey: async () => false, verifySlackToken: async () => null, logError: () => {}, }); @@ -473,33 +472,6 @@ test('durable worker enumerates only metadata in system context then exports wit expect(observed).toEqual([SYSTEM_TENANT_ID, tenantId]); }); -it('exports valid feedback even when independent receipt maintenance fails', async () => { - await post().expect(200); - const onError = jest.fn(); - const worker = createChannelFeedbackWorker({ - feedback: { - ...feedback, - compact: async () => { - throw new Error('maintenance unavailable'); - }, - }, - batchSize: 10, - intervalMs: 60000, - maxIdleIntervalMs: 60000, - onError, - listChannels: async () => ({ channels: [{ tenantId }] }), - }); - await worker.start(); - await worker.stop(); - expect(deps.sendScore).toHaveBeenCalledTimes(1); - expect(onError).toHaveBeenCalledWith( - expect.objectContaining({ message: 'maintenance unavailable' }), - ); - expect(await scope(() => mongoose.models.ChannelFeedback.findOne({}).lean())).toMatchObject({ - exportStatus: 'sent', - }); -}); - it('refuses to move a frozen feedback mapping to a changed source trace', async () => { await post().expect(200); await scope(async () => @@ -718,7 +690,6 @@ it('starts recovery and admits scoped feedback with a disabled deployment defaul serviceToken: () => serviceToken, config: async (req, channel) => policy.requestConfig(req, channel), getAgent: async () => ({ id: 'agent-a' }), - deleteApiKey: async () => false, verifySlackToken: async () => null, logError: () => {}, }).service; diff --git a/packages/api/src/channels/feedbackRuntime.spec.ts b/packages/api/src/channels/feedbackRuntime.spec.ts index b15fe22b75..e54663b9a4 100644 --- a/packages/api/src/channels/feedbackRuntime.spec.ts +++ b/packages/api/src/channels/feedbackRuntime.spec.ts @@ -77,7 +77,6 @@ beforeEach(async () => { serviceToken: () => serviceToken, config: async () => ({ enabled }), getAgent: async () => ({ id: 'agent-a' }), - deleteApiKey: async () => false, verifySlackToken: async () => null, logError: () => {}, }); @@ -175,7 +174,6 @@ test('normalizes default tenants only at the feedback worker application boundar return 0; }, flush: async () => 0, - compact: async () => 0, }, batchSize: 1, intervalMs: 60000, @@ -225,7 +223,7 @@ test.each(['read', 'schema', 'settings'] as const)( test('disposes a partially started feedback worker before retrying and exposing handlers', async () => { const partial = feedbackWorkers.createChannelFeedbackWorker({ ...deps.workerDeps, - feedback: { recover: async () => 0, flush: async () => 0, compact: async () => 0 }, + feedback: { recover: async () => 0, flush: async () => 0 }, batchSize: 1, intervalMs: 60000, maxIdleIntervalMs: 60000, diff --git a/packages/api/src/channels/feedbackWorker.ts b/packages/api/src/channels/feedbackWorker.ts index 8237b8bda2..9cd94a293b 100644 --- a/packages/api/src/channels/feedbackWorker.ts +++ b/packages/api/src/channels/feedbackWorker.ts @@ -8,7 +8,7 @@ export interface ChannelFeedbackWorkerDependencies { cursor: string | undefined, limit: number, ) => Promise<{ channels: Array<{ tenantId?: string }>; next?: string }>; - feedback: Pick, 'recover' | 'flush' | 'compact'>; + feedback: Pick, 'recover' | 'flush'>; batchSize: number; intervalMs: number; maxIdleIntervalMs: number; @@ -40,12 +40,6 @@ export function createChannelFeedbackWorker( idle = false; deps.onError(error); } - try { - if (await deps.feedback.compact(tenantId, deps.batchSize)) idle = false; - } catch (error) { - idle = false; - deps.onError(error); - } }); } return idle; diff --git a/packages/api/src/channels/identityHttp.spec.ts b/packages/api/src/channels/identityHttp.spec.ts index e3a0fd16c4..df185b7357 100644 --- a/packages/api/src/channels/identityHttp.spec.ts +++ b/packages/api/src/channels/identityHttp.spec.ts @@ -59,7 +59,6 @@ beforeEach(async () => { serviceToken: () => serviceToken, config: async () => ({ enabled: true }), getAgent: async () => ({ id: 'agent-a' }), - deleteApiKey: async () => false, verifySlackToken: async () => null, logError: () => {}, }); diff --git a/packages/api/src/channels/service.spec.ts b/packages/api/src/channels/service.spec.ts index a73ab16918..e620d4f5e1 100644 --- a/packages/api/src/channels/service.spec.ts +++ b/packages/api/src/channels/service.spec.ts @@ -18,7 +18,6 @@ let mongo: MongoMemoryReplSet; let db: ReturnType; let app: express.Express; let deps: ChannelDependencies; -let revokedKeys: string[]; beforeAll(async () => { mongo = await MongoMemoryReplSet.create({ @@ -38,19 +37,13 @@ afterAll(async () => { beforeEach(async () => { await runAsSystem(async () => { await mongoose.models.AgentChannel.deleteMany({}); - await mongoose.models.AgentChannelThread.deleteMany({}); await mongoose.models.ChannelRun.deleteMany({}); }); - revokedKeys = []; deps = { db, serviceToken: () => serviceToken, config: async () => ({ enabled: true }), getAgent: async (id) => ({ id, name: 'Test Agent' }), - deleteApiKey: async (id) => { - revokedKeys.push(id); - return true; - }, verifySlackToken: async () => ({ teamId: 'TTEST', botUserId: 'UTEST' }), logError: jest.fn(), }; @@ -100,8 +93,8 @@ it('persists encrypted credentials and returns them only through the authenticat const stored = await tenantStorage.run({ tenantId }, async () => db.getAgentChannel(id)); expect(stored?.tenantId).toBe(tenantId); expect(stored?.encryptedBotToken).toMatch(/^v3:/); - expect(stored?.encryptedApiKey).toBe(''); - expect(stored?.apiKeyId).toBe(''); + expect(stored).not.toHaveProperty('encryptedApiKey'); + expect(stored).not.toHaveProperty('apiKeyId'); expect(JSON.stringify(publicChannel(stored!))).not.toContain('encrypted'); await request(app).get(`/service/slack/${tenantId}/${id}`).expect(401); await request(app) @@ -210,36 +203,12 @@ it('isolates durable thread mappings and denies disabled channels', async () => }); }); -it('fails closed when retiring a legacy unrestricted owner key fails', async () => { - const id = await createChannel(); - await activation(id).expect(200); - await request(app) - .patch(`/ui/agent-test/channels/${id}`) - .send({ status: 'disabled' }) - .expect(200); - await tenantStorage.run({ tenantId }, () => - db.updateAgentChannel(id, owner, { apiKeyId: 'legacy-key' }), - ); - deps.deleteApiKey = async (keyId) => { - revokedKeys.push(keyId); - throw new Error('sensitive upstream diagnostics'); - }; - await activation(id).expect(503); - expect(revokedKeys).toContain('legacy-key'); - const stored = await tenantStorage.run({ tenantId }, async () => db.getAgentChannel(id)); - expect(stored?.apiKeyId).toBe('legacy-key'); - expect(stored?.status).toBe('disabled'); -}); - it('leaves a failed deletion closed and retryable without allowing reactivation', async () => { const id = await createChannel(); await activation(id).expect(200); - await tenantStorage.run({ tenantId }, () => - db.updateAgentChannel(id, owner, { apiKeyId: 'legacy-key' }), - ); - deps.deleteApiKey = async () => { - throw new Error('sensitive upstream diagnostics'); - }; + const deletion = jest + .spyOn(deps.db, 'deleteAgentChannel') + .mockRejectedValueOnce(new Error('sensitive upstream diagnostics')); const failed = await request(app).delete(`/ui/agent-test/channels/${id}`).expect(503); expect(failed.body).toEqual({ error: 'channel_operation_failed' }); await activation(id).expect(409); @@ -247,7 +216,7 @@ it('leaves a failed deletion closed and retryable without allowing reactivation' .get(`/service/slack/${tenantId}/${id}`) .set('Authorization', `Bearer ${serviceToken}`) .expect(404); - deps.deleteApiKey = async () => true; + deletion.mockRestore(); await request(app).delete(`/ui/agent-test/channels/${id}`).expect(204); }); @@ -282,25 +251,9 @@ it('returns a safe deleting state while a persisted run is still active', async }); }); -it('retains the deleting parent when dependent thread cleanup fails', async () => { +it('retains the deleting parent when dependent feedback cleanup fails', async () => { const id = await createChannel(); - await tenantStorage.run({ tenantId }, async () => { - await mongoose.models.AgentChannelThread.create({ - channelId: id, - tenantId, - externalThreadKey: 'CTEST:123.456', - conversationId: 'resp_test', - }); - expect(await db.deleteAgentChannel(id, 'another-owner')).toEqual({ status: 'not_found' }); - expect( - await mongoose.models.AgentChannelThread.findOne({ - channelId: id, - tenantId, - externalThreadKey: 'CTEST:123.456', - }), - ).not.toBeNull(); - }); - const cleanup = jest.spyOn(mongoose.models.AgentChannelThread, 'deleteMany'); + const cleanup = jest.spyOn(mongoose.models.ChannelReply, 'deleteMany'); cleanup.mockRejectedValueOnce(new Error('temporary database failure')); try { await request(app).delete(`/ui/agent-test/channels/${id}`).expect(503); @@ -308,16 +261,7 @@ it('retains the deleting parent when dependent thread cleanup fails', async () = expect(stored?.status).toBe('deleting'); await activation(id).expect(409); await request(app).delete(`/ui/agent-test/channels/${id}`).expect(204); - await tenantStorage.run({ tenantId }, async () => { - expect(await db.getAgentChannel(id)).toBeNull(); - expect( - await mongoose.models.AgentChannelThread.findOne({ - channelId: id, - tenantId, - externalThreadKey: 'CTEST:123.456', - }), - ).toBeNull(); - }); + expect(await tenantStorage.run({ tenantId }, () => db.getAgentChannel(id))).toBeNull(); } finally { cleanup.mockRestore(); } diff --git a/packages/api/src/channels/service.ts b/packages/api/src/channels/service.ts index eaafaf20dc..a2c690cbbc 100644 --- a/packages/api/src/channels/service.ts +++ b/packages/api/src/channels/service.ts @@ -39,7 +39,6 @@ export interface ChannelDependencies { channel?: AgentChannelRecord, ) => Promise | undefined>; getAgent: (agentId: string) => Promise<{ id: string; name?: string | null } | null>; - deleteApiKey: (id: string, userId: string) => Promise; verifySlackToken: (token: string) => Promise; logError: (metadata: ReturnType) => void; } @@ -93,7 +92,7 @@ export function publicChannel( type: channel.type, status: channel.status, credentialMode: channel.credentialMode, - revision: channel.revision ?? channel.channelId, + revision: channel.revision, slack: channel.slack, }; } @@ -233,30 +232,21 @@ export function createChannelHandlers(deps: ChannelDependencies): { res.status(409).json({ error: 'channel_changed' }); return; } - if (disabled?.apiKeyId) { - await deps.deleteApiKey(disabled.apiKeyId, channel.createdBy); - } res.json(publicChannel(disabled)); return; } const mode = modeInput.safeParse(req.body); if (mode.success) { - if ( - channel.status !== 'disabled' || - mode.data.revision !== (channel.revision ?? channel.channelId) - ) { + if (channel.status !== 'disabled' || mode.data.revision !== channel.revision) { res.status(409).json({ error: 'channel_changed' }); return; } - if (channel.apiKeyId) await deps.deleteApiKey(channel.apiKeyId, channel.createdBy); const updated = await deps.db.updateAgentChannel( channel.channelId, channel.createdBy, { credentialMode: mode.data.credentialMode, revision: nanoid(), - encryptedApiKey: '', - apiKeyId: '', }, 'disabled', channel.revision, @@ -282,8 +272,6 @@ export function createChannelHandlers(deps: ChannelDependencies): { res.status(400).json({ error: 'invalid_slack_token' }); return; } - // Retire legacy unrestricted owner keys before granting a new channel revision. - if (channel.apiKeyId) await deps.deleteApiKey(channel.apiKeyId, channel.createdBy); const [encryptedBotToken, encryptedSigningSecret] = await Promise.all([ encryptCredential(parsed.data.slack.botToken), parsed.data.slack.signingSecret @@ -298,9 +286,8 @@ export function createChannelHandlers(deps: ChannelDependencies): { revision: nanoid(), slack: { ...identity, appId: parsed.data.slack.appId }, encryptedBotToken, - encryptedApiKey: '', + encryptedSigningSecret, - apiKeyId: '', }, channel.status, channel.revision, @@ -320,9 +307,6 @@ export function createChannelHandlers(deps: ChannelDependencies): { } const deleting = await deps.db.beginAgentChannelDeletion(channel.channelId, channel.createdBy); if (!deleting) return void res.status(204).end(); - if (deleting?.apiKeyId) { - await deps.deleteApiKey(deleting.apiKeyId, channel.createdBy); - } const result = await deps.db.deleteAgentChannel(channel.channelId, channel.createdBy); if (result.status === 'pending_drain') { res.status(202).json(publicChannel(deleting)); @@ -424,7 +408,7 @@ export function createChannelHandlers(deps: ChannelDependencies): { res.json({ channel: { ...publicChannel(channel), - revision: channel.revision ?? channel.channelId, + revision: channel.revision, slack, }, credentials: { botToken, signingSecret }, diff --git a/packages/channel-gateway/README.md b/packages/channel-gateway/README.md index 574f4b25e4..7ba8ee3234 100644 --- a/packages/channel-gateway/README.md +++ b/packages/channel-gateway/README.md @@ -18,9 +18,9 @@ endpoints: Socket Mode, disabled optional native/lifecycle emission and a 300-second signature window are the defaults. The signature window accepts 1–300 seconds. Socket reconnect delay accepts 100–60,000 milliseconds and defaults to 5,000; consecutive failed attempts use linear backoff. For webhooks, set `transport: webhook` and `gatewayUrl` to the externally reachable HTTPS gateway base URL. Keep the private application service URL internal. -The gateway loads this versioned policy with its private channel bundle before opening its listener or selecting ingress. Deploy matching application and gateway versions: policy version 1 is required, and absent or unsupported versions fail closed. The four ingress/security/feature fields remain required; an older version-1 bundle may omit only the additive reconnect delay and receives its default. Configure `CHANNEL_SERVICE_TOKEN`, `CHANNEL_TENANT_ID`, `CHANNEL_ID`, `LIBRECHAT_INTERNAL_URL` and, for Socket Mode, `SLACK_APP_TOKEN` in the gateway environment. These are process bindings and secrets. `APP_TITLE`, listening settings, provider/service addresses and process request timeouts also remain deployment settings. +The gateway loads this versioned policy with its private channel bundle before opening its listener or selecting ingress. Deploy matching application and gateway versions: policy version 1 is required, and absent or unsupported versions fail closed. The private policy includes ingress, signature tolerance, optional effects, reconnect pacing and the server delivery budget. Configure `CHANNEL_SERVICE_TOKEN`, `CHANNEL_TENANT_ID`, `CHANNEL_ID`, `LIBRECHAT_INTERNAL_URL` and, for Socket Mode, `SLACK_APP_TOKEN` in the gateway environment. These are process bindings and secrets. `APP_TITLE`, listening settings, provider/service addresses and process request timeouts also remain deployment settings. -Remove legacy `SLACK_NATIVE_SESSIONS`, `SLACK_LIFECYCLE_REACTIONS` and `SLACK_SIGNATURE_TOLERANCE_SECS` environment variables; their presence now fails startup instead of overriding the server. Use the YAML fields above. `SLACK_TRANSPORT` is optional and only asserts the server-selected mode; a mismatch fails startup. Restart gateway replicas after changing ingress mode or the Slack app identity used by Socket Mode. A replica which observes a changed mode or Socket app binding rejects that delivery and becomes unhealthy. Workers read optional feature policy from the current private bundle, while already-frozen delivery plans retain their original operations. +Configure gateway feature and signature policy through the YAML fields above; process environment variables cannot override it. `SLACK_TRANSPORT` is optional and only asserts the server-selected mode; a mismatch fails startup. Restart gateway replicas after changing ingress mode or the Slack app identity used by Socket Mode. A replica which observes a changed mode or Socket app binding rejects that delivery and becomes unhealthy. Workers read optional feature policy from the current private bundle, while already-frozen delivery plans retain their original operations. ## Install a webhook app @@ -148,3 +148,5 @@ A document has at most 40 components and the final answer must fit the 128,000-b The gateway freezes versioned blocks and text together before posting. Retries replay that plan. A definitive Slack `invalid_blocks` or `invalid_attachments` rejection permits a text-only retry; ambiguous network outcomes retain the existing reconciliation behavior. Clients that cannot render layouts receive the generated stable fallback blocks. This is rich **message** support. Existing app-owned Stop, approval, question and feedback workflows remain responsible for interactions. New input forms, App Home, modals, Work Objects, plan/task execution controls and embedded video are not exposed to Agent output. Authorized file attachments continue through the existing artifact delivery path. Slack may limit newer blocks by client or app availability; automated tests cover SDK payloads and rejection fallback, while live rendering requires a configured Slack workspace. + +The initial gateway renderer uses one persisted format (`slack-structured-v1`). Saved current-format messages replay without rewriting their contents. Experimental pre-release formats and process-setting aliases are not supported; reset experimental test data before using this contract. diff --git a/packages/channel-gateway/src/app.spec.ts b/packages/channel-gateway/src/app.spec.ts index 614d57d766..9e6fe25328 100644 --- a/packages/channel-gateway/src/app.spec.ts +++ b/packages/channel-gateway/src/app.spec.ts @@ -11,6 +11,7 @@ import { SlackClient } from './slack'; import { GatewayApp } from './app'; const bundle: ChannelBundle = { + delivery: { maxDeliveryParts: 100 }, gateway: { version: 1, ...DEFAULT_CHANNEL_GATEWAY_POLICY, transport: 'webhook' }, channel: { channelId: 'binding', diff --git a/packages/channel-gateway/src/client.spec.ts b/packages/channel-gateway/src/client.spec.ts index 60bae5e7ab..73b578074b 100644 --- a/packages/channel-gateway/src/client.spec.ts +++ b/packages/channel-gateway/src/client.spec.ts @@ -34,6 +34,7 @@ const response = { status: 'in_progress', }; const bundle: ChannelBundle = { + delivery: { maxDeliveryParts: 100 }, channel: { channelId: 'channel-1', tenantId: 'tenant-1', @@ -82,7 +83,7 @@ describe('ChannelClient service boundary', () => { 'Unsupported channel gateway policy', ); }); - test.each([0, 101, 1.5, '1', null])( + test.each([undefined, 0, 101, 1.5, '1', null])( 'rejects malformed delivery budget %p', async (maxDeliveryParts) => { const transport = jest @@ -93,6 +94,14 @@ describe('ChannelClient service boundary', () => { ); }, ); + test('requires the complete current private bundle', async () => { + const transport = jest + .fn, Parameters>() + .mockResolvedValue(json({ ...bundle, delivery: undefined })); + await expect(new ChannelClient(config, transport).channel()).rejects.toThrow( + 'Invalid channel delivery policy', + ); + }); test('reads the server-selected reduced delivery budget', async () => { const transport = jest .fn, Parameters>() diff --git a/packages/channel-gateway/src/client.ts b/packages/channel-gateway/src/client.ts index 3c8cd2208f..649bc94bee 100644 --- a/packages/channel-gateway/src/client.ts +++ b/packages/channel-gateway/src/client.ts @@ -168,7 +168,10 @@ export class ChannelClient { if (!policy.success) throw new Error('Unsupported channel gateway policy'); const delivery = channelStorageConfigSchema .pick({ maxDeliveryParts: true }) - .safeParse(value.delivery === undefined ? {} : value.delivery); + .extend({ + maxDeliveryParts: channelStorageConfigSchema.shape.maxDeliveryParts.removeDefault(), + }) + .safeParse(value.delivery); if (!delivery.success) throw new Error('Invalid channel delivery policy'); return { ...value, gateway: policy.data, delivery: delivery.data }; } diff --git a/packages/channel-gateway/src/config.spec.ts b/packages/channel-gateway/src/config.spec.ts index d7578ae799..65fdd5687d 100644 --- a/packages/channel-gateway/src/config.spec.ts +++ b/packages/channel-gateway/src/config.spec.ts @@ -25,31 +25,21 @@ describe('gateway configuration', () => { ).toBe(channelConfigSchema.parse({}).transport); }); - test('older v1 policies default only reconnect pacing and newer ones honor the override', () => { + test('operator configuration defaults reconnect pacing and honors an explicit override', () => { const bootstrap = loadConfig({ ...required, SLACK_APP_TOKEN: 'test-app-token' }); - const older = { + const policy = { transport: 'socket_mode' as const, nativeSessions: false, lifecycleReactions: false, signatureToleranceSeconds: 300, }; - expect(resolveGatewayConfig(bootstrap, older).socketReconnectDelayMs).toBe(5000); + expect(resolveGatewayConfig(bootstrap, policy).socketReconnectDelayMs).toBe(5000); expect( - resolveGatewayConfig(bootstrap, { ...older, socketReconnectDelayMs: 100 }) + resolveGatewayConfig(bootstrap, { ...policy, socketReconnectDelayMs: 100 }) .socketReconnectDelayMs, ).toBe(100); }); - test('supports legacy binding aliases and equal canonical/legacy values', () => { - const legacy = { - CHANNEL_SERVICE_TOKEN: required.CHANNEL_SERVICE_TOKEN, - SLACK_SOCKET_TENANT_ID: 'tenant-1', - SLACK_SOCKET_CHANNEL_ID: 'channel-1', - }; - expect(loadConfig(legacy)).toMatchObject(required); - expect(loadConfig({ ...legacy, ...required })).toMatchObject(required); - }); - test.each(['CHANNEL_TENANT_ID', 'CHANNEL_ID'])( 'never falls back from an explicitly empty canonical %s', (name) => { @@ -64,16 +54,7 @@ describe('gateway configuration', () => { }, ); - test('conflicting bindings fail without disclosing either identifier', () => { - expect(() => - loadConfig({ ...required, SLACK_SOCKET_CHANNEL_ID: 'private-other-channel' }), - ).toThrow('Conflicting channel bindings'); - }); - - test('requires durable actor-bound execution and Socket Mode app token', () => { - expect(() => loadConfig({ ...required, DURABLE_RUNS_ENABLED: 'false' })).toThrow( - 'Actor-bound durable execution is required', - ); + test('requires a Socket Mode app token', () => { expect(() => resolveGatewayConfig(loadConfig(required), DEFAULT_CHANNEL_GATEWAY_POLICY), ).toThrow('SLACK_APP_TOKEN is required'); diff --git a/packages/channel-gateway/src/config.ts b/packages/channel-gateway/src/config.ts index 02da343132..166c9edaab 100644 --- a/packages/channel-gateway/src/config.ts +++ b/packages/channel-gateway/src/config.ts @@ -48,20 +48,7 @@ export function loadConfig(env: NodeJS.ProcessEnv): GatewayConfig { throw new Error( 'Configure gateway features and signature policy in librechat.yaml channel settings', ); - const input = { - ...env, - CHANNEL_TENANT_ID: env.CHANNEL_TENANT_ID ?? env.SLACK_SOCKET_TENANT_ID, - CHANNEL_ID: env.CHANNEL_ID ?? env.SLACK_SOCKET_CHANNEL_ID, - }; - for (const [name, alias] of [ - ['CHANNEL_TENANT_ID', 'SLACK_SOCKET_TENANT_ID'], - ['CHANNEL_ID', 'SLACK_SOCKET_CHANNEL_ID'], - ]) { - if (env[name] !== undefined && env[alias] && env[name] !== env[alias]) - throw new Error('Conflicting channel bindings'); - } - if (env.DURABLE_RUNS_ENABLED === 'false') - throw new Error('Actor-bound durable execution is required'); + const input = env; const parsed = environment.safeParse(input); if (!parsed.success) throw new Error( diff --git a/packages/channel-gateway/src/events.spec.ts b/packages/channel-gateway/src/events.spec.ts index 91152c06b4..4064d9347b 100644 --- a/packages/channel-gateway/src/events.spec.ts +++ b/packages/channel-gateway/src/events.spec.ts @@ -10,6 +10,7 @@ import { } from './events'; const bundle: ChannelBundle = { + delivery: { maxDeliveryParts: 100 }, channel: { channelId: 'binding', tenantId: 'tenant', diff --git a/packages/channel-gateway/src/identity.spec.ts b/packages/channel-gateway/src/identity.spec.ts index f3349a45ef..baa224bffc 100644 --- a/packages/channel-gateway/src/identity.spec.ts +++ b/packages/channel-gateway/src/identity.spec.ts @@ -12,6 +12,7 @@ const config = loadConfig({ CHANNEL_ID: 'binding', }); const bundle: ChannelBundle = { + delivery: { maxDeliveryParts: 100 }, gateway: { version: 1, ...DEFAULT_CHANNEL_GATEWAY_POLICY }, channel: { channelId: 'binding', diff --git a/packages/channel-gateway/src/presentation.spec.ts b/packages/channel-gateway/src/presentation.spec.ts index 3134bc9093..9f57ebd8de 100644 --- a/packages/channel-gateway/src/presentation.spec.ts +++ b/packages/channel-gateway/src/presentation.spec.ts @@ -230,7 +230,7 @@ describe('semantic Slack presentations', () => { ); }); - test('rejects duplicate pie labels before freezing new presentations while replaying existing v7 plans', () => { + test('rejects duplicate pie labels in new documents and persisted payloads', () => { const pie = { type: 'chart', title: 'Share', @@ -253,31 +253,9 @@ describe('semantic Slack presentations', () => { text: 'Share: Same 2; Same 1', presentation: { format: 'slack-blocks-v1', payload }, }; - expect(durableSlackMessage(frozen, 'slack-structured-v7').blocks).toEqual(JSON.parse(payload)); - }); - - test('keeps legacy layout labels optional on replay without changing new visible titles', () => { - const plan = makeSlackPlan( - run(document([{ type: 'layout', title: 'Visible title', rows: [[card]] }])), + expect(() => durableSlackMessage(frozen, rendererVersion)).toThrow( + 'Invalid persisted Slack blocks', ); - const part = plan.parts.find((item) => - item.presentation?.payload.includes('is_label_visible'), - )!; - const blocks = JSON.parse(part.presentation!.payload); - expect(blocks[0]).toMatchObject({ - type: 'layout', - label: 'Visible title', - is_label_visible: true, - }); - delete blocks[0].is_label_visible; - const legacy = { - ...part, - presentation: { ...part.presentation!, payload: JSON.stringify(blocks) }, - }; - expect(durableSlackMessage(legacy, 'slack-structured-v7').blocks?.[0]).toMatchObject({ - type: 'layout', - label: 'Visible title', - }); }); test('degrades a layout exceeding nested chart limits without losing its values', () => { @@ -359,7 +337,7 @@ describe('semantic Slack presentations', () => { test('replays frozen v6 plans without stripping their footer and rejects modified v7 payloads', () => { const plan = makeSlackPlan(run('Ordinary reply')); - expect(durableSlackMessage(plan.parts[0], 'slack-structured-v6').text).toBe(plan.parts[0].text); + expect(durableSlackMessage(plan.parts[0], 'slack-structured-v1').text).toBe(plan.parts[0].text); const rich = makeSlackPlan(run(document([card]))); const part = rich.parts.find((item) => item.presentation?.payload.includes('card'))!; expect(() => diff --git a/packages/channel-gateway/src/presentation.ts b/packages/channel-gateway/src/presentation.ts index 6a368db2f1..3f7813e033 100644 --- a/packages/channel-gateway/src/presentation.ts +++ b/packages/channel-gateway/src/presentation.ts @@ -70,7 +70,8 @@ const pieChart = z.strictObject({ segments: z .array(point.extend({ value: z.number().finite().positive() })) .min(1) - .max(12), + .max(12) + .refine((segments) => unique(segments.map((segment) => segment.label))), }); const chart = z.strictObject({ type: z.literal('data_visualization'), @@ -141,7 +142,7 @@ const layout = z .strictObject({ type: z.literal('layout'), label: characters(150), - is_label_visible: z.literal(true).optional(), + is_label_visible: z.literal(true), rows: z .array( z.strictObject({ @@ -197,10 +198,7 @@ const semanticStable = z.union([semanticText, semanticImage, divider]); const semanticChart = z.strictObject({ type: z.literal('chart'), title: characters(50), - chart: z.union([ - seriesChart, - pieChart.refine((chart) => unique(chart.segments.map((segment) => segment.label))), - ]), + chart: z.union([seriesChart, pieChart]), }); const semanticTable = z .strictObject({ diff --git a/packages/channel-gateway/src/render.spec.ts b/packages/channel-gateway/src/render.spec.ts index dabf6f6d6f..f142216659 100644 --- a/packages/channel-gateway/src/render.spec.ts +++ b/packages/channel-gateway/src/render.spec.ts @@ -272,7 +272,7 @@ describe('Slack answer rendering', () => { ).toBe(true); expect(message.text).toContain('Reply shortened'); expect(Buffer.byteLength(message.text)).toBeLessThan(40000); - expect(supportedRenderer('slack-structured-v5')).toBe(true); + expect(supportedRenderer(rendererVersion)).toBe(true); expect(() => durableSlackMessage( { @@ -280,11 +280,11 @@ describe('Slack answer rendering', () => { presentation: { format: presentationFormat, payload: JSON.stringify([ - { type: 'section', text: { type: 'mrkdwn', text: 'legacy' } }, + { type: 'section', text: { type: 'mrkdwn', text: 'current' } }, ]), }, }, - 'slack-structured-v5', + rendererVersion, ), ).not.toThrow(); }); @@ -509,12 +509,20 @@ describe('immutable Slack plans', () => { expect(() => makeSlackPlan(view, true)).toThrow('must not be regenerated'); }); - it.each(['slack-structured-v2', 'slack-structured-v3', 'slack-structured-v4'])( - 'recovers %s without rerendering', + it.each([ + 'slack-text-v1', + 'slack-structured-v2', + 'slack-structured-v3', + 'slack-structured-v4', + 'slack-structured-v5', + 'slack-structured-v6', + 'slack-structured-v7', + ])( + 'rejects unsupported pre-release renderer %s instead of rewriting its saved payload', (version) => { const part = makeSlackPlan(run()).parts[0]; - expect(durableSlackMessage(part, version).text).toBe(part.text); - expect(supportedRenderer(version)).toBe(true); + expect(supportedRenderer(version)).toBe(false); + expect(() => durableSlackMessage(part, version)).toThrow('Unsupported renderer'); }, ); @@ -524,41 +532,7 @@ describe('immutable Slack plans', () => { format: presentationFormat, payload: JSON.stringify([{ type: 'markdown', text: '🙂'.repeat(12000) }]), }; - expect(durableSlackMessage(part, 'slack-structured-v3').blocks).toHaveLength(1); - }); - - it('preserves v5 frozen content even when user text resembles an obsolete generated footer', () => { - const frozen = { - operationId: 'legacy', - kind: 'message' as const, - required: true, - text: 'User content\n\n_This run · ID example_\n', - }; - expect(durableSlackMessage(frozen, 'slack-structured-v5').text).toBe(frozen.text); - expect( - durableSlackMessage( - { - ...frozen, - presentation: { - format: presentationFormat, - payload: JSON.stringify([ - { type: 'section', text: { type: 'mrkdwn', text: frozen.text } }, - ]), - }, - }, - 'slack-structured-v5', - ).text, - ).toBe(frozen.text); - }); - - it('recovers legacy text but refuses structured presentation with a legacy or unknown renderer', () => { - const part = makeSlackPlan(run()).parts[0]; - expect(() => durableSlackMessage(part, 'slack-text-v1')).toThrow(); - expect(() => durableSlackMessage(part, 'future')).toThrow(); - delete part.presentation; - expect(durableSlackMessage(part, 'slack-text-v1')).toEqual({ text: part.text }); - expect(supportedRenderer('slack-text-v1')).toBe(true); - expect(supportedRenderer('future')).toBe(false); + expect(durableSlackMessage(part, rendererVersion).blocks).toHaveLength(1); }); it.each([ @@ -784,33 +758,3 @@ describe('presentation boundaries', () => { }, ); }); - -test('legacy required footer recovery removes embedded trace capability while preserving answer and metrics', () => { - const url = 'https://chat.example.com/c/session'; - const part = { - operationId: 'old:reply:0', - kind: 'message' as const, - required: true, - text: `Answer\n\n_This run · ID old_\n_This run: Cost unavailable_\n<${url}|View session>`, - presentation: { - format: presentationFormat, - payload: JSON.stringify([ - { type: 'section', text: { type: 'mrkdwn', text: 'Answer' } }, - { - type: 'context', - elements: [ - { type: 'mrkdwn', text: `Cost unavailable · Run old · <${url}|View session>` }, - ], - }, - ]), - }, - }; - const snapshot = structuredClone(part); - for (const version of ['slack-structured-v2', 'slack-structured-v3', 'slack-structured-v4']) { - const message = durableSlackMessage(part, version); - expect(JSON.stringify(message)).not.toContain(url); - expect(message.text).toContain('Answer'); - expect(message.text).toContain('Cost unavailable'); - } - expect(part).toEqual(snapshot); -}); diff --git a/packages/channel-gateway/src/render.ts b/packages/channel-gateway/src/render.ts index 52fba70f7f..2f48f32a04 100644 --- a/packages/channel-gateway/src/render.ts +++ b/packages/channel-gateway/src/render.ts @@ -22,21 +22,12 @@ import { richBlockCount, } from './presentation'; -export const rendererVersion = 'slack-structured-v7'; +export const rendererVersion = 'slack-structured-v1'; export const presentationFormat = 'slack-blocks-v1'; export const artifactFailureNotice = 'One or more attachments could not be confirmed. The answer is preserved; uncertain uploads will not be duplicated automatically.'; const chunkLimit = 39_000; const presentationLimit = 64_000; -const supportedStructured = new Set([ - rendererVersion, - 'slack-structured-v6', - 'slack-structured-v5', - 'slack-structured-v4', - 'slack-structured-v3', - 'slack-structured-v2', -]); - function withinCharacters(value: string, limit: number): boolean { let count = 0; for (let index = 0; index < value.length; index++) { @@ -146,55 +137,19 @@ function presentationFor(message: SlackMessage): ChannelDeliveryPart['presentati } export function supportedRenderer(version: string): boolean { - return version === 'slack-text-v1' || supportedStructured.has(version); -} - -/** Legacy plans embedded session links in required footers; never replay that capability. */ -function legacyMessage(text: string, blocks?: SlackBlock[]): SlackMessage { - const footer = text.lastIndexOf('\n\n_This run · ID '); - const fallback = - footer < 0 - ? text - : text.slice(0, footer) + text.slice(footer).replace(/\n<[^>\r\n]+\|View session>/g, ''); - return { - text: fallback, - ...(blocks - ? { - blocks: blocks.map((block) => - block.type === 'context' - ? { - ...block, - elements: block.elements.map((element) => ({ - ...element, - text: element.text.replace(/ · <[^>\r\n]+\|View session>/g, ''), - })), - } - : block, - ), - } - : {}), - }; + return version === rendererVersion; } export function durableSlackMessage(part: ChannelDeliveryPart, renderer: string): SlackMessage { - const preserve = - renderer === rendererVersion || - renderer === 'slack-structured-v6' || - renderer === 'slack-structured-v5' || - !!part.traceUrl; - if (!part.presentation) { - return preserve ? { text: part.text } : legacyMessage(part.text); - } + if (!supportedRenderer(renderer)) throw new Error('Unsupported renderer'); + if (!part.presentation) return { text: part.text }; if ( - !supportedStructured.has(renderer) || part.presentation.format !== presentationFormat || Buffer.byteLength(part.presentation.payload) > presentationLimit - ) { + ) throw new Error('Unsupported durable presentation'); - } try { - const blocks = blocksSchema.parse(JSON.parse(part.presentation.payload)); - return preserve ? { text: part.text, blocks } : legacyMessage(part.text, blocks); + return { text: part.text, blocks: blocksSchema.parse(JSON.parse(part.presentation.payload)) }; } catch { throw new Error('Invalid persisted Slack blocks'); } diff --git a/packages/channel-gateway/src/review.spec.ts b/packages/channel-gateway/src/review.spec.ts index 61b0decbb2..df7361abae 100644 --- a/packages/channel-gateway/src/review.spec.ts +++ b/packages/channel-gateway/src/review.spec.ts @@ -13,6 +13,7 @@ const config = loadConfig({ CHANNEL_ID: 'binding', }); const bundle: ChannelBundle = { + delivery: { maxDeliveryParts: 100 }, gateway: { version: 1, ...DEFAULT_CHANNEL_GATEWAY_POLICY }, channel: { channelId: 'binding', diff --git a/packages/channel-gateway/src/socket.spec.ts b/packages/channel-gateway/src/socket.spec.ts index 3f82198cc4..20b74e5d67 100644 --- a/packages/channel-gateway/src/socket.spec.ts +++ b/packages/channel-gateway/src/socket.spec.ts @@ -18,6 +18,7 @@ const config = resolveGatewayConfig( DEFAULT_CHANNEL_GATEWAY_POLICY, ); const bundle: ChannelBundle = { + delivery: { maxDeliveryParts: 100 }, gateway: { version: 1, ...DEFAULT_CHANNEL_GATEWAY_POLICY, transport: 'socket_mode' }, channel: { channelId: 'binding', diff --git a/packages/channel-gateway/src/worker.spec.ts b/packages/channel-gateway/src/worker.spec.ts index ae6b26fa5c..bc23cd284e 100644 --- a/packages/channel-gateway/src/worker.spec.ts +++ b/packages/channel-gateway/src/worker.spec.ts @@ -45,6 +45,7 @@ function fixture() { AGENT_INVOKE_TIMEOUT_MS: '2000', }); const bundle: ChannelBundle = { + delivery: { maxDeliveryParts: 100 }, gateway: { version: 1, ...DEFAULT_CHANNEL_GATEWAY_POLICY, transport: 'socket_mode' }, channel: { channelId: config.CHANNEL_ID, diff --git a/packages/channel-gateway/src/worker.ts b/packages/channel-gateway/src/worker.ts index bfc1f6db66..383f699644 100644 --- a/packages/channel-gateway/src/worker.ts +++ b/packages/channel-gateway/src/worker.ts @@ -328,7 +328,7 @@ export class ChannelWorker { plan: makeSlackPlan( run, bundle.gateway.nativeSessions, - bundle.delivery?.maxDeliveryParts, + bundle.delivery.maxDeliveryParts, ), }, signal, diff --git a/packages/data-provider/src/config.ts b/packages/data-provider/src/config.ts index 6ca4d0f677..1008f26c0f 100644 --- a/packages/data-provider/src/config.ts +++ b/packages/data-provider/src/config.ts @@ -1240,7 +1240,6 @@ export const channelGatewayPolicySchema = z.object({ nativeSessions: z.boolean(), lifecycleReactions: z.boolean(), signatureToleranceSeconds: z.number().int().min(1).max(300), - /** Additive v1 pacing setting; omitted older bundles retain the SDK's existing base delay. */ socketReconnectDelayMs: z .number() .int() @@ -1259,6 +1258,7 @@ export const DEFAULT_CHANNEL_GATEWAY_POLICY: ChannelGatewayPolicy = { export const CHANNEL_GATEWAY_POLICY_VERSION = 1; export const channelGatewayWirePolicySchema = channelGatewayPolicySchema.extend({ version: z.literal(CHANNEL_GATEWAY_POLICY_VERSION), + socketReconnectDelayMs: channelGatewayPolicySchema.shape.socketReconnectDelayMs.removeDefault(), }); export type ChannelGatewayWirePolicy = z.infer; diff --git a/packages/data-provider/src/types/channelFeedback.ts b/packages/data-provider/src/types/channelFeedback.ts index 336207c147..f28a2c9f16 100644 --- a/packages/data-provider/src/types/channelFeedback.ts +++ b/packages/data-provider/src/types/channelFeedback.ts @@ -33,8 +33,8 @@ export interface ChannelReplyMapping { } export interface ChannelFeedbackVote { - /** Canonical emoji alias with its skin-tone variant. Absent only in legacy ledgers. */ - reaction?: string; + /** Canonical emoji alias with its skin-tone variant. */ + reaction: string; externalMessageKey: string; rating: ChannelRating; active: boolean; diff --git a/packages/data-provider/src/types/channels.ts b/packages/data-provider/src/types/channels.ts index 64646061bd..f61be6fec1 100644 --- a/packages/data-provider/src/types/channels.ts +++ b/packages/data-provider/src/types/channels.ts @@ -26,7 +26,7 @@ export interface AgentChannel { status: 'pending_install' | 'active' | 'disabled' | 'deleting'; credentialMode: 'shared' | 'per_user'; /** Changes whenever existing identity/execution consent must be re-established. */ - revision?: string; + revision: string; slack?: { teamId: string; appId: string; botUserId: string }; } @@ -64,6 +64,5 @@ export interface ChannelServiceBundle { credentials: { botToken: string; signingSecret?: string }; agent: { id: string; name?: string }; gateway: ChannelGatewayWirePolicy; - /** Absent on older servers: retain the shared default; never infer a replica-local limit. */ - delivery?: Pick; + delivery: Pick; } diff --git a/packages/data-schemas/src/index.ts b/packages/data-schemas/src/index.ts index 5c95e759f7..a3035e64a1 100644 --- a/packages/data-schemas/src/index.ts +++ b/packages/data-schemas/src/index.ts @@ -15,7 +15,6 @@ export { createResponseStorage, sameResponseTerminalReceipt } from './methods/re export type { ResponseOwner, ResponseStorage, - ResponseTerminalGuard, ResponseExecutionGuard, ResponseCleanupIdentity, ResponseReservation, @@ -121,7 +120,6 @@ export { AGENT_TRIGGER_WORKER_CAPABILITY_QUEUED_TURN_V2, AGENT_TRIGGER_WORKER_CAPABILITY_QUEUED_TURN_V3, AGENT_TRIGGER_WORKER_CAPABILITY_RESPONSES_V1, - AGENT_TRIGGER_WORKER_CAPABILITY_RESPONSES_V2, AGENT_TRIGGER_WORKER_CAPABILITY_RESPONSES_RESUME_V1, } from './types/triggerDelivery'; export type * from './types'; diff --git a/packages/data-schemas/src/methods/channel.spec.ts b/packages/data-schemas/src/methods/channel.spec.ts index 27023ad1c6..32ddf08f00 100644 --- a/packages/data-schemas/src/methods/channel.spec.ts +++ b/packages/data-schemas/src/methods/channel.spec.ts @@ -53,7 +53,6 @@ beforeEach(async () => runAsSystem(async () => { for (const name of [ 'AgentChannel', - 'AgentChannelThread', 'ChannelIdentity', 'ChannelScope', 'ChannelRun', @@ -138,27 +137,15 @@ it('waits for an inserting admission before deciding whether deletion can finish it('leaves deletion retryable after dependent erasure fails', () => scope(async () => { await methods.createAgentChannel(channel()); - await mongoose.models.AgentChannelThread.create({ - tenantId, - channelId: 'channel', - externalThreadKey: 'thread', - conversationId: 'legacy-conversation', - }); jest - .spyOn(mongoose.models.AgentChannelThread.collection, 'deleteMany') - .mockRejectedValueOnce(new Error('mapping erasure failed')); + .spyOn(mongoose.models.ChannelReply.collection, 'deleteMany') + .mockRejectedValueOnce(new Error('feedback erasure failed')); await expect(methods.deleteAgentChannel('channel', 'owner')).rejects.toThrow( - 'mapping erasure failed', - ); - expect(await mongoose.models.AgentChannelThread.countDocuments({ channelId: 'channel' })).toBe( - 1, + 'feedback erasure failed', ); expect(await methods.getAgentChannel('channel')).toMatchObject({ status: 'deleting' }); await expect(runs.admit(event(), 100)).rejects.toMatchObject({ code: 'invalid_transition' }); expect(await methods.deleteAgentChannel('channel', 'owner')).toEqual({ status: 'deleted' }); - expect(await mongoose.models.AgentChannelThread.countDocuments({ channelId: 'channel' })).toBe( - 0, - ); })); it('does not close admission when a different owner requests deletion', () => @@ -243,8 +230,6 @@ it('redacts deleted-owner credentials while retaining a retry anchor for other p credentialMode: 'per_user', encryptedBotToken: 'private-bot-token', encryptedSigningSecret: 'private-signing-secret', - encryptedApiKey: 'private-key', - apiKeyId: 'private-key-id', }); await createChannelIdentityStorage(mongoose).saveChannelIdentity( { @@ -269,25 +254,11 @@ it('redacts deleted-owner credentials while retaining a retry anchor for other p }, 100, ); - await mongoose.models.AgentChannelThread.create({ - tenantId, - channelId: 'channel', - externalThreadKey: 'thread', - conversationId: 'legacy-conversation', - }); await createUserMethods(mongoose).deleteUserById(userId); const parent = await methods.getAgentChannel('channel'); expect(parent).toMatchObject({ status: 'deleting' }); - for (const field of [ - 'encryptedBotToken', - 'encryptedSigningSecret', - 'encryptedApiKey', - 'apiKeyId', - ]) + for (const field of ['encryptedBotToken', 'encryptedSigningSecret']) expect(parent).not.toHaveProperty(field); - expect(await mongoose.models.AgentChannelThread.countDocuments({ channelId: 'channel' })).toBe( - 0, - ); expect(await runs.read(tenantId, 'channel', other.run.runId)).not.toBeNull(); await expect( runs.admit({ ...event(), admission: { ...event().admission, eventId: 'late' } }, 101), @@ -299,7 +270,7 @@ it('redacts deleted-owner credentials while retaining a retry anchor for other p expect(await methods.deleteAgentChannel('channel', userId)).toEqual({ status: 'deleted' }); })); -it('keeps account erasure retryable after connection mapping cleanup fails', () => +it('keeps account erasure retryable after connection feedback cleanup fails', () => scope(async () => { const user = await mongoose.models.User.create({ email: 'connection-erasure-retry@example.com', @@ -312,26 +283,13 @@ it('keeps account erasure retryable after connection mapping cleanup fails', () createdBy: userId, encryptedBotToken: 'private-token', }); - await mongoose.models.AgentChannelThread.create({ - tenantId, - channelId: 'channel', - externalThreadKey: 'thread', - conversationId: 'legacy-conversation', - }); jest - .spyOn(mongoose.models.AgentChannelThread.collection, 'deleteMany') - .mockRejectedValueOnce(new Error('mapping store unavailable')); + .spyOn(mongoose.models.ChannelReply.collection, 'deleteMany') + .mockRejectedValueOnce(new Error('feedback store unavailable')); const users = createUserMethods(mongoose); - await expect(users.deleteUserById(userId)).rejects.toThrow('mapping store unavailable'); + await expect(users.deleteUserById(userId)).rejects.toThrow('feedback store unavailable'); expect(await mongoose.models.User.findById(userId).lean()).not.toBeNull(); - expect(await methods.getAgentChannel('channel')).not.toHaveProperty('encryptedBotToken'); - expect(await mongoose.models.AgentChannelThread.countDocuments({ channelId: 'channel' })).toBe( - 1, - ); await expect(users.deleteUserById(userId)).resolves.toMatchObject({ deletedCount: 1 }); - expect(await mongoose.models.AgentChannelThread.countDocuments({ channelId: 'channel' })).toBe( - 0, - ); expect(await methods.getAgentChannel('channel')).toBeNull(); })); @@ -367,13 +325,12 @@ it('requires the retirement operation for deletion transitions and blocks update methods.createAgentChannel({ ...channel(), channelId: 'invalid-create', status: 'deleting' }), ).rejects.toThrow('requires retirement'); expect(await methods.getAgentChannel('invalid-create')).toBeNull(); - await methods.createAgentChannel({ ...channel(), apiKeyId: 'original-key' }); + await methods.createAgentChannel(channel()); expect(await methods.updateAgentChannel('channel', 'owner', { status: 'deleting' })).toBeNull(); expect((await methods.getAgentChannel('channel'))?.status).toBe('active'); await runs.admit(event(), 100); expect(await methods.beginAgentChannelDeletion('channel', 'owner')).toMatchObject({ status: 'deleting', - apiKeyId: 'original-key', }); expect(await methods.deleteAgentChannel('channel', 'owner')).toEqual({ status: 'pending_drain', @@ -389,7 +346,6 @@ it('requires the retirement operation for deletion transitions and blocks update expect( await methods.updateAgentChannel('channel', 'owner', { revision: 'late' }, 'deleting'), ).toBeNull(); - expect(await methods.getAgentChannel('channel')).not.toHaveProperty('encryptedBotToken'); })); it('retires every linked identity before completing direct channel deletion', () => @@ -531,12 +487,12 @@ it.each([tenantId, undefined])( }), ); -it('treats an empty revision as an exact CAS token rather than an absent guard', () => +it('treats a revision as an exact CAS token rather than an absent guard', () => scope(async () => { - await methods.createAgentChannel({ ...channel(), revision: '' }); + await methods.createAgentChannel({ ...channel(), revision: 'opaque-revision' }); const updates = await Promise.all( ['one', 'two', 'three', 'four'].map((revision) => - methods.updateAgentChannel('channel', 'owner', { revision }, undefined, ''), + methods.updateAgentChannel('channel', 'owner', { revision }, undefined, 'opaque-revision'), ), ); const winners = updates.filter((record) => record !== null); diff --git a/packages/data-schemas/src/methods/channel.ts b/packages/data-schemas/src/methods/channel.ts index 5c5fc2baa6..c782d5a526 100644 --- a/packages/data-schemas/src/methods/channel.ts +++ b/packages/data-schemas/src/methods/channel.ts @@ -5,7 +5,6 @@ import type { AgentChannelRecord } from '~/models/channel'; import { retireAgentChannel, initializeChannelScope, touchChannelScope } from './channelScope'; import { eraseChannelFeedbackForChannel } from './channelFeedbackErasure'; import { getTenantId, SYSTEM_TENANT_ID } from '~/config/tenantContext'; -import { createAgentChannelThreadModel } from '~/models/channel'; import { prepareChannelOwnerAdmission } from './channelOwner'; import { createChannelIndexGate } from './channelIndexes'; @@ -21,7 +20,6 @@ export type AgentChannelMetadata = Pick< | 'credentialMode' | 'revision' | 'slack' - | 'apiKeyId' >; export type AgentChannelDeletionResult = @@ -65,9 +63,8 @@ export function createAgentChannelMethods( getStorageConfig: () => Promise> = async () => ({}), ): AgentChannelMethods { const channel = () => mongoose.model('AgentChannel'); - const threads = () => createAgentChannelThreadModel(mongoose); const ensureIndexes = createChannelIndexGate(() => [channel()]); - const secrets = '+encryptedBotToken +encryptedSigningSecret +encryptedApiKey +apiKeyId'; + const secrets = '+encryptedBotToken +encryptedSigningSecret'; async function ownerWrite( createdBy: string, @@ -160,7 +157,6 @@ export function createAgentChannelMethods( 'slack.teamId': 1, 'slack.appId': 1, 'slack.botUserId': 1, - apiKeyId: 1, }) .lean(); }, @@ -227,7 +223,6 @@ export function createAgentChannelMethods( return { status: 'pending_drain' }; // Keep the deleting parent available for a retry if dependent cleanup fails. await eraseChannelFeedbackForChannel(mongoose, deleting.tenantId, channelId); - await threads().deleteMany({ channelId, tenantId: deleting.tenantId || { $in: [null, ''] } }); const result = await channel().deleteOne({ channelId, createdBy, diff --git a/packages/data-schemas/src/methods/channelAuthority.spec.ts b/packages/data-schemas/src/methods/channelAuthority.spec.ts index 90ad01b226..cc1b52146e 100644 --- a/packages/data-schemas/src/methods/channelAuthority.spec.ts +++ b/packages/data-schemas/src/methods/channelAuthority.spec.ts @@ -70,7 +70,6 @@ beforeEach(async () => { await runAsSystem(async () => { for (const name of [ 'AgentChannel', - 'AgentChannelThread', 'ChannelScope', 'ChannelIdentity', 'ChannelRun', @@ -425,30 +424,28 @@ it('serializes linked admission with bulk revocation without creating another ev expect(await mongoose.models.ChannelRun.countDocuments({})).toBe(0); })); -it.each([undefined, ''] as const)( - 'preserves canonical revision fallback semantics for %s', - (revision) => - scope(async () => { - await channels().createAgentChannel({ - tenantId, - channelId: 'legacy', - agentId: 'agent', - createdBy: creatorId, - type: 'slack', - status: 'active', - credentialMode: 'shared', - revision, - slack: { teamId: 'T1', appId: 'A1', botUserId: 'B1' }, - }); - const input = { - ...event(), - channelId: 'legacy', - principal: { ...event().principal, channelRevision: revision ?? 'legacy' }, - }; - await expect(runs().admit(input, Date.now())).resolves.toMatchObject({ duplicate: false }); - await channels().updateAgentChannel('legacy', creatorId, { revision: 'rotated' }); - await expect(runs().admit(input, Date.now())).rejects.toMatchObject({ - code: 'invalid_transition', - }); - }), +it.each(['opaque-revision'] as const)('preserves an explicit opaque revision %s', (revision) => + scope(async () => { + await channels().createAgentChannel({ + tenantId, + channelId: 'legacy', + agentId: 'agent', + createdBy: creatorId, + type: 'slack', + status: 'active', + credentialMode: 'shared', + revision, + slack: { teamId: 'T1', appId: 'A1', botUserId: 'B1' }, + }); + const input = { + ...event(), + channelId: 'legacy', + principal: { ...event().principal, channelRevision: revision }, + }; + await expect(runs().admit(input, Date.now())).resolves.toMatchObject({ duplicate: false }); + await channels().updateAgentChannel('legacy', creatorId, { revision: 'rotated' }); + await expect(runs().admit(input, Date.now())).rejects.toMatchObject({ + code: 'invalid_transition', + }); + }), ); diff --git a/packages/data-schemas/src/methods/channelErasure.ts b/packages/data-schemas/src/methods/channelErasure.ts index 4bbbe37824..f8d25e72bf 100644 --- a/packages/data-schemas/src/methods/channelErasure.ts +++ b/packages/data-schemas/src/methods/channelErasure.ts @@ -1,11 +1,11 @@ import { channelStorageConfigSchema } from 'librechat-data-provider'; import type { ChannelStorageConfig } from 'librechat-data-provider'; import type { ResponseOwner } from '~/models/response'; -import { createAgentChannelModel, createAgentChannelThreadModel } from '~/models/channel'; import { createChannelRunModel, createChannelLaneModel } from '~/models/channelRun'; import { eraseChannelFeedbackForChannel } from './channelFeedbackErasure'; import { createChannelIdentityModel } from '~/models/channelIdentity'; import { createAgentApiKeyModel } from '~/models/agentApiKey'; +import { createAgentChannelModel } from '~/models/channel'; import { createIndexesWithRetry } from '~/utils/retry'; import { retireAgentChannel } from './channelScope'; @@ -22,7 +22,7 @@ export async function eraseChannelPersistenceForUser( owner: ResponseOwner, options: Pick, 'erasureBatchSize'> = {}, ): Promise { - const { erasureBatchSize } = resolveChannelErasureConfig(options); + resolveChannelErasureConfig(options); const tenantId = owner.tenantId || '_'; await createAgentApiKeyModel(mongoose).deleteMany({ userId: owner.userId, @@ -31,26 +31,10 @@ export async function eraseChannelPersistenceForUser( const runs = createChannelRunModel(mongoose); const lanes = createChannelLaneModel(mongoose); const filter = { tenantId, 'principal.userId': owner.userId }; - // New lanes have an indexed owner. Older rows are recovered from the still-retained runs. await lanes.deleteMany({ tenantId, userId: owner.userId }); - const cursor = runs.find(filter).select('laneId').lean().cursor(); - const batch = new Set(); - try { - for await (const run of cursor) { - batch.add(run.laneId); - if (batch.size === erasureBatchSize) { - await lanes.deleteMany({ tenantId, laneId: { $in: [...batch] } }); - batch.clear(); - } - } - if (batch.size) await lanes.deleteMany({ tenantId, laneId: { $in: [...batch] } }); - } finally { - await cursor.close(); - } await runs.deleteMany(filter); await createChannelIdentityModel(mongoose).deleteMany({ tenantId, userId: owner.userId }); const channels = createAgentChannelModel(mongoose); - const threads = createAgentChannelThreadModel(mongoose); await createIndexesWithRetry(channels); const partition = owner.tenantId || { $in: [null, ''] }; const connections = channels @@ -61,10 +45,9 @@ export async function eraseChannelPersistenceForUser( try { for await (const connection of connections) { // Keep redacted deletion metadata while other principals' runs drain. The - // deleted owner's credentials and thread mappings are erased immediately. + // deleted owner's credentials are erased immediately. await retireAgentChannel(mongoose, { ...connection, createdBy: owner.userId }, true); await eraseChannelFeedbackForChannel(mongoose, connection.tenantId, connection.channelId); - await threads.deleteMany({ channelId: connection.channelId, tenantId: partition }); if ( !(await runs.exists({ channelId: connection.channelId, diff --git a/packages/data-schemas/src/methods/channelFeedback.spec.ts b/packages/data-schemas/src/methods/channelFeedback.spec.ts index c721876f15..fa82e303e1 100644 --- a/packages/data-schemas/src/methods/channelFeedback.spec.ts +++ b/packages/data-schemas/src/methods/channelFeedback.spec.ts @@ -225,73 +225,6 @@ it('tightens existing feedback deadlines with canonical conversation retention', expect(await mongoose.models[name].findOne({ tenantId }).lean()).toMatchObject({ expiredAt }); })); -it('repairs bounded legacy feedback retention even when a channel is disabled', () => - scope(async () => { - const expiredAt = new Date(Date.now() + 60000); - await mongoose.models.Message.updateOne( - { messageId: mapping.messageId }, - { $set: { expiredAt } }, - ); - await persist(); - for (const name of ['ChannelReply', 'ChannelFeedbackEvent', 'ChannelFeedback']) - await mongoose.models[name].collection.updateMany( - { tenantId }, - { $set: { expiredAt: null }, $unset: { retentionVersion: '' } }, - ); - await mongoose.models.AgentChannel.updateOne( - { channelId: mapping.channelId }, - { $set: { status: 'disabled' } }, - ); - expect(await storage.repairChannelFeedbackRetention(tenantId, 1)).toBe(3); - for (const name of ['ChannelReply', 'ChannelFeedbackEvent', 'ChannelFeedback']) - expect(await mongoose.models[name].findOne({ tenantId }).lean()).toMatchObject({ - expiredAt, - retentionVersion: 1, - }); - expect(await storage.repairChannelFeedbackRetention(tenantId, 1)).toBe(0); - })); - -it('expires legacy orphan rows without touching another tenant or dropping permanent live rows', () => - scope(async () => { - // Inspect the exact repair deadline before this fixture's TTL worker deletes it. - await mongoose.connection.db!.admin().command({ setParameter: 1, ttlMonitorEnabled: false }); - try { - await persist(); - for (const name of ['ChannelReply', 'ChannelFeedbackEvent', 'ChannelFeedback']) - await mongoose.models[name].collection.updateMany( - { tenantId }, - { $unset: { expiredAt: '', retentionVersion: '' } }, - ); - await mongoose.models.ChannelReply.collection.insertOne({ - ...mapping, - tenantId: 'foreign-retention-tenant', - }); - expect(await storage.repairChannelFeedbackRetention(tenantId, 1)).toBe(3); - for (const name of ['ChannelReply', 'ChannelFeedbackEvent', 'ChannelFeedback']) { - const row = await mongoose.models[name].findOne({ tenantId }).lean<{ expiredAt?: Date }>(); - expect(row).toMatchObject({ retentionVersion: 1 }); - expect(row?.expiredAt).toBeUndefined(); - await mongoose.models[name].collection.updateMany( - { tenantId }, - { $unset: { retentionVersion: '' } }, - ); - } - await mongoose.models.Message.deleteOne({ messageId: mapping.messageId }); - expect(await storage.repairChannelFeedbackRetention(tenantId, 1)).toBe(3); - for (const name of ['ChannelReply', 'ChannelFeedbackEvent', 'ChannelFeedback']) - expect(await mongoose.models[name].findOne({ tenantId }).lean()).toMatchObject({ - expiredAt: new Date(0), - }); - expect( - await mongoose.models.ChannelReply.collection.findOne({ - tenantId: 'foreign-retention-tenant', - }), - ).not.toHaveProperty('retentionVersion'); - } finally { - await mongoose.connection.db!.admin().command({ setParameter: 1, ttlMonitorEnabled: true }); - } - })); - it('metadata backoff cannot modify a current export lease or recreate erased feedback', () => scope(async () => { await persist(); @@ -311,265 +244,6 @@ it('metadata backoff cannot modify a current export lease or recreate erased fee expect(await mongoose.models.ChannelFeedbackEvent.countDocuments({})).toBe(0); })); -it('pins legacy score dates once from ledger creation across lease takeover', () => - scope(async () => { - await persist(); - const original = (await storage.getChannelFeedback(tenantId, mapping.runId))!; - await mongoose.models.ChannelFeedback.updateOne( - { key: mapping.runId }, - { $set: { scoreTimestamp: null } }, - ); - const candidate = (await storage.getChannelFeedback(tenantId, mapping.runId))!; - const now = Date.now(); - const first = (await storage.claimChannelFeedbackExport(candidate, 'first', now, now + 1000))!; - expect(first.scoreTimestamp).toEqual(original.createdAt); - const next = (await storage.claimChannelFeedbackExport(first, 'next', now + 2000, now + 3000))!; - expect(next.scoreTimestamp).toEqual(first.scoreTimestamp); - })); - -it('compacts legacy history in bounded pages while retaining skin-tone and removal checkpoints', () => - scope(async () => { - const inputs = [ - { id: 'base-add', at: 1, reaction: '+1', action: 'added' as const }, - { id: 'base-remove', at: 2, reaction: 'thumbsup', action: 'removed' as const }, - { id: 'old-tone', at: 0, reaction: 'thumbsup::skin-tone-2', action: 'added' as const }, - { id: 'tone-two', at: 3, reaction: '+1::skin-tone-2', action: 'added' as const }, - { id: 'tone-three', at: 4, reaction: 'thumbsup::skin-tone-3', action: 'added' as const }, - { id: 'down', at: 5, reaction: '-1', action: 'added' as const }, - ]; - for (const input of inputs) { - const record = event(); - await storage.admitChannelFeedbackEvent({ - ...record, - eventId: input.id, - digest: input.id, - processed: true, - rating: input.reaction === '-1' ? 'thumbsDown' : 'thumbsUp', - event: { - ...record.event, - eventId: input.id, - eventTimestampUs: input.at, - reaction: input.reaction, - action: input.action, - }, - }); - } - const legacy = { - ...ledger(), - votes: [ - { - externalMessageKey: mapping.externalMessageKey, - rating: 'thumbsUp' as const, - active: false, - eventTimestampUs: 2, - eventId: 'base-remove', - }, - ], - }; - await storage.ensureChannelFeedback(legacy); - await mongoose.models.AgentChannel.updateOne( - { channelId: mapping.channelId }, - { $set: { status: 'disabled' } }, - ); - let compacted = 0; - for (let attempt = 0; attempt < 10; attempt++) { - const count = await storage.compactChannelFeedbackEvents(tenantId, 2); - compacted += count; - if (!count) break; - } - expect(compacted).toBe(6); - expect( - await mongoose.models.ChannelFeedbackEvent.countDocuments({ receiptCompacted: true }), - ).toBe(6); - expect( - await mongoose.models.ChannelFeedbackEvent.countDocuments({ - receiptExpiresAt: { $exists: true }, - }), - ).toBe(2); - await mongoose.models.ChannelFeedbackEvent.deleteMany({ receiptExpiresAt: { $exists: true } }); - const rebuilt = await storage.rebuildVotes(tenantId, mapping.runId, ['base-remove']); - expect(rebuilt).toHaveLength(4); - expect(rebuilt).toEqual( - expect.arrayContaining([ - expect.objectContaining({ reaction: 'thumbsup', active: false }), - expect.objectContaining({ reaction: 'thumbsup::skin-tone-2', active: true }), - expect.objectContaining({ reaction: 'thumbsup::skin-tone-3', active: true }), - expect.objectContaining({ reaction: 'thumbsdown', active: true }), - ]), - ); - expect(await storage.getChannelFeedback(tenantId, mapping.runId)).toMatchObject({ - revision: 0, - votes: legacy.votes, - exportStatus: 'pending', - }); - expect(await storage.isAuthorityCurrent(mapping)).toBe(false); - })); - -it('rechecks checkpoint membership after another legacy compactor initializes the cache', () => - scope(async () => { - for (const [index, id] of ['old', 'current'].entries()) { - const receipt = event(); - await storage.admitChannelFeedbackEvent({ - ...receipt, - eventId: id, - digest: id, - processed: true, - event: { ...receipt.event, eventId: id, eventTimestampUs: index + 1 }, - }); - } - await storage.ensureChannelFeedback({ - ...ledger(), - votes: [ - { - externalMessageKey: mapping.externalMessageKey, - rating: 'thumbsUp', - active: true, - eventTimestampUs: 1, - eventId: 'old', - }, - ], - }); - const events = mongoose.models.ChannelFeedbackEvent; - const find = events.find.bind(events); - // Worker A selects two rows; worker B then initializes the cache and compacts only its first row. - const selection = jest - .spyOn(events, 'find') - .mockImplementationOnce((filter?: mongoose.FilterQuery) => { - const query = find(filter ?? {}); - const execute = query.exec.bind(query); - jest.spyOn(query, 'exec').mockImplementationOnce(async () => { - const rows = await execute(); - expect(await storage.compactChannelFeedbackEvents(tenantId, 1)).toBe(1); - return rows; - }); - return query; - }); - try { - expect(await storage.compactChannelFeedbackEvents(tenantId, 2)).toBe(1); - } finally { - selection.mockRestore(); - } - const rows = await events.find({ tenantId }).lean(); - expect(rows).toHaveLength(2); - for (const row of rows) { - expect(row.receiptCompacted).toBe(true); - expect(row.receiptExpiresAt).toBeUndefined(); - } - expect(await storage.getChannelFeedback(tenantId, mapping.runId)).toMatchObject({ - revision: 0, - exportStatus: 'pending', - }); - })); - -it('retains a bounded latest receipt checkpoint without recreating a missing ledger', () => - scope(async () => { - for (let index = 0; index < 3; index++) { - const record = event(); - await storage.admitChannelFeedbackEvent({ - ...record, - eventId: `orphan-${index}`, - digest: `orphan-${index}`, - processed: true, - event: { ...record.event, eventId: `orphan-${index}`, eventTimestampUs: index }, - }); - } - for (let attempt = 0; attempt < 4; attempt++) - await storage.compactChannelFeedbackEvents(tenantId, 1); - expect(await mongoose.models.ChannelFeedback.countDocuments({})).toBe(0); - expect( - await mongoose.models.ChannelFeedbackEvent.countDocuments({ - receiptExpiresAt: { $exists: true }, - }), - ).toBe(2); - expect( - await mongoose.models.ChannelFeedbackEvent.findOne({ eventId: 'orphan-2' }).lean(), - ).not.toHaveProperty('receiptExpiresAt'); - })); - -it('serializes compaction with a newer canonical projection without expiring its checkpoint', () => - scope(async () => { - const first = { ...event(), processed: true }; - const firstVote = { - ...first.event, - rating: 'thumbsUp' as const, - reaction: 'thumbsup', - active: true, - }; - await storage.admitChannelFeedbackEvent(first); - await storage.ensureChannelFeedback({ ...ledger(), votes: [firstVote] }); - const second = { - ...event(), - eventId: 'new-remove', - digest: 'new-remove', - event: { - ...event().event, - eventId: 'new-remove', - eventTimestampUs: 2, - action: 'removed' as const, - }, - }; - await storage.admitChannelFeedbackEvent(second); - await Promise.all([ - storage.compactChannelFeedbackEvents(tenantId, 10), - storage.withAuthority(mapping, async (active) => { - const previous = (await active.getChannelFeedback(tenantId, mapping.runId))!; - expect( - await active.saveChannelFeedback( - { - ...previous, - revision: previous.revision + 1, - rating: null, - votes: [ - { ...firstVote, eventId: second.eventId, eventTimestampUs: 2, active: false }, - ], - }, - previous.revision, - ), - ).toBe(true); - await active.markChannelFeedbackEvent(second); - }), - ]); - expect(await storage.getChannelFeedback(tenantId, mapping.runId)).toMatchObject({ - revision: 1, - rating: null, - }); - expect( - await mongoose.models.ChannelFeedbackEvent.findOne({ eventId: first.eventId }).lean(), - ).toHaveProperty('receiptExpiresAt'); - expect( - await mongoose.models.ChannelFeedbackEvent.findOne({ eventId: second.eventId }).lean(), - ).not.toHaveProperty('receiptExpiresAt'); - })); - -it('rotates a failed compaction page even after its retry delay expires', () => - scope(async () => { - for (const id of ['failed', 'valid']) { - const source = { ...mapping, runId: id }; - const record = { ...event(source), processed: true }; - await storage.admitChannelFeedbackEvent(record); - await storage.ensureChannelFeedback({ - ...ledger(source), - votes: [{ ...record.event, reaction: 'thumbsup', rating: 'thumbsUp', active: true }], - }); - } - const now = Date.now(); - const failure = jest - .spyOn(mongoose.models.ChannelFeedback, 'findOneAndUpdate') - .mockImplementationOnce(() => { - throw new Error('compaction unavailable'); - }); - await expect(storage.compactChannelFeedbackEvents(tenantId, 1, now, 1000)).rejects.toThrow( - 'compaction unavailable', - ); - failure.mockRestore(); - expect(await storage.compactChannelFeedbackEvents(tenantId, 1, now + 60000, 1000)).toBe(1); - expect( - await mongoose.models.ChannelFeedbackEvent.findOne({ eventId: 'valid' }).lean(), - ).toMatchObject({ receiptCompacted: true }); - expect(await storage.compactChannelFeedbackEvents(tenantId, 1, now + 60000, 1000)).toBe(1); - await expect(storage.compactChannelFeedbackEvents('foreign-tenant', 1)).rejects.toThrow(); - })); - it('erases all owner feedback and rejects delayed standalone writes after actual account deletion', () => scope(async () => { await persist(); @@ -716,45 +390,6 @@ it('propagates database outages and rejects foreign tenant context without claim } })); -it('rebuilds canonical votes without merging skin tones and uses the latest event on ties', () => - scope(async () => { - const inputs = [ - { id: 'base-a-add', reaction: '+1', action: 'added', at: 20 }, - { id: 'base-z-remove', reaction: 'thumbsup', action: 'removed', at: 20 }, - { id: 'tone-two', reaction: '+1::skin-tone-2', action: 'added', at: 30 }, - { id: 'tone-three', reaction: 'thumbsup::skin-tone-3', action: 'added', at: 40 }, - { id: 'down', reaction: '-1', action: 'added', at: 50 }, - ] as const; - for (const input of inputs) { - const record = event(); - await storage.admitChannelFeedbackEvent({ - ...record, - eventId: input.id, - digest: input.id, - rating: input.reaction === '-1' ? 'thumbsDown' : 'thumbsUp', - event: { - ...record.event, - eventId: input.id, - reaction: input.reaction, - action: input.action, - eventTimestampUs: input.at, - }, - }); - } - const ids = inputs.map((input) => input.id); - const votes = await storage.rebuildVotes(tenantId, mapping.runId, ids); - expect(votes).toHaveLength(4); - expect(votes).toEqual( - expect.arrayContaining([ - expect.objectContaining({ reaction: 'thumbsup', active: false, eventId: 'base-z-remove' }), - expect.objectContaining({ reaction: 'thumbsup::skin-tone-2', active: true }), - expect.objectContaining({ reaction: 'thumbsup::skin-tone-3', active: true }), - expect.objectContaining({ reaction: 'thumbsdown', rating: 'thumbsDown', active: true }), - ]), - ); - expect(await storage.rebuildVotes(tenantId, mapping.runId, [...ids, 'missing'])).toBeNull(); - })); - it('erases imported-conversation feedback while preserving another owner in that conversation', () => scope(async () => { const other = { diff --git a/packages/data-schemas/src/methods/channelFeedback.ts b/packages/data-schemas/src/methods/channelFeedback.ts index a69da43eeb..161c50a292 100644 --- a/packages/data-schemas/src/methods/channelFeedback.ts +++ b/packages/data-schemas/src/methods/channelFeedback.ts @@ -1,26 +1,19 @@ -import type { - ChannelReplyMapping, - ChannelFeedbackActor, - ChannelFeedbackVote, -} from 'librechat-data-provider'; +import type { ChannelReplyMapping, ChannelFeedbackActor } from 'librechat-data-provider'; import type { ClientSession } from 'mongoose'; import type { ChannelFeedbackLedger, ChannelFeedbackEventRecord } from '~/models/channelFeedback'; import type { ChannelFeedbackReplySource } from './channelFeedbackSource'; import type { ChannelFeedbackSourceState } from './channelFeedbackScope'; -import type { FeedbackCheckpoint } from './channelFeedbackCompaction'; +import type { FeedbackCheckpoint } from './channelFeedbackCheckpoint'; import { withChannelFeedbackAuthority, sameFeedbackSource, - readChannelFeedbackRetention, ChannelFeedbackAuthorityError, } from './channelFeedbackScope'; import { feedbackCheckpoint, pinFeedbackCheckpoint, acknowledgeFeedbackReceipt, - compactFeedbackReceipts, -} from './channelFeedbackCompaction'; -import { CHANNEL_FEEDBACK_VOTE_SLOTS_HARD_MAX } from './channelFeedbackProtocol'; +} from './channelFeedbackCheckpoint'; import { readChannelFeedbackReplySource } from './channelFeedbackSource'; import { createChannelIndexGate } from './channelIndexes'; import { assertChannelTenant } from './channelScope'; @@ -36,13 +29,6 @@ export { isLaterChannelFeedbackEvent, } from './channelFeedbackProtocol'; export interface ChannelFeedbackStorage { - repairChannelFeedbackRetention(tenantId: string, limit: number): Promise; - compactChannelFeedbackEvents( - tenantId: string, - limit: number, - now?: number, - retryMs?: number, - ): Promise; deferChannelFeedbackEvent(record: ChannelFeedbackEventRecord, retryAtMs: number): Promise; deferChannelFeedbackExport( record: ChannelFeedbackLedger, @@ -60,11 +46,6 @@ export interface ChannelFeedbackStorage { actor: ChannelFeedbackActor, externalMessageKey: string, ): Promise; - rebuildVotes( - tenantId: string, - ledgerKey: string, - requiredEventIds: string[], - ): Promise; putChannelReply(mapping: ChannelReplyMapping): Promise; getChannelReply( actor: ChannelFeedbackActor, @@ -145,90 +126,12 @@ export function createChannelFeedbackStorage( function retainedInsert(record: object, expiredAt: Date | null) { const insert = { ...record } as Record; delete insert.expiredAt; - delete insert.retentionVersion; return { $setOnInsert: insert, - $set: { retentionVersion: 1 }, ...(expiredAt ? { $min: { expiredAt } } : {}), }; } - async function retainLegacyNull( - name: string, - filter: object, - expiredAt: Date | null, - active: ClientSession, - ) { - if (expiredAt) - await mongoose - .model(name) - .updateOne({ ...filter, expiredAt: null }, { $set: { expiredAt } }, { session: active }); - } const storage: ChannelFeedbackStorage = { - async compactChannelFeedbackEvents(tenantId, limit, now = Date.now(), retryMs = 5000) { - page(tenantId, limit); - if (!Number.isSafeInteger(now) || !Number.isSafeInteger(retryMs) || retryMs < 1) - throw new TypeError('Invalid feedback compaction clock'); - await ensureIndexes(); - return compactFeedbackReceipts(mongoose, tenantId, limit, now, retryMs, (key, active) => - createStorage(active).rebuildVotes(tenantId, key, []), - ); - }, - async repairChannelFeedbackRetention(tenantId, limit) { - page(tenantId, limit); - await ensureIndexes(); - let repaired = 0; - const retention = new Map(); - for (const name of ['ChannelReply', 'ChannelFeedbackEvent', 'ChannelFeedback']) { - const model = mongoose.model(name); - const rows = await model - .find({ tenantId, retentionVersion: { $ne: 1 } }) - .select( - name === 'ChannelReply' - ? 'tenantId principalId conversationId messageId' - : 'mapping.tenantId mapping.principalId mapping.conversationId mapping.messageId', - ) - .sort({ _id: 1 }) - .limit(limit) - .lean(); - for (const row of rows) { - const mapping = (name === 'ChannelReply' ? row : row.mapping) as ChannelReplyMapping; - const key = JSON.stringify([ - mapping.tenantId, - mapping.principalId, - mapping.conversationId, - mapping.messageId, - ]); - if (!retention.has(key)) { - try { - retention.set(key, await readChannelFeedbackRetention(mongoose, mapping)); - } catch (error) { - if (!(error instanceof ChannelFeedbackAuthorityError)) throw error; - retention.set(key, new Date(0)); - } - } - const expiredAt = retention.get(key)!; - const filter = { _id: row._id, tenantId, retentionVersion: { $ne: 1 } }; - const result = await model.updateOne( - { - ...filter, - ...(expiredAt - ? { $or: [{ expiredAt: null }, { expiredAt: { $gt: expiredAt } }] } - : {}), - }, - { $set: { retentionVersion: 1, ...(expiredAt ? { expiredAt } : {}) } }, - ); - repaired += result.modifiedCount; - if (expiredAt && !result.modifiedCount) { - const retained = await model.updateOne( - { ...filter, expiredAt: { $lte: expiredAt } }, - { $set: { retentionVersion: 1 } }, - ); - repaired += retained.modifiedCount; - } - } - } - return repaired; - }, async deferChannelFeedbackEvent(record, retryAtMs) { assertChannelTenant(record.tenantId); if (!Number.isSafeInteger(retryAtMs) || retryAtMs < 0) @@ -285,64 +188,6 @@ export function createChannelFeedbackStorage( } }, readReplySource: (actor, key) => readChannelFeedbackReplySource(mongoose, actor, key), - async rebuildVotes(tenantId, ledgerKey, requiredEventIds) { - assertChannelTenant(tenantId); - if (requiredEventIds.length > CHANNEL_FEEDBACK_VOTE_SLOTS_HARD_MAX) - throw new TypeError('Invalid feedback vote footprint'); - const ids = [...new Set(requiredEventIds)]; - if ( - (await events() - .countDocuments({ tenantId, ledgerKey, eventId: { $in: ids } }) - .session(session ?? null)) !== ids.length - ) - return null; - return events() - .aggregate([ - { - $match: { - tenantId, - ledgerKey, - 'event.reaction': { $regex: '^(\\+1|thumbsup|-1|thumbsdown)(::skin-tone-[2-6])?$' }, - }, - }, - { $sort: { 'event.eventTimestampUs': -1, 'event.eventId': -1 } }, - { - $addFields: { - reaction: { - $replaceOne: { - input: { - $replaceOne: { - input: '$event.reaction', - find: '+1', - replacement: 'thumbsup', - }, - }, - find: '-1', - replacement: 'thumbsdown', - }, - }, - }, - }, - { - $group: { - _id: { externalMessageKey: '$event.externalMessageKey', reaction: '$reaction' }, - vote: { - $first: { - externalMessageKey: '$event.externalMessageKey', - reaction: '$reaction', - rating: '$rating', - active: { $eq: ['$event.action', 'added'] }, - eventId: '$event.eventId', - eventTimestampUs: '$event.eventTimestampUs', - }, - }, - }, - }, - { $limit: CHANNEL_FEEDBACK_VOTE_SLOTS_HARD_MAX + 1 }, - { $replaceRoot: { newRoot: '$vote' } }, - ]) - .session(session ?? null); - }, async putChannelReply(mapping) { return guard(mapping, async (active, expiredAt) => { const filter = { @@ -355,7 +200,7 @@ export function createChannelFeedbackStorage( upsert: true, session: active, }); - await retainLegacyNull('ChannelReply', filter, expiredAt, active); + const stored = await replies().findOne(filter).session(active).lean(); if (!stored) throw new Error('Channel reply disappeared during admission'); return stored; @@ -386,7 +231,7 @@ export function createChannelFeedbackStorage( upsert: true, session: active, }); - await retainLegacyNull('ChannelFeedbackEvent', filter, expiredAt, active); + const stored = await events().findOne(filter).session(active).lean(); if (!stored) throw new Error('Channel feedback event disappeared during admission'); return stored; @@ -439,7 +284,7 @@ export function createChannelFeedbackStorage( ), { upsert: true, session: active }, ); - await retainLegacyNull('ChannelFeedback', filter, expiredAt, active); + const stored = await ledger().findOne(filter).session(active).lean(); if (!stored) throw new Error('Channel feedback ledger disappeared during admission'); remember(stored); @@ -485,20 +330,8 @@ export function createChannelFeedbackStorage( ) throw new TypeError('Invalid feedback lease'); return guard(candidate.mapping, async (active) => { - if (!candidate.scoreTimestamp) { - const scoreTimestamp = candidate.createdAt ?? new Date(); - if (!Number.isFinite(scoreTimestamp.getTime())) - throw new TypeError('Invalid feedback score timestamp'); - await ledger().updateOne( - { - tenantId: candidate.tenantId, - key: candidate.key, - scoreTimestamp: null, - }, - { $set: { scoreTimestamp } }, - { session: active, timestamps: false }, - ); - } + if (!candidate.scoreTimestamp || !Number.isFinite(candidate.scoreTimestamp.getTime())) + throw new TypeError('Invalid feedback score timestamp'); return ledger() .findOneAndUpdate( { diff --git a/packages/data-schemas/src/methods/channelFeedbackCheckpoint.ts b/packages/data-schemas/src/methods/channelFeedbackCheckpoint.ts new file mode 100644 index 0000000000..b7a900e889 --- /dev/null +++ b/packages/data-schemas/src/methods/channelFeedbackCheckpoint.ts @@ -0,0 +1,135 @@ +import type { ChannelReplyMapping, ChannelFeedbackVote } from 'librechat-data-provider'; +import type { ClientSession } from 'mongoose'; +import type { ChannelFeedbackLedger, ChannelFeedbackEventRecord } from '~/models/channelFeedback'; +import { + CHANNEL_FEEDBACK_RECEIPT_RETENTION_MS, + CHANNEL_FEEDBACK_VOTE_SLOTS_HARD_MAX, + channelFeedbackReactionIdentity, + isLaterChannelFeedbackEvent, +} from './channelFeedbackProtocol'; +import { sameFeedbackSource, ChannelFeedbackAuthorityError } from './channelFeedbackScope'; + +export interface FeedbackCheckpoint { + key: string; + revision: number; + mapping: ChannelReplyMapping; + ids: Set; + slots: Map; +} +const slotKey = (message: string, reaction: string) => + JSON.stringify([message, channelFeedbackReactionIdentity(reaction)]); + +/** A transaction-local snapshot, never retained by the shared storage instance. */ +export function feedbackCheckpoint(record: ChannelFeedbackLedger): FeedbackCheckpoint { + if (record.votes.length > CHANNEL_FEEDBACK_VOTE_SLOTS_HARD_MAX) + throw new TypeError('Invalid feedback checkpoint footprint'); + return { + key: record.key, + revision: record.revision, + mapping: { ...record.mapping }, + ids: new Set(record.votes.map((vote) => vote.eventId)), + slots: new Map( + record.votes.map((vote) => [slotKey(vote.externalMessageKey, vote.reaction), { ...vote }]), + ), + }; +} + +/** Only rows already classified as pinned can be superseded inline: this is bounded by vote slots. */ +export async function pinFeedbackCheckpoint( + mongoose: typeof import('mongoose'), + record: ChannelFeedbackLedger, + session: ClientSession, +): Promise { + const checkpoint = feedbackCheckpoint(record); + const model = mongoose.model('ChannelFeedbackEvent'); + const filter = { tenantId: record.tenantId, ledgerKey: record.key, processed: true }; + await model.updateMany( + { ...filter, eventId: { $in: [...checkpoint.ids] }, receiptExpiresAt: { $exists: true } }, + { $unset: { receiptExpiresAt: '' } }, + { session, timestamps: false }, + ); + await model.updateMany( + { + ...filter, + receiptExpiresAt: null, + eventId: { $nin: [...checkpoint.ids] }, + }, + { $set: { receiptExpiresAt: new Date(Date.now() + CHANNEL_FEEDBACK_RECEIPT_RETENTION_MS) } }, + { session, timestamps: false }, + ); +} + +async function expireReceipts( + mongoose: typeof import('mongoose'), + filter: object, + expiresAt: Date, + session: ClientSession, + set: { processed: true }, +): Promise { + const model = mongoose.model('ChannelFeedbackEvent'); + const first = await model.updateMany( + { ...filter, $or: [{ receiptExpiresAt: null }, { receiptExpiresAt: { $gt: expiresAt } }] }, + { $set: { ...set, receiptExpiresAt: expiresAt } }, + { session, timestamps: false }, + ); + const earlier = await model.updateMany( + { ...filter, receiptExpiresAt: { $lte: expiresAt } }, + { $set: set }, + { session, timestamps: false }, + ); + return first.modifiedCount + earlier.modifiedCount; +} + +/** Checkpoint and receipt acknowledgement share one transaction and ledger serialization anchor. */ +export async function acknowledgeFeedbackReceipt( + mongoose: typeof import('mongoose'), + record: ChannelFeedbackEventRecord, + checkpoint: FeedbackCheckpoint, + session: ClientSession, +): Promise { + if ( + record.tenantId !== record.mapping.tenantId || + record.channelId !== record.mapping.channelId || + checkpoint.key !== record.ledgerKey || + !sameFeedbackSource(checkpoint.mapping, record.mapping) + ) + throw new ChannelFeedbackAuthorityError(); + const anchored = await mongoose + .model('ChannelFeedback') + .updateOne( + { tenantId: record.tenantId, key: checkpoint.key, revision: checkpoint.revision }, + { $inc: { receiptEpoch: 1 } }, + { session, timestamps: false }, + ); + if (!anchored.matchedCount) throw new Error('Feedback checkpoint changed before acknowledgement'); + const filter = { + tenantId: record.tenantId, + channelId: record.channelId, + eventId: record.eventId, + digest: record.digest, + }; + const model = mongoose.model('ChannelFeedbackEvent'); + const current = checkpoint.slots.get( + slotKey(record.event.externalMessageKey, record.event.reaction), + ); + if ( + !current || + (current.eventId !== record.eventId && !isLaterChannelFeedbackEvent(current, record.event)) + ) + throw new Error('Feedback acknowledgement precedes its canonical checkpoint'); + if (current.eventId === record.eventId) { + await model.updateOne( + filter, + { $set: { processed: true }, $unset: { receiptExpiresAt: '' } }, + { session }, + ); + } else { + await expireReceipts( + mongoose, + filter, + new Date(Date.now() + CHANNEL_FEEDBACK_RECEIPT_RETENTION_MS), + session, + { processed: true }, + ); + } +} diff --git a/packages/data-schemas/src/methods/channelFeedbackCompaction.ts b/packages/data-schemas/src/methods/channelFeedbackCompaction.ts deleted file mode 100644 index e55a0ef8d5..0000000000 --- a/packages/data-schemas/src/methods/channelFeedbackCompaction.ts +++ /dev/null @@ -1,269 +0,0 @@ -import type { ChannelReplyMapping, ChannelFeedbackVote } from 'librechat-data-provider'; -import type { ClientSession, Types } from 'mongoose'; -import type { ChannelFeedbackLedger, ChannelFeedbackEventRecord } from '~/models/channelFeedback'; -import { - CHANNEL_FEEDBACK_RECEIPT_RETENTION_MS, - CHANNEL_FEEDBACK_VOTE_SLOTS_HARD_MAX, - channelFeedbackReactionIdentity, - isLaterChannelFeedbackEvent, -} from './channelFeedbackProtocol'; -import { sameFeedbackSource, ChannelFeedbackAuthorityError } from './channelFeedbackScope'; - -export interface FeedbackCheckpoint { - key: string; - revision: number; - mapping: ChannelReplyMapping; - canonical: boolean; - ids: Set; - slots: Map; -} -const slotKey = (message: string, reaction: string) => - JSON.stringify([message, channelFeedbackReactionIdentity(reaction)]); - -/** A transaction-local snapshot, never retained by the shared storage instance. */ -export function feedbackCheckpoint(record: ChannelFeedbackLedger): FeedbackCheckpoint { - if (record.votes.length > CHANNEL_FEEDBACK_VOTE_SLOTS_HARD_MAX) - throw new TypeError('Invalid feedback checkpoint footprint'); - return { - key: record.key, - revision: record.revision, - mapping: { ...record.mapping }, - canonical: record.votes.every((vote) => Boolean(vote.reaction)), - ids: new Set(record.votes.map((vote) => vote.eventId)), - slots: new Map( - record.votes - .filter((vote) => vote.reaction) - .map((vote) => [slotKey(vote.externalMessageKey, vote.reaction!), { ...vote }]), - ), - }; -} - -/** Only rows already classified as pinned can be superseded inline: this is bounded by vote slots. */ -export async function pinFeedbackCheckpoint( - mongoose: typeof import('mongoose'), - record: ChannelFeedbackLedger, - session: ClientSession, -): Promise { - const checkpoint = feedbackCheckpoint(record); - if (!checkpoint.canonical) return; - const model = mongoose.model('ChannelFeedbackEvent'); - const filter = { tenantId: record.tenantId, ledgerKey: record.key, processed: true }; - await model.updateMany( - { ...filter, eventId: { $in: [...checkpoint.ids] }, receiptExpiresAt: { $exists: true } }, - { $unset: { receiptExpiresAt: '' } }, - { session, timestamps: false }, - ); - await model.updateMany( - { - ...filter, - receiptCompacted: true, - receiptExpiresAt: null, - eventId: { $nin: [...checkpoint.ids] }, - }, - { $set: { receiptExpiresAt: new Date(Date.now() + CHANNEL_FEEDBACK_RECEIPT_RETENTION_MS) } }, - { session, timestamps: false }, - ); -} - -async function expireReceipts( - mongoose: typeof import('mongoose'), - filter: object, - expiresAt: Date, - session: ClientSession, - set: { processed?: boolean; receiptCompacted: true }, -): Promise { - const model = mongoose.model('ChannelFeedbackEvent'); - const first = await model.updateMany( - { ...filter, $or: [{ receiptExpiresAt: null }, { receiptExpiresAt: { $gt: expiresAt } }] }, - { $set: { ...set, receiptExpiresAt: expiresAt } }, - { session, timestamps: false }, - ); - const earlier = await model.updateMany( - { ...filter, receiptExpiresAt: { $lte: expiresAt } }, - { $set: set }, - { session, timestamps: false }, - ); - return first.modifiedCount + earlier.modifiedCount; -} - -/** Checkpoint and receipt acknowledgement share one transaction and ledger serialization anchor. */ -export async function acknowledgeFeedbackReceipt( - mongoose: typeof import('mongoose'), - record: ChannelFeedbackEventRecord, - checkpoint: FeedbackCheckpoint, - session: ClientSession, -): Promise { - if ( - record.tenantId !== record.mapping.tenantId || - record.channelId !== record.mapping.channelId || - checkpoint.key !== record.ledgerKey || - !sameFeedbackSource(checkpoint.mapping, record.mapping) - ) - throw new ChannelFeedbackAuthorityError(); - const anchored = await mongoose - .model('ChannelFeedback') - .updateOne( - { tenantId: record.tenantId, key: checkpoint.key, revision: checkpoint.revision }, - { $inc: { receiptCompactionEpoch: 1 } }, - { session, timestamps: false }, - ); - if (!anchored.matchedCount) throw new Error('Feedback checkpoint changed before acknowledgement'); - const filter = { - tenantId: record.tenantId, - channelId: record.channelId, - eventId: record.eventId, - digest: record.digest, - }; - const model = mongoose.model('ChannelFeedbackEvent'); - if (!checkpoint.canonical) { - await model.updateOne( - filter, - { $set: { processed: true, receiptCompacted: false } }, - { session }, - ); - return; - } - const current = checkpoint.slots.get( - slotKey(record.event.externalMessageKey, record.event.reaction), - ); - if ( - !current || - (current.eventId !== record.eventId && !isLaterChannelFeedbackEvent(current, record.event)) - ) - throw new Error('Feedback acknowledgement precedes its canonical checkpoint'); - if (current.eventId === record.eventId) { - await model.updateOne( - filter, - { $set: { processed: true, receiptCompacted: true }, $unset: { receiptExpiresAt: '' } }, - { session }, - ); - } else { - await expireReceipts( - mongoose, - filter, - new Date(Date.now() + CHANNEL_FEEDBACK_RECEIPT_RETENTION_MS), - session, - { processed: true, receiptCompacted: true }, - ); - } -} - -type ReceiptPage = { - _id: Types.ObjectId; - ledgerKey: string; -}; - -/** Legacy history is compacted in bounded pages, independently of recording/export authority. */ -export async function compactFeedbackReceipts( - mongoose: typeof import('mongoose'), - tenantId: string, - limit: number, - now: number, - retryMs: number, - rebuild: (key: string, session: ClientSession) => Promise, -): Promise { - const events = mongoose.model('ChannelFeedbackEvent'); - const ledgers = mongoose.model('ChannelFeedback'); - const rows = await events - .find({ - tenantId, - processed: true, - receiptCompacted: { $ne: true }, - $or: [{ compactionRetryAtMs: { $exists: false } }, { compactionRetryAtMs: { $lte: now } }], - }) - .select('_id ledgerKey') - .sort({ updatedAt: 1, _id: 1 }) - .limit(limit) - .lean(); - const groups = new Map(); - for (const row of rows) { - const group = groups.get(row.ledgerKey) ?? []; - group.push(row); - groups.set(row.ledgerKey, group); - } - let completed = 0; - for (const [key, page] of groups) { - const ids = page.map((row) => row._id); - const filter = { - tenantId, - ledgerKey: key, - _id: { $in: ids }, - processed: true, - receiptCompacted: { $ne: true }, - }; - const session = await mongoose.startSession(); - try { - let changed = 0; - await session.withTransaction(async () => { - changed = 0; - const record = await ledgers - .findOneAndUpdate( - { tenantId, key }, - { $inc: { receiptCompactionEpoch: 1 } }, - { new: true, session, timestamps: false }, - ) - .select('key revision mapping votes receiptCheckpointRevision') - .lean(); - let protectedIds: Set; - if (record && record.votes.every((vote) => Boolean(vote.reaction))) { - protectedIds = feedbackCheckpoint(record).ids; - } else if (record && record.receiptCheckpointRevision === record.revision) { - // Scheduling rows may predate another compactor's cache write. Read membership - // after taking the ledger anchor, within this transaction's snapshot. - const protectedRows = await events - .find({ ...filter, receiptCheckpointRevision: record.revision }) - .select('eventId') - .session(session) - .lean(); - protectedIds = new Set(protectedRows.map((row) => row.eventId)); - } else { - const votes = await rebuild(key, session); - if (!votes || votes.length > CHANNEL_FEEDBACK_VOTE_SLOTS_HARD_MAX) - throw new Error('Legacy feedback checkpoint unavailable'); - protectedIds = new Set([ - ...(record?.votes.map((vote) => vote.eventId) ?? []), - ...votes.map((vote) => vote.eventId), - ]); - if (protectedIds.size > CHANNEL_FEEDBACK_VOTE_SLOTS_HARD_MAX * 2) - throw new Error('Legacy feedback checkpoint footprint exceeded'); - if (record) { - await events.updateMany( - { tenantId, ledgerKey: key, processed: true, eventId: { $in: [...protectedIds] } }, - { - $set: { receiptCheckpointRevision: record.revision }, - $unset: { receiptExpiresAt: '' }, - }, - { session, timestamps: false }, - ); - await ledgers.updateOne( - { tenantId, key, revision: record.revision }, - { $set: { receiptCheckpointRevision: record.revision } }, - { session, timestamps: false }, - ); - } - } - const pinned = await events.updateMany( - { ...filter, eventId: { $in: [...protectedIds] } }, - { $set: { receiptCompacted: true }, $unset: { receiptExpiresAt: '' } }, - { session, timestamps: false }, - ); - changed += pinned.modifiedCount; - changed += await expireReceipts( - mongoose, - { ...filter, eventId: { $nin: [...protectedIds] } }, - new Date(now + CHANNEL_FEEDBACK_RECEIPT_RETENTION_MS), - session, - { receiptCompacted: true }, - ); - }); - completed += changed; - } catch (error) { - // Failed metadata work rotates too, so it cannot poison the maintenance head page. - await events.updateMany(filter, { $max: { compactionRetryAtMs: now + retryMs } }); - throw error; - } finally { - await session.endSession(); - } - } - return completed; -} diff --git a/packages/data-schemas/src/methods/channelIdentity.spec.ts b/packages/data-schemas/src/methods/channelIdentity.spec.ts index 6820445aed..1b66d5850f 100644 --- a/packages/data-schemas/src/methods/channelIdentity.spec.ts +++ b/packages/data-schemas/src/methods/channelIdentity.spec.ts @@ -494,12 +494,15 @@ it('serializes revoked insertion with retirement and erases its private payload expect(await mongoose.models.ChannelIdentity.countDocuments({ tenantId })).toBe(0); })); -it('creates and activates a per-user link at an empty canonical channel revision and admits its run', () => +it('creates and activates a per-user link at the canonical channel revision and fences rotation', () => scope(async () => { await createAgentChannelMethods(mongoose).updateAgentChannel('channel', 'connection-owner', { - revision: '', + revision: 'canonical', }); - const pending = { ...identity(), identity: { ...identity().identity, channelRevision: '' } }; + const pending = { + ...identity(), + identity: { ...identity().identity, channelRevision: 'canonical' }, + }; expect(await storage.saveChannelIdentity(pending, null)).toBe(true); const active = { ...pending, @@ -508,19 +511,21 @@ it('creates and activates a per-user link at an empty canonical channel revision revision: 'active', }; expect(await storage.saveChannelIdentity(active, 'one')).toBe(true); - expect((await storage.readChannelLink(tenantId, 'link'))?.identity.channelRevision).toBe(''); + expect((await storage.readChannelLink(tenantId, 'link'))?.identity.channelRevision).toBe( + 'canonical', + ); const input = { tenantId, channelId: 'channel', agentId: 'agent', principal: { userId: 'linked-owner', - channelRevision: '', + channelRevision: 'canonical', linkId: 'link', linkRevision: 'active', }, admission: { - eventId: 'empty-revision', + eventId: 'canonical-revision', externalChannelId: 'C1', externalThreadKey: '123.456', externalMessageKey: '123.457', @@ -530,17 +535,17 @@ it('creates and activates a per-user link at an empty canonical channel revision }; const runs = createChannelRunStorage(mongoose); await expect(runs.admit(input, 100)).resolves.toMatchObject({ - run: { principal: { channelRevision: '' } }, + run: { principal: { channelRevision: 'canonical' } }, }); await createAgentChannelMethods(mongoose).updateAgentChannel('channel', 'connection-owner', { revision: 'rotated', }); await expect( - runs.admit({ ...input, admission: { ...input.admission, eventId: 'stale-empty' } }, 101), + runs.admit({ ...input, admission: { ...input.admission, eventId: 'stale-revision' } }, 101), ).rejects.toMatchObject({ code: 'invalid_transition' }); })); -it.each([undefined, null])( +it.each([undefined, null, ''])( 'still requires a channel revision when its value is %s', (channelRevision) => scope(async () => { @@ -556,7 +561,7 @@ it.each(['channelId', 'ticketHash'] as const)( 'does not relax required nonempty %s validation', (field) => scope(async () => { - const record = { ...identity(), identity: { ...identity().identity, channelRevision: '' } }; + const record = identity(); const model = new mongoose.models.ChannelIdentity( field === 'ticketHash' ? { ...record, ticketHash: '' } diff --git a/packages/data-schemas/src/methods/channelIdentity.ts b/packages/data-schemas/src/methods/channelIdentity.ts index d765dbfa7c..df655f45fe 100644 --- a/packages/data-schemas/src/methods/channelIdentity.ts +++ b/packages/data-schemas/src/methods/channelIdentity.ts @@ -64,7 +64,7 @@ export function createChannelIdentityStorage( assertChannelTenant(record.tenantId); if (record.tenantId !== record.identity.tenantId) throw new TypeError('Channel identity tenant mismatch'); - if (typeof record.identity.channelRevision !== 'string') + if (typeof record.identity.channelRevision !== 'string' || !record.identity.channelRevision) throw new TypeError('Channel revision required'); await ensureIndexes(); if ( diff --git a/packages/data-schemas/src/methods/channelRun.spec.ts b/packages/data-schemas/src/methods/channelRun.spec.ts index d64f453685..cf5ab9bf14 100644 --- a/packages/data-schemas/src/methods/channelRun.spec.ts +++ b/packages/data-schemas/src/methods/channelRun.spec.ts @@ -388,7 +388,7 @@ it('does not couple Stop occurrences for distinct external actors', () => expect(observations.map((o) => o?.run.runId)).toEqual([a.runId, b.runId]); })); -it('owns legacy empty Stop lanes for erasure and cannot recreate them after account deletion', () => +it('owns empty Stop lanes for erasure and cannot recreate them after account deletion', () => scope(async () => { const user = await mongoose.models.User.create({ email: 'stop-erasure@example.com', @@ -397,12 +397,8 @@ it('owns legacy empty Stop lanes for erasure and cannot recreate them after acco }); const input = { ...event(), principal: { ...event().principal, userId: String(user._id) } }; await storage.admit(input, 100); - // Retained legacy lane after run cleanup, before lane ownership was recorded. + // A retained empty lane remains owned after its runs are cleaned up. await mongoose.models.ChannelRun.deleteMany({ tenantId }); - await mongoose.models.ChannelLane.updateMany( - { userId: input.principal.userId }, - { $unset: { userId: 1 } }, - ); await storage.observeThreadStop(input, 120, 101); await createUserMethods(mongoose).deleteUserById(input.principal.userId); expect(await storage.observeThreadStop(input, 121, 110)).toBeNull(); @@ -695,7 +691,7 @@ it('does not replay an admitted event through a different principal or linked ac }); })); -it('erases channel request/output and legacy lane identity before deleting its account', () => +it('erases channel request/output and owned lane identity before deleting its account', () => scope(async () => { const user = await mongoose.models.User.create({ email: 'channel-erasure@example.com', @@ -706,10 +702,6 @@ it('erases channel request/output and legacy lane identity before deleting its a const input = { ...event(), principal: { ...event().principal, userId } }; const own = await storage.admit(input, 100); const other = await storage.admit(event('other', 'other-owner'), 100); - await mongoose.models.ChannelLane.updateOne( - { laneId: own.run.laneId }, - { $unset: { userId: 1 } }, - ); await createUserMethods(mongoose).deleteUserById(userId); expect(await storage.read(tenantId, 'channel', own.run.runId)).toBeNull(); expect(await mongoose.models.ChannelLane.exists({ laneId: own.run.laneId })).toBeNull(); @@ -1060,36 +1052,6 @@ const testArtifact = { sha256: 'hash', }; -it('migrates the prototype event constraint before admitting colliding event IDs from different apps', () => - scope(async () => { - const collection = mongoose.models.ChannelRun.collection; - const legacy = { - tenantId: 1, - channelId: 1, - 'admission.actor.teamId': 1, - 'admission.eventId': 1, - }; - await collection.createIndex(legacy, { unique: true, name: 'prototype_event' }); - const revised = createChannelRunStorage(mongoose); - const second = event(); - second.admission.actor.appId = 'other-app'; - const a = await revised.admit(event(), 100); - await createAgentChannelMethods(mongoose).updateAgentChannel('channel', 'channel-owner', { - revision: 'rotated', - slack: { teamId: 'T1', appId: 'other-app', botUserId: 'B1' }, - }); - second.principal.channelRevision = 'rotated'; - const b = await revised.admit(second, 100); - expect(a.run.runId).not.toBe(b.run.runId); - expect((await revised.admit(second, 101)).run.runId).toBe(b.run.runId); - const indexes = await collection.indexes(); - expect(indexes.some((index) => index.name === 'prototype_event')).toBe(false); - expect(indexes.find((index) => index.name === 'channel_run_event_v2')).toMatchObject({ - unique: true, - key: { 'admission.actor.appId': 1 }, - }); - })); - it.each(['completed', 'failed', 'cancelled'] as const)( 'never rolls a %s snapshot back into execution', (status) => @@ -1534,18 +1496,14 @@ it('injects account erasure work bounds before closing admission', () => input.admission.externalThreadKey = `thread-${i}`; await storage.admit(input, 100 + i); } - await mongoose.models.ChannelLane.updateMany({ userId }, { $unset: { userId: 1 } }); const getConfig = jest.fn(async () => ({ erasureBatchSize: 1 })); const deleted = jest.spyOn(mongoose.models.ChannelLane, 'deleteMany'); try { await createUserMethods(mongoose, { getChannelStorageConfig: getConfig }).deleteUserById( userId, ); - const batches = deleted.mock.calls - .map(([filter]) => filter as { laneId?: { $in?: string[] } }) - .filter((filter) => filter?.laneId?.$in); - expect(batches).toHaveLength(3); - for (const batch of batches) expect(batch?.laneId?.$in).toHaveLength(1); + expect(deleted).toHaveBeenCalledWith({ tenantId, userId }); + expect(await mongoose.models.ChannelLane.countDocuments({ tenantId, userId })).toBe(0); expect(getConfig).toHaveBeenCalledTimes(1); } finally { deleted.mockRestore(); diff --git a/packages/data-schemas/src/methods/channelRun.ts b/packages/data-schemas/src/methods/channelRun.ts index 334b8835ee..7b67eb1458 100644 --- a/packages/data-schemas/src/methods/channelRun.ts +++ b/packages/data-schemas/src/methods/channelRun.ts @@ -212,27 +212,7 @@ export function createChannelRunStorage( const limits = channelStorageConfigSchema.parse(options); const runs = () => mongoose.model('ChannelRun'); const lanes = () => mongoose.model('ChannelLane'); - const ensureIndexes = createChannelIndexGate( - () => [runs(), lanes()], - async () => { - // The prototype index was narrower than provider event identity. Build its - // replacement first, then remove only that exact old unique constraint. - const legacy = { - tenantId: 1, - channelId: 1, - 'admission.actor.teamId': 1, - 'admission.eventId': 1, - }; - for (const index of await runs().listIndexes()) { - if (!index.unique || !index.name || digest(index.key) !== digest(legacy)) continue; - try { - await runs().cleanIndexes({ toDrop: [index.name] }); - } catch (error) { - if (!(error instanceof mongoose.mongo.MongoServerError) || error.code !== 27) throw error; - } - } - }, - ); + const ensureIndexes = createChannelIndexGate(() => [runs(), lanes()]); async function transaction(work: (session: ClientSession) => Promise): Promise { await ensureIndexes(); const session = await mongoose.startSession(); diff --git a/packages/data-schemas/src/methods/channelScope.ts b/packages/data-schemas/src/methods/channelScope.ts index be09f08b02..1bbaf5e0e1 100644 --- a/packages/data-schemas/src/methods/channelScope.ts +++ b/packages/data-schemas/src/methods/channelScope.ts @@ -195,15 +195,13 @@ export async function retireAgentChannel( $unset: { encryptedBotToken: '', encryptedSigningSecret: '', - encryptedApiKey: '', - apiKeyId: '', }, } : {}), }, { new: true, session }, ) - .select('+encryptedBotToken +encryptedSigningSecret +encryptedApiKey +apiKeyId') + .select('+encryptedBotToken +encryptedSigningSecret') .lean(); if (!record) return null; await closeChannelScope(mongoose, tenantId, channelId, session); diff --git a/packages/data-schemas/src/methods/response.spec.ts b/packages/data-schemas/src/methods/response.spec.ts index b5401bb99a..bdb431606b 100644 --- a/packages/data-schemas/src/methods/response.spec.ts +++ b/packages/data-schemas/src/methods/response.spec.ts @@ -23,6 +23,11 @@ let mongo: MongoMemoryReplSet; let storage: ResponseStorage; const owner = { userId: 'owner', tenantId: 'tenant-a' }; const scoped = (work: () => Promise) => tenantStorage.run({ tenantId: owner.tenantId }, work); +async function executionGuard(responseId: string) { + const execution = await scoped(() => storage.read(owner, responseId)); + if (!execution) throw new Error('Missing execution fixture'); + return { execution }; +} const reservation = (overrides: Partial = {}): ResponseReservation => ({ ...owner, responseId: 'resp_one', @@ -499,7 +504,7 @@ async function pause() { const paused = await storage.publishReview( owner, 'resp_one', - 1, + await executionGuard('resp_one'), 10, review, '{"status":"requires_action"}', @@ -509,11 +514,11 @@ async function pause() { it('retains a valid private visible-token seed and rejects fractional, negative or overflowing counters', () => scoped(async () => { - await pause(); + await runningForReview(); const result = await storage.publishReview( owner, 'resp_one', - 2, + await executionGuard('resp_one'), 10, { ...review, visibleOutputTokens: 13 }, '{"status":"requires_action"}', @@ -526,12 +531,12 @@ it('retains a valid private visible-token seed and rejects fractional, negative storage.publishReview( owner, 'resp_one', - result.record.revision, + await executionGuard('resp_one'), 10, { ...review, visibleOutputTokens }, '{}', ), - ).rejects.toThrow(); + ).resolves.toEqual({ ok: false, code: 'response_conflict' }); expect((await storage.read(owner, 'resp_one'))?.review?.visibleOutputTokens).toBe(13); })); @@ -631,7 +636,16 @@ it('keeps review and resume mutations owner- and tenant-scoped', () => { ...owner, tenantId: 'other' }, ]) { await tenantStorage.run({ tenantId: stranger.tenantId }, async () => { - expect(await storage.publishReview(stranger, 'resp_one', 2, 10, review, '{}')).toEqual({ + expect( + await storage.publishReview( + stranger, + 'resp_one', + await executionGuard('resp_one'), + 10, + review, + '{}', + ), + ).toEqual({ ok: false, code: 'response_conflict', }); @@ -659,7 +673,16 @@ it('fences late pause callbacks and allows only the admitted next segment to pau scoped(async () => { await pause(); await storage.reserveResume(owner, 'resp_one', resume(), 100); - expect(await storage.publishReview(owner, 'resp_one', 3, 10, review, '{}')).toEqual({ + expect( + await storage.publishReview( + owner, + 'resp_one', + await executionGuard('resp_one'), + 10, + review, + '{}', + ), + ).toEqual({ ok: false, code: 'response_conflict', }); @@ -689,12 +712,32 @@ it('fences late pause callbacks and allows only the admitted next segment to pau ) ).ok, ).toBe(true); - expect(await storage.publishReview(owner, 'resp_one', 4, 10, review, '{}')).toEqual({ + expect( + await storage.publishReview( + owner, + 'resp_one', + await executionGuard('resp_one'), + 10, + review, + '{}', + ), + ).toEqual({ ok: false, code: 'response_conflict', }); const next = { ...review, actionId: 'action-two', providerExecutionId: 'provider-two' }; - expect((await storage.publishReview(owner, 'resp_one', 4, 10, next, '{}')).ok).toBe(true); + expect( + ( + await storage.publishReview( + owner, + 'resp_one', + await executionGuard('resp_one'), + 10, + next, + '{}', + ) + ).ok, + ).toBe(true); expect(await storage.read(owner, 'resp_one')).toMatchObject({ status: 'requires_action', review: next, @@ -715,7 +758,16 @@ it('cancellation prevents a reserved decision from admitting execution', () => expect( await storage.admitResume(owner, 'resp_one', 'intent-one', 10, 'provider-two', '{}', 102), ).toEqual({ ok: false, code: 'response_conflict' }); - expect(await storage.publishReview(owner, 'resp_one', 3, 10, review, '{}')).toEqual({ + expect( + await storage.publishReview( + owner, + 'resp_one', + await executionGuard('resp_one'), + 10, + review, + '{}', + ), + ).toEqual({ ok: false, code: 'response_conflict', }); @@ -736,7 +788,16 @@ it('does not admit or repause terminal responses but retains the identical decis expect( await storage.admitResume(owner, 'resp_one', 'intent-one', 10, 'provider-two', '{}', 102), ).toEqual({ ok: false, code: 'response_conflict' }); - expect(await storage.publishReview(owner, 'resp_one', 4, 10, review, '{}')).toEqual({ + expect( + await storage.publishReview( + owner, + 'resp_one', + await executionGuard('resp_one'), + 10, + review, + '{}', + ), + ).toEqual({ ok: false, code: 'response_conflict', }); @@ -886,7 +947,10 @@ it('stages an initial terminal receipt without requiring a review or resume', () responseJSON: '{}', generationCreatedAt: 10, }); - expect((await storage.stageTerminal(owner, 'resp_one', 1, outcome)).ok).toBe(true); + expect( + (await storage.stageTerminal(owner, 'resp_one', await executionGuard('resp_one'), outcome)) + .ok, + ).toBe(true); expect((await storage.finishTerminal(owner, 'resp_one', 2, outcome)).ok).toBe(true); })); @@ -896,7 +960,10 @@ it('stages the admitted provider terminal receipt without requiring another revi await storage.reserveResume(owner, 'resp_one', resume(), 100); await storage.admitResume(owner, 'resp_one', 'intent-one', 10, 'provider-two', '{}', 101); const current = { ...outcome, providerExecutionId: 'provider-two' }; - expect((await storage.stageTerminal(owner, 'resp_one', 4, current)).ok).toBe(true); + expect( + (await storage.stageTerminal(owner, 'resp_one', await executionGuard('resp_one'), current)) + .ok, + ).toBe(true); expect((await storage.finishTerminal(owner, 'resp_one', 5, current)).ok).toBe(true); })); @@ -926,18 +993,32 @@ it('rejects terminal receipts from an earlier segment after resume admission and await pause(); await storage.reserveResume(owner, 'resp_one', resume(), 100); await storage.admitResume(owner, 'resp_one', 'intent-one', 10, 'provider-two', '{}', 101); - expect(await storage.stageTerminal(owner, 'resp_one', 4, outcome)).toEqual({ + expect( + await storage.stageTerminal(owner, 'resp_one', await executionGuard('resp_one'), outcome), + ).toEqual({ ok: false, code: 'response_conflict', }); const next = { ...review, actionId: 'action-two', providerExecutionId: 'provider-two' }; - await storage.publishReview(owner, 'resp_one', 4, 10, next, '{}'); - expect(await storage.stageTerminal(owner, 'resp_one', 5, outcome)).toEqual({ + await storage.publishReview( + owner, + 'resp_one', + await executionGuard('resp_one'), + 10, + next, + '{}', + ); + expect( + await storage.stageTerminal(owner, 'resp_one', await executionGuard('resp_one'), outcome), + ).toEqual({ ok: false, code: 'response_conflict', }); const current = { ...outcome, providerExecutionId: 'provider-two' }; - expect((await storage.stageTerminal(owner, 'resp_one', 5, current)).ok).toBe(true); + expect( + (await storage.stageTerminal(owner, 'resp_one', await executionGuard('resp_one'), current)) + .ok, + ).toBe(true); expect((await storage.finishTerminal(owner, 'resp_one', 6, current)).ok).toBe(true); })); @@ -945,7 +1026,9 @@ it('durably stages one immutable terminal receipt while keeping its output priva scoped(async () => { await pause(); const results = await Promise.all( - Array.from({ length: 12 }, () => storage.stageTerminal(owner, 'resp_one', 2, outcome)), + Array.from({ length: 12 }, async () => + storage.stageTerminal(owner, 'resp_one', await executionGuard('resp_one'), outcome), + ), ); expect(results.every((result) => result.ok)).toBe(true); const record = (await storage.read(owner, 'resp_one'))!; @@ -956,24 +1039,39 @@ it('durably stages one immutable terminal receipt while keeping its output priva }); expect(record.responseJSON).not.toContain('private until cleanup'); expect( - await storage.stageTerminal(owner, 'resp_one', 3, { ...outcome, status: 'failed' }), + await storage.stageTerminal(owner, 'resp_one', await executionGuard('resp_one'), { + ...outcome, + status: 'failed', + }), ).toEqual({ ok: false, code: 'response_conflict' }); expect( - await storage.stageTerminal(owner, 'resp_one', 3, { ...outcome, ownerId: 'other-worker' }), + await storage.stageTerminal(owner, 'resp_one', await executionGuard('resp_one'), { + ...outcome, + ownerId: 'other-worker', + }), ).toEqual({ ok: false, code: 'response_conflict' }); })); it('prevents Stop, review and resume writes from overwriting a staged terminal outcome', () => scoped(async () => { await pause(); - await storage.stageTerminal(owner, 'resp_one', 2, outcome); + await storage.stageTerminal(owner, 'resp_one', await executionGuard('resp_one'), outcome); expect( await storage.publish(owner, 'resp_one', 3, { status: 'cancelled', responseJSON: 'overwrite', }), ).toEqual({ ok: false, code: 'response_conflict' }); - expect(await storage.publishReview(owner, 'resp_one', 3, 10, review, '{}')).toEqual({ + expect( + await storage.publishReview( + owner, + 'resp_one', + await executionGuard('resp_one'), + 10, + review, + '{}', + ), + ).toEqual({ ok: false, code: 'response_conflict', }); @@ -989,7 +1087,7 @@ it('prevents Stop, review and resume writes from overwriting a staged terminal o it('publishes only the exact saved receipt and removes it atomically with terminal publication', () => scoped(async () => { await pause(); - await storage.stageTerminal(owner, 'resp_one', 2, outcome); + await storage.stageTerminal(owner, 'resp_one', await executionGuard('resp_one'), outcome); for (const changed of [ { ownerId: 'other' }, { checkpointNamespace: 'other' }, @@ -1010,7 +1108,9 @@ it('publishes only the exact saved receipt and removes it atomically with termin revision: 4, }); expect(record.pendingTerminal).toBeUndefined(); - expect(await storage.stageTerminal(owner, 'resp_one', 4, outcome)).toEqual({ + expect( + await storage.stageTerminal(owner, 'resp_one', await executionGuard('resp_one'), outcome), + ).toEqual({ ok: false, code: 'response_conflict', }); @@ -1019,13 +1119,20 @@ it('publishes only the exact saved receipt and removes it atomically with termin it('does not stage or publish another user or tenant terminal result', () => scoped(async () => { await pause(); - await storage.stageTerminal(owner, 'resp_one', 2, outcome); + await storage.stageTerminal(owner, 'resp_one', await executionGuard('resp_one'), outcome); for (const stranger of [ { ...owner, userId: 'other' }, { ...owner, tenantId: 'other' }, ]) { await tenantStorage.run({ tenantId: stranger.tenantId }, async () => { - expect(await storage.stageTerminal(stranger, 'resp_one', 3, outcome)).toEqual({ + expect( + await storage.stageTerminal( + stranger, + 'resp_one', + await executionGuard('resp_one'), + outcome, + ), + ).toEqual({ ok: false, code: 'response_conflict', }); @@ -1215,14 +1322,19 @@ it('requires checkpoint cleanup for an initial staged terminal without a review }); expect(running.ok).toBe(true); if (!running.ok) return; - const staged = await storage.stageTerminal(owner, 'resp_one', running.record.revision, { - generationCreatedAt: 42, - providerExecutionId: 'initial-provider', - status: 'failed', - responseJSON: '{}', - checkpointNamespace: 'checkpoint-one', - checkpointConfigJSON: '{"saver":"initial"}', - }); + const staged = await storage.stageTerminal( + owner, + 'resp_one', + await executionGuard('resp_one'), + { + generationCreatedAt: 42, + providerExecutionId: 'initial-provider', + status: 'failed', + responseJSON: '{}', + checkpointNamespace: 'checkpoint-one', + checkpointConfigJSON: '{"saver":"initial"}', + }, + ); expect(staged.ok).toBe(true); expect(await storage.readCleanupIdentity(owner, 'resp_one')).toMatchObject({ checkpointNamespace: 'checkpoint-one', @@ -1669,9 +1781,6 @@ it('stages one exact execution receipt independently of repeated projection revi ).ok, ).toBe(true); } - expect( - await storage.stageTerminal(owner, 'resp_one', running.record.revision, outcome), - ).toEqual({ ok: false, code: 'response_conflict' }); const attempts = await Promise.all( Array.from({ length: 12 }, () => storage.stageTerminal(owner, 'resp_one', { execution: running.record }, outcome), @@ -1772,9 +1881,6 @@ it('publishes one immutable pause independently of polling before and after publ ).ok, ).toBe(true); } - expect( - await storage.publishReview(owner, 'resp_one', running.revision, 10, review, '{}'), - ).toEqual({ ok: false, code: 'response_conflict' }); const attempts = await Promise.all( Array.from({ length: 12 }, () => storage.publishReview(owner, 'resp_one', { execution: running }, 10, review, '{}'), @@ -2060,7 +2166,7 @@ it('does not return an aborted attempt success when the transaction retries afte })); it.each(['prepared', 'pending'] as const)( - 'fences new-format %s receipts from legacy-active writers and unfenced retries', + 'keeps %s materialization private until exact publication completes', (phase) => scoped(async () => { const running = await runningForReview(); @@ -2259,9 +2365,9 @@ it('fences late create acknowledgements from a newer resume and does not bump ex ).toEqual([]); })); -it('recovers legacy reservations and resume intents without acknowledging another tenant or closed scope', () => +it('recovers pending dispatch tokens without acknowledging another tenant or closed scope', () => scoped(async () => { - await storage.reserve(reservation()); + await storage.reserve(reservation({ dispatch: true })); const page = await runAsSystem(() => storage.listPendingDispatches(undefined, 1, false)); expect(page.responses).toEqual([{ ...owner, responseId: 'resp_one' }]); await expect(storage.listPendingDispatches(undefined, 1, false)).rejects.toThrow('system'); @@ -2277,7 +2383,7 @@ it('recovers legacy reservations and resume intents without acknowledging anothe ).toBe(false); })); -it('finds legacy resume writes after an earlier create marker was settled', () => +it('does not repair an inconsistent resume marker from an earlier create', () => scoped(async () => { await pause(); await storage.reserveResume(owner, 'resp_one', resume(), 100); @@ -2290,10 +2396,10 @@ it('finds legacy resume writes after an earlier create marker was settled', () = ).toEqual([]); expect( (await runAsSystem(() => storage.listPendingDispatches(undefined, 10, true))).responses, - ).toEqual([{ ...owner, responseId: 'resp_one' }]); + ).toEqual([]); expect( await storage.settleDispatch(owner, 'resp_one', { kind: 'resume', id: 'intent-one' }), - ).toBe(true); + ).toBe(false); })); it('bounds metadata pages across named and default tenants and omits expired requests', async () => { diff --git a/packages/data-schemas/src/methods/response.ts b/packages/data-schemas/src/methods/response.ts index 88a0795f8b..79f10d76ef 100644 --- a/packages/data-schemas/src/methods/response.ts +++ b/packages/data-schemas/src/methods/response.ts @@ -85,17 +85,12 @@ export type ResponseResumeResult = | { ok: false; code: 'resume_conflict' }; /** Canonical execution writes are independent of polling's projection revision. */ -export type ResponseExecutionGuard = - | number - | { - execution: Pick< - StoredResponseRecord, - 'agentId' | 'sourceKeyId' | 'conversationId' | 'generationCreatedAt' | 'review' | 'resume' - >; - }; - -/** Compatibility name for existing terminal producers. */ -export type ResponseTerminalGuard = ResponseExecutionGuard; +export interface ResponseExecutionGuard { + execution: Pick< + StoredResponseRecord, + 'agentId' | 'sourceKeyId' | 'conversationId' | 'generationCreatedAt' | 'review' | 'resume' + >; +} function responseReviewFilter( review: ResponseReviewCheckpoint | undefined, @@ -122,7 +117,7 @@ function responseReviewFilter( return filter; } -function responseExecutionFilter(guard: Exclude) { +function responseExecutionFilter(guard: ResponseExecutionGuard) { const { execution } = guard; const filter: Record = { agentId: execution.agentId, @@ -158,13 +153,13 @@ export interface ResponseStorage { prepareTerminal( owner: ResponseOwner, responseId: string, - guard: Exclude, + guard: ResponseExecutionGuard, receipt: ResponseTerminalReceipt, ): Promise; stageTerminal( owner: ResponseOwner, responseId: string, - guard: ResponseTerminalGuard, + guard: ResponseExecutionGuard, receipt: ResponseTerminalReceipt, promotion?: { receipt: ResponseTerminalReceipt; cancellationRequestedAt?: number }, ): Promise; @@ -438,12 +433,9 @@ export function createResponseStorage( prepared.checkpointConfigJSON !== receipt.checkpointConfigJSON) ) return { ok: false, code: 'response_conflict' }; - if ( - typeof guard !== 'number' && - guard.execution.generationCreatedAt !== receipt.generationCreatedAt - ) + if (guard.execution.generationCreatedAt !== receipt.generationCreatedAt) return { ok: false, code: 'response_conflict' }; - const execution = typeof guard === 'number' ? undefined : responseExecutionFilter(guard); + const execution = responseExecutionFilter(guard); const pendingTerminal = terminalReceipt(receipt); return commitInOpenScope( owner, @@ -454,7 +446,7 @@ export function createResponseStorage( { ...scope(owner), responseId, - ...(execution ?? { revision: guard }), + ...execution, generationCreatedAt: receipt.generationCreatedAt, status: promotion && receipt.turnJSON != null ? 'materializing' : { $in: active }, pendingTerminal: { $exists: false }, @@ -568,28 +560,24 @@ export function createResponseStorage( ); }, async publishReview(owner, responseId, guard, generationCreatedAt, review, responseJSON) { - if (typeof guard !== 'number' && guard.execution.generationCreatedAt !== generationCreatedAt) + if (guard.execution.generationCreatedAt !== generationCreatedAt) return { ok: false, code: 'response_conflict' }; - const execution = typeof guard === 'number' ? undefined : responseExecutionFilter(guard); - const receiptFilter = - typeof guard === 'number' - ? undefined - : { - ...scope(owner), - responseId, - generationCreatedAt, - agentId: guard.execution.agentId, - sourceKeyId: guard.execution.sourceKeyId ?? { $exists: false }, - conversationId: guard.execution.conversationId, - ...responseReviewFilter(review), - status: 'requires_action', - resume: { $exists: false }, - pendingTerminal: { $exists: false }, - preparedTerminal: { $exists: false }, - cancellationRequestedAt: { $exists: false }, - }; + const execution = responseExecutionFilter(guard); + const receiptFilter = { + ...scope(owner), + responseId, + generationCreatedAt, + agentId: guard.execution.agentId, + sourceKeyId: guard.execution.sourceKeyId ?? { $exists: false }, + conversationId: guard.execution.conversationId, + ...responseReviewFilter(review), + status: 'requires_action', + resume: { $exists: false }, + pendingTerminal: { $exists: false }, + preparedTerminal: { $exists: false }, + cancellationRequestedAt: { $exists: false }, + }; if ( - typeof guard !== 'number' && guard.execution.review?.actionId === review.actionId && guard.execution.review.providerExecutionId === review.providerExecutionId && guard.execution.resume == null @@ -602,7 +590,7 @@ export function createResponseStorage( { ...scope(owner), responseId, - ...(execution ?? { revision: guard }), + ...execution, generationCreatedAt, status: { $in: ['in_progress', 'requires_action'] }, pendingTerminal: { $exists: false }, @@ -877,28 +865,11 @@ export function createResponseStorage( 'dispatchOutbox.state': 'pending', 'dispatchOutbox.kind': { $in: includeResume ? ['create', 'resume'] : ['create'] }, }; - const candidates: Record[] = [ - pending, - { status: 'queued', dispatchOutbox: { $exists: false } }, - ]; - if (includeResume) - candidates.push({ - status: { $in: ['requires_action', 'in_progress'] }, - 'resume.state': 'reserved', - $or: [ - { dispatchOutbox: { $exists: false } }, - { - 'dispatchOutbox.kind': { $in: ['create', 'resume'] }, - 'dispatchOutbox.state': { $in: ['pending', 'settled'] }, - }, - ], - $expr: { $ne: [{ $ifNull: ['$dispatchOutbox.id', null] }, '$resume.intentId'] }, - }); const rows = await model .find({ ...(cursor ? { _id: { $gt: new mongoose.Types.ObjectId(cursor) } } : {}), $and: [activeExpirationFilter()], - $or: candidates, + ...pending, }) .select('_id tenantId userId responseId') .sort({ _id: 1 }) @@ -923,23 +894,17 @@ export function createResponseStorage( async (session, record) => { const marker = record.dispatchOutbox; if ( - marker && - (!['create', 'resume'].includes(marker.kind) || - !['pending', 'settled'].includes(marker.state)) + !marker || + !['create', 'resume'].includes(marker.kind) || + !['pending', 'settled'].includes(marker.state) ) return false; - const same = marker?.kind === token.kind && marker.id === token.id; + if (marker.kind !== token.kind || marker.id !== token.id) return false; + // A consumed resume may pause again and clear its intent before enqueue acknowledgement. if (token.kind === 'create') { - if ( - token.id !== record.responseId || - record.resume?.state === 'reserved' || - (marker && !same) - ) - return false; - } else if (!same && record.resume?.intentId !== token.id) return false; - if (token.kind === 'resume' && record.resume && record.resume.intentId !== token.id) - return false; - if (same && marker.state === 'settled') return true; + if (token.id !== record.responseId || record.resume?.state === 'reserved') return false; + } else if (record.resume && record.resume.intentId !== token.id) return false; + if (marker.state === 'settled') return true; const updated = await model.updateOne( { ...scope(owner), responseId }, { diff --git a/packages/data-schemas/src/methods/triggerDelivery.ts b/packages/data-schemas/src/methods/triggerDelivery.ts index 5f504e85b4..0f8b9c902d 100644 --- a/packages/data-schemas/src/methods/triggerDelivery.ts +++ b/packages/data-schemas/src/methods/triggerDelivery.ts @@ -29,7 +29,6 @@ import { AGENT_TRIGGER_WORKER_CAPABILITY_QUEUED_TURN_V2, AGENT_TRIGGER_WORKER_CAPABILITY_QUEUED_TURN_V3, AGENT_TRIGGER_WORKER_CAPABILITY_RESPONSES_V1, - AGENT_TRIGGER_WORKER_CAPABILITY_RESPONSES_V2, AGENT_TRIGGER_WORKER_CAPABILITY_RESPONSES_RESUME_V1, } from '~/types/triggerDelivery'; import { createIndexesWithRetry } from '~/utils/retry'; @@ -1197,7 +1196,6 @@ export function createAgentTriggerDeliveryMethods( input.requiredWorkerCapability !== AGENT_TRIGGER_WORKER_CAPABILITY_QUEUED_TURN_V2 && input.requiredWorkerCapability !== AGENT_TRIGGER_WORKER_CAPABILITY_QUEUED_TURN_V3 && input.requiredWorkerCapability !== AGENT_TRIGGER_WORKER_CAPABILITY_RESPONSES_V1 && - input.requiredWorkerCapability !== AGENT_TRIGGER_WORKER_CAPABILITY_RESPONSES_V2 && input.requiredWorkerCapability !== AGENT_TRIGGER_WORKER_CAPABILITY_RESPONSES_RESUME_V1 ) { throw new TypeError('Agent trigger delivery requires an unsupported worker capability'); diff --git a/packages/data-schemas/src/models/channel.ts b/packages/data-schemas/src/models/channel.ts index e263cb8140..b9ad725649 100644 --- a/packages/data-schemas/src/models/channel.ts +++ b/packages/data-schemas/src/models/channel.ts @@ -6,16 +6,6 @@ import { applyTenantIsolation } from './plugins/tenantIsolation'; export interface AgentChannelRecord extends AgentChannel { encryptedBotToken?: string; encryptedSigningSecret?: string; - encryptedApiKey?: string; - apiKeyId?: string; -} - -/** Legacy prototype rows are retained only for privacy erasure, never conversation allocation. */ -interface LegacyAgentChannelThreadRecord { - channelId: string; - tenantId?: string; - externalThreadKey: string; - conversationId: string; } export function createAgentChannelModel( @@ -34,12 +24,10 @@ export function createAgentChannelModel( required: true, }, credentialMode: { type: String, enum: ['shared', 'per_user'], required: true }, - revision: String, + revision: { type: String, required: true }, slack: { teamId: String, appId: String, botUserId: String }, encryptedBotToken: { type: String, select: false }, encryptedSigningSecret: { type: String, select: false }, - encryptedApiKey: { type: String, select: false }, - apiKeyId: { type: String, select: false }, }, { timestamps: true }, ); @@ -50,23 +38,3 @@ export function createAgentChannelModel( applyTenantIsolation(schema); return mongoose.models.AgentChannel ?? mongoose.model('AgentChannel', schema); } - -export function createAgentChannelThreadModel( - mongoose: typeof import('mongoose'), -): Model { - const schema = new Schema( - { - channelId: { type: String, required: true }, - tenantId: String, - externalThreadKey: { type: String, required: true }, - conversationId: { type: String, required: true }, - }, - { timestamps: true }, - ); - schema.index({ tenantId: 1, channelId: 1, externalThreadKey: 1 }, { unique: true }); - applyTenantIsolation(schema); - return ( - mongoose.models.AgentChannelThread ?? - mongoose.model('AgentChannelThread', schema) - ); -} diff --git a/packages/data-schemas/src/models/channelFeedback.ts b/packages/data-schemas/src/models/channelFeedback.ts index 2048099dfe..b63644b4b8 100644 --- a/packages/data-schemas/src/models/channelFeedback.ts +++ b/packages/data-schemas/src/models/channelFeedback.ts @@ -10,7 +10,6 @@ import { applyTenantIsolation } from './plugins/tenantIsolation'; export interface ChannelFeedbackRetention { expiredAt?: Date | null; - retentionVersion?: number; } export type ChannelReplyRecord = ChannelReplyMapping & ChannelFeedbackRetention; @@ -25,10 +24,7 @@ export interface ChannelFeedbackEventRecord extends ChannelFeedbackRetention { mapping: ChannelReplyMapping; rating: ChannelRating; processed: boolean; - receiptCompacted?: boolean; receiptExpiresAt?: Date; - receiptCheckpointRevision?: number; - compactionRetryAtMs?: number; retryAtMs?: number; } @@ -38,8 +34,7 @@ export interface ChannelFeedbackLedger extends ChannelFeedbackRetention { mapping: ChannelReplyMapping; externalUserId: string; scoreId: string; - receiptCompactionEpoch?: number; - receiptCheckpointRevision?: number; + receiptEpoch?: number; scoreTimestamp?: Date; createdAt?: Date; votes: ChannelFeedbackVote[]; @@ -71,7 +66,6 @@ function mappingSchema(): Schema { destinationIds: { type: [String], default: undefined }, centralTraceExportEnabled: { type: Boolean, required: true }, expiredAt: Date, - retentionVersion: Number, }, { _id: false }, ); @@ -84,7 +78,6 @@ export function createChannelReplyModel( schema.index({ tenantId: 1, channelId: 1, teamId: 1, externalMessageKey: 1 }, { unique: true }); schema.index({ tenantId: 1, principalId: 1, conversationId: 1 }); schema.index({ expiredAt: 1 }, { expireAfterSeconds: 0 }); - schema.index({ tenantId: 1, retentionVersion: 1, _id: 1 }); applyTenantIsolation(schema); return mongoose.models.ChannelReply ?? mongoose.model('ChannelReply', schema); } @@ -114,31 +107,24 @@ export function createChannelFeedbackEventModel( mapping: { type: mappingSchema(), required: true }, rating: { type: String, enum: ['thumbsUp', 'thumbsDown'], required: true }, processed: { type: Boolean, required: true }, - receiptCompacted: Boolean, receiptExpiresAt: Date, - receiptCheckpointRevision: Number, - compactionRetryAtMs: { type: Number, default: 0 }, retryAtMs: { type: Number, default: 0 }, expiredAt: Date, - retentionVersion: Number, }, { timestamps: true }, ); schema.index({ tenantId: 1, channelId: 1, eventId: 1 }, { unique: true }); schema.index({ tenantId: 1, processed: 1, updatedAt: 1, _id: 1 }); schema.index({ receiptExpiresAt: 1 }, { expireAfterSeconds: 0 }); - schema.index({ tenantId: 1, processed: 1, receiptCompacted: 1, updatedAt: 1, _id: 1 }); schema.index({ tenantId: 1, ledgerKey: 1, processed: 1, - receiptCompacted: 1, receiptExpiresAt: 1, }); schema.index({ tenantId: 1, ledgerKey: 1, 'event.eventTimestampUs': -1, 'event.eventId': -1 }); schema.index({ tenantId: 1, 'mapping.principalId': 1, 'mapping.conversationId': 1 }); schema.index({ expiredAt: 1 }, { expireAfterSeconds: 0 }); - schema.index({ tenantId: 1, retentionVersion: 1, _id: 1 }); applyTenantIsolation(schema); return ( mongoose.models.ChannelFeedbackEvent ?? @@ -151,7 +137,7 @@ export function createChannelFeedbackModel( ): Model { const vote = new Schema( { - reaction: String, + reaction: { type: String, required: true }, externalMessageKey: { type: String, required: true }, rating: { type: String, enum: ['thumbsUp', 'thumbsDown'], required: true }, active: { type: Boolean, required: true }, @@ -167,11 +153,9 @@ export function createChannelFeedbackModel( mapping: { type: mappingSchema(), required: true }, externalUserId: { type: String, required: true }, scoreId: { type: String, required: true }, - receiptCompactionEpoch: Number, - receiptCheckpointRevision: Number, - scoreTimestamp: Date, + receiptEpoch: Number, + scoreTimestamp: { type: Date, required: true, default: Date.now }, expiredAt: Date, - retentionVersion: Number, votes: { type: [vote], default: [] }, revision: { type: Number, required: true }, rating: { type: String, enum: ['thumbsUp', 'thumbsDown', null], default: null }, @@ -187,7 +171,6 @@ export function createChannelFeedbackModel( schema.index({ tenantId: 1, 'mapping.principalId': 1, 'mapping.conversationId': 1 }); schema.index({ tenantId: 1, 'mapping.channelId': 1 }); schema.index({ expiredAt: 1 }, { expireAfterSeconds: 0 }); - schema.index({ tenantId: 1, retentionVersion: 1, _id: 1 }); applyTenantIsolation(schema); return ( mongoose.models.ChannelFeedback ?? diff --git a/packages/data-schemas/src/models/channelIdentity.ts b/packages/data-schemas/src/models/channelIdentity.ts index 8ecc910533..ca0be5dc15 100644 --- a/packages/data-schemas/src/models/channelIdentity.ts +++ b/packages/data-schemas/src/models/channelIdentity.ts @@ -30,10 +30,6 @@ export function createChannelIdentityModel( }, { _id: false }, ); - // Channel revisions are opaque tokens: require presence without rejecting an empty string. - Object.assign(identity.path('channelRevision'), { - checkRequired: (value: unknown) => typeof value === 'string', - }); const schema = new Schema( { tenantId: { type: String, required: true }, diff --git a/packages/data-schemas/src/models/index.ts b/packages/data-schemas/src/models/index.ts index eacd922b22..6aa56df687 100644 --- a/packages/data-schemas/src/models/index.ts +++ b/packages/data-schemas/src/models/index.ts @@ -4,7 +4,6 @@ import { createChannelFeedbackModel, } from './channelFeedback'; import { createAgentQueuedTurnModel, createAgentQueuedTurnSequenceModel } from './queuedTurn'; -import { createAgentChannelModel, createAgentChannelThreadModel } from './channel'; import { createChannelRunModel, createChannelLaneModel } from './channelRun'; import { createAgentTriggerLaneSequenceModel } from './triggerLaneSequence'; import { createScheduleModel, createScheduleRunModel } from './schedule'; @@ -36,6 +35,7 @@ import { createPluginAuthModel } from './pluginAuth'; import { createSharedLinkModel } from './sharedLink'; import { createAccessRoleModel } from './accessRole'; import { createToolFavoriteModel } from './favorite'; +import { createAgentChannelModel } from './channel'; import { createMCPServerModel } from './mcpServer'; import { createAssistantModel } from './assistant'; import { createSkillFileModel } from './skillFile'; @@ -83,7 +83,6 @@ export function createModels(mongoose: typeof import('mongoose')): { Agent: ReturnType; AgentApiKey: ReturnType; AgentChannel: ReturnType; - AgentChannelThread: ReturnType; ChannelIdentity: ReturnType; ChannelScope: ReturnType; ChannelRun: ReturnType; @@ -149,7 +148,6 @@ export function createModels(mongoose: typeof import('mongoose')): { Agent: createAgentModel(mongoose), AgentApiKey: createAgentApiKeyModel(mongoose), AgentChannel: createAgentChannelModel(mongoose), - AgentChannelThread: createAgentChannelThreadModel(mongoose), ChannelIdentity: createChannelIdentityModel(mongoose), ChannelScope: createChannelScopeModel(mongoose), ChannelRun: createChannelRunModel(mongoose), diff --git a/packages/data-schemas/src/models/response.ts b/packages/data-schemas/src/models/response.ts index c193995362..0526d3930b 100644 --- a/packages/data-schemas/src/models/response.ts +++ b/packages/data-schemas/src/models/response.ts @@ -11,7 +11,7 @@ export type StoredResponseStatus = | 'queued' | 'in_progress' | 'requires_action' - /** Private terminal protocol phase; legacy active-only writers cannot finalize it. */ + /** Private preparation/publication phase; output is not yet materialized. */ | 'materializing' | 'completed' | 'failed' diff --git a/packages/data-schemas/src/types/triggerDelivery.ts b/packages/data-schemas/src/types/triggerDelivery.ts index 1a80561e81..50c8a5f4de 100644 --- a/packages/data-schemas/src/types/triggerDelivery.ts +++ b/packages/data-schemas/src/types/triggerDelivery.ts @@ -24,7 +24,6 @@ export const AGENT_TRIGGER_WORKER_CAPABILITY_QUEUED_TURN_V2 = 'agent_queued_turn /** v3 workers prove a promoted steer's ownership before running its turn. */ export const AGENT_TRIGGER_WORKER_CAPABILITY_QUEUED_TURN_V3 = 'agent_queued_turn_v3'; export const AGENT_TRIGGER_WORKER_CAPABILITY_RESPONSES_V1 = 'responses_background_v1'; -export const AGENT_TRIGGER_WORKER_CAPABILITY_RESPONSES_V2 = 'responses_background_v2'; export const AGENT_TRIGGER_WORKER_CAPABILITY_RESPONSES_RESUME_V1 = 'responses_resume_v1'; export const AGENT_BACKGROUND_TOOL_RESULT_STORAGE_MAX_CHARS: number = 64 * 1024; export type AgentTriggerDeliveryOutcome = 'succeeded' | 'retry' | 'dead';