Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(soup): keep long filters under the server's depth limit (#8739)
gbirmanbcea99ca42fd
diff --git a/apps/web/src/lib/queries/soup/graphql/ast.test.ts b/apps/web/src/lib/queries/soup/graphql/ast.test.tsindex 55727db84d..41f8dc9e31 100644--- a/apps/web/src/lib/queries/soup/graphql/ast.test.ts+++ b/apps/web/src/lib/queries/soup/graphql/ast.test.ts@@ -428,6 +428,51 @@ describe('makeGraphqlSoupInput', () => {).toThrow('unknown file association');});+ it('balances clause chains the server would reject as too deep', () => {+ // Mirrors graphql_soup_filter_input::validate_json_bounds: the filters+ // value is level 1 and every nested value, scalars included, adds one.+ const serverDepth = (value: unknown): number =>+ value && typeof value === 'object'+ ? 1 + Math.max(0, ...Object.values(value).map(serverDepth))+ : 1;+ const channelIds = (input: ReturnType<typeof makeInput>) =>+ [...JSON.stringify(input).matchAll(/"channelId":"([^"]+)"/g)].map(+ (found) => found[1]+ );++ for (const count of [31, 32, 500]) {+ const ids = Array.from(+ { length: count },+ (_, index) => `channel-${index}`+ );+ const input = makeInput({ include: { channelId: ids } });+ expect(serverDepth(input.initial?.filters)).toBeLessThanOrEqual(64);+ expect(channelIds(input)).toEqual(ids);+ }+ });++ it('keeps the shape of clause chains that already fit the depth limit', () => {+ expect(+ makeInput({ include: { channelId: ['first', 'second', 'third'] } })+ ).toMatchObject({+ initial: {+ filters: {+ channelFilter: {+ or: {+ left: { literal: { channelId: 'first' } },+ right: {+ or: {+ left: { literal: { channelId: 'second' } },+ right: { literal: { channelId: 'third' } },+ },+ },+ },+ },+ },+ },+ });+ });+it('preserves the stable favorites constraint without expanding entity IDs', () => {const body = { ...compileToAst(queryStateFrom({})), favorites_only: true };expect(diff --git a/apps/web/src/lib/queries/soup/graphql/ast.ts b/apps/web/src/lib/queries/soup/graphql/ast.tsindex 3d9ead4aab..d2337735d7 100644--- a/apps/web/src/lib/queries/soup/graphql/ast.ts+++ b/apps/web/src/lib/queries/soup/graphql/ast.ts@@ -108,28 +108,107 @@ function compileExpr<TLiteral>(}));}+/**+ * The server rejects a `filters` value nested deeper than 64 JSON levels+ * (`graphql_soup_filter_input::MAX_FILTER_DEPTH`), and `filters` wraps an+ * expression in at most two of them (`emailFilter.tree`).+ */+const MAX_EXPR_DEPTH = 62;++/**+ * Filter builders chain clauses one-sidedly and each and/or adds two JSON+ * levels, so about thirty clauses (the Channels view looking up labelled+ * channels by id) exceed the server's depth limit. Those expressions are+ * regrouped into balanced trees; expressions that already fit keep their+ * shape, so their variables and normalized-cache entries stay the same.+ */function compileExprWithLeaf<TLiteral>(ast: RestAst,mapLeaf: (literal: unknown) => GraphqlExprInput<TLiteral>+): GraphqlExprInput<TLiteral> {+ const compiled = compileExprNode(ast, mapLeaf);+ return jsonDepth(compiled) > MAX_EXPR_DEPTH+ ? compileExprNode(balanceOperatorRuns(ast), mapLeaf)+ : compiled;+}++function compileExprNode<TLiteral>(+ ast: RestAst,+ mapLeaf: (literal: unknown) => GraphqlExprInput<TLiteral>): GraphqlExprInput<TLiteral> {if ('&' in ast)return {and: {- left: compileExprWithLeaf(ast['&'][0], mapLeaf),- right: compileExprWithLeaf(ast['&'][1], mapLeaf),+ left: compileExprNode(ast['&'][0], mapLeaf),+ right: compileExprNode(ast['&'][1], mapLeaf),},};if ('|' in ast)return {or: {- left: compileExprWithLeaf(ast['|'][0], mapLeaf),- right: compileExprWithLeaf(ast['|'][1], mapLeaf),+ left: compileExprNode(ast['|'][0], mapLeaf),+ right: compileExprNode(ast['|'][1], mapLeaf),},};- if ('!' in ast) return { not: compileExprWithLeaf(ast['!'], mapLeaf) };+ if ('!' in ast) return { not: compileExprNode(ast['!'], mapLeaf) };return mapLeaf(ast.l);}+function jsonDepth(value: unknown): number {+ if (typeof value !== 'object' || value === null) return 1;+ let deepest = 0;+ for (const child of Object.values(value))+ deepest = Math.max(deepest, jsonDepth(child));+ return 1 + deepest;+}++type AssociativeOperator = '&' | '|';++function operatorPair(+ ast: RestAst,+ operator: AssociativeOperator+): [RestAst, RestAst] | undefined {+ if (operator === '&') return '&' in ast ? ast['&'] : undefined;+ return '|' in ast ? ast['|'] : undefined;+}++/** Flattens nested uses of one operator into its operands, in order. */+function collectOperands(+ ast: RestAst,+ operator: AssociativeOperator,+ operands: RestAst[]+): RestAst[] {+ const pair = operatorPair(ast, operator);+ if (!pair) {+ operands.push(ast);+ return operands;+ }+ collectOperands(pair[0], operator, operands);+ return collectOperands(pair[1], operator, operands);+}++function joinBalanced(+ operator: AssociativeOperator,+ operands: readonly RestAst[]+): RestAst {+ if (operands.length === 1) return operands[0];+ const middle = Math.ceil(operands.length / 2);+ const left = joinBalanced(operator, operands.slice(0, middle));+ const right = joinBalanced(operator, operands.slice(middle));+ return operator === '&' ? { '&': [left, right] } : { '|': [left, right] };+}++function balanceOperatorRuns(ast: RestAst): RestAst {+ if ('!' in ast) return { '!': balanceOperatorRuns(ast['!']) };+ const operator: AssociativeOperator | undefined =+ '&' in ast ? '&' : '|' in ast ? '|' : undefined;+ if (!operator) return ast;+ return joinBalanced(+ operator,+ collectOperands(ast, operator, []).map(balanceOperatorRuns)+ );+}+function mapDocumentPredicate(literal: unknown): GraphqlExprInput<GraphqlDocumentLiteralInput> {