Skip to content

Commit 5d430ea

Browse files
committed
fix(service-knowledge): resolve a queued record sync's adapter through the source's binding
Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016WV4cwPhGV4g9p1BGQaZbP
1 parent 76ff5ed commit 5d430ea

2 files changed

Lines changed: 39 additions & 7 deletions

File tree

‎packages/services/service-knowledge/src/__tests__/record-sync-queue.test.ts‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,12 @@ function makeRealtime() {
161161
* Registers the SQL driver, the test object and, when asked, a realtime
162162
* service, in init(); the knowledge adapter in start(), as a store plugin does.
163163
*/
164-
function hostPlugin(file: string, realtime: IRealtimeService | undefined, adapter: IKnowledgeAdapter): Plugin {
164+
function hostPlugin(
165+
file: string,
166+
realtime: IRealtimeService | undefined,
167+
adapter: IKnowledgeAdapter,
168+
others: Record<string, IKnowledgeAdapter> = {},
169+
): Plugin {
165170
return {
166171
name: 'test.knowledge-queue-host',
167172
version: '1.0.0',
@@ -175,7 +180,9 @@ function hostPlugin(file: string, realtime: IRealtimeService | undefined, adapte
175180
if (realtime) ctx.registerService('realtime', realtime);
176181
},
177182
async start(ctx: PluginContext) {
178-
ctx.getService<KnowledgeService>('knowledge').registerAdapter('memory', adapter);
183+
const service = ctx.getService<KnowledgeService>('knowledge');
184+
service.registerAdapter('memory', adapter);
185+
for (const [id, other] of Object.entries(others)) service.registerAdapter(id, other);
179186
},
180187
} as unknown as Plugin;
181188
}
@@ -221,6 +228,8 @@ interface BootOptions {
221228
realtime?: boolean;
222229
queue?: boolean;
223230
knowledge?: KnowledgeServicePluginOptions;
231+
/** Further stores, registered beside `memory`. */
232+
adapters?: Record<string, IKnowledgeAdapter>;
224233
}
225234

226235
async function boot(opts: BootOptions = {}) {
@@ -231,7 +240,7 @@ async function boot(opts: BootOptions = {}) {
231240
const kernel = new ObjectKernel({ logger: { level: 'fatal' }, gracefulShutdown: false } as never);
232241
kernels.push(kernel);
233242
await kernel.use(new ObjectQLPlugin());
234-
await kernel.use(hostPlugin(file, realtime, memory.adapter));
243+
await kernel.use(hostPlugin(file, realtime, memory.adapter, opts.adapters));
235244
if (opts.queue !== false) {
236245
await kernel.use(new QueueServicePlugin({ adapter: 'db', db: { pollIntervalMs: 10, maxIdleIntervalMs: 20 } }));
237246
}
@@ -439,6 +448,24 @@ describe.each(TRIGGERS)('record sync through the queue — %s', (_label, realtim
439448
expect(t.docs.size).toBe(0);
440449
});
441450

451+
it("a source that names no adapter syncs through the queue to the deployment's store", async () => {
452+
const { adapter: _named, ...unplaced } = source();
453+
const decoy = makeAdapter();
454+
const t = await boot({
455+
realtime,
456+
adapters: { decoy: decoy.adapter },
457+
knowledge: { sources: [unplaced as KnowledgeSource], deploymentAdapter: 'memory' },
458+
});
459+
expect(t.service.recordSyncQueued).toBe(true);
460+
const row = await t.insert({ title: 'Leave policy', body: 'Twenty days of paid leave', status: 'published' });
461+
await t.drained();
462+
const rows = await t.queueRows();
463+
expect(rows.map((r) => r.status)).toEqual(['completed']);
464+
expect(t.docs.size).toBe(1);
465+
expect(decoy.docs.size).toBe(0);
466+
expect(await t.found('paid leave')).toEqual([String(row.id)]);
467+
});
468+
442469
it('a source unregistered before its message is processed: completed, no write, no retry', async () => {
443470
const t = await boot({ realtime });
444471
await t.queue.stop();

‎packages/services/service-knowledge/src/knowledge-service.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2481,10 +2481,15 @@ export class KnowledgeService implements IKnowledgeService {
24812481
);
24822482
return 'not-synced';
24832483
}
2484-
// An adapter not registered (yet) throws its refusal, so the message is
2485-
// retried: a store that registers late is met by a later attempt, and one
2486-
// that never does leaves a dead letter naming the source and the record.
2487-
const adapter = this.getAdapter(source.adapter);
2484+
// The source's adapter is resolved now ({@link adapterFor}): the one it
2485+
// names, else the deployment's store, else the only one registered. Where
2486+
// none can be resolved it throws its coded refusal — the named adapter is
2487+
// not mounted, no adapter is mounted (`503 SERVICE_UNAVAILABLE`), or
2488+
// several are and none is the deployment's (`400 VALIDATION_ERROR`) — so
2489+
// the message is retried: a store that registers late is met by a later
2490+
// attempt, and one that never does leaves a dead letter naming the source
2491+
// and the record.
2492+
const adapter = this.adapterFor(source);
24882493
await chainAfter(this.recordSyncTails, documentIdFor(source.name, message.recordId), () =>
24892494
this.syncRow(source, adapter, message.object, message.recordId, engine),
24902495
);

0 commit comments

Comments
 (0)