📬 feat: Persist Channel Events and Delivery Receipts (#17595)

* 📬 feat: Persist Channel Events and Delivery Receipts

* fix: enforce channel run identity and fenced recovery

* fix: fence and erase channel state during account deletion

* refactor: share channel owner admission with private persistence

* refactor: name channel account-erasure boundary explicitly

* fix: prove channel identity and delivery authority at storage boundaries

* refactor: enforce channel delivery protocol and terminal lifecycle

* fix: enforce channel tenant and operation boundaries

* fix: initialize channel config from its composition owner

* test: verify config installs channel storage reader

* test: complete config initializer contract in model mocks

* test: include channel config reader in migration fixture

* test: await SSE subscription before advancing keepalive time

* test: Observe and Drain Concurrent Fixture Work
This commit is contained in:
Dustin Healy 2026-11-10 21:03:57 -08:00 • committed by GitHub
parent 1458e841e4
commit 7dd4fafdf3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
48 changed files with 3816 additions and 54 deletions

View file

@ -5,16 +5,19 @@ const {
findMatchingPattern,
isDeploymentSkillId,
createMessageBudgetReader,
createChannelStorageConfigReader,
} = require('@librechat/api');
const getLogStores = require('~/cache/getLogStores');
const messageBudget = createMessageBudgetReader();
const channelStorageConfig = createChannelStorageConfigReader();
const methods = createMethods(mongoose, {
matchModelName,
findMatchingPattern,
isExternalSkillId: isDeploymentSkillId,
getCache: getLogStores,
getChannelStorageConfig: channelStorageConfig.getConfig,
getMCPAppMessageBudget: messageBudget.getBudget,
});
@ -28,5 +31,6 @@ const seedDatabase = async () => {
module.exports = {
...methods,
initializeMessageBudget: messageBudget.initialize,
initializeChannelStorageConfig: channelStorageConfig.initialize,
seedDatabase,
};

View file

@ -3,15 +3,21 @@ jest.mock('@librechat/api', () => ({
matchModelName: jest.fn(),
findMatchingPattern: jest.fn(),
isDeploymentSkillId: jest.fn(),
createChannelStorageConfigReader: jest.fn(() => ({
getConfig: jest.fn(),
initialize: jest.fn(),
})),
createMessageBudgetReader: jest.fn(() => ({ getBudget: jest.fn(), initialize: jest.fn() })),
}));
jest.mock('~/cache/getLogStores', () => jest.fn());
const { createMethods } = require('@librechat/data-schemas');
const { createMessageBudgetReader } = require('@librechat/api');
const { initializeMessageBudget } = require('./index');
const { createMessageBudgetReader, createChannelStorageConfigReader } = require('@librechat/api');
const { initializeMessageBudget, initializeChannelStorageConfig } = require('./index');
const reader = createMessageBudgetReader.mock.results[0].value;
const { getMCPAppMessageBudget } = createMethods.mock.calls[0][1];
const channelStorageConfigReader = createChannelStorageConfigReader.mock.results[0].value;
const { getMCPAppMessageBudget, getChannelStorageConfig: channelStorageConfigDependency } =
createMethods.mock.calls[0][1];
describe('message App budget dependency wiring', () => {
it('passes the same reader to storage and initializer to the composition root', () => {
@ -20,3 +26,11 @@ describe('message App budget dependency wiring', () => {
expect(reader.getBudget).not.toHaveBeenCalled();
});
});
describe('channel storage configuration dependency wiring', () => {
it('injects the deployment reader without reading config during module initialization', () => {
expect(channelStorageConfigDependency).toBe(channelStorageConfigReader.getConfig);
expect(initializeChannelStorageConfig).toBe(channelStorageConfigReader.initialize);
expect(channelStorageConfigReader.getConfig).not.toHaveBeenCalled();
});
});

View file

@ -35,6 +35,7 @@ jest.mock('@librechat/api', () => ({
jest.mock('~/models', () => ({
initializeMessageBudget: jest.fn(),
initializeChannelStorageConfig: jest.fn(),
saveMessage: jest.fn(async (req, message) => message),
getConvo: jest.fn(async () => null),
getMessages: jest.fn(async () => []),

View file

@ -3,6 +3,7 @@ const mockGetCacheMultiplier = jest.fn(() => 1);
jest.mock('~/models', () => ({
initializeMessageBudget: jest.fn(),
initializeChannelStorageConfig: jest.fn(),
getMultiplier: (...args) => mockGetMultiplier(...args),
getCacheMultiplier: (...args) => mockGetCacheMultiplier(...args),
}));

View file

@ -2,6 +2,7 @@ const mockSpendTokens = jest.fn().mockResolvedValue(undefined);
jest.mock('~/models', () => ({
initializeMessageBudget: jest.fn(),
initializeChannelStorageConfig: jest.fn(),
spendTokens: mockSpendTokens,
}));

View file

@ -33,6 +33,7 @@ jest.mock('@librechat/api', () => ({
jest.mock('~/models', () => ({
initializeMessageBudget: jest.fn(),
initializeChannelStorageConfig: jest.fn(),
saveMessage: jest.fn(async (req, message) => message),
getConvo: jest.fn(async () => null),
getMessages: jest.fn(async () => []),

View file

@ -20,6 +20,7 @@ const mockRecordCollectedUsage = jest
jest.mock('~/models', () => ({
initializeMessageBudget: jest.fn(),
initializeChannelStorageConfig: jest.fn(),
spendTokens: (...args) => mockSpendTokens(...args),
spendStructuredTokens: (...args) => mockSpendStructuredTokens(...args),
getMultiplier: mockGetMultiplier,

View file

@ -51,6 +51,7 @@ jest.mock('@librechat/api', () => ({
jest.mock('~/models', () => ({
initializeMessageBudget: jest.fn(),
initializeChannelStorageConfig: jest.fn(),
saveMessage: (...args) => mockSaveMessage(...args),
getPersistedPrivateTextId: async (...args) =>
(await mockHasPersistedPrivateText(...args)) ? 'protected-row-id' : null,

View file

@ -39,6 +39,7 @@ jest.mock('@librechat/api', () => ({
jest.mock('~/models', () => ({
initializeMessageBudget: jest.fn(),
initializeChannelStorageConfig: jest.fn(),
saveMessage: (...args) => mockSaveMessage(...args),
}));
@ -86,6 +87,28 @@ function mockSubscribeSuccess() {
});
}
/** A real HTTP socket must reach subscribe before fake time can exercise its keepalive. */
function openSilentStream() {
let resolveAttached;
const subscribed = new Promise((resolve) => {
resolveAttached = resolve;
});
mockGenerationJobManager.subscribe.mockImplementation(async (_streamId, _writeEvent, onDone) => {
resolveAttached(onDone);
return { unsubscribe: jest.fn() };
});
const pending = request(app)
.get('/agents/chat/stream/stream-123')
.then((res) => res);
const attached = Promise.race([
subscribed,
pending.then((res) => {
throw new Error(`Stream ended before subscribing: HTTP ${res.status}`);
}),
]);
return { pending, attached };
}
describe('SSE stream tenant isolation', () => {
beforeEach(() => {
jest.clearAllMocks();
@ -411,19 +434,8 @@ describe('SSE stream tenant isolation', () => {
status: 'running',
createdAt: 1000,
});
let finish;
mockGenerationJobManager.subscribe.mockImplementation(
async (_streamId, _writeEvent, onDone) => {
finish = onDone;
return { unsubscribe: jest.fn() };
},
);
const pending = request(app)
.get('/agents/chat/stream/stream-123')
.then((res) => res);
await jest.advanceTimersByTimeAsync(0);
await new Promise((resolve) => setImmediate(resolve));
const { pending, attached } = openSilentStream();
const finish = await attached;
await jest.advanceTimersByTimeAsync(60_000);
finish({ final: true });
const res = await pending;
@ -470,19 +482,8 @@ describe('SSE stream tenant isolation', () => {
status: 'running',
createdAt: 1000,
});
let finish;
mockGenerationJobManager.subscribe.mockImplementation(
async (_streamId, _writeEvent, onDone) => {
finish = onDone;
return { unsubscribe: jest.fn() };
},
);
const pending = request(app)
.get('/agents/chat/stream/stream-123')
.then((res) => res);
await jest.advanceTimersByTimeAsync(0);
await new Promise((resolve) => setImmediate(resolve));
const { pending, attached } = openSilentStream();
const finish = await attached;
await jest.advanceTimersByTimeAsync(60_000);
finish({ final: true });
const res = await pending;
@ -502,19 +503,8 @@ describe('SSE stream tenant isolation', () => {
status: 'running',
createdAt: 1000,
});
let finish;
mockGenerationJobManager.subscribe.mockImplementation(
async (_streamId, _writeEvent, onDone) => {
finish = onDone;
return { unsubscribe: jest.fn() };
},
);
const pending = request(app)
.get('/agents/chat/stream/stream-123')
.then((res) => res);
await jest.advanceTimersByTimeAsync(0);
await new Promise((resolve) => setImmediate(resolve));
const { pending, attached } = openSilentStream();
const finish = await attached;
await jest.advanceTimersByTimeAsync(60_000);
expect(mockGenerationJobManager.subscribe).toHaveBeenCalled();

View file

@ -17,6 +17,7 @@ jest.mock('~/server/services/start/tools', () => ({
jest.mock('~/models', () => ({
initializeMessageBudget: jest.fn(),
initializeChannelStorageConfig: jest.fn(),
getApplicableConfigs: jest.fn().mockResolvedValue([]),
getUserPrincipals: jest.fn().mockResolvedValue([]),
}));

View file

@ -22,6 +22,7 @@ jest.mock('@librechat/data-schemas', () => {
jest.mock('~/models', () => ({
initializeMessageBudget: jest.fn(),
initializeChannelStorageConfig: jest.fn(),
getApplicableConfigs: jest.fn().mockResolvedValue([]),
getUserPrincipals: jest.fn().mockResolvedValue([]),
}));
@ -61,14 +62,16 @@ jest.mock('@librechat/api', () => ({
// ── Tests ──────────────────────────────────────────────────────────────
const { getAppConfig, invalidateConfigCaches } = require('../app');
const { initializeMessageBudget } = require('~/models');
const { initializeMessageBudget, initializeChannelStorageConfig } = require('~/models');
// Capture module-composition calls before clearMocks resets the spy history.
const budgetInitializations = [...initializeMessageBudget.mock.calls];
const channelInitializations = [...initializeChannelStorageConfig.mock.calls];
const invalidationOptions = mockCreateConfigCacheInvalidation.mock.calls[0][0];
const pubSubOptions = mockCreateRedisPubSub.mock.calls[0][0];
test('Config supplies its actual reader exactly once during composition', () => {
expect(budgetInitializations).toEqual([[getAppConfig]]);
expect(channelInitializations).toEqual([[getAppConfig]]);
});
describe('invalidateConfigCaches', () => {

View file

@ -88,6 +88,7 @@ const {
// Config owns the reader; models never import this module to obtain it.
db.initializeMessageBudget(getAppConfig);
db.initializeChannelStorageConfig(getAppConfig);
/**
* Admin config writes clear the per-principal override caches, the MCP config-source servers

View file

@ -3,7 +3,11 @@ jest.mock('@librechat/api', () => ({
fetchModels: jest.fn(),
}));
jest.mock('./app');
jest.mock('~/models', () => ({ getUserKeyValues: jest.fn(), initializeMessageBudget: jest.fn() }));
jest.mock('~/models', () => ({
getUserKeyValues: jest.fn(),
initializeMessageBudget: jest.fn(),
initializeChannelStorageConfig: jest.fn(),
}));
jest.mock('@librechat/data-schemas', () => ({
...jest.requireActual('@librechat/data-schemas'),
logger: { debug: jest.fn(), error: jest.fn(), warn: jest.fn(), info: jest.fn() },

View file

@ -14,6 +14,7 @@ jest.mock('@librechat/data-schemas', () => ({
jest.mock('~/models', () => ({
getUserKeyValues: jest.fn(),
initializeMessageBudget: jest.fn(),
initializeChannelStorageConfig: jest.fn(),
}));
const exampleConfig = {

View file

@ -28,6 +28,7 @@ const { ImportBatchBuilder } = require('./importBatchBuilder');
jest.mock('~/models', () => ({
initializeMessageBudget: jest.fn(),
initializeChannelStorageConfig: jest.fn(),
bulkIncrementTagCounts: jest.fn(),
bulkSaveConvos: jest.fn(),
bulkSaveMessages: jest.fn(),

View file

@ -8,6 +8,7 @@ const importConversations = require('./importConversations');
jest.mock('~/models', () => ({
initializeMessageBudget: jest.fn(),
initializeChannelStorageConfig: jest.fn(),
bulkIncrementTagCounts: jest.fn(),
bulkSaveConvos: jest.fn(),
bulkSaveMessages: jest.fn(),