π€― LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
π fix(message): avoid jsonb arrow IS NULL predicate that crashes serverless PG engine (#16693)
Arvin Xu4697184f2473
diff --git a/packages/database/src/models/__tests__/messages/message.query.test.ts b/packages/database/src/models/__tests__/messages/message.query.test.tsindex f4ad5666eb5..7c731898aaf 100644--- a/packages/database/src/models/__tests__/messages/message.query.test.ts+++ b/packages/database/src/models/__tests__/messages/message.query.test.ts@@ -3189,4 +3189,81 @@ describe('MessageModel Query Tests', () => {expect(await messageModel.getLatestSpineMessageId({ topicId: 'nope' })).toBeUndefined();});});++ // Regression for the signal-tag exclusion predicate. It used to be+ // `metadata -> 'signal' IS NULL`, which crashes the production serverless+ // Postgres engine when used as a WHERE qual (rt_fetch out-of-bounds, SQLSTATE+ // XX000) and made the agent verifier fail to start. It was rewritten to+ // `NOT COALESCE(jsonb_exists(metadata, 'signal'), false)`; these lock in the+ // three metadata shapes the rewrite must handle. Server-DB (node-postgres)+ // only β the client PGlite path is skipped.+ describe.skipIf(!isServerDB)('getLatestSpineMessageId β signal predicate regression', () => {+ it('keeps null and signal-free object metadata, excludes only signal-tagged', async () => {+ await serverDB.insert(sessions).values([{ id: 'session1', userId }]);+ await serverDB.insert(topics).values([{ id: 'topic1', sessionId: 'session1', userId }]);+ await serverDB.insert(messages).values([+ {+ // null metadata β a spine candidate, must not be dropped by the predicate+ id: 'null-meta',+ userId,+ topicId: 'topic1',+ role: 'assistant',+ content: 'plain',+ createdAt: new Date('2023-01-01T00:00:00'),+ },+ {+ // object metadata without a `signal` key β also a spine candidate+ id: 'obj-meta',+ userId,+ topicId: 'topic1',+ role: 'assistant',+ content: 'has usage',+ metadata: { usage: { totalTokens: 10 } } as any,+ createdAt: new Date('2023-01-01T00:00:01'),+ },+ {+ // newest, but signal-tagged β the only row the predicate must exclude+ id: 'sig-meta',+ userId,+ topicId: 'topic1',+ role: 'assistant',+ content: 'callback',+ metadata: { signal: { type: 'monitor' } } as any,+ createdAt: new Date('2023-01-01T00:00:02'),+ },+ ]);++ // latest non-signal spine row is the signal-free object, not the newer+ // signal-tagged callback+ expect(await messageModel.getLatestSpineMessageId({ topicId: 'topic1' })).toBe('obj-meta');+ });++ it('returns the null-metadata row when it is the only spine candidate', async () => {+ await serverDB.insert(sessions).values([{ id: 'session1', userId }]);+ await serverDB.insert(topics).values([{ id: 'topic1', sessionId: 'session1', userId }]);+ await serverDB.insert(messages).values([+ {+ id: 'null-only',+ userId,+ topicId: 'topic1',+ role: 'assistant',+ content: 'plain',+ createdAt: new Date('2023-01-01T00:00:00'),+ },+ {+ id: 'sig-newer',+ userId,+ topicId: 'topic1',+ role: 'assistant',+ content: 'callback',+ metadata: { signal: { type: 'monitor' } } as any,+ createdAt: new Date('2023-01-01T00:00:01'),+ },+ ]);++ // guards the COALESCE: a naive `NOT jsonb_exists(metadata, 'signal')` yields+ // NULL for null metadata and would wrongly drop this row+ expect(await messageModel.getLatestSpineMessageId({ topicId: 'topic1' })).toBe('null-only');+ });+ });});diff --git a/packages/database/src/models/message.ts b/packages/database/src/models/message.tsindex 44e6ee76091..c9935e150e0 100644--- a/packages/database/src/models/message.ts+++ b/packages/database/src/models/message.ts@@ -2592,7 +2592,13 @@ export class MessageModel {eq(messages.topicId, topicId),not(eq(messages.role, 'tool')),threadId ? eq(messages.threadId, threadId) : isNull(messages.threadId),- sql`${messages.metadata} -> 'signal' IS NULL`,+ // Exclude signal-tagged assistants by key existence. The equivalent+ // `metadata -> 'signal' IS NULL` crashes the serverless Postgres engine+ // when used as a WHERE predicate (rt_fetch out-of-bounds, SQLSTATE+ // XX000) β the `->` operator only survives in the SELECT/ORDER BY+ // position, not as a filter qual. `jsonb_exists` is GIN-friendly and+ // equivalent for real data, since the signal tag is always an object.+ sql`NOT COALESCE(jsonb_exists(${messages.metadata}, 'signal'), false)`,this.ownership(),),)