Multi-Agent Collaboration for Team and Friends
TypeScript agent-automationagent-orchestrationagent-workflowai-agentsai-collaborationai-memoryai-workforceautonomous-agentsclaude-codeclaude-code-skillscodexcoding-agentmulti-agentmultiple-agentone-person-businessone-person-companyone-person-teamopencodesolopreneur
fix: harden dynamic D1 bind paths (#592)
Generfdaecfbb8304
diff --git a/scripts/ci/d1-dynamic-bind-contract.test.ts b/scripts/ci/d1-dynamic-bind-contract.test.tsnew file mode 100644index 000000000..cdd469f74--- /dev/null+++ b/scripts/ci/d1-dynamic-bind-contract.test.ts@@ -0,0 +1,62 @@+import { readFileSync } from "node:fs"+import { resolve } from "node:path"+import { describe, expect, it } from "vitest"+import { scanDynamicBindSites } from "./d1-dynamic-bind-contract"++type Strategy = "fixed-literal" | "exact-chunk" | "json-set" | "subquery" | "bounded-public-input"+interface ManifestEntry {+ key: string+ strategy: Strategy+ fixedParams?: number+ maxExport?: string+}++const root = resolve(import.meta.dirname, "../..")+const manifest = JSON.parse(+ readFileSync(resolve(import.meta.dirname, "fixtures/d1-dynamic-bind-sites.json"), "utf8"),+) as ManifestEntry[]++describe("D1 dynamic bind strategy contract", () => {+ it("requires every source site to have one reviewed strategy", () => {+ const sites = scanDynamicBindSites(root)+ const actual = sites.map((site) => site.key)+ const expected = manifest.map((entry) => entry.key).sort()+ expect(new Set(expected).size).toBe(expected.length)+ expect(actual).toEqual(expected)+ const expectedSites = new Map(sites.map((site) => [site.key, site]))+ for (const entry of manifest) {+ const site = expectedSites.get(entry.key)+ expect(entry.strategy).toBe(site?.strategyHint)+ expect(entry.fixedParams).toBe(site?.fixedParamsHint)+ }+ })++ it("requires strategy-specific bind budgets", () => {+ const exportedLimits = [+ "src/shared/src/constants.ts",+ "src/shared/src/schemas.ts",+ "src/shared/src/index.ts",+ ].map((file) => readFileSync(resolve(root, file), "utf8")).join("\n")++ for (const entry of manifest) {+ if (entry.strategy === "exact-chunk") {+ expect(entry.fixedParams, `${entry.key} needs fixedParams`).toBeTypeOf("number")+ expect(entry.fixedParams!, `${entry.key} has an invalid fixed-bind budget`).toBeGreaterThanOrEqual(0)+ expect(entry.fixedParams!, `${entry.key} has an invalid fixed-bind budget`).toBeLessThan(100)+ }+ if (entry.strategy === "bounded-public-input") {+ expect(entry.maxExport, `${entry.key} needs maxExport`).toMatch(/^[A-Z][A-Z0-9_]+$/)+ expect(exportedLimits).toMatch(new RegExp(`export const ${entry.maxExport}\\b`))+ }+ }+ })++ it("detects an unreviewed dynamic bind site", () => {+ const fixture = scanDynamicBindSites(root, {+ file: "fixture.ts",+ source: "export function naked(db: any, ids: string[]) { return db.select().where(inArray(table.id, ids)); }",+ })+ expect(fixture.map((site) => site.key)).toEqual(["fixture.ts:naked:inArray:1"])+ expect(manifest.some((entry) => entry.key === fixture[0]?.key)).toBe(false)+ })+})diff --git a/scripts/ci/d1-dynamic-bind-contract.ts b/scripts/ci/d1-dynamic-bind-contract.tsnew file mode 100644index 000000000..976b243f5--- /dev/null+++ b/scripts/ci/d1-dynamic-bind-contract.ts@@ -0,0 +1,158 @@+import { readFileSync, readdirSync } from "node:fs"+import { relative, resolve } from "node:path"+import ts from "typescript"++export type DynamicBindOperator = "inArray" | "notInArray" | "values" | "or-map" | "sql.join"++export interface DynamicBindSite {+ key: string+ file: string+ functionName: string+ operator: DynamicBindOperator+ strategyHint: "fixed-literal" | "exact-chunk" | "json-set" | "subquery"+ fixedParamsHint?: number+}++function sourceFiles(directory: string): string[] {+ return readdirSync(directory, { withFileTypes: true }).flatMap((entry) => {+ const path = resolve(directory, entry.name)+ if (entry.isDirectory()) return sourceFiles(path)+ return entry.isFile() && entry.name.endsWith(".ts") ? [path] : []+ })+}++function propertyName(node: ts.Node): string | undefined {+ if (ts.isIdentifier(node)) return node.text+ if (ts.isStringLiteral(node) || ts.isNumericLiteral(node)) return node.text+ return undefined+}++function isArrayType(type: ts.TypeNode | undefined): boolean {+ if (!type) return false+ if (ts.isArrayTypeNode(type) || ts.isTupleTypeNode(type)) return true+ return ts.isTypeReferenceNode(type)+ && ts.isIdentifier(type.typeName)+ && (type.typeName.text === "Array" || type.typeName.text === "ReadonlyArray")+}++function isDynamicValuesArgument(node: ts.Node): boolean {+ if (ts.isObjectLiteralExpression(node)) return false+ if (ts.isArrayLiteralExpression(node)) return node.elements.some(ts.isSpreadElement)+ if (!ts.isIdentifier(node)) return true+ for (let current: ts.Node | undefined = node.parent; current; current = current.parent) {+ if (ts.isFunctionLike(current)) {+ const parameter = current.parameters.find(+ (candidate) => ts.isIdentifier(candidate.name) && candidate.name.text === node.text,+ )+ if (parameter) return isArrayType(parameter.type)+ break+ }+ }+ return true+}++function enclosingFunctionName(node: ts.Node): string {+ for (let current: ts.Node | undefined = node.parent; current; current = current.parent) {+ if (ts.isFunctionDeclaration(current) && current.name) return current.name.text+ if (ts.isMethodDeclaration(current) && current.name) return propertyName(current.name) ?? "<method>"+ if ((ts.isArrowFunction(current) || ts.isFunctionExpression(current)) && current.parent) {+ if (ts.isVariableDeclaration(current.parent) && ts.isIdentifier(current.parent.name)) return current.parent.name.text+ if (ts.isPropertyAssignment(current.parent)) return propertyName(current.parent.name) ?? "<property>"+ }+ }+ return "<module>"+}++function enclosingFunction(node: ts.Node): ts.FunctionLikeDeclaration | undefined {+ for (let current: ts.Node | undefined = node.parent; current; current = current.parent) {+ if (ts.isFunctionLike(current)) return current+ }+ return undefined+}++function bindStrategyHint(node: ts.CallExpression, operator: DynamicBindOperator) {+ if (operator !== "inArray" && operator !== "notInArray") {+ return { strategyHint: "exact-chunk" as const, fixedParamsHint: 10 }+ }+ const argument = node.arguments[1]+ if (argument && ts.isArrayLiteralExpression(argument) && !argument.elements.some(ts.isSpreadElement)) {+ return { strategyHint: "fixed-literal" as const }+ }+ const sourceFile = node.getSourceFile()+ const argumentText = argument?.getText(sourceFile).replace(/!$/, "") ?? ""+ const functionText = enclosingFunction(node)?.getText(sourceFile) ?? ""+ if (argumentText.includes("jsonTextSet(")+ || new RegExp(`(?:const|let)\\s+${argumentText.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}\\s*=[\\s\\S]{0,80}?jsonTextSet\\(`).test(functionText)) {+ return { strategyHint: "json-set" as const }+ }+ if (argumentText.includes(".select(")+ || new RegExp(`(?:const|let)\\s+${argumentText.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}\\s*=\\s*db[\\s\\S]{0,80}?\\.select\\(`).test(functionText)) {+ return { strategyHint: "subquery" as const }+ }+ return { strategyHint: "exact-chunk" as const, fixedParamsHint: 10 }+}++function callOperator(node: ts.CallExpression): DynamicBindOperator | undefined {+ if (ts.isIdentifier(node.expression) && (node.expression.text === "inArray" || node.expression.text === "notInArray")) {+ return node.expression.text+ }+ if (ts.isPropertyAccessExpression(node.expression)) {+ if (node.expression.name.text === "join" && ts.isIdentifier(node.expression.expression) && node.expression.expression.text === "sql") {+ return "sql.join"+ }+ if (node.expression.name.text === "values" && node.arguments[0] && isDynamicValuesArgument(node.arguments[0])) {+ return "values"+ }+ }+ if (ts.isIdentifier(node.expression) && node.expression.text === "or"+ && node.arguments.some((argument) => ts.isSpreadElement(argument))) {+ return "or-map"+ }+ return undefined+}++export function scanDynamicBindSites(root: string, sourceOverride?: { file: string; source: string }): DynamicBindSite[] {+ const queryRoot = resolve(root, "src/shared/src/db/queries")+ const files = sourceOverride ? [sourceOverride.file] : sourceFiles(queryRoot)+ const sites: DynamicBindSite[] = []+ const ordinals = new Map<string, number>()++ for (const file of files) {+ const source = sourceOverride?.source ?? readFileSync(file, "utf8")+ const parsed = ts.createSourceFile(file, source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS)+ const visit = (node: ts.Node) => {+ if (ts.isCallExpression(node)) {+ const operator = callOperator(node)+ if (operator) {+ const functionName = enclosingFunctionName(node)+ const scope = `${file}:${functionName}:${operator}`+ const ordinal = (ordinals.get(scope) ?? 0) + 1+ ordinals.set(scope, ordinal)+ const relativeFile = sourceOverride ? sourceOverride.file : relative(root, file).replaceAll("\\", "/")+ const key = `${relativeFile}:${functionName}:${operator}:${ordinal}`+ const hint = bindStrategyHint(node, operator)+ const fixedParamsByKey: Record<string, number> = {+ "src/shared/src/db/queries/community/channel.ts:resolveVisibleChannelIdSet:inArray:1": 0,+ "src/shared/src/db/queries/community/channel.ts:resolveVisibleChannelIdSet:inArray:2": 2,+ "src/shared/src/db/queries/community/member.ts:getMembersByUserIds:inArray:1": 1,+ "src/shared/src/db/queries/community/reaction.ts:listReactionsByMessageIds:inArray:1": 0,+ }+ sites.push({+ key,+ file: relativeFile,+ functionName,+ operator,+ ...hint,+ ...(hint.strategyHint === "exact-chunk" && key in fixedParamsByKey+ ? { fixedParamsHint: fixedParamsByKey[key] }+ : {}),+ })+ }+ }+ ts.forEachChild(node, visit)+ }+ visit(parsed)+ }++ return sites.sort((left, right) => left.key.localeCompare(right.key))+}diff --git a/scripts/ci/fixtures/d1-dynamic-bind-sites.json b/scripts/ci/fixtures/d1-dynamic-bind-sites.jsonnew file mode 100644index 000000000..0581fdb8c--- /dev/null+++ b/scripts/ci/fixtures/d1-dynamic-bind-sites.json@@ -0,0 +1,678 @@+[+ {+ "key": "src/shared/src/db/queries/agent-skill.ts:syncAgentSkills:values:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/agent-skill.ts:syncGlobalSkills:values:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/agent.ts:getAgentsByIds:inArray:1",+ "strategy": "json-set"+ },+ {+ "key": "src/shared/src/db/queries/agent.ts:getExistingHandles:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/agent-inbox.ts:getInboxSnapshotForAgent:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/agent-inbox.ts:getInboxSnapshotForAgent:or-map:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/agent-inbox.ts:listAgentAllowedChannelIds:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/agent-inbox.ts:resolveScopeRefs:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/agent-inbox.ts:resolveScopeRefs:inArray:2",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/agent-inbox.ts:resolveScopeRefs:inArray:3",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/agent-inbox.ts:resolveScopeRefs:inArray:4",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/agent-inbox.ts:resolveScopeRefs:inArray:5",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/agent-inbox.ts:resolveScopeRefs:inArray:6",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/agent-inbox.ts:runChunk:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/agent-inbox.ts:runChunk:inArray:2",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/agent-inbox.ts:runChunk:inArray:3",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/agent-inbox.ts:toAgentMessages:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/attachment.ts:findPendingAttachmentsForSender:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/attachment.ts:listByMessageIds:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/attachment.ts:rebindPendingAttachmentsToChild:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/attachment.ts:rebindPendingAttachmentsToChild:inArray:2",+ "strategy": "fixed-literal"+ },+ {+ "key": "src/shared/src/db/queries/community/attachment.ts:rebindPendingAttachmentsToChild:inArray:3",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/attachment.ts:rebindPendingAttachmentsToChild:inArray:4",+ "strategy": "fixed-literal"+ },+ {+ "key": "src/shared/src/db/queries/community/attachment.ts:reserveAttachmentsForMessage:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/attachment.ts:reserveAttachmentsForMessage:sql.join:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/attachment.ts:unreserveAttachments:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/bot-audit-log.ts:pruneBotActivityEventsStatement:notInArray:1",+ "strategy": "subquery"+ },+ {+ "key": "src/shared/src/db/queries/community/bot.ts:findWakeCandidates:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/bot.ts:softDeleteBot:inArray:1",+ "strategy": "subquery"+ },+ {+ "key": "src/shared/src/db/queries/community/bot.ts:softDeleteBot:inArray:2",+ "strategy": "subquery"+ },+ {+ "key": "src/shared/src/db/queries/community/bot.ts:softDeleteBot:inArray:3",+ "strategy": "subquery"+ },+ {+ "key": "src/shared/src/db/queries/community/bot.ts:updateBot:inArray:1",+ "strategy": "subquery"+ },+ {+ "key": "src/shared/src/db/queries/community/bot.ts:updateBot:inArray:2",+ "strategy": "subquery"+ },+ {+ "key": "src/shared/src/db/queries/community/bot.ts:updateBot:inArray:3",+ "strategy": "subquery"+ },+ {+ "key": "src/shared/src/db/queries/community/bot.ts:updateBotModel:inArray:1",+ "strategy": "subquery"+ },+ {+ "key": "src/shared/src/db/queries/community/bot.ts:updateBotRuntime:inArray:1",+ "strategy": "subquery"+ },+ {+ "key": "src/shared/src/db/queries/community/category.ts:getCategoriesByIds:inArray:1",+ "strategy": "json-set"+ },+ {+ "key": "src/shared/src/db/queries/community/category.ts:reorderCategories:inArray:1",+ "strategy": "subquery"+ },+ {+ "key": "src/shared/src/db/queries/community/channel.ts:deleteChannelMemberAndChildParticipants:inArray:1",+ "strategy": "subquery"+ },+ {+ "key": "src/shared/src/db/queries/community/channel.ts:getChannelsByIds:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/channel.ts:getDirectChildThreadsByIds:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/channel.ts:listChildChannelsByParentMessageIds:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/channel.ts:resolveVisibleChannelIdSet:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 0+ },+ {+ "key": "src/shared/src/db/queries/community/channel.ts:resolveVisibleChannelIdSet:inArray:2",+ "strategy": "exact-chunk",+ "fixedParams": 2+ },+ {+ "key": "src/shared/src/db/queries/community/delete-media.ts:deleteChannelWithMediaAttempt:inArray:1",+ "strategy": "subquery"+ },+ {+ "key": "src/shared/src/db/queries/community/delete-media.ts:deleteChannelWithMediaAttempt:inArray:2",+ "strategy": "subquery"+ },+ {+ "key": "src/shared/src/db/queries/community/delete-media.ts:deleteChannelWithMediaAttempt:inArray:3",+ "strategy": "subquery"+ },+ {+ "key": "src/shared/src/db/queries/community/delete-media.ts:deleteChannelWithMediaAttempt:inArray:4",+ "strategy": "subquery"+ },+ {+ "key": "src/shared/src/db/queries/community/delete-media.ts:deleteChannelWithMediaAttempt:inArray:5",+ "strategy": "subquery"+ },+ {+ "key": "src/shared/src/db/queries/community/delete-media.ts:deleteChannelWithMediaAttempt:inArray:6",+ "strategy": "subquery"+ },+ {+ "key": "src/shared/src/db/queries/community/delete-media.ts:deleteServerWithMediaAttempt:inArray:1",+ "strategy": "subquery"+ },+ {+ "key": "src/shared/src/db/queries/community/delete-media.ts:deleteServerWithMediaAttempt:inArray:2",+ "strategy": "subquery"+ },+ {+ "key": "src/shared/src/db/queries/community/delete-media.ts:deleteServerWithMediaAttempt:inArray:3",+ "strategy": "subquery"+ },+ {+ "key": "src/shared/src/db/queries/community/delete-media.ts:deleteServerWithMediaAttempt:inArray:4",+ "strategy": "subquery"+ },+ {+ "key": "src/shared/src/db/queries/community/delete-media.ts:deleteServerWithMediaAttempt:inArray:5",+ "strategy": "subquery"+ },+ {+ "key": "src/shared/src/db/queries/community/delete-media.ts:deleteServerWithMediaAttempt:inArray:6",+ "strategy": "subquery"+ },+ {+ "key": "src/shared/src/db/queries/community/forum-post-delete.ts:deleteForumPostAttempt:inArray:1",+ "strategy": "subquery"+ },+ {+ "key": "src/shared/src/db/queries/community/forum-post-delete.ts:deleteForumPostAttempt:inArray:2",+ "strategy": "subquery"+ },+ {+ "key": "src/shared/src/db/queries/community/friendship.ts:findActive:inArray:1",+ "strategy": "fixed-literal"+ },+ {+ "key": "src/shared/src/db/queries/community/friendship.ts:getFriendUserIds:inArray:1",+ "strategy": "json-set"+ },+ {+ "key": "src/shared/src/db/queries/community/friendship.ts:hydrateApprovalsForDmMessages:inArray:1",+ "strategy": "json-set"+ },+ {+ "key": "src/shared/src/db/queries/community/friendship.ts:hydrateApprovalsForDmMessages:inArray:2",+ "strategy": "json-set"+ },+ {+ "key": "src/shared/src/db/queries/community/friendship.ts:listPending:inArray:1",+ "strategy": "json-set"+ },+ {+ "key": "src/shared/src/db/queries/community/friendship.ts:loadBotFlags:inArray:1",+ "strategy": "json-set"+ },+ {+ "key": "src/shared/src/db/queries/community/friendship.ts:loadProfiles:inArray:1",+ "strategy": "json-set"+ },+ {+ "key": "src/shared/src/db/queries/community/friendship.ts:sendRequest:inArray:1",+ "strategy": "fixed-literal"+ },+ {+ "key": "src/shared/src/db/queries/community/inbox.ts:listEligibleUnreadChannels:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/inbox.ts:listEligibleUnreadDms:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/inbox.ts:listThreadOpenersByChildIds:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/inbox.ts:listUnreadChannels:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/inbox.ts:listUnreadDms:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/inbox.ts:listUnreadForumOpeners:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/machine-session-epoch.ts:transitionLiveLease:inArray:1",+ "strategy": "subquery"+ },+ {+ "key": "src/shared/src/db/queries/community/machine.ts:revokeRunnerKeysForAgentStatement:inArray:1",+ "strategy": "subquery"+ },+ {+ "key": "src/shared/src/db/queries/community/mark.ts:countMarksForUser:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/mark.ts:listMarksForUser:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/member.ts:getCoMemberUserIds:inArray:1",+ "strategy": "subquery"+ },+ {+ "key": "src/shared/src/db/queries/community/member.ts:getMembersByUserIds:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 1+ },+ {+ "key": "src/shared/src/db/queries/community/member.ts:getMemberships:inArray:1",+ "strategy": "json-set"+ },+ {+ "key": "src/shared/src/db/queries/community/mention.ts:createMentions:values:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/mention.ts:listUnreadMentions:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/mention.ts:markChannelMentionsReadBuilder:inArray:1",+ "strategy": "subquery"+ },+ {+ "key": "src/shared/src/db/queries/community/message-tag.ts:filterMessageIdsByTag:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/message-tag.ts:listTagsForMessages:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/message.ts:getMessagesByIds:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/message.ts:getMessagesByIdsInScope:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/message.ts:listMessagesReferencingFriendship:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/message.ts:runChunk:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/message.ts:runChunk:inArray:2",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/notification-eligibility.ts:runChunk:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/notification-setting.ts:loadScopedSettings:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/notification-setting.ts:loadScopedSettings:inArray:2",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/reaction.ts:listReactionsByMessageIds:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 0+ },+ {+ "key": "src/shared/src/db/queries/community/read-state.ts:advanceReadStateRevisionWhenAnyBuilder:or-map:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/search.ts:searchMessagesInServer:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/server-folder.ts:insertFolderItems:values:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/server.ts:getServersByIds:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/thread.ts:addThreadParticipants:values:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/thread.ts:listParticipantsForChannels:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/thread.ts:listParticipantsForChannels:inArray:2",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/thread.ts:listParticipatingForumThreads:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/thread.ts:listParticipatingForumThreads:inArray:2",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/thread.ts:listParticipatingThreadIds:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/community/thread.ts:removeParticipantFromChildChannels:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/conversation.ts:getConversationsByIds:inArray:1",+ "strategy": "json-set"+ },+ {+ "key": "src/shared/src/db/queries/conversation.ts:getThreadsByParentMessages:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/email-account.ts:getEmailAccountsByAgents:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/inbox.ts:getUnreadCount:sql.join:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/inbox.ts:listUnreadConversations:sql.join:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/issue.ts:listIssues:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/issue.ts:listIssues:inArray:2",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/meeting-session.ts:claimMeetingSessions:inArray:1",+ "strategy": "json-set"+ },+ {+ "key": "src/shared/src/db/queries/overview.ts:getConversationCountsByAgent:inArray:1",+ "strategy": "json-set"+ },+ {+ "key": "src/shared/src/db/queries/overview.ts:getRecentTerminalTasks:inArray:1",+ "strategy": "json-set"+ },+ {+ "key": "src/shared/src/db/queries/overview.ts:getRecentTerminalTasks:inArray:2",+ "strategy": "fixed-literal"+ },+ {+ "key": "src/shared/src/db/queries/runtime.ts:deleteRuntimesByDaemonId:inArray:1",+ "strategy": "subquery"+ },+ {+ "key": "src/shared/src/db/queries/runtime.ts:getAgentRuntimesForWorkspace:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/task-message.ts:listTaskMessages:notInArray:1",+ "strategy": "fixed-literal"+ },+ {+ "key": "src/shared/src/db/queries/task-message.ts:listTaskMessages:notInArray:2",+ "strategy": "fixed-literal"+ },+ {+ "key": "src/shared/src/db/queries/task-message.ts:listTaskMessagesSince:notInArray:1",+ "strategy": "fixed-literal"+ },+ {+ "key": "src/shared/src/db/queries/task.ts:cancelActiveTasksByConversation:inArray:1",+ "strategy": "fixed-literal"+ },+ {+ "key": "src/shared/src/db/queries/task.ts:cancelTask:inArray:1",+ "strategy": "fixed-literal"+ },+ {+ "key": "src/shared/src/db/queries/task.ts:claimKillTasks:inArray:1",+ "strategy": "json-set"+ },+ {+ "key": "src/shared/src/db/queries/task.ts:claimKillTasks:inArray:2",+ "strategy": "subquery"+ },+ {+ "key": "src/shared/src/db/queries/task.ts:claimTask:inArray:1",+ "strategy": "fixed-literal"+ },+ {+ "key": "src/shared/src/db/queries/task.ts:claimTask:inArray:2",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/task.ts:countRunningTasks:inArray:1",+ "strategy": "fixed-literal"+ },+ {+ "key": "src/shared/src/db/queries/task.ts:failStaleKillTasks:inArray:1",+ "strategy": "fixed-literal"+ },+ {+ "key": "src/shared/src/db/queries/task.ts:failStaleRunningTasks:inArray:1",+ "strategy": "subquery"+ },+ {+ "key": "src/shared/src/db/queries/task.ts:failTask:inArray:1",+ "strategy": "fixed-literal"+ },+ {+ "key": "src/shared/src/db/queries/task.ts:findSteerableReplacement:inArray:1",+ "strategy": "fixed-literal"+ },+ {+ "key": "src/shared/src/db/queries/task.ts:findSteerableReplacement:inArray:2",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/task.ts:getActiveTaskByConversation:inArray:1",+ "strategy": "fixed-literal"+ },+ {+ "key": "src/shared/src/db/queries/task.ts:getTraceAgentsByTaskIds:inArray:1",+ "strategy": "json-set"+ },+ {+ "key": "src/shared/src/db/queries/task.ts:getTraceAgentsByTaskIds:inArray:2",+ "strategy": "json-set"+ },+ {+ "key": "src/shared/src/db/queries/task.ts:hasPendingTaskForConversation:inArray:1",+ "strategy": "fixed-literal"+ },+ {+ "key": "src/shared/src/db/queries/task.ts:listActiveTaskCountsByWorkspace:inArray:1",+ "strategy": "fixed-literal"+ },+ {+ "key": "src/shared/src/db/queries/task.ts:listActiveTaskCountsByWorkspace:inArray:2",+ "strategy": "json-set"+ },+ {+ "key": "src/shared/src/db/queries/task.ts:listActiveTasksByAgent:inArray:1",+ "strategy": "fixed-literal"+ },+ {+ "key": "src/shared/src/db/queries/task.ts:listActiveTasksByWorkspace:inArray:1",+ "strategy": "fixed-literal"+ },+ {+ "key": "src/shared/src/db/queries/task.ts:listActiveTasksByWorkspace:inArray:2",+ "strategy": "json-set"+ },+ {+ "key": "src/shared/src/db/queries/task.ts:listPendingTasksByRuntimes:inArray:1",+ "strategy": "json-set"+ },+ {+ "key": "src/shared/src/db/queries/task.ts:listPendingTasksByRuntimes:inArray:2",+ "strategy": "fixed-literal"+ },+ {+ "key": "src/shared/src/db/queries/task.ts:listTaskHistory:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/task.ts:listTaskHistory:inArray:2",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/task.ts:listTraces:inArray:1",+ "strategy": "fixed-literal"+ },+ {+ "key": "src/shared/src/db/queries/task.ts:listTraces:inArray:2",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/task.ts:supersedeTask:inArray:1",+ "strategy": "fixed-literal"+ },+ {+ "key": "src/shared/src/db/queries/user.ts:getUsersByIds:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ },+ {+ "key": "src/shared/src/db/queries/workspace-file-request.ts:markDispatched:inArray:1",+ "strategy": "exact-chunk",+ "fixedParams": 10+ }+]diff --git a/src/cli/daemon/config.test.ts b/src/cli/daemon/config.test.tsindex 2429723d5..fe025b23c 100644--- a/src/cli/daemon/config.test.ts+++ b/src/cli/daemon/config.test.ts@@ -60,6 +60,11 @@ describe("loadDaemonConfig env overrides", () => {expect(loadDaemonConfig().maxConcurrentTasks).toBe(10);});+ it("caps ALOOK_DAEMON_MAX_CONCURRENT_TASKS at the server poll limit", () => {+ process.env.ALOOK_DAEMON_MAX_CONCURRENT_TASKS = "125";+ expect(loadDaemonConfig().maxConcurrentTasks).toBe(50);+ });+it("ALOOK_MESSAGE_INACTIVITY_TIMEOUT='10m' → 600000", () => {process.env.ALOOK_MESSAGE_INACTIVITY_TIMEOUT = "10m";expect(loadDaemonConfig().messageInactivityTimeout).toBe(600000);diff --git a/src/cli/daemon/config.ts b/src/cli/daemon/config.tsindex db3d73bb6..c681d9bc3 100644--- a/src/cli/daemon/config.ts+++ b/src/cli/daemon/config.ts@@ -3,6 +3,7 @@ import { join } from "path";import { configDir } from "../lib/config.js";import { getServerUrl } from "../lib/env.js";import { getCurrentVersion } from "../lib/version.js";+import { MAX_POLL_TASKS } from "@alook/shared";export function pidFilePath(profile?: string): string {const name = profile ? `daemon_${profile}.pid` : "daemon.pid";@@ -112,8 +113,9 @@ export function loadDaemonConfig(profile?: string): DaemonConfig {sweepInterval: parseDuration(process.env.ALOOK_DAEMON_SWEEP_INTERVAL || "60s"),agentTimeout: parseDuration(process.env.ALOOK_AGENT_TIMEOUT || "12h"),messageInactivityTimeout: parseDuration(process.env.ALOOK_MESSAGE_INACTIVITY_TIMEOUT || "20m"),- maxConcurrentTasks: parseInt(- process.env.ALOOK_DAEMON_MAX_CONCURRENT_TASKS || "20",+ maxConcurrentTasks: Math.min(+ parseInt(process.env.ALOOK_DAEMON_MAX_CONCURRENT_TASKS || "20"),+ MAX_POLL_TASKS,),enableSteering: process.env.ALOOK_ENABLE_STEERING === "1",daemonId,diff --git a/src/shared/src/constants.ts b/src/shared/src/constants.tsindex c9e8acf97..b64fb9777 100644--- a/src/shared/src/constants.ts+++ b/src/shared/src/constants.ts@@ -100,6 +100,7 @@ export const COMMUNITY_MACHINE_PAIR_TOKEN_TTL_MS = 15 * 60_000;export const EVENT_POLL_INTERVAL_MS = Number(process.env.EVENT_POLL_INTERVAL_MS) || 2_000;export const AGENT_HANDLE_MIN_LENGTH = 4;export const MAX_TASKS_PER_TRACE = 256;+export const MAX_MEETING_CLAIMS_PER_POLL = 50;export const MeetingStatus = {PENDING: "pending",diff --git a/src/shared/src/db/queries/_chunk.ts b/src/shared/src/db/queries/_chunk.tsindex 32feba999..48eb9ae78 100644--- a/src/shared/src/db/queries/_chunk.ts+++ b/src/shared/src/db/queries/_chunk.ts@@ -12,6 +12,16 @@ export const D1_MAX_BIND_PARAMS = 100;// arrays at this size, not at D1_MAX_BIND_PARAMS, so the fixed params fit.export const D1_MAX_IN_PARAMS = 90;+export function maxInParams(fixedParams: number): number {+ if (!Number.isInteger(fixedParams) || fixedParams < 0) {+ throw new Error("fixed params must be a non-negative integer");+ }+ if (fixedParams >= D1_MAX_BIND_PARAMS) {+ throw new Error("fixed params must leave room for at least one IN parameter");+ }+ return D1_MAX_BIND_PARAMS - fixedParams;+}+/*** Split `arr` into ordered slices of at most `size` elements. `chunk([], n)`* returns `[]`; order is preserved. `size` must be >= 1.diff --git a/src/shared/src/db/queries/_json-set.ts b/src/shared/src/db/queries/_json-set.tsnew file mode 100644index 000000000..0e32c6f88--- /dev/null+++ b/src/shared/src/db/queries/_json-set.ts@@ -0,0 +1,8 @@+import { sql } from "drizzle-orm";+import type { Database } from "../index";++export function jsonTextSet(db: Database, values: readonly string[]) {+ return db+ .select({ value: sql<string>`CAST(value AS TEXT)`.as("value") })+ .from(sql`json_each(${JSON.stringify(values)})`);+}diff --git a/src/shared/src/db/queries/agent-link.ts b/src/shared/src/db/queries/agent-link.tsindex 9254eb3d5..e23cad218 100644--- a/src/shared/src/db/queries/agent-link.ts+++ b/src/shared/src/db/queries/agent-link.ts@@ -1,4 +1,4 @@-import { eq, and, or, inArray } from "drizzle-orm";+import { eq, and, or } from "drizzle-orm";import { agentLink, agent } from "../schema";import type { Database } from "../index";@@ -182,56 +182,6 @@ export async function getColleaguesForAgent(return [...asSource, ...asTarget];}-export async function getColleaguesForAgents(- db: Database,- agentIds: string[],- workspaceId: string,-) {- if (agentIds.length === 0) return [];-- const asSource = await db- .select({- agentId: agentLink.sourceAgentId,- name: agent.name,- emailHandle: agent.emailHandle,- description: agent.description,- instruction: agentLink.instruction,- })- .from(agentLink)- .innerJoin(- agent,- and(eq(agent.id, agentLink.targetAgentId), eq(agent.workspaceId, agentLink.workspaceId)),- )- .where(- and(- inArray(agentLink.sourceAgentId, agentIds),- eq(agentLink.workspaceId, workspaceId),- ),- );-- const asTarget = await db- .select({- agentId: agentLink.targetAgentId,- name: agent.name,- emailHandle: agent.emailHandle,- description: agent.description,- instruction: agentLink.instruction,- })- .from(agentLink)- .innerJoin(- agent,- and(eq(agent.id, agentLink.sourceAgentId), eq(agent.workspaceId, agentLink.workspaceId)),- )- .where(- and(- inArray(agentLink.targetAgentId, agentIds),- eq(agentLink.workspaceId, workspaceId),- ),- );-- return [...asSource, ...asTarget];-}-export async function getAllColleaguesForWorkspace(db: Database,workspaceId: string,diff --git a/src/shared/src/db/queries/agent.ts b/src/shared/src/db/queries/agent.tsindex 9b06499a4..7c5ea9ada 100644--- a/src/shared/src/db/queries/agent.ts+++ b/src/shared/src/db/queries/agent.ts@@ -1,6 +1,7 @@import { eq, and, desc, or, exists, inArray } from "drizzle-orm";import { agent, agentAccess } from "../schema";import type { Database } from "../index";+import { jsonTextSet } from "./_json-set";import { isPublic } from "../../utils/visibility";export async function getAgent(db: Database, id: string, workspaceId: string, userId?: string) {@@ -160,10 +161,11 @@ export async function getExistingHandles(db: Database, handles: string[]) {export async function getAgentsByIds(db: Database, ids: string[], workspaceId: string) {if (ids.length === 0) return [];+ const agentIds = jsonTextSet(db, [...new Set(ids)]);return db.select().from(agent)- .where(and(inArray(agent.id, ids), eq(agent.workspaceId, workspaceId)));+ .where(and(inArray(agent.id, agentIds), eq(agent.workspaceId, workspaceId)));}export async function getAllAgentsForWorkspace(db: Database, workspaceId: string) {diff --git a/src/shared/src/db/queries/community/bot.ts b/src/shared/src/db/queries/community/bot.tsindex 93253db01..a44713cf5 100644--- a/src/shared/src/db/queries/community/bot.ts+++ b/src/shared/src/db/queries/community/bot.ts@@ -1112,18 +1112,6 @@ export async function getApprovalRequestByDmMessageId(return (rows[0] as ApprovalRequestRow | undefined) ?? null;}-/** Batch-hydrate approval-request rows for a set of DM message ids. */-export async function listApprovalRequestsByDmMessageIds(- db: Database,- ids: string[]-): Promise<ApprovalRequestRow[]> {- if (ids.length === 0) return [];- return (await db- .select()- .from(communityBotApprovalRequest)- .where(inArray(communityBotApprovalRequest.dmMessageId, ids))) as ApprovalRequestRow[];-}-// ─── Machine cascade helpers ─────────────────────────────────────────────/**diff --git a/src/shared/src/db/queries/community/category.ts b/src/shared/src/db/queries/community/category.tsindex a5a15bc64..322902720 100644--- a/src/shared/src/db/queries/community/category.ts+++ b/src/shared/src/db/queries/community/category.ts@@ -1,13 +1,15 @@-import { asc, eq, inArray, count } from "drizzle-orm";+import { and, asc, eq, inArray, count, sql } from "drizzle-orm";import { communityCategory, communityChannel } from "../../community-schema";import type { Database } from "../../index";+import { jsonTextSet } from "../_json-set";export async function getCategoriesByIds(db: Database, categoryIds: string[]) {if (categoryIds.length === 0) return [];+ const ids = jsonTextSet(db, [...new Set(categoryIds)]);return db.select().from(communityCategory)- .where(inArray(communityCategory.id, categoryIds));+ .where(inArray(communityCategory.id, ids));}/**@@ -93,16 +95,47 @@ export async function hasChannels(db: Database, categoryId: string): Promise<booexport async function reorderCategories(db: Database,- _serverId: string,+ serverId: string,categoryIds: string[]) {- const statements = categoryIds.map((id, index) =>+ if (categoryIds.length === 0) return [];+ const desired = db.$with("desired_category_order").as(db- .update(communityCategory)- .set({ position: index })- .where(eq(communityCategory.id, id))+ .select({+ id: sql<string>`CAST(value AS TEXT)`.as("id"),+ position: sql<number>`CAST(key AS INTEGER)`.as("position"),+ })+ .from(sql`json_each(${JSON.stringify(categoryIds)})`));- if (statements.length > 0) {- await db.batch(statements as [typeof statements[0], ...typeof statements]);- }+ const desiredIds = db.select({ id: desired.id }).from(desired);+ const submittedCount = categoryIds.length;+ const uniqueCountMatches = sql`(+ SELECT COUNT(DISTINCT ${desired.id}) FROM ${desired}+ ) = ${submittedCount}`;+ const scopedCountMatches = sql`(+ SELECT COUNT(*)+ FROM ${communityCategory}+ WHERE ${communityCategory.serverId} = ${serverId}+ AND ${communityCategory.id} IN (${desiredIds})+ ) = ${submittedCount}`;++ return db+ .with(desired)+ .update(communityCategory)+ .set({+ position: sql<number>`(+ SELECT ${desired.position}+ FROM ${desired}+ WHERE ${desired.id} = ${communityCategory.id}+ )`,+ })+ .where(+ and(+ eq(communityCategory.serverId, serverId),+ inArray(communityCategory.id, desiredIds),+ uniqueCountMatches,+ scopedCountMatches+ )+ )+ .returning({ id: communityCategory.id, position: communityCategory.position });}diff --git a/src/shared/src/db/queries/community/channel.ts b/src/shared/src/db/queries/community/channel.tsindex 3085ae791..6fb9e9495 100644--- a/src/shared/src/db/queries/community/channel.ts+++ b/src/shared/src/db/queries/community/channel.ts@@ -1,4 +1,4 @@-import { eq, and, asc, desc, isNotNull, isNull, max, inArray, count, or, sql } from "drizzle-orm";+import { eq, and, asc, desc, isNotNull, isNull, inArray, count, or, sql } from "drizzle-orm";import type { SQLWrapper } from "drizzle-orm";import {communityChannel,@@ -11,7 +11,7 @@ import {import type { Database } from "../../index";import { PARTICIPANT_SOURCE } from "../../../constants/community";import { canSeePrivateChannel, visibilityIsDmParticipant } from "../../../utils/community-roles";-import { chunk, D1_MAX_IN_PARAMS } from "../_chunk";+import { chunk, D1_MAX_IN_PARAMS, maxInParams } from "../_chunk";// Column selection shared by every read query.const CHANNEL_COLUMNS = {@@ -478,22 +478,6 @@ export async function reorderChannels(}}-export async function getServersLastActivity(- db: Database,- serverIds: string[]-): Promise<Map<string, string>> {- if (serverIds.length === 0) return new Map();- const rows = await db- .select({- serverId: communityChannel.serverId,- latestAt: max(communityChannel.lastMessageAt),- })- .from(communityChannel)- .where(inArray(communityChannel.serverId, serverIds))- .groupBy(communityChannel.serverId);- return new Map(rows.filter((r) => r.latestAt).map((r) => [r.serverId, r.latestAt!]));-}-export async function getChannelsByIds(db: Database, channelIds: string[]) {if (channelIds.length === 0) return [];// Hydration on the mentions/unreads paths passes up to a full page (200) of@@ -943,31 +927,44 @@ async function resolveVisibleChannelIdSet(const { serverIds } = opts;if (serverIds.length === 0) return visible;- const rows = await db- .select({- id: communityChannel.id,- type: communityChannel.type,- categoryId: communityChannel.categoryId,- categoryPrivate: communityCategory.private,- creatorId: communityChannel.creatorId,- parentChannelId: communityChannel.parentChannelId,- })- .from(communityChannel)- .leftJoin(communityCategory, eq(communityCategory.id, communityChannel.categoryId))- .where(inArray(communityChannel.serverId, serverIds));+ const uniqueServerIds = [...new Set(serverIds)];+ const rows = (+ await Promise.all(+ chunk(uniqueServerIds, maxInParams(0)).map((ids) =>+ db+ .select({+ id: communityChannel.id,+ type: communityChannel.type,+ categoryId: communityChannel.categoryId,+ categoryPrivate: communityCategory.private,+ creatorId: communityChannel.creatorId,+ parentChannelId: communityChannel.parentChannelId,+ })+ .from(communityChannel)+ .leftJoin(communityCategory, eq(communityCategory.id, communityChannel.categoryId))+ .where(inArray(communityChannel.serverId, ids))+ )+ )+ ).flat();// The viewer's explicit channel/post member rows in these servers.- const memberRows = await db- .select({ channelId: communityChannelMember.channelId })- .from(communityChannelMember)- .innerJoin(communityChannel, eq(communityChannel.id, communityChannelMember.channelId))- .where(- and(- eq(communityChannelMember.userId, userId),- eq(communityChannelMember.relation, "access"),- inArray(communityChannel.serverId, serverIds)+ const memberRows = (+ await Promise.all(+ chunk(uniqueServerIds, maxInParams(2)).map((ids) =>+ db+ .select({ channelId: communityChannelMember.channelId })+ .from(communityChannelMember)+ .innerJoin(communityChannel, eq(communityChannel.id, communityChannelMember.channelId))+ .where(+ and(+ eq(communityChannelMember.userId, userId),+ eq(communityChannelMember.relation, "access"),+ inArray(communityChannel.serverId, ids)+ )+ ))- );+ )+ ).flat();const memberChannelIds = new Set(memberRows.map((r) => r.channelId));const byId = new Map(rows.map((r) => [r.id, r]));diff --git a/src/shared/src/db/queries/community/dm.ts b/src/shared/src/db/queries/community/dm.tsindex f4cb860c2..c0660628c 100644--- a/src/shared/src/db/queries/community/dm.ts+++ b/src/shared/src/db/queries/community/dm.ts@@ -1,4 +1,4 @@-import { eq, and, desc, isNull, inArray, ne, notExists, or } from "drizzle-orm";+import { eq, and, asc, desc, exists, isNull, ne, notExists, or, sql } from "drizzle-orm";import { alias } from "drizzle-orm/sqlite-core";import { communityChannel, communityChannelMember, communityFriendship } from "../../community-schema";import { user } from "../../schema";@@ -20,51 +20,57 @@ async function findDmChannelId(userAId: string,userBId: string): Promise<string | null> {- // Candidate DM channels the pair could share: only the DM channels userA- // holds an access row on (bounded to A's own DMs, not every DM in the- // system). A shared DM must appear here.- const aRows = await db- .select({ channelId: communityChannelMember.channelId })- .from(communityChannelMember)- .innerJoin(communityChannel, eq(communityChannel.id, communityChannelMember.channelId))+ const selfMember = alias(communityChannelMember, "dm_self_member");+ const peerMember = alias(communityChannelMember, "dm_peer_member");+ const thirdMember = alias(communityChannelMember, "dm_third_member");+ const rows = await db+ .select({ id: communityChannel.id })+ .from(communityChannel).where(and(eq(communityChannel.type, "dm"),- eq(communityChannelMember.relation, "access"),- eq(communityChannelMember.userId, userAId)- )- );- const candidateIds = aRows.map((r) => r.channelId);- if (candidateIds.length === 0) return null;-- // The full access-member set of A's DM channels — a pair's DM has EXACTLY- // {a,b} (guards against a 3rd member) and contains b.- const memberRows = await db- .select({- channelId: communityChannelMember.channelId,- userId: communityChannelMember.userId,- })- .from(communityChannelMember)- .where(- and(- inArray(communityChannelMember.channelId, candidateIds),- eq(communityChannelMember.relation, "access")+ exists(+ db+ .select({ one: sql<number>`1` })+ .from(selfMember)+ .where(+ and(+ eq(selfMember.channelId, communityChannel.id),+ eq(selfMember.userId, userAId),+ eq(selfMember.relation, "access")+ )+ )+ ),+ exists(+ db+ .select({ one: sql<number>`1` })+ .from(peerMember)+ .where(+ and(+ eq(peerMember.channelId, communityChannel.id),+ eq(peerMember.userId, userBId),+ eq(peerMember.relation, "access")+ )+ )+ ),+ notExists(+ db+ .select({ one: sql<number>`1` })+ .from(thirdMember)+ .where(+ and(+ eq(thirdMember.channelId, communityChannel.id),+ eq(thirdMember.relation, "access"),+ ne(thirdMember.userId, userAId),+ ne(thirdMember.userId, userBId)+ )+ )+ ))- );-- const byChannel = new Map<string, Set<string>>();- for (const r of memberRows) {- let set = byChannel.get(r.channelId);- if (!set) {- set = new Set<string>();- byChannel.set(r.channelId, set);- }- set.add(r.userId);- }- for (const [channelId, set] of byChannel) {- if (set.size === 2 && set.has(userAId) && set.has(userBId)) return channelId;- }- return null;+ )+ .orderBy(asc(communityChannel.id))+ .limit(1);+ return rows[0]?.id ?? null;}export async function createOrGetDM(@@ -163,22 +169,8 @@ export async function listDmChannelIdsForUser(}export async function listDMs(db: Database, userId: string) {- // DM channels the user holds an access row on, joined to the OTHER access- // member → user for display. Order by last_message_at DESC.- const selfRows = await db- .select({ channelId: communityChannelMember.channelId })- .from(communityChannelMember)- .innerJoin(communityChannel, eq(communityChannel.id, communityChannelMember.channelId))- .where(- and(- eq(communityChannelMember.userId, userId),- eq(communityChannelMember.relation, "access"),- eq(communityChannel.type, "dm")- )- );- const dmIds = selfRows.map((r) => r.channelId);- if (dmIds.length === 0) return [];-+ const selfMember = alias(communityChannelMember, "dm_list_self");+ const peerMember = alias(communityChannelMember, "dm_list_peer");const rows = await db.select({id: communityChannel.id,@@ -192,20 +184,32 @@ export async function listDMs(db: Database, userId: string) {}).from(communityChannel).innerJoin(- communityChannelMember,+ selfMember,and(- eq(communityChannelMember.channelId, communityChannel.id),- eq(communityChannelMember.relation, "access")+ eq(selfMember.channelId, communityChannel.id),+ eq(selfMember.userId, userId),+ eq(selfMember.relation, "access")))- .innerJoin(user, eq(user.id, communityChannelMember.userId))+ .innerJoin(+ peerMember,+ and(+ eq(peerMember.channelId, communityChannel.id),+ eq(peerMember.relation, "access"),+ ne(peerMember.userId, userId)+ )+ )+ .innerJoin(user, eq(user.id, peerMember.userId)).where(and(- inArray(communityChannel.id, dmIds),+ eq(communityChannel.type, "dm"),isNull(user.deletedAt)))- .orderBy(desc(communityChannel.lastMessageAt));+ .orderBy(+ desc(sql`COALESCE(${communityChannel.lastMessageAt}, ${communityChannel.createdAt})`),+ asc(communityChannel.id)+ );// The join above returns EVERY access member (including self). Keep only the// peer rows, then de-dupe defensively by peer (guards the deferred@@ -213,7 +217,6 @@ export async function listDMs(db: Database, userId: string) {const seenPeers = new Set<string>();const result: typeof rows = [];for (const r of rows) {- if (r.otherUserId === userId) continue;if (seenPeers.has(r.otherUserId)) continue;seenPeers.add(r.otherUserId);result.push(r);diff --git a/src/shared/src/db/queries/community/friendship.ts b/src/shared/src/db/queries/community/friendship.tsindex 221f043e8..747dc492d 100644--- a/src/shared/src/db/queries/community/friendship.ts+++ b/src/shared/src/db/queries/community/friendship.ts@@ -17,6 +17,7 @@ import type {import { createOrGetDM } from "./dm";import { createMessage, listMessagesReferencingFriendship } from "./message";import { nanoid } from "nanoid";+import { jsonTextSet } from "../_json-set";function newFriendshipId(): string {return "fr_" + nanoid();@@ -46,6 +47,7 @@ async function loadProfiles(): Promise<Map<string, ProfileWithFlags>> {const unique = [...new Set(ids.filter(Boolean))];if (unique.length === 0) return new Map();+ const profileIds = jsonTextSet(db, unique);const rows = await db.select({id: user.id,@@ -56,7 +58,7 @@ async function loadProfiles(ownerUserId: user.ownerUserId,}).from(user)- .where(inArray(user.id, unique));+ .where(inArray(user.id, profileIds));return new Map(rows.map((r) => [r.id,@@ -242,6 +244,8 @@ async function loadBotFlags(ids: string[]): Promise<Map<string, { isBot: boolean; ownerUserId: string | null; name: string }>> {const unique = [...new Set(ids)];+ if (unique.length === 0) return new Map();+ const flagIds = jsonTextSet(db, unique);const rows = await db.select({id: user.id,@@ -250,7 +254,7 @@ async function loadBotFlags(name: user.name,}).from(user)- .where(inArray(user.id, unique));+ .where(inArray(user.id, flagIds));return new Map(rows.map((r) => [r.id, { isBot: !!r.isBot, ownerUserId: r.ownerUserId, name: r.name }]));@@ -806,10 +810,11 @@ export async function getFriendUserIds(db: Database, userId: string): Promise<stif (otherSideIds.length === 0) {return [...new Set([...friendIds, ...selfBotIds])];}+ const liveIds = jsonTextSet(db, [...new Set(otherSideIds)]);const liveOthers = await db.select({ id: user.id }).from(user)- .where(and(inArray(user.id, otherSideIds), isNull(user.deletedAt)));+ .where(and(inArray(user.id, liveIds), isNull(user.deletedAt)));const liveOtherSet = new Set(liveOthers.map((r) => r.id));const liveSelfBotIds = selfBotIds.filter((id) => liveOtherSet.has(id));return [...new Set([...friendIds, ...liveSelfBotIds])];@@ -988,13 +993,14 @@ export async function listPending(db: Database, userId: string) {// Own outgoing + own bots' outgoing. The peer displayed is the addressee.const outgoingRequesterIds = [userId, ...ownBotIds];+ const requesterIds = jsonTextSet(db, [...new Set(outgoingRequesterIds)]);const outgoing = await db.select(rowProjection).from(communityFriendship).innerJoin(user, eq(user.id, communityFriendship.addresseeId)).where(and(- inArray(communityFriendship.requesterId, outgoingRequesterIds),+ inArray(communityFriendship.requesterId, requesterIds),eq(communityFriendship.status, "pending")));@@ -1456,6 +1462,7 @@ export async function hydrateApprovalsForDmMessages(): Promise<Map<string, FriendApprovalPayload>> {const out = new Map<string, FriendApprovalPayload>();if (dmMessageIds.length === 0) return out;+ const messageIds = jsonTextSet(db, [...new Set(dmMessageIds)]);const rows = await db.select({@@ -1473,10 +1480,11 @@ export async function hydrateApprovalsForDmMessages(communityFriendship,eq(communityFriendship.id, communityMessage.friendshipId))- .where(inArray(communityMessage.id, dmMessageIds));+ .where(inArray(communityMessage.id, messageIds));// Resolve each card message's DM-channel peers (relation='access' members).const cardChannelIds = [...new Set(rows.map((r) => r.channelId))];+ const channelIds = cardChannelIds.length ? jsonTextSet(db, cardChannelIds) : null;const memberRows = cardChannelIds.length? await db.select({@@ -1486,7 +1494,7 @@ export async function hydrateApprovalsForDmMessages(.from(communityChannelMember).where(and(- inArray(communityChannelMember.channelId, cardChannelIds),+ inArray(communityChannelMember.channelId, channelIds!),eq(communityChannelMember.relation, "access")))diff --git a/src/shared/src/db/queries/community/machine.ts b/src/shared/src/db/queries/community/machine.tsindex d78373929..271cec2fc 100644--- a/src/shared/src/db/queries/community/machine.ts+++ b/src/shared/src/db/queries/community/machine.ts@@ -1,4 +1,4 @@-import { and, eq, gt, desc, isNull, inArray } from "drizzle-orm";+import { and, eq, gt, desc, isNull, exists, inArray, sql } from "drizzle-orm";import {communityMachineToken,communityMachine,@@ -290,12 +290,38 @@ export async function reconcileBotActivityFromRunningAgents(});if (stale.length === 0) return [];- await db+ const staleStatuses = db.$with("stale_bot_statuses").as(+ db+ .select({+ userId: sql<string>`CAST(json_extract(value, '$[0]') AS TEXT)`.as("user_id"),+ statusEmoji: sql<string>`CAST(json_extract(value, '$[1]') AS TEXT)`.as("status_emoji"),+ statusText: sql<string>`CAST(json_extract(value, '$[2]') AS TEXT)`.as("status_text"),+ })+ .from(sql`json_each(${JSON.stringify(+ stale.map((row) => [row.userId, row.statusEmoji, row.statusText])+ )})`)+ );+ const updated = await db+ .with(staleStatuses).update(communityUserProfile).set({ statusEmoji: idle.emoji, statusText: idle.text })- .where(inArray(communityUserProfile.userId, stale.map((r) => r.userId)));+ .where(+ exists(+ db+ .select({ one: sql<number>`1` })+ .from(staleStatuses)+ .where(+ and(+ eq(staleStatuses.userId, communityUserProfile.userId),+ eq(staleStatuses.statusEmoji, communityUserProfile.statusEmoji),+ eq(staleStatuses.statusText, communityUserProfile.statusText)+ )+ )+ )+ )+ .returning({ userId: communityUserProfile.userId });- return stale.map((r) => ({+ return updated.map((r) => ({botUserId: r.userId,statusEmoji: idle.emoji,statusText: idle.text,diff --git a/src/shared/src/db/queries/community/member.ts b/src/shared/src/db/queries/community/member.tsindex c76571cff..1a5b56812 100644--- a/src/shared/src/db/queries/community/member.ts+++ b/src/shared/src/db/queries/community/member.ts@@ -15,6 +15,8 @@ import { escapeLikePattern } from "../../../utils/sql-like";import { canSeePrivateChannel, reachIsParticipantSet } from "../../../utils/community-roles";import { resolveChannelAccessContext } from "./channel";import { isThreadParticipant } from "./thread";+import { chunk, maxInParams } from "../_chunk";+import { jsonTextSet } from "../_json-set";export async function addMember(db: Database,@@ -138,69 +140,39 @@ export async function getMembersByUserIds(userIds: string[]) {if (userIds.length === 0) return [];- return db- .select({- id: communityServerMember.id,- serverId: communityServerMember.serverId,- userId: communityServerMember.userId,- role: communityServerMember.role,- joinedAt: communityServerMember.joinedAt,- userName: user.name,- userEmail: user.email,- userImage: user.image,- userIsBot: user.isBot,- userOwnerUserId: user.ownerUserId,- discriminator: user.discriminator,- statusEmoji: communityUserProfile.statusEmoji,- statusText: communityUserProfile.statusText,- })- .from(communityServerMember)- .innerJoin(user, eq(communityServerMember.userId, user.id))- .leftJoin(communityUserProfile, eq(communityUserProfile.userId, user.id))- .where(- and(- eq(communityServerMember.serverId, serverId),- inArray(communityServerMember.userId, userIds),- isNull(user.deletedAt)- )- );-}--export async function updateRailOrder(- db: Database,- serverId: string,- userId: string,- railOrder: number-) {- await db- .update(communityServerMember)- .set({ railOrder })- .where(- and(- eq(communityServerMember.serverId, serverId),- eq(communityServerMember.userId, userId)+ const uniqueUserIds = [...new Set(userIds)];+ return (+ await Promise.all(+ chunk(uniqueUserIds, maxInParams(1)).map((ids) =>+ db+ .select({+ id: communityServerMember.id,+ serverId: communityServerMember.serverId,+ userId: communityServerMember.userId,+ role: communityServerMember.role,+ joinedAt: communityServerMember.joinedAt,+ userName: user.name,+ userEmail: user.email,+ userImage: user.image,+ userIsBot: user.isBot,+ userOwnerUserId: user.ownerUserId,+ discriminator: user.discriminator,+ statusEmoji: communityUserProfile.statusEmoji,+ statusText: communityUserProfile.statusText,+ })+ .from(communityServerMember)+ .innerJoin(user, eq(communityServerMember.userId, user.id))+ .leftJoin(communityUserProfile, eq(communityUserProfile.userId, user.id))+ .where(+ and(+ eq(communityServerMember.serverId, serverId),+ inArray(communityServerMember.userId, ids),+ isNull(user.deletedAt)+ )+ ))- );-}--export async function bulkUpdateRailOrder(- db: Database,- userId: string,- orderedServerIds: string[]-) {- if (orderedServerIds.length === 0) return;- const statements = orderedServerIds.map((serverId, railOrder) =>- db- .update(communityServerMember)- .set({ railOrder })- .where(- and(- eq(communityServerMember.userId, userId),- eq(communityServerMember.serverId, serverId)- )- )- );- await db.batch(statements as [typeof statements[0], ...typeof statements]);+ )+ ).flat();}export async function listMemberServerIds(db: Database, userId: string) {@@ -423,13 +395,14 @@ export async function getMemberById(export async function getMemberships(db: Database, userId: string, serverIds: string[]) {if (serverIds.length === 0) return [];+ const ids = jsonTextSet(db, [...new Set(serverIds)]);return db.select().from(communityServerMember).where(and(eq(communityServerMember.userId, userId),- inArray(communityServerMember.serverId, serverIds)+ inArray(communityServerMember.serverId, ids)));}diff --git a/src/shared/src/db/queries/community/mention.ts b/src/shared/src/db/queries/community/mention.tsindex 0d583261c..dfd502377 100644--- a/src/shared/src/db/queries/community/mention.ts+++ b/src/shared/src/db/queries/community/mention.ts@@ -128,24 +128,6 @@ export async function listUnreadMentions(return opts.limit !== undefined ? merged.slice(0, opts.limit) : merged;}-export async function markMentionsRead(- db: Database,- userId: string,- messageIds: string[]-) {- if (messageIds.length === 0) return;-- await db- .update(communityMention)- .set({ read: 1 })- .where(- and(- eq(communityMention.userId, userId),- inArray(communityMention.messageId, messageIds)- )- );-}-export async function markAllMentionsRead(db: Database, userId: string) {await db.update(communityMention)@@ -188,22 +170,6 @@ export async function markAllMentionsReadWithRevision(};}-export async function markChannelMentionsRead(db: Database, userId: string, channelId: string) {- const mentionIds = await db- .select({ id: communityMention.id })- .from(communityMention)- .innerJoin(communityMessage, eq(communityMention.messageId, communityMessage.id))- .where(and(- eq(communityMention.userId, userId),- eq(communityMention.read, 0),- eq(communityMessage.channelId, channelId)- ));- if (mentionIds.length === 0) return;- await db.update(communityMention)- .set({ read: 1 })- .where(inArray(communityMention.id, mentionIds.map((r) => r.id)));-}-/*** Batch-friendly builder version of `markChannelMentionsRead`. Collapses the* two-step "select-ids-then-update" into a single UPDATE with a correlateddiff --git a/src/shared/src/db/queries/community/reaction.ts b/src/shared/src/db/queries/community/reaction.tsindex 2581af3e7..11fb5463c 100644--- a/src/shared/src/db/queries/community/reaction.ts+++ b/src/shared/src/db/queries/community/reaction.ts@@ -1,6 +1,7 @@import { eq, and, inArray } from "drizzle-orm";import { communityReaction } from "../../community-schema";import type { Database } from "../../index";+import { chunk, maxInParams } from "../_chunk";export async function addReaction(db: Database,@@ -47,8 +48,14 @@ export async function listReactionsByMessageIds(_currentUserId: string) {if (messageIds.length === 0) return [];- return db- .select()- .from(communityReaction)- .where(inArray(communityReaction.messageId, messageIds));+ return (+ await Promise.all(+ chunk([...new Set(messageIds)], maxInParams(0)).map((ids) =>+ db+ .select()+ .from(communityReaction)+ .where(inArray(communityReaction.messageId, ids))+ )+ )+ ).flat();}diff --git a/src/shared/src/db/queries/conversation.ts b/src/shared/src/db/queries/conversation.tsindex aa15d1135..a57275ada 100644--- a/src/shared/src/db/queries/conversation.ts+++ b/src/shared/src/db/queries/conversation.ts@@ -1,6 +1,7 @@import { eq, and, desc, ne, lt, sql, inArray, isNull, isNotNull } from "drizzle-orm";import { conversation, message } from "../schema";import type { Database } from "../index";+import { jsonTextSet } from "./_json-set";import { TASK_TYPES, type TaskType } from "../../constants";@@ -42,10 +43,11 @@ export async function getConversation(db: Database, id: string, workspaceId: strexport async function getConversationsByIds(db: Database, ids: string[], workspaceId: string) {if (ids.length === 0) return [];+ const conversationIds = jsonTextSet(db, [...new Set(ids)]);return db.select().from(conversation)- .where(and(inArray(conversation.id, ids), eq(conversation.workspaceId, workspaceId)));+ .where(and(inArray(conversation.id, conversationIds), eq(conversation.workspaceId, workspaceId)));}export async function listConversations(diff --git a/src/shared/src/db/queries/meeting-session.ts b/src/shared/src/db/queries/meeting-session.tsindex d799390b2..dc907c7d3 100644--- a/src/shared/src/db/queries/meeting-session.ts+++ b/src/shared/src/db/queries/meeting-session.ts@@ -1,6 +1,8 @@import { eq, and, desc, lte, isNotNull, inArray } from "drizzle-orm";import { meetingSession, agent } from "../schema";import type { Database } from "../index";+import { jsonTextSet } from "./_json-set";+import { MAX_MEETING_CLAIMS_PER_POLL } from "../../constants";export async function createMeetingSession(db: Database,@@ -127,12 +129,13 @@ export async function claimMeetingSessions(startedAt: string,) {if (ids.length === 0) return [];+ const meetingIds = jsonTextSet(db, [...new Set(ids)]);return db.update(meetingSession).set({ status: "joining", startedAt, updatedAt: new Date().toISOString() }).where(and(- inArray(meetingSession.id, ids),+ inArray(meetingSession.id, meetingIds),eq(meetingSession.workspaceId, workspaceId),eq(meetingSession.status, "scheduled"),)@@ -157,7 +160,8 @@ export async function deleteMeetingSession(export async function listScheduledMeetings(db: Database,workspaceId: string,- beforeOrAt: string+ beforeOrAt: string,+ limit = MAX_MEETING_CLAIMS_PER_POLL) {return db.select({@@ -181,7 +185,8 @@ export async function listScheduledMeetings(lte(meetingSession.scheduledAt, beforeOrAt)))- .orderBy(meetingSession.scheduledAt);+ .orderBy(meetingSession.scheduledAt, meetingSession.id)+ .limit(limit);}export async function listMeetingsWithSchedule(diff --git a/src/shared/src/db/queries/overview.ts b/src/shared/src/db/queries/overview.tsindex a5b6f5fc3..9b5dec1a9 100644--- a/src/shared/src/db/queries/overview.ts+++ b/src/shared/src/db/queries/overview.ts@@ -2,6 +2,7 @@ import { eq, and, count, desc, inArray, ne, sql } from "drizzle-orm";import { emails, agentEmailAccount, agentTaskQueue, conversation } from "../schema";import type { Database } from "../index";import { TASK_TYPES } from "../../constants";+import { jsonTextSet } from "./_json-set";export async function getEmailStatsByWorkspace(db: Database, workspaceId: string) {const [stats] = await db@@ -70,6 +71,7 @@ export async function getRecentTerminalTasks(limit = 15) {if (visibleAgentIds.length === 0) return [];+ const agentIds = jsonTextSet(db, [...new Set(visibleAgentIds)]);return db.select({id: agentTaskQueue.id,@@ -85,7 +87,7 @@ export async function getRecentTerminalTasks(.where(and(eq(agentTaskQueue.workspaceId, workspaceId),- inArray(agentTaskQueue.agentId, visibleAgentIds),+ inArray(agentTaskQueue.agentId, agentIds),ne(agentTaskQueue.type, TASK_TYPES.KILL_TASK),inArray(agentTaskQueue.status, ["completed", "failed", "cancelled"]))@@ -96,6 +98,7 @@ export async function getRecentTerminalTasks(export async function getConversationCountsByAgent(db: Database, workspaceId: string, visibleAgentIds: string[]) {if (visibleAgentIds.length === 0) return [];+ const agentIds = jsonTextSet(db, [...new Set(visibleAgentIds)]);return db.select({agentId: conversation.agentId,@@ -105,7 +108,7 @@ export async function getConversationCountsByAgent(db: Database, workspaceId: st.where(and(eq(conversation.workspaceId, workspaceId),- inArray(conversation.agentId, visibleAgentIds)+ inArray(conversation.agentId, agentIds))).groupBy(conversation.agentId);diff --git a/src/shared/src/db/queries/runtime.ts b/src/shared/src/db/queries/runtime.tsindex 6d8a7dd59..890ceb3a3 100644--- a/src/shared/src/db/queries/runtime.ts+++ b/src/shared/src/db/queries/runtime.ts@@ -175,7 +175,7 @@ export async function deleteRuntimesByDaemonId(daemonId: string,workspaceId: string) {- const runtimes = await db+ const runtimeIds = db.select({ id: agentRuntime.id }).from(agentRuntime).where(@@ -185,15 +185,17 @@ export async function deleteRuntimesByDaemonId());- if (runtimes.length === 0) return;-- const runtimeIds = runtimes.map(r => r.id);- await db+ const detachAgents = db.update(agent).set({ runtimeId: null, updatedAt: new Date().toISOString() })- .where(inArray(agent.runtimeId, runtimeIds));+ .where(+ and(+ eq(agent.workspaceId, workspaceId),+ inArray(agent.runtimeId, runtimeIds)+ )+ );- await db+ const deleteRuntimes = db.delete(agentRuntime).where(and(@@ -201,6 +203,7 @@ export async function deleteRuntimesByDaemonId(eq(agentRuntime.workspaceId, workspaceId)));+ await db.batch([detachAgents, deleteRuntimes]);}export async function updateRuntimeCliVersionByDaemon(diff --git a/src/shared/src/db/queries/task.ts b/src/shared/src/db/queries/task.tsindex ba7e0346d..d03f5493f 100644--- a/src/shared/src/db/queries/task.ts+++ b/src/shared/src/db/queries/task.ts@@ -1,10 +1,11 @@-import { eq, and, desc, asc, inArray, notInArray, ne, count, lt, or, sql, exists } from "drizzle-orm";+import { eq, and, desc, asc, inArray, ne, count, lt, or, sql, exists, notExists } from "drizzle-orm";import { alias } from "drizzle-orm/sqlite-core";import { agentTaskQueue, taskMessage, conversation } from "../schema";import type { Database } from "../index";import { ClaimedTaskRowSchema } from "../../schemas";import { chunk, D1_MAX_IN_PARAMS } from "./_chunk";import { TASK_TYPES } from "../../constants";+import { jsonTextSet } from "./_json-set";export async function createTask(db: Database,@@ -176,6 +177,18 @@ export async function claimTask(db: Database, agentId: string, workspaceId: strifor (const convId of steerableConvContextKeys.keys()) {allBlockedConvIds.push(convId);}+ const uniqueBlockedConvIds = [...new Set(allBlockedConvIds)];+ const isNotBlocked = () => {+ if (uniqueBlockedConvIds.length === 0) return undefined;+ const blockedId = sql<string>`CAST(blocked_conversation.value AS TEXT)`;+ return notExists(+ db+ .select({ one: sql<number>`1` })+ .from(sql`json_each(${JSON.stringify(uniqueBlockedConvIds)}) AS blocked_conversation`)+ .where(eq(blockedId, agentTaskQueue.conversationId))+ );+ };+ const initialNotBlocked = isNotBlocked();// Try non-steerable candidates firstconst candidateQuery = db@@ -186,9 +199,7 @@ export async function claimTask(db: Database, agentId: string, workspaceId: strieq(agentTaskQueue.agentId, agentId),eq(agentTaskQueue.workspaceId, workspaceId),eq(agentTaskQueue.status, "queued"),- ...(allBlockedConvIds.length > 0- ? [notInArray(agentTaskQueue.conversationId, allBlockedConvIds)]- : [])+ ...(initialNotBlocked ? [initialNotBlocked] : []))).orderBy(desc(agentTaskQueue.priority), asc(agentTaskQueue.createdAt))@@ -234,6 +245,7 @@ export async function claimTask(db: Database, agentId: string, workspaceId: stri// If another runtime raced us to this candidate, retry with a fresh candidate.const now = new Date().toISOString();for (let attempt = 0; attempt < 3; attempt++) {+ const retryNotBlocked = isNotBlocked();const targetId = attempt === 0? candidates[0].id: (await db@@ -244,9 +256,7 @@ export async function claimTask(db: Database, agentId: string, workspaceId: strieq(agentTaskQueue.agentId, agentId),eq(agentTaskQueue.workspaceId, workspaceId),eq(agentTaskQueue.status, "queued"),- ...(allBlockedConvIds.length > 0- ? [notInArray(agentTaskQueue.conversationId, allBlockedConvIds)]- : [])+ ...(retryNotBlocked ? [retryNotBlocked] : []))).orderBy(desc(agentTaskQueue.priority), asc(agentTaskQueue.createdAt))@@ -351,13 +361,14 @@ export async function listPendingTasksByRuntimes(workspaceId: string) {if (runtimeIds.length === 0) return [];+ const runtimes = jsonTextSet(db, [...new Set(runtimeIds)]);return db.select().from(agentTaskQueue).where(and(eq(agentTaskQueue.workspaceId, workspaceId),- inArray(agentTaskQueue.runtimeId, runtimeIds),+ inArray(agentTaskQueue.runtimeId, runtimes),inArray(agentTaskQueue.status, ["queued", "dispatched"]),ne(agentTaskQueue.type, TASK_TYPES.KILL_TASK))@@ -509,7 +520,7 @@ export async function listActiveTaskCountsByWorkspace(ne(agentTaskQueue.type, TASK_TYPES.KILL_TASK),];if (agentIds && agentIds.length > 0) {- conditions.push(inArray(agentTaskQueue.agentId, agentIds));+ conditions.push(inArray(agentTaskQueue.agentId, jsonTextSet(db, [...new Set(agentIds)])));}if (userId) {conditions.push(eq(conversation.userId, userId));@@ -538,7 +549,7 @@ export async function listActiveTasksByWorkspace(ne(agentTaskQueue.type, TASK_TYPES.KILL_TASK),];if (agentIds && agentIds.length > 0) {- conditions.push(inArray(agentTaskQueue.agentId, agentIds));+ conditions.push(inArray(agentTaskQueue.agentId, jsonTextSet(db, [...new Set(agentIds)])));}if (userId) {conditions.push(eq(conversation.userId, userId));@@ -618,8 +629,9 @@ export async function claimKillTasks(limit: number) {if (runtimeIds.length === 0 || limit <= 0) return [];+ const runtimes = jsonTextSet(db, [...new Set(runtimeIds)]);- const candidates = await db+ const candidates = db.select({ id: agentTaskQueue.id }).from(agentTaskQueue).where(@@ -627,21 +639,18 @@ export async function claimKillTasks(eq(agentTaskQueue.workspaceId, workspaceId),eq(agentTaskQueue.type, TASK_TYPES.KILL_TASK),eq(agentTaskQueue.status, "queued"),- inArray(agentTaskQueue.runtimeId, runtimeIds)+ inArray(agentTaskQueue.runtimeId, runtimes))).orderBy(asc(agentTaskQueue.createdAt)).limit(limit);- const ids = candidates.map(c => c.id);- if (ids.length === 0) return [];-const rows = await db.update(agentTaskQueue).set({ status: "dispatched", dispatchedAt: new Date().toISOString() }).where(and(- inArray(agentTaskQueue.id, ids),+ inArray(agentTaskQueue.id, candidates),eq(agentTaskQueue.status, "queued")))@@ -677,7 +686,7 @@ const DEFAULT_STALE_RUNNING_SECONDS = Number(process.env.ALOOK_STALE_RUNNING_TIMexport async function failStaleRunningTasks(db: Database, workspaceId: string, staleSeconds = DEFAULT_STALE_RUNNING_SECONDS) {const threshold = new Date(Date.now() - staleSeconds * 1000).toISOString();- const staleRows = await db+ const staleRows = db.select({id: agentTaskQueue.id,})@@ -694,9 +703,6 @@ export async function failStaleRunningTasks(db: Database, workspaceId: string, ssql`COALESCE(MAX(${taskMessage.createdAt}), ${agentTaskQueue.startedAt}) < ${threshold}`);- if (staleRows.length === 0) return [];-- const staleIds = staleRows.map((r) => r.id);const rows = await db.update(agentTaskQueue).set({@@ -704,7 +710,7 @@ export async function failStaleRunningTasks(db: Database, workspaceId: string, scompletedAt: new Date().toISOString(),error: `timed out in running state (no message activity for ${Math.round(staleSeconds / 60)} minutes)`,})- .where(and(inArray(agentTaskQueue.id, staleIds), eq(agentTaskQueue.status, "running")))+ .where(and(inArray(agentTaskQueue.id, staleRows), eq(agentTaskQueue.status, "running"))).returning({ agentId: agentTaskQueue.agentId, workspaceId: agentTaskQueue.workspaceId, conversationId: agentTaskQueue.conversationId });return rows;}@@ -953,13 +959,14 @@ export async function getTraceAgentsByTaskIds(workspaceId: string): Promise<Map<string, string[]>> {if (taskIds.length === 0) return new Map();+ const taskIdSet = jsonTextSet(db, [...new Set(taskIds)]);const taskTraces = await db.select({ id: agentTaskQueue.id, traceId: agentTaskQueue.traceId }).from(agentTaskQueue).where(and(- inArray(agentTaskQueue.id, taskIds),+ inArray(agentTaskQueue.id, taskIdSet),sql`${agentTaskQueue.traceId} IS NOT NULL`,eq(agentTaskQueue.workspaceId, workspaceId))@@ -975,6 +982,7 @@ export async function getTraceAgentsByTaskIds(const traceIds = [...traceToTaskIds.keys()];if (traceIds.length === 0) return new Map();+ const traceIdSet = jsonTextSet(db, traceIds);const traceAgents = await db.selectDistinct({@@ -984,7 +992,7 @@ export async function getTraceAgentsByTaskIds(.from(agentTaskQueue).where(and(- inArray(agentTaskQueue.traceId, traceIds),+ inArray(agentTaskQueue.traceId, traceIdSet),eq(agentTaskQueue.workspaceId, workspaceId),ne(agentTaskQueue.type, TASK_TYPES.KILL_TASK))diff --git a/src/shared/src/index.ts b/src/shared/src/index.tsindex 61cb8ba10..b11a64316 100644--- a/src/shared/src/index.ts+++ b/src/shared/src/index.ts@@ -98,6 +98,7 @@ export {COMMUNITY_MACHINE_HEARTBEAT_MS,COMMUNITY_MACHINE_PAIR_TOKEN_TTL_MS,EVENT_POLL_INTERVAL_MS,+ MAX_MEETING_CLAIMS_PER_POLL,AGENT_HANDLE_MIN_LENGTH,MAX_TASKS_PER_TRACE,DEV_PASSWORD,@@ -207,6 +208,7 @@ export {HeartbeatRequestSchema,SweepRequestSchema,PollRequestSchema,+ MAX_POLL_TASKS,PollResponseSchema,PollMeetingItemSchema,RegisterResponseSchema,diff --git a/src/shared/src/schemas.ts b/src/shared/src/schemas.tsindex 90f51fb38..371feaf0c 100644--- a/src/shared/src/schemas.ts+++ b/src/shared/src/schemas.ts@@ -136,6 +136,8 @@ export type SweepRequest = HeartbeatRequest;// Poll request/response (replaces heartbeat + per-runtime claim)// ---------------------------------------------------------------------------+export const MAX_POLL_TASKS = 50;+export const PollRequestSchema = z.object({daemon_id: z.string().min(1),max_tasks: z.number().int().min(1).default(1),@@ -216,10 +218,14 @@ export const ActivateTokenRuntimeSchema = z.object({});export type ActivateTokenRuntime = z.infer<typeof ActivateTokenRuntimeSchema>;+export const COMMUNITY_RUNTIME_ID_MAX = 64;+export const COMMUNITY_RUNTIME_VERSION_MAX = 64;+export const COMMUNITY_RUNTIME_LIST_MAX = 64;+export const ActivateTokenRequestSchema = z.object({token: z.string().min(1),hostname: z.string().min(1),- runtimes: z.array(ActivateTokenRuntimeSchema).min(1),+ runtimes: z.array(ActivateTokenRuntimeSchema).min(1).max(COMMUNITY_RUNTIME_LIST_MAX),});export type ActivateTokenRequest = z.infer<typeof ActivateTokenRequestSchema>;@@ -229,7 +235,7 @@ export const RegisterDaemonRequestSchema = z.object({device_name: z.string().optional().default(""),cli_version: z.string().optional().default(""),workspaces_root: z.string().optional().default(""),- runtimes: z.array(DaemonRuntimeItemSchema).min(1),+ runtimes: z.array(DaemonRuntimeItemSchema).min(1).max(COMMUNITY_RUNTIME_LIST_MAX),});export type RegisterDaemonRequest = z.infer<typeof RegisterDaemonRequestSchema>;@@ -822,9 +828,6 @@ export type CreateThreadRequest = z.infer<typeof CreateThreadRequestSchema>;// Runtime id charset: alnum + `._@/-`. Length capped at 64 to match the// on-wire, on-disk, and DB expectations. Version optional, length-capped.-export const COMMUNITY_RUNTIME_ID_MAX = 64;-export const COMMUNITY_RUNTIME_VERSION_MAX = 64;-export const COMMUNITY_RUNTIME_LIST_MAX = 64;const RUNTIME_ID_RE = /^[A-Za-z0-9._@/-]+$/;// Per-runtime health, reported by the daemon. `status` defaults to "healthy"@@ -864,6 +867,17 @@ export const CommunityMachineRuntimeListSchema = zreturn out;});+const CommunityRunningAgentListSchema = z.array(z.string()).transform((list) => {+ const seen = new Set<string>();+ const out: string[] = [];+ for (const id of list) {+ if (seen.has(id)) continue;+ seen.add(id);+ out.push(id);+ }+ return out;+});+export const CommunityMachineSummarySchema = z.object({id: z.string(),hostname: z.string(),@@ -901,7 +915,7 @@ export const HostReadyMessageSchema = z.object({type: z.literal("ready"),runtimeReport: CommunityMachineRuntimeListSchema,capabilities: z.array(z.string().min(1).max(64)).max(16).optional().default([]),- runningAgents: z.array(z.string()).default([]),+ runningAgents: CommunityRunningAgentListSchema.default([]),hostname: z.string().optional(),platform: z.string().optional(),arch: z.string().optional(),@@ -916,7 +930,7 @@ export type HostReadyMessage = z.infer<typeof HostReadyMessageSchema>;*/export const CommunityDaemonReadySchema = z.object({runtimeReport: CommunityMachineRuntimeListSchema.optional(),- runningAgents: z.array(z.string()).default([]),+ runningAgents: CommunityRunningAgentListSchema.default([]),hostname: z.string().optional(),os: z.string().optional(),arch: z.string().optional(),diff --git a/src/shared/test/queries/agent-link.test.ts b/src/shared/test/queries/agent-link.test.tsindex 41a244735..cdf11556b 100644--- a/src/shared/test/queries/agent-link.test.ts+++ b/src/shared/test/queries/agent-link.test.ts@@ -8,7 +8,6 @@ describe("agent-link query module exports", () => {it("exports update", () => { expect(typeof agentLinkQueries.update).toBe("function"); });it("exports remove", () => { expect(typeof agentLinkQueries.remove).toBe("function"); });it("exports getColleaguesForAgent", () => { expect(typeof agentLinkQueries.getColleaguesForAgent).toBe("function"); });- it("exports getColleaguesForAgents", () => { expect(typeof agentLinkQueries.getColleaguesForAgents).toBe("function"); });it("exports getAllColleaguesForWorkspace", () => { expect(typeof agentLinkQueries.getAllColleaguesForWorkspace).toBe("function"); });it("exports getByPair", () => { expect(typeof agentLinkQueries.getByPair).toBe("function"); });it("exports upsertByPair", () => { expect(typeof agentLinkQueries.upsertByPair).toBe("function"); });@@ -170,12 +169,6 @@ describe("remove", () => {});});-describe("getColleaguesForAgents", () => {- it("returns empty array for empty agentIds", async () => {- expect(await agentLinkQueries.getColleaguesForAgents(null as any, [], "ws_1")).toEqual([]);- });-});-describe("getColleaguesForAgent", () => {it("combines asSource and asTarget results", async () => {const c1 = { name: "Alice" };diff --git a/src/shared/test/queries/chunk.test.ts b/src/shared/test/queries/chunk.test.tsindex a6f54d12b..80c67ac64 100644--- a/src/shared/test/queries/chunk.test.ts+++ b/src/shared/test/queries/chunk.test.ts@@ -2,6 +2,7 @@ import { describe, it, expect } from "vitest";import {chunk,maxRowsPerInsert,+ maxInParams,D1_MAX_BIND_PARAMS,D1_MAX_IN_PARAMS,} from "../../src/db/queries/_chunk";@@ -62,6 +63,22 @@ describe("maxRowsPerInsert", () => {});});+describe("maxInParams", () => {+ it.each([+ [0, 100],+ [1, 99],+ [2, 98],+ [5, 95],+ [6, 94],+ ])("reserves %i fixed binds", (fixed, expected) => {+ expect(maxInParams(fixed)).toBe(expected);+ });++ it.each([-1, 1.5, 100, 101])("rejects invalid fixed bind budget %s", (fixed) => {+ expect(() => maxInParams(fixed)).toThrow();+ });+});+describe("constants", () => {it("D1_MAX_IN_PARAMS leaves headroom under the hard limit", () => {expect(D1_MAX_BIND_PARAMS).toBe(100);diff --git a/src/shared/test/queries/community-bot.test.ts b/src/shared/test/queries/community-bot.test.tsindex f72b797d8..e7733b43c 100644--- a/src/shared/test/queries/community-bot.test.ts+++ b/src/shared/test/queries/community-bot.test.ts@@ -46,7 +46,6 @@ describe("community/bot exports", () => {expect(typeof q.createApprovalRequestStatement).toBe("function")expect(typeof q.resolveApprovalRequest).toBe("function")expect(typeof q.getApprovalRequestByDmMessageId).toBe("function")- expect(typeof q.listApprovalRequestsByDmMessageIds).toBe("function")})it("exports OwnerHasBotsError as a real Error subclass", () => {diff --git a/src/shared/test/queries/community-friendship.test.ts b/src/shared/test/queries/community-friendship.test.tsindex 33ae8ccfc..8b69fb87d 100644--- a/src/shared/test/queries/community-friendship.test.ts+++ b/src/shared/test/queries/community-friendship.test.ts@@ -98,7 +98,7 @@ describe("getFriendUserIds", () => {const db = createDb({ selfBotRows: [{ id: "bot-1", ownerUserId: "owner-1" }] });await q.getFriendUserIds(db, "bot-1");expect(db.__whereCalls).toHaveLength(3);- expect(db.select).toHaveBeenCalledTimes(3);+ expect(db.select).toHaveBeenCalledTimes(4);});it("filters out a soft-deleted OWNER from the returned audience (regression guard)", async () => {@@ -365,17 +365,19 @@ function createBlockDb(opts: {memberRows?: any[]profiles?: any[]}) {- // `listMessagesReferencingFriendship` now runs TWO selects: the friendship's- // card message rows ({ messageId, channelId }), then the access-member rows- // of those DM channels ({ channelId, userId }) to build `peerUserIds`. It- // short-circuits after the first select when no card message exists, so the- // member-rows select only fires when `refMessages` is non-empty.+ // `listMessagesReferencingFriendship` runs the card-message and access-member+ // selects. `loadProfiles` then builds a JSON-set subquery before its user+ // select, so the mock reserves one builder-only select result.const refMessages = opts.refMessages ?? []const selectReturns = [opts.existing ? [opts.existing] : [], // #1 findActiverefMessages, // #2 listMessagesReferencingFriendship — card message rows...(refMessages.length > 0- ? [opts.memberRows ?? [], opts.profiles ?? []] // #3 member rows, #4 loadProfiles+ ? [+ opts.memberRows ?? [],+ [],+ opts.profiles ?? [],+ ]: []),]let selectCall = 0diff --git a/src/shared/test/queries/community-member.test.ts b/src/shared/test/queries/community-member.test.tsindex 3633d17a5..60c83a85f 100644--- a/src/shared/test/queries/community-member.test.ts+++ b/src/shared/test/queries/community-member.test.ts@@ -435,30 +435,3 @@ describe("getMemberById", () => {expect(result!.userId).toBe("u_2");});});--describe("bulkUpdateRailOrder", () => {- function createBatchMock() {- const chain: any = {};- chain.update = vi.fn(() => chain);- chain.set = vi.fn(() => chain);- chain.where = vi.fn(() => chain);- chain.batch = vi.fn(() => Promise.resolve());- return chain;- }-- it("calls db.batch once with a tuple whose length matches the input", async () => {- const db = createBatchMock();- await memberQueries.bulkUpdateRailOrder(db, "u_1", ["s_1", "s_2", "s_3"]);- expect(db.batch).toHaveBeenCalledTimes(1);- const arg = db.batch.mock.calls[0][0];- expect(Array.isArray(arg)).toBe(true);- expect(arg).toHaveLength(3);- });-- it("returns early on empty input without touching db.batch", async () => {- const db = createBatchMock();- await memberQueries.bulkUpdateRailOrder(db, "u_1", []);- expect(db.batch).not.toHaveBeenCalled();- expect(db.update).not.toHaveBeenCalled();- });-});diff --git a/src/shared/test/queries/param-limit.test.ts b/src/shared/test/queries/param-limit.test.tsindex c61149659..06429aed6 100644--- a/src/shared/test/queries/param-limit.test.ts+++ b/src/shared/test/queries/param-limit.test.ts@@ -10,6 +10,16 @@ import * as mentionQueries from "../../src/db/queries/community/mention";import * as attachmentQueries from "../../src/db/queries/community/attachment";import * as inboxQueries from "../../src/db/queries/community/inbox";import * as readStateQueries from "../../src/db/queries/community/read-state";+import * as channelQueries from "../../src/db/queries/community/channel";+import * as categoryQueries from "../../src/db/queries/community/category";+import * as memberQueries from "../../src/db/queries/community/member";+import * as reactionQueries from "../../src/db/queries/community/reaction";+import * as dmQueries from "../../src/db/queries/community/dm";+import * as overviewQueries from "../../src/db/queries/overview";+import * as taskQueries from "../../src/db/queries/task";+import * as agentQueries from "../../src/db/queries/agent";+import * as conversationQueries from "../../src/db/queries/conversation";+import * as meetingQueries from "../../src/db/queries/meeting-session";import { D1_MAX_BIND_PARAMS } from "../../src/db/queries/_chunk";const fakeDb = drizzle({} as never);@@ -165,6 +175,26 @@ function makeD1SelectCapture() {return { db: drizzle(client as never), statements };}+function makeD1Capture(rawResponses: unknown[][][] = []) {+ const statements: Array<{ sql: string; params: unknown[] }> = [];+ const responses = [...rawResponses];+ const client = {+ prepare(sql: string) {+ return {+ bind(...params: unknown[]) {+ statements.push({ sql, params });+ return {+ raw: async () => responses.shift() ?? [],+ all: async () => ({ results: [] }),+ run: async () => ({ success: true, meta: { changes: 0 } }),+ };+ },+ };+ },+ };+ return { db: drizzle(client as never), statements };+}+describe("listEligibleUnreadChannels bound parameters", () => {it("95 visible ids use 80-id chunks and keep every statement within D1's limit", async () => {const { db, statements } = makeD1SelectCapture();@@ -218,3 +248,59 @@ describe("mark-all-read revision guard bound parameters", () => {expect(JSON.parse(encodedTargets as string)).toHaveLength(95);});});++describe("high-cardinality query bind matrix", () => {+ const sizes = [33, 90, 100, 125];++ it.each(sizes)("keeps JSON/subquery reads constant at %i ids", async (size) => {+ const ids = Array.from({ length: size }, (_, index) => `id_${index}`);+ const calls: Array<(db: ReturnType<typeof drizzle>) => Promise<unknown>> = [+ (db) => memberQueries.getMemberships(db as never, "user_1", ids),+ (db) => categoryQueries.getCategoriesByIds(db as never, ids),+ (db) => agentQueries.getAgentsByIds(db as never, ids, "workspace_1"),+ (db) => conversationQueries.getConversationsByIds(db as never, ids, "workspace_1"),+ (db) => overviewQueries.getRecentTerminalTasks(db as never, "workspace_1", ids),+ (db) => overviewQueries.getConversationCountsByAgent(db as never, "workspace_1", ids),+ (db) => taskQueries.listPendingTasksByRuntimes(db as never, ids, "workspace_1"),+ (db) => taskQueries.listActiveTaskCountsByWorkspace(db as never, "workspace_1", ids, "user_1"),+ (db) => taskQueries.listActiveTasksByWorkspace(db as never, "workspace_1", ids, "user_1"),+ (db) => taskQueries.getTraceAgentsByTaskIds(db as never, ids, "workspace_1"),+ (db) => meetingQueries.claimMeetingSessions(db as never, ids, "workspace_1", "2026-01-01T00:00:00.000Z"),+ (db) => dmQueries.listDMs(db as never, "user_1"),+ (db) => dmQueries.getDMBetween(db as never, "user_1", "user_2"),+ ];++ for (const call of calls) {+ const { db, statements } = makeD1Capture();+ await call(db);+ expect(statements.length).toBeGreaterThan(0);+ for (const statement of statements) {+ expect(statement.params.length).toBeLessThanOrEqual(D1_MAX_BIND_PARAMS);+ }+ expect(statements.some(({ sql }) => sql.includes("json_each")) || call === calls.at(-2) || call === calls.at(-1)).toBe(true);+ }+ });++ it("uses exact 99-id member chunks and 100-id reaction chunks", async () => {+ const ids = Array.from({ length: 125 }, (_, index) => `id_${index}`);+ const memberCapture = makeD1Capture();+ await memberQueries.getMembersByUserIds(memberCapture.db as never, "server_1", ids);+ expect(memberCapture.statements.map(({ params }) => params.length)).toEqual([100, 27]);++ const reactionCapture = makeD1Capture();+ await reactionQueries.listReactionsByMessageIds(reactionCapture.db as never, ids, "user_1");+ expect(reactionCapture.statements.map(({ params }) => params.length)).toEqual([100, 25]);+ });++ it("uses 100/98 server chunks before the visibility merge", async () => {+ const memberships = Array.from({ length: 125 }, (_, index) => [`server_${index}`]);+ const capture = makeD1Capture([memberships, [], [], [], []]);++ await channelQueries.listVisibleChannelIdsForUser(capture.db as never, "user_1");++ expect(capture.statements.map(({ params }) => params.length)).toEqual([1, 100, 25, 100, 29]);+ for (const { params } of capture.statements) {+ expect(params.length).toBeLessThanOrEqual(D1_MAX_BIND_PARAMS);+ }+ });+});diff --git a/src/shared/test/queries/task.test.ts b/src/shared/test/queries/task.test.tsindex 71dfca05f..c13f76818 100644--- a/src/shared/test/queries/task.test.ts+++ b/src/shared/test/queries/task.test.ts@@ -1,3 +1,5 @@+import Sqlite from "better-sqlite3";+import { drizzle } from "drizzle-orm/better-sqlite3";import { describe, it, expect, vi } from "vitest";import * as taskQueries from "../../src/db/queries/task";@@ -70,6 +72,93 @@ describe("claimKillTasks", () => {});});+describe("claimTask", () => {+ it("preserves blocked-conversation filtering when retrying a lost atomic claim", async () => {+ const sqlite = new Sqlite(":memory:");+ try {+ sqlite.exec(`+ CREATE TABLE agent_task_queue (+ id TEXT PRIMARY KEY,+ agent_id TEXT NOT NULL,+ runtime_id TEXT NOT NULL,+ workspace_id TEXT NOT NULL,+ conversation_id TEXT NOT NULL,+ prompt TEXT NOT NULL,+ type TEXT NOT NULL DEFAULT 'user_dm_message',+ context_key TEXT,+ status TEXT NOT NULL DEFAULT 'queued',+ priority INTEGER NOT NULL DEFAULT 0,+ result TEXT,+ context TEXT,+ session_id TEXT,+ created_at TEXT NOT NULL,+ dispatched_at TEXT,+ started_at TEXT,+ completed_at TEXT,+ error TEXT,+ trace_id TEXT,+ parent_task_id TEXT+ );+ `);+ const insert = sqlite.prepare(`+ INSERT INTO agent_task_queue (+ id, agent_id, runtime_id, workspace_id, conversation_id, prompt,+ context_key, status, priority, created_at+ ) VALUES (?, 'agent_1', 'runtime_1', 'workspace_1', ?, ?, ?, ?, ?, ?)+ `);+ insert.run(+ "running_blocker",+ "blocked_conversation",+ "running",+ null,+ "running",+ 0,+ "2026-08-28T00:00:00.000Z"+ );+ insert.run(+ "queued_candidate",+ "free_conversation",+ "queued",+ null,+ "queued",+ 1,+ "2026-08-28T00:01:00.000Z"+ );++ const realDb = drizzle(sqlite);+ let updateCalls = 0;+ const db = new Proxy(realDb as any, {+ get(target, property) {+ if (property === "update") {+ return (...args: unknown[]) => {+ updateCalls += 1;+ if (updateCalls === 1) {+ const lostClaim: any = {};+ lostClaim.set = () => lostClaim;+ lostClaim.where = () => lostClaim;+ lostClaim.returning = () => Promise.resolve([]);+ return lostClaim;+ }+ return target.update(...args);+ };+ }+ const value = target[property];+ return typeof value === "function" ? value.bind(target) : value;+ },+ });++ const claimed = await taskQueries.claimTask(db, "agent_1", "workspace_1");++ expect(updateCalls).toBe(2);+ expect(claimed?.id).toBe("queued_candidate");+ expect(sqlite.prepare("SELECT status FROM agent_task_queue WHERE id = ?").get("running_blocker"))+ .toEqual({ status: "running" });+ } finally {+ sqlite.close();+ }+ });+});+describe("countTasksByTrace", () => {it("returns count from query", async () => {const chain: any = {};diff --git a/src/shared/test/schemas/community-runtime.test.ts b/src/shared/test/schemas/community-runtime.test.tsindex cc0c2e7b9..189637630 100644--- a/src/shared/test/schemas/community-runtime.test.ts+++ b/src/shared/test/schemas/community-runtime.test.ts@@ -179,6 +179,16 @@ describe("HostReadyMessageSchema", () => {});expect(parsed.runtimeReport).toEqual([{ id: "claude", status: "healthy" }]);});++ it.each([125, 256, 257])("accepts and de-dupes a complete %i-item runningAgents report", (count) => {+ const ids = Array.from({ length: count }, (_, index) => `agent_${index}`);+ const parsed = HostReadyMessageSchema.parse({+ type: "ready",+ runtimeReport: [],+ runningAgents: [...ids, ids[0]],+ });+ expect(parsed.runningAgents).toEqual(ids);+ });});describe("control-plane receipt schemas", () => {diff --git a/src/shared/test/schemas/daemon-input-limits.test.ts b/src/shared/test/schemas/daemon-input-limits.test.tsnew file mode 100644index 000000000..8a5387cf5--- /dev/null+++ b/src/shared/test/schemas/daemon-input-limits.test.ts@@ -0,0 +1,30 @@+import { describe, expect, it } from "vitest";+import {+ ActivateTokenRequestSchema,+ COMMUNITY_RUNTIME_LIST_MAX,+ MAX_POLL_TASKS,+ PollRequestSchema,+ RegisterDaemonRequestSchema,+} from "../../src/schemas";++describe("daemon input limits", () => {+ it("keeps zero invalid and positive legacy poll counts compatible", () => {+ expect(PollRequestSchema.safeParse({ daemon_id: "d1", max_tasks: 0 }).success).toBe(false);+ expect(PollRequestSchema.parse({ daemon_id: "d1", max_tasks: 125 }).max_tasks).toBe(125);+ expect(MAX_POLL_TASKS).toBe(50);+ });++ it.each([+ ["register", RegisterDaemonRequestSchema, { daemon_id: "d1" }, { type: "codex" }],+ ["activate", ActivateTokenRequestSchema, { token: "t1", hostname: "host" }, { type: "codex" }],+ ] as const)("caps %s runtime arrays at the shared maximum", (_name, schema, base, runtime) => {+ expect(schema.safeParse({+ ...base,+ runtimes: Array.from({ length: COMMUNITY_RUNTIME_LIST_MAX }, () => runtime),+ }).success).toBe(true);+ expect(schema.safeParse({+ ...base,+ runtimes: Array.from({ length: COMMUNITY_RUNTIME_LIST_MAX + 1 }, () => runtime),+ }).success).toBe(false);+ });+});diff --git a/src/web/src/app/api/community/servers/[id]/categories/reorder/route.test.ts b/src/web/src/app/api/community/servers/[id]/categories/reorder/route.test.tsnew file mode 100644index 000000000..cf73b1065--- /dev/null+++ b/src/web/src/app/api/community/servers/[id]/categories/reorder/route.test.ts@@ -0,0 +1,98 @@+import { beforeEach, describe, expect, it, vi } from "vitest"+import { NextRequest } from "next/server"++const getMember = vi.fn()+const getCategoriesByIds = vi.fn()+const reorderCategories = vi.fn()+const fanOut = vi.fn()++vi.mock("@opennextjs/cloudflare", () => ({+ getCloudflareContext: vi.fn(() => ({ env: { DB: {} } })),+}))+vi.mock("@/lib/db", () => ({ getDb: vi.fn(() => ({})) }))+vi.mock("@alook/shared", async () => {+ const actual = await vi.importActual<typeof import("@alook/shared")>("@alook/shared")+ return {+ ...actual,+ queries: {+ communityMember: { getMember: (...args: unknown[]) => getMember(...args) },+ communityCategory: {+ getCategoriesByIds: (...args: unknown[]) => getCategoriesByIds(...args),+ reorderCategories: (...args: unknown[]) => reorderCategories(...args),+ },+ },+ }+})+vi.mock("@/lib/community/fanout", () => ({+ fanOutToServerMembers: (...args: unknown[]) => fanOut(...args),+}))+vi.mock("@/lib/middleware/auth", () => ({+ withAuth: vi.fn((handler: any) => async (req: any, ctx?: any) => {+ const params = ctx?.params instanceof Promise ? await ctx.params : ctx?.params+ return handler(req, { env: {}, userId: "u1", email: "[email protected]", params })+ }),+}))+vi.mock("@/lib/middleware/helpers", async () => {+ const { NextResponse } = require("next/server")+ return {+ writeJSON: (data: unknown, status = 200) => NextResponse.json(data, { status }),+ writeError: (message: string, status: number) => NextResponse.json({ error: message }, { status }),+ }+})++import { PATCH } from "./route"++const ctx = { params: { id: "s1" } } as never+function request(categoryIds: string[]) {+ return new NextRequest("http://localhost/api/community/servers/s1/categories/reorder", {+ method: "PATCH",+ body: JSON.stringify({ categoryIds }),+ headers: { "Content-Type": "application/json" },+ })+}++describe("PATCH /api/community/servers/[id]/categories/reorder", () => {+ beforeEach(() => {+ vi.clearAllMocks()+ getMember.mockResolvedValue({ id: "m1", role: "admin" })+ fanOut.mockResolvedValue(undefined)+ })++ it("fans out the exact validated subset after every row updates", async () => {+ getCategoriesByIds.mockResolvedValue([+ { id: "cat2", serverId: "s1" },+ { id: "cat1", serverId: "s1" },+ ])+ reorderCategories.mockResolvedValue([{ id: "cat2" }, { id: "cat1" }])++ const response = await PATCH(request(["cat2", "cat1"]), ctx)++ expect(response.status).toBe(200)+ expect(reorderCategories).toHaveBeenCalledWith({}, "s1", ["cat2", "cat1"])+ expect(fanOut).toHaveBeenCalledWith("s1", expect.objectContaining({+ categories: [{ id: "cat2", position: 0 }, { id: "cat1", position: 1 }],+ }))+ })++ it("returns 409 and emits no WS projection on a concurrent validation miss", async () => {+ getCategoriesByIds.mockResolvedValue([+ { id: "cat2", serverId: "s1" },+ { id: "cat1", serverId: "s1" },+ ])+ reorderCategories.mockResolvedValue([{ id: "cat2" }])++ const response = await PATCH(request(["cat2", "cat1"]), ctx)++ expect(response.status).toBe(409)+ expect(await response.json()).toEqual({ error: "category order changed concurrently" })+ expect(fanOut).not.toHaveBeenCalled()+ })++ it("rejects duplicates before reading or writing categories", async () => {+ const response = await PATCH(request(["cat1", "cat1"]), ctx)+ expect(response.status).toBe(400)+ expect(getCategoriesByIds).not.toHaveBeenCalled()+ expect(reorderCategories).not.toHaveBeenCalled()+ expect(fanOut).not.toHaveBeenCalled()+ })+})diff --git a/src/web/src/app/api/community/servers/[id]/categories/reorder/route.ts b/src/web/src/app/api/community/servers/[id]/categories/reorder/route.tsindex 5404ce773..d17d3fb41 100644--- a/src/web/src/app/api/community/servers/[id]/categories/reorder/route.ts+++ b/src/web/src/app/api/community/servers/[id]/categories/reorder/route.ts@@ -37,7 +37,10 @@ export const PATCH = withAuth(async (req: NextRequest, ctx) => {return writeError("category does not belong to this server", 400)}- await queries.communityCategory.reorderCategories(db, serverId, body.categoryIds)+ const updated = await queries.communityCategory.reorderCategories(db, serverId, body.categoryIds)+ if (updated.length !== body.categoryIds.length) {+ return writeError("category order changed concurrently", 409)+ }await fanOutToServerMembers(serverId, {type: WS_EVENTS.CATEGORY_REORDER,diff --git a/src/web/src/app/api/community/servers/reorder/route.test.ts b/src/web/src/app/api/community/servers/reorder/route.test.tsdeleted file mode 100644index 1cea10129..000000000--- a/src/web/src/app/api/community/servers/reorder/route.test.ts+++ /dev/null@@ -1,97 +0,0 @@-import { describe, it, expect, vi, beforeEach } from "vitest"-import { NextRequest } from "next/server"--const getMemberships = vi.fn()-const bulkUpdateRailOrder = vi.fn()-const updateRailOrder = vi.fn()--vi.mock("@opennextjs/cloudflare", () => ({- getCloudflareContext: vi.fn(() => ({ env: { DB: {} } })),-}))--vi.mock("@/lib/db", () => ({ getDb: vi.fn(() => ({})) }))--vi.mock("@alook/shared", async () => {- const actual = await vi.importActual<typeof import("@alook/shared")>("@alook/shared")- return {- ...actual,- queries: {- communityMember: {- getMemberships: (...a: unknown[]) => getMemberships(...a),- bulkUpdateRailOrder: (...a: unknown[]) => bulkUpdateRailOrder(...a),- updateRailOrder: (...a: unknown[]) => updateRailOrder(...a),- },- },- }-})--vi.mock("@/lib/middleware/auth", () => ({- withAuth: vi.fn((handler: any) => async (req: any, ctx?: any) => {- const params = ctx?.params instanceof Promise ? await ctx.params : ctx?.params- return handler(req, { env: {}, userId: "u1", email: "[email protected]", params })- }),-}))--vi.mock("@/lib/middleware/helpers", async () => {- const { NextResponse } = require("next/server")- return {- writeJSON: (data: unknown, status = 200) => NextResponse.json(data, { status }),- writeError: (message: string, status: number) => NextResponse.json({ error: message }, { status }),- }-})--import { PATCH } from "./route"--function patchReq(body: unknown) {- return new NextRequest("http://localhost/api/community/servers/reorder", {- method: "PATCH",- body: JSON.stringify(body),- headers: { "Content-Type": "application/json" },- })-}--describe("PATCH /api/community/servers/reorder", () => {- beforeEach(() => {- vi.clearAllMocks()- getMemberships.mockResolvedValue([- { serverId: "s1" },- { serverId: "s2" },- { serverId: "s3" },- ])- bulkUpdateRailOrder.mockResolvedValue(undefined)- updateRailOrder.mockResolvedValue(undefined)- })-- it("batched update on happy path: bulkUpdateRailOrder called exactly once, updateRailOrder not called", async () => {- const res = await PATCH(patchReq({ serverIds: ["s1", "s2", "s3"] }))- expect(res.status).toBe(200)- expect(bulkUpdateRailOrder).toHaveBeenCalledTimes(1)- expect(bulkUpdateRailOrder).toHaveBeenCalledWith({}, "u1", ["s1", "s2", "s3"])- expect(updateRailOrder).not.toHaveBeenCalled()- })-- it("positions applied match input order (third arg preserves order)", async () => {- await PATCH(patchReq({ serverIds: ["s3", "s1", "s2"] }))- const third = bulkUpdateRailOrder.mock.calls[0]![2]- expect(third).toEqual(["s3", "s1", "s2"])- })-- it("400 when serverIds is empty", async () => {- const res = await PATCH(patchReq({ serverIds: [] }))- expect(res.status).toBe(400)- expect(bulkUpdateRailOrder).not.toHaveBeenCalled()- })-- it("400 when serverIds contains duplicates", async () => {- const res = await PATCH(patchReq({ serverIds: ["s1", "s1"] }))- expect(res.status).toBe(400)- expect(bulkUpdateRailOrder).not.toHaveBeenCalled()- })-- it("403 when the caller is not a member of every server", async () => {- getMemberships.mockResolvedValue([{ serverId: "s1" }]) // missing s2- const res = await PATCH(patchReq({ serverIds: ["s1", "s2"] }))- expect(res.status).toBe(403)- expect(bulkUpdateRailOrder).not.toHaveBeenCalled()- })-})diff --git a/src/web/src/app/api/community/servers/reorder/route.ts b/src/web/src/app/api/community/servers/reorder/route.tsdeleted file mode 100644index c2c29cb57..000000000--- a/src/web/src/app/api/community/servers/reorder/route.ts+++ /dev/null@@ -1,34 +0,0 @@-import { NextRequest } from "next/server"-import { withAuth } from "@/lib/middleware/auth"-import { writeJSON, writeError } from "@/lib/middleware/helpers"-import { getDb } from "@/lib/db"-import { queries } from "@alook/shared"--export const PATCH = withAuth(async (req: NextRequest, ctx) => {- const db = getDb(ctx.env.DB)-- let body: { serverIds?: string[] }- try {- body = await req.json()- } catch {- return writeError("invalid request body", 400)- }-- if (!Array.isArray(body.serverIds) || body.serverIds.length === 0) {- return writeError("serverIds must be a non-empty array", 400)- }-- const unique = new Set(body.serverIds)- if (unique.size !== body.serverIds.length) {- return writeError("serverIds must be unique", 400)- }-- const memberships = await queries.communityMember.getMemberships(db, ctx.userId, body.serverIds)- if (memberships.length !== body.serverIds.length) {- return writeError("not a member of all servers", 403)- }-- await queries.communityMember.bulkUpdateRailOrder(db, ctx.userId, body.serverIds)-- return writeJSON({ ok: true })-})diff --git a/src/web/src/app/api/daemon/routes.test.ts b/src/web/src/app/api/daemon/routes.test.tsindex 97e87dfb5..0ef1cb6d5 100644--- a/src/web/src/app/api/daemon/routes.test.ts+++ b/src/web/src/app/api/daemon/routes.test.ts@@ -125,6 +125,24 @@ describe("daemon route body validation", () => {expect(res.status).toBe(400);});+ it.each([[64, 200], [65, 400]] as const)(+ "returns %i-runtime boundary status %i",+ async (count, status) => {+ const POST = await loadRegister();+ const res = await POST(+ postReq("http://localhost/api/daemon/register", {+ workspace_id: "w1",+ daemon_id: "d1",+ device_name: "host",+ runtimes: Array.from({ length: count }, (_, index) => ({+ type: `runtime-${index}`,+ })),+ }),+ );+ expect(res.status).toBe(status);+ },+ );+it("returns 400 on malformed JSON", async () => {const POST = await loadRegister();const res = await POST(diff --git a/src/web/src/app/api/daemon/tasks/poll/route.auth-order.test.ts b/src/web/src/app/api/daemon/tasks/poll/route.auth-order.test.tsnew file mode 100644index 000000000..fe5f8d5bc--- /dev/null+++ b/src/web/src/app/api/daemon/tasks/poll/route.auth-order.test.ts@@ -0,0 +1,79 @@+import { beforeEach, describe, expect, it, vi } from "vitest"+import { NextRequest } from "next/server"++const getMachineTokenByToken = vi.fn()+const updateMachineTokenLastUsed = vi.fn()+const runtimeLookup = vi.fn()+const kvGet = vi.fn().mockResolvedValue(null)+const kvPut = vi.fn().mockResolvedValue(undefined)++vi.mock("@opennextjs/cloudflare", () => ({+ getCloudflareContext: vi.fn(async () => ({+ env: { DB: {}, CACHE_KV: { get: kvGet, put: kvPut, delete: vi.fn() } },+ })),+}))+vi.mock("@/lib/db", () => ({+ getDb: vi.fn(() => ({})),+ withD1Retry: vi.fn((fn: () => Promise<unknown>) => fn()),+}))+vi.mock("@alook/shared", async () => {+ const actual = await vi.importActual<typeof import("@alook/shared")>("@alook/shared")+ return {+ ...actual,+ queries: {+ machineToken: {+ getMachineTokenByToken: (...args: unknown[]) => getMachineTokenByToken(...args),+ updateMachineTokenLastUsed: (...args: unknown[]) => updateMachineTokenLastUsed(...args),+ },+ runtime: { getRuntimeIdsByDaemon: (...args: unknown[]) => runtimeLookup(...args) },+ },+ }+})+vi.mock("@/lib/cache", () => ({+ bindCacheKV: vi.fn(),+ getKV: vi.fn(() => ({ get: kvGet, put: kvPut, delete: vi.fn() })),+ cacheKeys: {+ machineToken: (token: string) => `mt:${token}`,+ runtimeIds: (workspaceId: string, daemonId: string) => `runtime:${workspaceId}:${daemonId}`,+ },+ cached: vi.fn(),+ throttled: vi.fn(),+}))+vi.mock("@/lib/auth", () => ({ createAuth: vi.fn() }))++import { POST } from "./route"++function request(token: string) {+ return new NextRequest("http://localhost/api/daemon/tasks/poll", {+ method: "POST",+ headers: { "Content-Type": "application/json", Authorization: `Bearer ${token}` },+ body: JSON.stringify({ daemon_id: "daemon-1", max_tasks: 0 }),+ })+}++describe("poll authentication precedes max_tasks validation", () => {+ beforeEach(() => {+ vi.clearAllMocks()+ kvGet.mockResolvedValue(null)+ })++ it("returns 401 for invalid-token + zero before entering route work", async () => {+ getMachineTokenByToken.mockResolvedValue(null)+ const response = await POST(request("al_invalid"))+ expect(response.status).toBe(401)+ expect(await response.json()).toEqual({ error: "invalid token" })+ expect(runtimeLookup).not.toHaveBeenCalled()+ })++ it("returns 400 for valid-token + zero before any poll work", async () => {+ getMachineTokenByToken.mockResolvedValue({+ id: "token-1",+ userId: "user-1",+ userEmail: "[email protected]",+ workspaceId: "workspace-1",+ })+ const response = await POST(request("al_valid"))+ expect(response.status).toBe(400)+ expect(runtimeLookup).not.toHaveBeenCalled()+ })+})diff --git a/src/web/src/app/api/daemon/tasks/poll/route.test.ts b/src/web/src/app/api/daemon/tasks/poll/route.test.tsindex 1e8f1798b..187997930 100644--- a/src/web/src/app/api/daemon/tasks/poll/route.test.ts+++ b/src/web/src/app/api/daemon/tasks/poll/route.test.ts@@ -79,7 +79,6 @@ vi.mock("@alook/shared", async () => {claimMeetingSessions: (...args: unknown[]) => mockClaimMeetingSessions(...args),},agentLink: {- getColleaguesForAgents: (...args: unknown[]) => mockGetAllColleaguesForWorkspace(...args),getAllColleaguesForWorkspace: (...args: unknown[]) => mockGetAllColleaguesForWorkspace(...args),},},@@ -269,6 +268,27 @@ describe("POST /api/daemon/tasks/poll", () => {expect(mockClaimTasksForRuntimes).toHaveBeenCalledWith(["r1"], 5, "w1");});+ it.each([51, 125])("clamps legacy max_tasks=%i requests to 50", async (requested) => {+ mockGetRuntimeIdsByDaemon.mockResolvedValue(["r1"]);+ mockClaimTasksForRuntimes.mockResolvedValue([]);++ const res = await POST(postReq({ daemon_id: "d1", max_tasks: requested }));++ expect(res.status).toBe(200);+ expect(mockClaimTasksForRuntimes).toHaveBeenCalledWith(["r1"], 50, "w1");+ });++ it("rejects max_tasks=0 before any poll work", async () => {+ const res = await POST(postReq({ daemon_id: "d1", max_tasks: 0 }));++ expect(res.status).toBe(400);+ expect(mockGetRuntimeIdsByDaemon).not.toHaveBeenCalled();+ expect(mockClaimTasksForRuntimes).not.toHaveBeenCalled();+ expect(mockListScheduledMeetings).not.toHaveBeenCalled();+ expect(mockGetPendingFileRequests).not.toHaveBeenCalled();+ expect(mockBroadcastToUser).not.toHaveBeenCalled();+ });+it("defaults max_tasks to 1", async () => {mockUpsertMachine.mockResolvedValue({});mockGetRuntimeIdsByDaemon.mockResolvedValue(["r1"]);diff --git a/src/web/src/app/api/daemon/tasks/poll/route.ts b/src/web/src/app/api/daemon/tasks/poll/route.tsindex c287157a4..2edf3b212 100644--- a/src/web/src/app/api/daemon/tasks/poll/route.ts+++ b/src/web/src/app/api/daemon/tasks/poll/route.ts@@ -1,5 +1,5 @@import { NextRequest } from "next/server";-import { queries, PollRequestSchema, semverGte, type FileRequestItem, type PollMeetingItem } from "@alook/shared";+import { queries, MAX_MEETING_CLAIMS_PER_POLL, MAX_POLL_TASKS, PollRequestSchema, semverGte, type FileRequestItem, type PollMeetingItem } from "@alook/shared";import { getDb, withD1Retry } from "@/lib/db"import { withAuth } from "@/lib/middleware/auth";import { writeJSON, writeError, parseBody } from "@/lib/middleware/helpers";@@ -32,9 +32,10 @@ export const POST = withAuth(async (req: NextRequest, ctx) => {// 2. Task claimingconst taskService = new TaskService(db);+ const claimLimit = Math.min(body.max_tasks, MAX_POLL_TASKS);const claimed = await withD1Retry(() => taskService.claimTasksForRuntimes(runtimeIds,- body.max_tasks,+ claimLimit,ctx.workspaceId!,));@@ -63,7 +64,12 @@ export const POST = withAuth(async (req: NextRequest, ctx) => {const [machineResult, meetingResult] = await Promise.allSettled([queries.machine.getMachineByDaemon(db, body.daemon_id, ctx.workspaceId),- queries.meetingSession.listScheduledMeetings(db, ctx.workspaceId, windowEnd.toISOString()),+ queries.meetingSession.listScheduledMeetings(+ db,+ ctx.workspaceId,+ windowEnd.toISOString(),+ MAX_MEETING_CLAIMS_PER_POLL,+ ),]);if (machineResult.status === "fulfilled") {diff --git a/src/web/src/app/api/machine-tokens/activate/route.test.ts b/src/web/src/app/api/machine-tokens/activate/route.test.tsindex 3955d3dc0..7e37506e0 100644--- a/src/web/src/app/api/machine-tokens/activate/route.test.ts+++ b/src/web/src/app/api/machine-tokens/activate/route.test.ts@@ -138,6 +138,27 @@ describe("POST /api/machine-tokens/activate", () => {);});+ it.each([[64, 200], [65, 400]] as const)(+ "returns %i-runtime boundary status %i",+ async (count, status) => {+ const POST = await loadRoute();+ mockGetMachineTokenByToken.mockResolvedValue(pendingToken);+ mockUpsertMachine.mockResolvedValue(undefined);+ mockBatchUpsertAgentRuntimes.mockImplementation(async (_db, runtimes: unknown[]) =>+ runtimes.map((_, index) => ({ id: `rt_${index}`, provider: `runtime-${index}` })),+ );+ mockActivateMachineToken.mockResolvedValue(undefined);+ mockBroadcastToUser.mockResolvedValue(undefined);++ const res = await POST(makeReq({+ ...validBody,+ runtimes: Array.from({ length: count }, (_, index) => ({ type: `runtime-${index}` })),+ }));+ expect(res.status).toBe(status);+ if (status === 400) expect(mockGetMachineTokenByToken).not.toHaveBeenCalled();+ },+ );+it("warms the token cache with the activated row (not a delete)", async () => {const POST = await loadRoute();diff --git a/src/web/test-runtime/d1-bind-community.runtime.test.ts b/src/web/test-runtime/d1-bind-community.runtime.test.tsnew file mode 100644index 000000000..7062e5353--- /dev/null+++ b/src/web/test-runtime/d1-bind-community.runtime.test.ts@@ -0,0 +1,268 @@+/// <reference types="@cloudflare/vitest-plugin/types" />++import { env } from "cloudflare:workers";+import { afterEach, describe, expect, it } from "vitest";+import { createDb, queries } from "@alook/shared";++const runtimeEnv = env as unknown as CloudflareEnv;+const prefixes: string[] = [];++function fixturePrefix(label: string) {+ const prefix = `bind_${label}_${crypto.randomUUID().replaceAll("-", "")}`;+ prefixes.push(prefix);+ return prefix;+}++async function run(sql: string, ...bindings: unknown[]) {+ await runtimeEnv.DB.prepare(sql).bind(...bindings).run();+}++async function runBatched(statements: D1PreparedStatement[]) {+ for (let offset = 0; offset < statements.length; offset += 50) {+ await runtimeEnv.DB.batch(statements.slice(offset, offset + 50));+ }+}++async function rows<T>(sql: string, ...bindings: unknown[]): Promise<T[]> {+ return (await runtimeEnv.DB.prepare(sql).bind(...bindings).all<T>()).results;+}++afterEach(async () => {+ for (const prefix of prefixes.splice(0)) {+ await run("DELETE FROM community_bot_binding WHERE user_id GLOB ?", `${prefix}*`);+ await run("DELETE FROM community_machine WHERE id GLOB ?", `${prefix}*`);+ await run("DELETE FROM community_channel WHERE id GLOB ?", `${prefix}*`);+ await run("DELETE FROM community_server WHERE id GLOB ?", `${prefix}*`);+ await run("DELETE FROM agent_task_queue WHERE id GLOB ?", `${prefix}*`);+ await run("DELETE FROM conversation WHERE id GLOB ?", `${prefix}*`);+ await run("DELETE FROM agent WHERE id GLOB ?", `${prefix}*`);+ await run("DELETE FROM agent_runtime WHERE id GLOB ?", `${prefix}*`);+ await run("DELETE FROM workspace WHERE id GLOB ?", `${prefix}*`);+ await run("DELETE FROM user WHERE id GLOB ?", `${prefix}*`);+ }+});++describe("community high-cardinality D1 queries", () => {+ it("resolves visibility and memberships across 125 servers", async () => {+ const prefix = fixturePrefix("visibility");+ const userId = `${prefix}_user`;+ const now = "2026-08-28T00:00:00.000Z";+ await run(+ "INSERT INTO user (id, email, name, discriminator) VALUES (?, ?, 'Viewer', '1000')",+ userId,+ `${userId}@example.com`,+ );++ const serverIds = Array.from({ length: 125 }, (_, index) => `${prefix}_server_${index}`);+ await runBatched(serverIds.flatMap((serverId, index) => [+ runtimeEnv.DB.prepare(+ "INSERT INTO community_server (id, name, discriminator, owner_id, created_at) VALUES (?, ?, ?, ?, ?)",+ ).bind(serverId, `${prefix}-server-${index}`, String(index).padStart(4, "0"), userId, now),+ runtimeEnv.DB.prepare(+ "INSERT INTO community_server_member (id, server_id, user_id, role, rail_order, joined_at) VALUES (?, ?, ?, 'owner', ?, ?)",+ ).bind(`${prefix}_member_${index}`, serverId, userId, index, now),+ runtimeEnv.DB.prepare(+ "INSERT INTO community_channel (id, server_id, name, type, position, created_at) VALUES (?, ?, ?, 'text', 0, ?)",+ ).bind(`${prefix}_channel_${index}`, serverId, `general-${index}`, now),+ ]));++ const db = createDb(runtimeEnv.DB);+ const visible = await queries.communityChannel.listVisibleChannelIdsForUser(db, userId);+ expect(new Set(visible)).toEqual(new Set(serverIds.map((_, index) => `${prefix}_channel_${index}`)));+ expect(await queries.communityMember.getMemberships(db, userId, serverIds)).toHaveLength(125);+ });++ it("hydrates and reorders 125 members, categories, and reactions", async () => {+ const prefix = fixturePrefix("collections");+ const ownerId = `${prefix}_owner`;+ const serverId = `${prefix}_server`;+ const now = "2026-08-28T01:00:00.000Z";+ await run(+ "INSERT INTO user (id, email, name, discriminator) VALUES (?, ?, 'Owner', '1001')",+ ownerId,+ `${ownerId}@example.com`,+ );+ await run(+ "INSERT INTO community_server (id, name, discriminator, owner_id, created_at) VALUES (?, ?, '1001', ?, ?)",+ serverId,+ `${prefix}-server`,+ ownerId,+ now,+ );++ const userIds = Array.from({ length: 125 }, (_, index) => `${prefix}_user_${index}`);+ const categoryIds = Array.from({ length: 125 }, (_, index) => `${prefix}_category_${index}`);+ const channelIds = Array.from({ length: 125 }, (_, index) => `${prefix}_channel_${index}`);+ const messageIds = Array.from({ length: 125 }, (_, index) => `${prefix}_message_${index}`);+ await runBatched(userIds.flatMap((userId, index) => [+ runtimeEnv.DB.prepare(+ "INSERT INTO user (id, email, name, discriminator) VALUES (?, ?, ?, ?)",+ ).bind(userId, `${userId}@example.com`, `Member ${index}`, String(2000 + index)),+ runtimeEnv.DB.prepare(+ "INSERT INTO community_server_member (id, server_id, user_id, role, rail_order, joined_at) VALUES (?, ?, ?, 'member', ?, ?)",+ ).bind(`${prefix}_member_${index}`, serverId, userId, index, now),+ runtimeEnv.DB.prepare(+ "INSERT INTO community_category (id, server_id, name, position, private) VALUES (?, ?, ?, ?, 0)",+ ).bind(categoryIds[index], serverId, `Category ${index}`, index),+ runtimeEnv.DB.prepare(+ "INSERT INTO community_channel (id, server_id, name, type, position, created_at) VALUES (?, ?, ?, 'text', ?, ?)",+ ).bind(channelIds[index], serverId, `channel-${index}`, index, now),+ runtimeEnv.DB.prepare(+ "INSERT INTO community_message (id, author_id, content, created_at, channel_id, seq) VALUES (?, ?, 'message', ?, ?, 1)",+ ).bind(messageIds[index], userId, now, channelIds[index]),+ runtimeEnv.DB.prepare(+ "INSERT INTO community_reaction (id, message_id, user_id, emoji, created_at) VALUES (?, ?, ?, '👍', ?)",+ ).bind(`${prefix}_reaction_${index}`, messageIds[index], ownerId, now),+ ]));++ const db = createDb(runtimeEnv.DB);+ expect(await queries.communityMember.getMembersByUserIds(db, serverId, userIds)).toHaveLength(125);+ expect(await queries.communityCategory.getCategoriesByIds(db, categoryIds)).toHaveLength(125);+ expect(await queries.communityReaction.listReactionsByMessageIds(db, messageIds, ownerId)).toHaveLength(125);++ const submitted = categoryIds.slice(0, 100).reverse();+ expect(await queries.communityCategory.reorderCategories(db, serverId, submitted)).toHaveLength(100);+ const persisted = await rows<{ id: string; position: number }>(+ "SELECT id, position FROM community_category WHERE server_id = ?",+ serverId,+ );+ const positionById = new Map(persisted.map((row) => [row.id, row.position]));+ submitted.forEach((id, index) => expect(positionById.get(id)).toBe(index));+ categoryIds.slice(100).forEach((id, index) => expect(positionById.get(id)).toBe(100 + index));++ const beforeDuplicate = new Map(positionById);+ expect(await queries.communityCategory.reorderCategories(db, serverId, [categoryIds[0], categoryIds[0]])).toEqual([]);+ const afterDuplicate = await rows<{ id: string; position: number }>(+ "SELECT id, position FROM community_category WHERE server_id = ?",+ serverId,+ );+ expect(new Map(afterDuplicate.map((row) => [row.id, row.position]))).toEqual(beforeDuplicate);+ });++ it("lists and resolves 125 DMs without dynamic IN binds", async () => {+ const prefix = fixturePrefix("dm");+ const viewerId = `${prefix}_viewer`;+ const now = "2026-08-28T02:00:00.000Z";+ await run(+ "INSERT INTO user (id, email, name, discriminator) VALUES (?, ?, 'Viewer', '3000')",+ viewerId,+ `${viewerId}@example.com`,+ );+ const peerIds = Array.from({ length: 125 }, (_, index) => `${prefix}_peer_${index}`);+ await runBatched(peerIds.flatMap((peerId, index) => {+ const channelId = `${prefix}_dm_${index}`;+ return [+ runtimeEnv.DB.prepare(+ "INSERT INTO user (id, email, name, discriminator) VALUES (?, ?, ?, ?)",+ ).bind(peerId, `${peerId}@example.com`, `Peer ${index}`, String(4000 + index)),+ runtimeEnv.DB.prepare(+ "INSERT INTO community_channel (id, server_id, name, type, last_message_at, created_at) VALUES (?, NULL, NULL, 'dm', ?, ?)",+ ).bind(channelId, `2026-08-28T02:${String(index % 60).padStart(2, "0")}:00.000Z`, now),+ runtimeEnv.DB.prepare(+ "INSERT INTO community_channel_member (id, channel_id, user_id, relation, source, added_at) VALUES (?, ?, ?, 'access', 'added', ?)",+ ).bind(`${prefix}_self_${index}`, channelId, viewerId, now),+ runtimeEnv.DB.prepare(+ "INSERT INTO community_channel_member (id, channel_id, user_id, relation, source, added_at) VALUES (?, ?, ?, 'access', 'added', ?)",+ ).bind(`${prefix}_peer_member_${index}`, channelId, peerId, now),+ ];+ }));++ const db = createDb(runtimeEnv.DB);+ const dms = await queries.communityDm.listDMs(db, viewerId);+ expect(dms).toHaveLength(125);+ expect(new Set(dms.map((dm) => dm.otherUserId))).toEqual(new Set(peerIds));+ expect((await queries.communityDm.getDMBetween(db, viewerId, peerIds[124])).id).toBe(`${prefix}_dm_124`);+ });++ it("reconciles 125 stale bot statuses in one fixed-bind update", async () => {+ const prefix = fixturePrefix("machine");+ const ownerId = `${prefix}_owner`;+ const machineId = `${prefix}_machine`;+ const now = "2026-08-28T03:00:00.000Z";+ await run(+ "INSERT INTO user (id, email, name, discriminator) VALUES (?, ?, 'Owner', '5000')",+ ownerId,+ `${ownerId}@example.com`,+ );+ await run(+ "INSERT INTO community_machine (id, user_id, display_name, hostname, available_runtimes, status, created_at, updated_at) VALUES (?, ?, 'Machine', 'host', '[]', 'online', ?, ?)",+ machineId,+ ownerId,+ now,+ now,+ );+ const botIds = Array.from({ length: 125 }, (_, index) => `${prefix}_bot_${index}`);+ await runBatched(botIds.flatMap((botId, index) => [+ runtimeEnv.DB.prepare(+ "INSERT INTO user (id, email, name, discriminator, isBot, ownerUserId) VALUES (?, ?, ?, ?, 1, ?)",+ ).bind(botId, `${botId}@example.com`, `Bot ${index}`, String(6000 + index), ownerId),+ runtimeEnv.DB.prepare(+ "INSERT INTO community_bot_binding (user_id, machine_id, runtime, instruction, created_at) VALUES (?, ?, 'codex', '', ?)",+ ).bind(botId, machineId, now),+ runtimeEnv.DB.prepare(+ "INSERT INTO community_user_profile (user_id, about_me, status_emoji, status_text) VALUES (?, '', '⚡', 'Working on it')",+ ).bind(botId),+ ]));++ const db = createDb(runtimeEnv.DB);+ expect(await queries.communityMachine.reconcileBotActivityFromRunningAgents(db, machineId, [])).toHaveLength(125);+ expect(await queries.communityMachine.reconcileBotActivityFromRunningAgents(db, machineId, [])).toEqual([]);+ expect(await rows<{ total: number }>(+ "SELECT COUNT(*) AS total FROM community_user_profile WHERE user_id GLOB ? AND status_emoji = '💤' AND status_text = 'Idle'",+ `${prefix}_bot_*`,+ )).toEqual([{ total: 125 }]);+ });++ it("hydrates 125 owned-bot friendships and DM approval cards", async () => {+ const prefix = fixturePrefix("friendship");+ const ownerId = `${prefix}_owner`;+ const now = "2026-08-28T03:30:00.000Z";+ await run(+ "INSERT INTO user (id, email, name, discriminator) VALUES (?, ?, 'Owner', '6500')",+ ownerId,+ `${ownerId}@example.com`,+ );++ const botIds = Array.from({ length: 125 }, (_, index) => `${prefix}_bot_${index}`);+ const peerIds = Array.from({ length: 125 }, (_, index) => `${prefix}_peer_${index}`);+ const friendshipIds = Array.from({ length: 125 }, (_, index) => `${prefix}_friendship_${index}`);+ const messageIds = Array.from({ length: 125 }, (_, index) => `${prefix}_approval_message_${index}`);+ await runBatched(botIds.flatMap((botId, index) => {+ const peerId = peerIds[index];+ const friendshipId = friendshipIds[index];+ const channelId = `${prefix}_approval_dm_${index}`;+ return [+ runtimeEnv.DB.prepare(+ "INSERT INTO user (id, email, name, discriminator, isBot, ownerUserId) VALUES (?, ?, ?, ?, 1, ?)",+ ).bind(botId, `${botId}@example.com`, `Bot ${index}`, String(6600 + index), ownerId),+ runtimeEnv.DB.prepare(+ "INSERT INTO user (id, email, name, discriminator) VALUES (?, ?, ?, ?)",+ ).bind(peerId, `${peerId}@example.com`, `Peer ${index}`, String(6800 + index)),+ runtimeEnv.DB.prepare(+ "INSERT INTO community_friendship (id, requester_id, addressee_id, status, needs_owner_approval, created_at, updated_at) VALUES (?, ?, ?, 'pending', ?, ?, ?)",+ ).bind(friendshipId, botId, peerId, ownerId, now, now),+ runtimeEnv.DB.prepare(+ "INSERT INTO community_channel (id, server_id, name, type, created_at) VALUES (?, NULL, NULL, 'dm', ?)",+ ).bind(channelId, now),+ runtimeEnv.DB.prepare(+ "INSERT INTO community_channel_member (id, channel_id, user_id, relation, source, added_at) VALUES (?, ?, ?, 'access', 'added', ?)",+ ).bind(`${prefix}_approval_owner_member_${index}`, channelId, ownerId, now),+ runtimeEnv.DB.prepare(+ "INSERT INTO community_channel_member (id, channel_id, user_id, relation, source, added_at) VALUES (?, ?, ?, 'access', 'added', ?)",+ ).bind(`${prefix}_approval_bot_member_${index}`, channelId, botId, now),+ runtimeEnv.DB.prepare(+ "INSERT INTO community_message (id, author_id, content, type, created_at, channel_id, seq, friendship_id) VALUES (?, ?, 'Approve?', 'friend_approval', ?, ?, 1, ?)",+ ).bind(messageIds[index], botId, now, channelId, friendshipId),+ ];+ }));++ const db = createDb(runtimeEnv.DB);+ expect(new Set(await queries.communityFriendship.getFriendUserIds(db, ownerId))).toEqual(new Set(botIds));+ const pending = await queries.communityFriendship.listPending(db, ownerId);+ expect(pending).toHaveLength(125);+ expect(new Set(pending.map((row) => row.id))).toEqual(new Set(friendshipIds));+ const approvals = await queries.communityFriendship.hydrateApprovalsForDmMessages(db, messageIds, ownerId);+ expect(approvals.size).toBe(125);+ expect(new Set(approvals.keys())).toEqual(new Set(messageIds));+ });+});diff --git a/src/web/test-runtime/d1-bind-workspace-daemon.runtime.test.ts b/src/web/test-runtime/d1-bind-workspace-daemon.runtime.test.tsnew file mode 100644index 000000000..03be99d0e--- /dev/null+++ b/src/web/test-runtime/d1-bind-workspace-daemon.runtime.test.ts@@ -0,0 +1,230 @@+/// <reference types="@cloudflare/vitest-plugin/types" />++import { env } from "cloudflare:workers";+import { afterEach, describe, expect, it } from "vitest";+import { createDb, queries } from "@alook/shared";++const runtimeEnv = env as unknown as CloudflareEnv;+const prefixes: string[] = [];+const triggers: string[] = [];++function fixturePrefix(label: string) {+ const prefix = `bind_${label}_${crypto.randomUUID().replaceAll("-", "")}`;+ prefixes.push(prefix);+ return prefix;+}++async function run(sql: string, ...bindings: unknown[]) {+ await runtimeEnv.DB.prepare(sql).bind(...bindings).run();+}++async function runBatched(statements: D1PreparedStatement[]) {+ for (let offset = 0; offset < statements.length; offset += 50) {+ await runtimeEnv.DB.batch(statements.slice(offset, offset + 50));+ }+}++async function first<T>(sql: string, ...bindings: unknown[]): Promise<T | null> {+ return runtimeEnv.DB.prepare(sql).bind(...bindings).first<T>();+}++afterEach(async () => {+ for (const trigger of triggers.splice(0)) {+ await run(`DROP TRIGGER IF EXISTS ${trigger}`);+ }+ for (const prefix of prefixes.splice(0)) {+ await run("DELETE FROM agent_task_queue WHERE id GLOB ?", `${prefix}*`);+ await run("DELETE FROM meeting_session WHERE id GLOB ?", `${prefix}*`);+ await run("DELETE FROM conversation WHERE id GLOB ?", `${prefix}*`);+ await run("DELETE FROM agent WHERE id GLOB ?", `${prefix}*`);+ await run("DELETE FROM agent_runtime WHERE id GLOB ?", `${prefix}*`);+ await run("DELETE FROM workspace WHERE id GLOB ?", `${prefix}*`);+ await run("DELETE FROM user WHERE id GLOB ?", `${prefix}*`);+ }+});++describe("workspace and daemon high-cardinality D1 queries", () => {+ it("preserves global overview/task/trace ordering and fixed-bind claims at 125", async () => {+ const prefix = fixturePrefix("workspace");+ const userId = `${prefix}_user`;+ const workspaceId = `${prefix}_workspace`;+ const now = "2026-08-28T04:00:00.000Z";+ await run(+ "INSERT INTO user (id, email, name, discriminator) VALUES (?, ?, 'Owner', '7000')",+ userId,+ `${userId}@example.com`,+ );+ await run(+ "INSERT INTO workspace (id, name, slug, onboarded, created_at, updated_at) VALUES (?, 'Workspace', ?, 1, ?, ?)",+ workspaceId,+ `${prefix}-workspace`,+ now,+ now,+ );++ const runtimeIds = Array.from({ length: 125 }, (_, index) => `${prefix}_runtime_${index}`);+ const agentIds = Array.from({ length: 125 }, (_, index) => `${prefix}_agent_${index}`);+ const conversationIds = Array.from({ length: 125 }, (_, index) => `${prefix}_conversation_${index}`);+ const terminalTaskIds = Array.from({ length: 125 }, (_, index) => `${prefix}_terminal_${index}`);+ const queuedTaskIds = Array.from({ length: 125 }, (_, index) => `${prefix}_queued_${index}`);+ await runBatched(agentIds.flatMap((agentId, index) => {+ const completedAt = `2026-08-28T04:${String(index % 60).padStart(2, "0")}:${String(index).padStart(3, "0")}Z`;+ return [+ runtimeEnv.DB.prepare(+ "INSERT INTO agent_runtime (id, workspace_id, daemon_id, runtime_mode, provider, device_info, created_at, updated_at) VALUES (?, ?, 'daemon-main', 'local', ?, '', ?, ?)",+ ).bind(runtimeIds[index], workspaceId, `provider-${index}`, now, now),+ runtimeEnv.DB.prepare(+ "INSERT INTO agent (id, workspace_id, name, runtime_id, owner_id, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)",+ ).bind(agentId, workspaceId, `Agent ${index}`, runtimeIds[index], userId, now, now),+ runtimeEnv.DB.prepare(+ "INSERT INTO conversation (id, workspace_id, agent_id, user_id, title, type, channel, created_at) VALUES (?, ?, ?, ?, '', 'user_dm_message', 'default', ?)",+ ).bind(conversationIds[index], workspaceId, agentId, userId, now),+ runtimeEnv.DB.prepare(+ "INSERT INTO agent_task_queue (id, agent_id, runtime_id, workspace_id, conversation_id, prompt, type, status, priority, created_at, completed_at, trace_id) VALUES (?, ?, ?, ?, ?, ?, 'user_dm_message', 'completed', 0, ?, ?, ?)",+ ).bind(terminalTaskIds[index], agentId, runtimeIds[index], workspaceId, conversationIds[index], `terminal-${index}`, now, completedAt, `${prefix}_trace_${index}`),+ runtimeEnv.DB.prepare(+ "INSERT INTO agent_task_queue (id, agent_id, runtime_id, workspace_id, conversation_id, prompt, type, status, priority, created_at) VALUES (?, ?, ?, ?, ?, ?, 'user_dm_message', 'queued', ?, ?)",+ ).bind(queuedTaskIds[index], agentId, runtimeIds[index], workspaceId, conversationIds[index], `queued-${index}`, index, now),+ ];+ }));++ const db = createDb(runtimeEnv.DB);+ const recent = await queries.overview.getRecentTerminalTasks(db, workspaceId, agentIds, 15);+ expect(recent).toHaveLength(15);+ expect(recent.map((task) => task.completedAt)).toEqual(+ [...recent.map((task) => task.completedAt)].sort().reverse(),+ );+ expect(await queries.overview.getConversationCountsByAgent(db, workspaceId, agentIds)).toHaveLength(125);+ expect(await queries.task.listActiveTaskCountsByWorkspace(db, workspaceId, agentIds, userId)).toHaveLength(125);+ expect(await queries.task.listActiveTasksByWorkspace(db, workspaceId, agentIds, userId)).toHaveLength(50);+ expect(await queries.task.listPendingTasksByRuntimes(db, runtimeIds, workspaceId)).toHaveLength(125);+ const traceAgents = await queries.task.getTraceAgentsByTaskIds(db, terminalTaskIds, workspaceId);+ expect(traceAgents.size).toBe(125);+ terminalTaskIds.forEach((taskId, index) => expect(traceAgents.get(taskId)).toEqual([agentIds[index]]));++ const killIds = Array.from({ length: 60 }, (_, index) => `${prefix}_kill_${index}`);+ await runBatched(killIds.map((taskId, index) =>+ runtimeEnv.DB.prepare(+ "INSERT INTO agent_task_queue (id, agent_id, runtime_id, workspace_id, conversation_id, prompt, type, status, priority, created_at) VALUES (?, ?, ?, ?, ?, 'kill', 'kill_task', 'queued', 0, ?)",+ ).bind(taskId, agentIds[index], runtimeIds[index], workspaceId, conversationIds[index], now),+ ));+ expect(await queries.task.claimKillTasks(db, runtimeIds, workspaceId, 50)).toHaveLength(50);+ expect(await first<{ total: number }>(+ "SELECT COUNT(*) AS total FROM agent_task_queue WHERE id GLOB ? AND type = 'kill_task' AND status = 'queued'",+ `${prefix}_kill_*`,+ )).toEqual({ total: 10 });+ });++ it("claims around 125 blocked conversations and fails stale running tasks atomically", async () => {+ const prefix = fixturePrefix("blocked");+ const userId = `${prefix}_user`;+ const workspaceId = `${prefix}_workspace`;+ const runtimeId = `${prefix}_runtime`;+ const agentId = `${prefix}_agent`;+ const now = "2026-08-28T05:00:00.000Z";+ const staleStart = "2026-08-27T00:00:00.000Z";+ await run("INSERT INTO user (id, email, name, discriminator) VALUES (?, ?, 'Owner', '7100')", userId, `${userId}@example.com`);+ await run("INSERT INTO workspace (id, name, slug, created_at, updated_at) VALUES (?, 'Workspace', ?, ?, ?)", workspaceId, `${prefix}-workspace`, now, now);+ await run("INSERT INTO agent_runtime (id, workspace_id, daemon_id, runtime_mode, provider, device_info, created_at, updated_at) VALUES (?, ?, 'daemon-blocked', 'local', 'codex', '', ?, ?)", runtimeId, workspaceId, now, now);+ await run("INSERT INTO agent (id, workspace_id, name, runtime_id, owner_id, created_at, updated_at) VALUES (?, ?, 'Agent', ?, ?, ?, ?)", agentId, workspaceId, runtimeId, userId, now, now);++ const blockedConversationIds = Array.from({ length: 125 }, (_, index) => `${prefix}_blocked_conversation_${index}`);+ await runBatched(blockedConversationIds.flatMap((conversationId, index) => [+ runtimeEnv.DB.prepare(+ "INSERT INTO conversation (id, workspace_id, agent_id, user_id, title, type, channel, created_at) VALUES (?, ?, ?, ?, '', 'user_dm_message', 'default', ?)",+ ).bind(conversationId, workspaceId, agentId, userId, now),+ runtimeEnv.DB.prepare(+ "INSERT INTO agent_task_queue (id, agent_id, runtime_id, workspace_id, conversation_id, prompt, type, status, priority, created_at, started_at) VALUES (?, ?, ?, ?, ?, 'running', 'user_dm_message', 'running', 0, ?, ?)",+ ).bind(`${prefix}_running_${index}`, agentId, runtimeId, workspaceId, conversationId, now, staleStart),+ ]));+ const freeConversationId = `${prefix}_free_conversation`;+ await run(+ "INSERT INTO conversation (id, workspace_id, agent_id, user_id, title, type, channel, created_at) VALUES (?, ?, ?, ?, '', 'user_dm_message', 'default', ?)",+ freeConversationId,+ workspaceId,+ agentId,+ userId,+ now,+ );+ await run(+ "INSERT INTO agent_task_queue (id, agent_id, runtime_id, workspace_id, conversation_id, prompt, type, status, priority, created_at) VALUES (?, ?, ?, ?, ?, 'free', 'user_dm_message', 'queued', 1, ?)",+ `${prefix}_free_task`,+ agentId,+ runtimeId,+ workspaceId,+ freeConversationId,+ now,+ );++ const db = createDb(runtimeEnv.DB);+ expect((await queries.task.claimTask(db, agentId, workspaceId))?.id).toBe(`${prefix}_free_task`);+ expect(await queries.task.failStaleRunningTasks(db, workspaceId, 60)).toHaveLength(125);+ expect(await queries.task.failStaleRunningTasks(db, workspaceId, 60)).toEqual([]);+ });++ it("drains 125 due meetings in stable 50/50/25 pages", async () => {+ const prefix = fixturePrefix("meetings");+ const userId = `${prefix}_user`;+ const workspaceId = `${prefix}_workspace`;+ const runtimeId = `${prefix}_runtime`;+ const agentId = `${prefix}_agent`;+ const now = "2026-08-28T06:00:00.000Z";+ await run("INSERT INTO user (id, email, name, discriminator) VALUES (?, ?, 'Owner', '7200')", userId, `${userId}@example.com`);+ await run("INSERT INTO workspace (id, name, slug, created_at, updated_at) VALUES (?, 'Workspace', ?, ?, ?)", workspaceId, `${prefix}-workspace`, now, now);+ await run("INSERT INTO agent_runtime (id, workspace_id, daemon_id, runtime_mode, provider, device_info, created_at, updated_at) VALUES (?, ?, 'daemon-meeting', 'local', 'codex', '', ?, ?)", runtimeId, workspaceId, now, now);+ await run("INSERT INTO agent (id, workspace_id, name, runtime_id, owner_id, created_at, updated_at) VALUES (?, ?, 'Agent', ?, ?, ?, ?)", agentId, workspaceId, runtimeId, userId, now, now);+ await runBatched(Array.from({ length: 125 }, (_, index) =>+ runtimeEnv.DB.prepare(+ "INSERT INTO meeting_session (id, agent_id, workspace_id, title, meeting_url, status, is_whitelisted, participants, scheduled_at, created_at, updated_at) VALUES (?, ?, ?, ?, ?, 'scheduled', 1, '[]', ?, ?, ?)",+ ).bind(`${prefix}_meeting_${String(index).padStart(3, "0")}`, agentId, workspaceId, `Meeting ${index}`, `https://example.com/${index}`, now, now, now),+ ));++ const db = createDb(runtimeEnv.DB);+ const claimedIds: string[] = [];+ for (const expected of [50, 50, 25]) {+ const due = await queries.meetingSession.listScheduledMeetings(db, workspaceId, now);+ expect(due).toHaveLength(expected);+ const claimed = await queries.meetingSession.claimMeetingSessions(db, due.map((meeting) => meeting.id), workspaceId, now);+ expect(claimed).toHaveLength(expected);+ claimedIds.push(...claimed.map((meeting) => meeting.id));+ }+ expect(new Set(claimedIds).size).toBe(125);+ expect(await queries.meetingSession.listScheduledMeetings(db, workspaceId, now)).toEqual([]);+ });++ it("deletes 125 daemon runtimes atomically and rolls back detach on failure", async () => {+ const prefix = fixturePrefix("runtime_delete");+ const userId = `${prefix}_user`;+ const workspaceId = `${prefix}_workspace`;+ const now = "2026-08-28T07:00:00.000Z";+ await run("INSERT INTO user (id, email, name, discriminator) VALUES (?, ?, 'Owner', '7300')", userId, `${userId}@example.com`);+ await run("INSERT INTO workspace (id, name, slug, created_at, updated_at) VALUES (?, 'Workspace', ?, ?, ?)", workspaceId, `${prefix}-workspace`, now, now);++ const runtimeIds = Array.from({ length: 125 }, (_, index) => `${prefix}_runtime_${index}`);+ const agentIds = Array.from({ length: 125 }, (_, index) => `${prefix}_agent_${index}`);+ await runBatched(runtimeIds.flatMap((runtimeId, index) => [+ runtimeEnv.DB.prepare(+ "INSERT INTO agent_runtime (id, workspace_id, daemon_id, runtime_mode, provider, device_info, created_at, updated_at) VALUES (?, ?, 'daemon-delete', 'local', ?, '', ?, ?)",+ ).bind(runtimeId, workspaceId, `provider-${index}`, now, now),+ runtimeEnv.DB.prepare(+ "INSERT INTO agent (id, workspace_id, name, runtime_id, owner_id, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)",+ ).bind(agentIds[index], workspaceId, `Agent ${index}`, runtimeId, userId, now, now),+ ]));+ const db = createDb(runtimeEnv.DB);+ await queries.runtime.deleteRuntimesByDaemonId(db, "daemon-delete", workspaceId);+ expect(await first<{ total: number }>("SELECT COUNT(*) AS total FROM agent_runtime WHERE workspace_id = ?", workspaceId)).toEqual({ total: 0 });+ expect(await first<{ total: number }>("SELECT COUNT(*) AS total FROM agent WHERE workspace_id = ? AND runtime_id IS NULL", workspaceId)).toEqual({ total: 125 });++ const rollbackRuntime = `${prefix}_rollback_runtime`;+ const rollbackAgent = `${prefix}_rollback_agent`;+ await run("INSERT INTO agent_runtime (id, workspace_id, daemon_id, runtime_mode, provider, device_info, created_at, updated_at) VALUES (?, ?, 'daemon-rollback', 'local', 'rollback-provider', '', ?, ?)", rollbackRuntime, workspaceId, now, now);+ await run("INSERT INTO agent (id, workspace_id, name, runtime_id, owner_id, created_at, updated_at) VALUES (?, ?, 'Rollback Agent', ?, ?, ?, ?)", rollbackAgent, workspaceId, rollbackRuntime, userId, now, now);+ const trigger = `abort_runtime_delete_${crypto.randomUUID().replaceAll("-", "")}`;+ triggers.push(trigger);+ await run(`CREATE TRIGGER ${trigger} BEFORE DELETE ON agent_runtime BEGIN SELECT RAISE(ABORT, 'forced runtime delete failure'); END`);++ await expect(queries.runtime.deleteRuntimesByDaemonId(db, "daemon-rollback", workspaceId)).rejects.toThrow(/forced runtime delete failure/);+ expect(await first<{ runtimeId: string }>("SELECT runtime_id AS runtimeId FROM agent WHERE id = ?", rollbackAgent)).toEqual({ runtimeId: rollbackRuntime });+ expect(await first<{ total: number }>("SELECT COUNT(*) AS total FROM agent_runtime WHERE id = ?", rollbackRuntime)).toEqual({ total: 1 });+ });+});