Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): bound database reads across remote listings (#4563)
Chris Zhange787b57b33a0
diff --git a/apps/desktop/src/main/device-link/__tests__/dispatchSendSafety.test.ts b/apps/desktop/src/main/device-link/__tests__/dispatchSendSafety.test.tsindex 4533300d13..b125b060fd 100644--- a/apps/desktop/src/main/device-link/__tests__/dispatchSendSafety.test.ts+++ b/apps/desktop/src/main/device-link/__tests__/dispatchSendSafety.test.ts@@ -45,6 +45,7 @@ vi.mock('../settings-store', () => ({import { __testing, runInvoke, wireInboundDispatch } from '../dispatch';import { __testing as registry } from '../invoke-registry';import { setRemoteBotSessionLookup } from '../remoteBotSessionBoundary';+import { currentDbRpcAdmissionClass } from '../../localDb/client/rpcAdmission';import { getDeviceLinkInvokeContext } from '../invoke-context';import { HistoryViewController, type HistoryViewPage, type HistoryMessageSource } from '@cindy/maker-shared/message-window';import * as subscriptions from '../subscriptions';@@ -946,3 +947,185 @@ describe('remote companion Session visibility at the device-link boundary', () =expect(newClient.sendPush).not.toHaveBeenCalled();});});+++describe('background database admission covers the complete remote list lifecycle', () => {+ it.each(['local-db:sessions:list', 'local-db:bots:list', 'maker:list-active', 'local-db:sessions:interrupted-pending', 'maker:remote-resources:list'])(+ 'keeps %s handler and visibility checks in background admission', async (channel) => {+ const admissions: string[] = [];+ setRemoteBotSessionLookup(async () => {+ admissions.push(currentDbRpcAdmissionClass());+ return 'ordinary';+ });+ registry.register(channel, () => {+ admissions.push(currentDbRpcAdmissionClass());+ return channel === 'maker:remote-resources:list'+ ? { items: [{ ref: { id: 's1', kind: 'bot' } }] } : [{ id: 's1' }];+ });+ expect(await runInvoke('ctrl-1', { channel, args: ['s1'] })).toMatchObject({ ok: true });+ expect(admissions.length).toBeGreaterThanOrEqual(4);+ expect(new Set(admissions)).toEqual(new Set(['background']));+ expect(currentDbRpcAdmissionClass()).toBe('interactive');+ },+ );++ it('keeps sending interactive while an unrelated background list is awaiting permission checks', async () => {+ let finish!: () => void;+ const pending = new Promise<void>((resolve) => { finish = resolve; });+ const admissions: string[] = [];+ setRemoteBotSessionLookup(async (id) => {+ if (id === 'listing') await pending;+ admissions.push(currentDbRpcAdmissionClass());+ return 'ordinary';+ });+ registry.register('local-db:sessions:list', () => []);+ registry.register('maker:send', () => { admissions.push(currentDbRpcAdmissionClass()); return {}; });+ const list = runInvoke('ctrl-1', { channel: 'local-db:sessions:list', args: ['listing'] });+ try {+ expect(await runInvoke('ctrl-2', { channel: 'maker:send', args: ['s1', { text: 'hello' }] })).toMatchObject({ ok: true });+ expect(new Set(admissions)).toEqual(new Set(['interactive']));+ } finally { finish(); await list; }+ });++ it('rechecks cached and backpressured lists using background admission and fresh visibility', async () => {+ const admissions: string[] = [];+ let hidden = false;+ setRemoteBotSessionLookup(async () => {+ admissions.push(currentDbRpcAdmissionClass());+ return hidden ? 'hidden' : 'ordinary';+ });+ const handler = vi.fn(() => [{ id: 's1' }]);+ registry.register('local-db:sessions:list', handler);+ const client = mkClient();+ wireInboundDispatch(client as never);+ const frame = client.onFrame.mock.calls[0][0];+ const request = { v: PROTOCOL_VERSION, kind: 'invoke', src: 'ctrl-1', id: 'cached-list', payload: { channel: 'local-db:sessions:list', args: [] } };+ frame(request);+ await vi.waitFor(() => expect(client.sendInvokeResult).toHaveBeenCalledTimes(1));+ admissions.length = 0;+ hidden = true;+ frame(request);+ await vi.waitFor(() => expect(client.sendInvokeResult).toHaveBeenCalledTimes(2));+ expect(client.sendInvokeResult.mock.calls[1][2]).toMatchObject({ ok: true, result: [] });+ expect(handler).toHaveBeenCalledTimes(1);+ expect(admissions.length).toBeGreaterThan(0);+ expect(new Set(admissions)).toEqual(new Set(['background']));++ admissions.length = 0;+ client.sendInvokeResult.mockImplementationOnce(() => { throw new DeviceLinkError('BACKPRESSURE', 'full'); });+ __testing.sendInvokeResultSafe(client as never, 'ctrl-1', 'queued-list', { ok: true, result: [{ id: 's1' }] }, 'local-db:sessions:list', []);+ __testing.flushRemoteInvokeResultOutbox();+ await vi.waitFor(() => expect(__testing.remoteInvokeResultOutboxSize()).toBe(0));+ expect(client.sendInvokeResult.mock.calls.at(-1)![2]).toMatchObject({ ok: true, result: [] });+ expect(admissions.length).toBeGreaterThan(0);+ expect(new Set(admissions)).toEqual(new Set(['background']));+ });+});+++it.each(['local-db:sessions:list', 'local-db:sessions:get', 'local-db:sessions:interrupted-pending', 'maker:list-active'])('reports DB overload during %s replay as backpressure without sending unchecked data', async (channel) => {+ const client = mkClient({ sendInvokeResult: vi.fn().mockImplementationOnce(() => { throw new DeviceLinkError('BACKPRESSURE', 'full'); }) });+ __testing.setActiveClient(client as never);+ __testing.sendInvokeResultSafe(client as never, 'ctrl-1', 'overloaded-list', { ok: true, result: [{ id: 'private' }] }, channel, ['s1']);+ setRemoteBotSessionLookup(async () => { throw new Error('db worker RPC queue overloaded: op="rawAll"'); });+ __testing.flushRemoteInvokeResultOutbox();+ await vi.waitFor(() => expect(__testing.remoteInvokeResultOutboxSize()).toBe(0));+ expect(client.sendInvokeResult.mock.calls.at(-1)![2]).toEqual({+ ok: false, error: { code: 'BACKPRESSURE', message: 'db worker RPC queue overloaded: op="rawAll"' },+ });+});++it.each(['maker:send', 'maker:input:enqueue', 'maker:remote-resources:list', 'maker:remote-resources:get', 'local-db:bots:list', 'local-db:bots:get'])(+ 'never marks a completed %s as safely retryable when outbox authorization overloads', async (channel) => {+ const client = mkClient({ sendInvokeResult: vi.fn().mockImplementationOnce(() => { throw new DeviceLinkError('BACKPRESSURE', 'full'); }) });+ __testing.setActiveClient(client as never);+ __testing.sendInvokeResultSafe(client as never, 'ctrl-1', 'completed-send', { ok: true, result: { privateReceipt: 'accepted' } }, channel, ['s1']);+ expect(__testing.remoteInvokeResultOutboxSize()).toBe(1);+ setRemoteBotSessionLookup(async () => { throw new Error('db worker RPC queue overloaded: op="rawAll"'); });+ __testing.flushRemoteInvokeResultOutbox();+ await vi.waitFor(() => expect(__testing.remoteInvokeResultOutboxSize()).toBe(0));+ expect(client.sendInvokeResult.mock.calls.at(-1)![2]).toEqual({+ ok: false, error: { code: 'IPC_ERROR', message: '[NOT_FOUND] Session does not exist' },+ });+ },+);++it('does not reexecute a cached send after authorization overloads and another controller stays usable', async () => {+ let overloaded = false;+ setRemoteBotSessionLookup(async (id) => {+ if (overloaded && id === 's1') throw new Error('db worker RPC queue overloaded: op="rawAll"');+ return 'ordinary';+ });+ const handler = vi.fn(() => ({ accepted: true }));+ registry.register('maker:send', handler);+ const client = mkClient();+ wireInboundDispatch(client as never);+ const frame = client.onFrame.mock.calls[0][0];+ const request = { v: PROTOCOL_VERSION, kind: 'invoke', src: 'ctrl-1', id: 'cached-send', payload: { channel: 'maker:send', args: ['s1', { text: 'hello' }] } };+ frame(request);+ await vi.waitFor(() => expect(client.sendInvokeResult).toHaveBeenCalledTimes(1));+ expect(client.sendInvokeResult.mock.calls[0][2]).toMatchObject({ ok: true });+ overloaded = true;+ frame(request);+ await vi.waitFor(() => expect(client.sendInvokeResult).toHaveBeenCalledTimes(2));+ expect(client.sendInvokeResult.mock.calls[1][2]).toMatchObject({ ok: false, error: { code: 'IPC_ERROR' } });+ expect(handler).toHaveBeenCalledTimes(1);+ frame({ ...request, src: 'ctrl-2', id: 'other-send', payload: { ...request.payload, args: ['s2', { text: 'other' }] } });+ await vi.waitFor(() => expect(client.sendInvokeResult).toHaveBeenCalledTimes(3));+ expect(client.sendInvokeResult.mock.calls[2]).toEqual(['ctrl-2', 'other-send', { ok: true, result: { accepted: true } }]);+ expect(handler).toHaveBeenCalledTimes(2);+ expect(client.closeLink).not.toHaveBeenCalled();+});++it.each(['maker:send', 'maker:input:enqueue', 'maker:remote-resources:list', 'maker:remote-resources:get', 'local-db:bots:list', 'local-db:bots:get'])('does not advertise retry after %s succeeds and post-handler authorization overloads', async (channel) => {+ const handler = vi.fn(() => ({ accepted: true }));+ registry.register(channel, handler);+ setRemoteBotSessionLookup(async () => {+ if (handler.mock.calls.length) throw new Error('db worker RPC queue overloaded: op="rawAll"');+ return 'ordinary';+ });+ expect(await runInvoke('ctrl-1', { channel, args: ['s1', { text: 'hello' }] })).toMatchObject({ ok: false, error: { code: 'IPC_ERROR' } });+ expect(handler).toHaveBeenCalledTimes(1);+});++it.each(['local-db:sessions:list', 'local-db:sessions:get', 'local-db:sessions:interrupted-pending', 'maker:list-active'])(+ 'keeps completed %s reads retryable after post-handler authorization overloads', async (channel) => {+ const handler = vi.fn(() => []);+ registry.register(channel, handler);+ setRemoteBotSessionLookup(async () => {+ if (handler.mock.calls.length) throw new Error('db worker RPC queue overloaded: op="rawAll"');+ return 'ordinary';+ });+ expect(await runInvoke('ctrl-1', { channel, args: ['s1'] })).toMatchObject({ ok: false, error: { code: 'BACKPRESSURE' } });+ expect(handler).toHaveBeenCalledTimes(1);+ },+);++it('keeps pre-handler overload safely retryable without executing the mutation', async () => {+ const handler = vi.fn();+ registry.register('maker:send', handler);+ setRemoteBotSessionLookup(async () => { throw new Error('db worker RPC queue overloaded: op="rawAll"'); });+ expect(await runInvoke('ctrl-1', { channel: 'maker:send', args: ['s1', { text: 'hello' }] })).toMatchObject({ ok: false, error: { code: 'BACKPRESSURE' } });+ expect(handler).not.toHaveBeenCalled();+});++it('isolates an unresponsive peer with an overloaded mutation replay from another peer', async () => {+ const client = mkClient({ sendInvokeResult: vi.fn((dst) => {+ if (dst === 'ctrl-1') throw new DeviceLinkError('BACKPRESSURE', 'peer stopped acknowledging');+ }) });+ wireInboundDispatch(client as never);+ __testing.sendInvokeResultSafe(client as never, 'ctrl-1', 'completed-send', { ok: true, result: { accepted: true } }, 'maker:send', ['s1']);+ setRemoteBotSessionLookup(async (id) => {+ if (id === 's1') throw new Error('db worker RPC queue overloaded: op="rawAll"');+ return 'ordinary';+ });+ const handler = vi.fn(() => ({ accepted: true }));+ registry.register('maker:send', handler);+ __testing.flushRemoteInvokeResultOutbox();+ client.onFrame.mock.calls[0][0]({ v: PROTOCOL_VERSION, kind: 'invoke', src: 'ctrl-2', id: 'other-send', payload: { channel: 'maker:send', args: ['s2', { text: 'other' }] } });+ await vi.waitFor(() => expect(client.sendInvokeResult).toHaveBeenCalledWith('ctrl-2', 'other-send', { ok: true, result: { accepted: true } }));+ expect(client.sendInvokeResult.mock.calls.filter(([dst]) => dst === 'ctrl-1').at(-1)![2]).toMatchObject({ ok: false, error: { code: 'IPC_ERROR' } });+ expect(__testing.remoteInvokeResultOutboxSize()).toBe(1);+ expect(handler).toHaveBeenCalledTimes(1);+ expect(client.closeLink).not.toHaveBeenCalled();+});diff --git a/apps/desktop/src/main/device-link/__tests__/remoteBotSessionBoundary.test.ts b/apps/desktop/src/main/device-link/__tests__/remoteBotSessionBoundary.test.tsnew file mode 100644index 0000000000..c126b7a49e--- /dev/null+++ b/apps/desktop/src/main/device-link/__tests__/remoteBotSessionBoundary.test.ts@@ -0,0 +1,45 @@+import { afterEach, expect, it, vi } from 'vitest';+import { projectRemoteSessionResult, setRemoteBotSessionLookup } from '../remoteBotSessionBoundary';++afterEach(() => setRemoteBotSessionLookup(null));++it('bounds single-lookup adapters, deduplicates checks and preserves collection order', async () => {+ let active = 0;+ let peak = 0;+ const lookup = vi.fn(async (id: string) => {+ peak = Math.max(peak, ++active);+ await new Promise<void>((resolve) => setImmediate(resolve));+ active--;+ return id === 'hidden' ? 'hidden' as const : 'ordinary' as const;+ });+ setRemoteBotSessionLookup(lookup);+ const rows = Array.from({ length: 1000 }, (_, i) => ({ id: `${i}` }));+ expect(await projectRemoteSessionResult('local-db:sessions:list', { sessions: [null, ...rows, { id: 'hidden' }, rows[0]], count: 1002 }))+ .toEqual({ sessions: [...rows, rows[0]], count: 1002 });+ expect(lookup).toHaveBeenCalledTimes(1001);+ expect(peak).toBe(1);+});++it('batches Bot resource identities, preserves other resource kinds and recomputes filtered revision', async () => {+ const single = vi.fn(async () => 'visible' as const);+ const batch = vi.fn(async () => new Map([['visible', 'visible' as const], ['hidden', 'hidden' as const]]));+ setRemoteBotSessionLookup(single, batch);+ const other = { ref: { id: 'other', kind: 'document' }, revision: 'a' };+ const visible = { ref: { id: 'visible', kind: 'bot' }, revision: 'b' };+ const result = await projectRemoteSessionResult('maker:remote-resources:list', {+ items: [other, visible, { ref: { id: 'hidden', kind: 'bot' } }], revision: 'old', collectionId: 'teammates',+ });+ expect(result).toEqual({ items: [other, visible], revision: 'a|b', collectionId: 'teammates' });+ expect(batch).toHaveBeenCalledWith(['visible', 'hidden'], 'bot');+ expect(single).not.toHaveBeenCalled();+});++it('fails closed for incomplete batch results, rejects hidden gets and clears a replaced batch adapter', async () => {+ const batch = vi.fn(async () => new Map());+ setRemoteBotSessionLookup(async () => 'hidden', batch);+ expect(await projectRemoteSessionResult('maker:list-active', [{ sessionId: 's1' }])).toEqual([]);+ await expect(projectRemoteSessionResult('local-db:sessions:get', { id: 's1' })).rejects.toThrow('[NOT_FOUND]');+ setRemoteBotSessionLookup(async () => 'ordinary');+ expect(await projectRemoteSessionResult('maker:list-active', [{ sessionId: 's1' }])).toEqual([{ sessionId: 's1' }]);+ expect(batch).toHaveBeenCalledTimes(1);+});diff --git a/apps/desktop/src/main/device-link/dispatch.ts b/apps/desktop/src/main/device-link/dispatch.tsindex 7b118e3d84..f87bc87c11 100644--- a/apps/desktop/src/main/device-link/dispatch.ts+++ b/apps/desktop/src/main/device-link/dispatch.ts@@ -24,6 +24,7 @@ import { requestFilePeer, stopFilePeers } from './filePeer';import {computeAllowlistHash,canCoalesceRemoteListing,+ isCompletedInvokeRetryableReadChannel,INVOKE_TIMEOUT_OVERRIDES_MS,MAX_FRAME_BYTES,PROTOCOL_VERSION,@@ -644,11 +645,19 @@ const REMOTE_INVOKE_IN_FLIGHT_LIMIT = 64;/** Host DB admission is independent of whether a read can share an in-flight snapshot. */const BACKGROUND_REMOTE_INVOKE_CHANNELS: ReadonlySet<string> = new Set(['local-db:sessions:list',+ 'local-db:sessions:interrupted-pending',+ 'maker:list-active',+ 'local-db:bots:list',+ 'maker:remote-resources:list','maker:get-capabilities','maker:provider:list','maker:git-safety:get','maker:schedule:list-sidebar-index-runs',]);+/** Include pre/post authorization and cached delivery, not only the IPC handler. */+function withRemoteDbAdmission<T>(channel: string | undefined, fn: () => T): T {+ return channel && BACKGROUND_REMOTE_INVOKE_CHANNELS.has(channel) ? runAsBackgroundDbRpc(fn) : fn();+}/** 隧道回包必须低于 4MB 传输上限与 per-controller 4MB 准入;2MB 给 envelope 留余量。 */const REMOTE_SCHEDULE_INDEX_MAX_BYTES = 2 * 1024 * 1024;/**@@ -2824,13 +2833,19 @@ function sanitizeMessageInvokeResult(async function authorizeRemoteBotResult(channel: string | undefined, args: unknown[] | undefined, result: InvokeResultPayload,): Promise<InvokeResultPayload> {- if (!result.ok) return result;- try {- await assertRemoteBotInvocationAllowed(args ?? [], channel);- return { ok: true, result: await projectRemoteSessionResult(channel ?? '', result.result) };- } catch {- return { ok: false, error: { code: 'IPC_ERROR', message: '[NOT_FOUND] Session does not exist' } };- }+ return withRemoteDbAdmission(channel, async () => {+ if (!result.ok) return result;+ try {+ await assertRemoteBotInvocationAllowed(args ?? [], channel);+ return { ok: true, result: await projectRemoteSessionResult(channel ?? '', result.result) };+ } catch (err) {+ const message = err instanceof Error ? err.message : String(err);+ if (isDbWorkerOverloadedError(message) && isCompletedInvokeRetryableReadChannel(channel)) {+ return { ok: false, error: { code: 'BACKPRESSURE', message } };+ }+ return { ok: false, error: { code: 'IPC_ERROR', message: '[NOT_FOUND] Session does not exist' } };+ }+ });}/** Revalidate cached/replayed replies without executing a mutation twice. */@@ -3509,6 +3524,10 @@ export async function runInvoke(src: string,payload: InvokePayload | undefined,): Promise<InvokeResultPayload> {+ return withRemoteDbAdmission(payload?.channel, () => executeRemoteInvoke(src, payload));+}++async function executeRemoteInvoke(src: string, payload: InvokePayload | undefined): Promise<InvokeResultPayload> {if (!payload || typeof payload.channel !== 'string') {return { ok: false, error: { code: 'INTERNAL', message: 'malformed invoke payload' } };}@@ -3701,6 +3720,7 @@ export async function runInvoke(}}+ let handlerCompleted = false;try {const args = payload.args ?? [];const invocationOwner = broadcastTap.captureDataOwnerBroadcastScope();@@ -3720,17 +3740,12 @@ export async function runInvoke(historyView,},// provider:list 的首参只承载隧道能力协商,不进入本机 IPC handler。- // 对账 listing 走后台读配额,不占满写入名额。- () => {- const invoke = () => dispatchLocalInvoke(- payload.channel,- payload.channel === 'maker:provider:list' ? [] : args,- );- return BACKGROUND_REMOTE_INVOKE_CHANNELS.has(payload.channel)- ? runAsBackgroundDbRpc(invoke)- : invoke();- },+ () => dispatchLocalInvoke(+ payload.channel,+ payload.channel === 'maker:provider:list' ? [] : args,+ ),);+ handlerCompleted = true;if (hasRemoteBotSessionLookup()) await assertRemoteBotInvocationAllowed(args, payload.channel);if (!broadcastTap.isDataOwnerBroadcastScopeCurrent(invocationOwner)) throw new Error('[NOT_FOUND] Session does not exist');// 远程 set-* 回流:被控端 set-* runtime-only,补一次 DB 持久化 + 广播 patched,让控制端@@ -3753,7 +3768,7 @@ export async function runInvoke(// 被控端 handler 的 throwIpcError `[CODE] message` 原样透传,// 控制端 renderer 继续用 extractIpcError 解码const message = err instanceof Error ? err.message : String(err);- if (isDbWorkerOverloadedError(message)) {+ if (isDbWorkerOverloadedError(message) && (!handlerCompleted || isCompletedInvokeRetryableReadChannel(payload.channel))) {return { ok: false, error: { code: 'BACKPRESSURE', message } };}return { ok: false, error: { code: 'IPC_ERROR', message } };diff --git a/apps/desktop/src/main/device-link/remoteBotSessionBoundary.ts b/apps/desktop/src/main/device-link/remoteBotSessionBoundary.tsindex dc48237f00..eee53fd8dc 100644--- a/apps/desktop/src/main/device-link/remoteBotSessionBoundary.ts+++ b/apps/desktop/src/main/device-link/remoteBotSessionBoundary.ts@@ -1,8 +1,13 @@export type RemoteBotSessionAccess = 'ordinary' | 'visible' | 'hidden' | 'missing';type Lookup = (sessionId: string, kind?: 'session' | 'bot') => Promise<RemoteBotSessionAccess>;+export type RemoteBotSessionBatchLookup = (ids: readonly string[], kind: 'session' | 'bot') => Promise<ReadonlyMap<string, RemoteBotSessionAccess>>;let lookup: Lookup | null = null;+let batchLookup: RemoteBotSessionBatchLookup | null = null;-export function setRemoteBotSessionLookup(value: Lookup | null): void { lookup = value; }+export function setRemoteBotSessionLookup(value: Lookup | null, batch: RemoteBotSessionBatchLookup | null = null): void {+ lookup = value;+ batchLookup = value ? batch : null;+}function record(value: unknown): Record<string, unknown> | null {return value && typeof value === 'object' && !Array.isArray(value) ? value as Record<string, unknown> : null;@@ -45,16 +50,41 @@ export async function assertRemoteBotInvocationAllowed(args: unknown[], channelexport async function projectRemoteSessionResult(channel: string, value: unknown): Promise<unknown> {if (!lookup || !['local-db:sessions:get', 'local-db:sessions:list', 'maker:list-active', 'local-db:sessions:interrupted-pending', 'local-db:bots:get', 'local-db:bots:list', 'maker:remote-resources:get', 'maker:remote-resources:list'].includes(channel)) return value;- const project = async (item: unknown) => {+ const activeLookup = lookup;+ const activeBatchLookup = batchLookup;+ const identity = (item: unknown) => {const row = record(item);const ref = record(row?.ref);- if (channel.startsWith('maker:remote-resources:') && ref?.kind !== 'bot') return item;+ if (channel.startsWith('maker:remote-resources:') && ref?.kind !== 'bot') return null;const id = ref?.id ?? row?.id ?? row?.sessionId;- if (!row || typeof id !== 'string') return item;- const access = await lookup!(id, ref?.kind === 'bot' || channel.startsWith('local-db:bots:') ? 'bot' : 'session');- return access === 'hidden' ? null : item;+ if (!row || typeof id !== 'string') return null;+ return { id, kind: ref?.kind === 'bot' || channel.startsWith('local-db:bots:') ? 'bot' as const : 'session' as const };+ };+ const project = async (item: unknown) => {+ const key = identity(item);+ if (!key) return item;+ return await activeLookup(key.id, key.kind) === 'hidden' ? null : item;};- if (Array.isArray(value)) return (await Promise.all(value.map(project))).filter((item) => item !== null);+ if (Array.isArray(value)) {+ const identities = value.map(identity);+ const accessByKind = new Map<'session' | 'bot', ReadonlyMap<string, RemoteBotSessionAccess>>();+ for (const kind of ['session', 'bot'] as const) {+ const ids = [...new Set(identities.filter((key) => key?.kind === kind).map((key) => key!.id))];+ if (!ids.length) continue;+ if (activeBatchLookup) {+ accessByKind.set(kind, await activeBatchLookup(ids, kind));+ } else {+ // Injected single-lookup consumers must not recreate the per-row RPC burst.+ const access = new Map<string, RemoteBotSessionAccess>();+ for (const id of ids) access.set(id, await activeLookup(id, kind));+ accessByKind.set(kind, access);+ }+ }+ return value.filter((item, index) => {+ const key = identities[index];+ return item !== null && (!key || (accessByKind.get(key.kind)?.get(key.id) ?? 'hidden') !== 'hidden');+ });+ }const row = record(value);if (row && !row.id && Array.isArray(row.sessions)) return { ...row, sessions: await projectRemoteSessionResult(channel, row.sessions) };if (row && Array.isArray(row.items)) {diff --git a/apps/desktop/src/main/localDb/client/__tests__/WorkerThreadTransport.test.ts b/apps/desktop/src/main/localDb/client/__tests__/WorkerThreadTransport.test.tsindex 4a69af2d78..df17e967ab 100644--- a/apps/desktop/src/main/localDb/client/__tests__/WorkerThreadTransport.test.ts+++ b/apps/desktop/src/main/localDb/client/__tests__/WorkerThreadTransport.test.ts@@ -206,6 +206,7 @@ describe('WorkerThreadTransport', () => {total_cost_is_approximate INTEGER NOT NULL DEFAULT 0,context_tokens INTEGER NOT NULL,context_window INTEGER NOT NULL,+ context_window_runtime INTEGER,fast_mode INTEGER NOT NULL,cleared_at INTEGER,pinned_at INTEGER,diff --git a/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts b/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.tsindex ab238932b2..dc744e44cc 100644--- a/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts+++ b/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts@@ -1,3 +1,5 @@+import { createDrizzleProxy } from '../../client/drizzleProxy';+import type { DbTransport } from '../../client/DbTransport';import { getSelectedNewMakerRoute, setNewMakerDraftCache } from '../../../maker-host/newMakerDefaultsCache';import { setModelVisibilityMirror } from '../../../maker-host/model-visibility-mirror';import { setMainLocale } from '../../../i18n';@@ -5701,3 +5703,65 @@ describe('Teammate model selection shares profile persistence and route reconcilexpect(getSelectedNewMakerRoute(h.ownerScopeKey)).toEqual(appDefault);});});+++/** Delay real SQL replies to expose fan-out hidden by the synchronous fixture. */+function observeBotReadConcurrency() {+ let active = 0;+ let peak = 0;+ const transport: DbTransport = {+ async send<R>(op: string, args: unknown): Promise<R> {+ if (op !== 'rawAll') throw new Error(`unexpected read operation: ${op}`);+ const { sql, params } = args as { sql: string; params: unknown[] };+ peak = Math.max(peak, ++active);+ try {+ const rows = h.sqlite!.prepare(sql).raw().all(...params);+ await new Promise<void>((resolve) => setImmediate(resolve));+ return rows as R;+ } finally { active--; }+ },+ on() {}, onTerminated() {}, async close() {},+ };+ h.db = createDrizzleProxy(transport);+ return () => peak;+}++it.each(['local', 'remote', 'resources'])('bounds %s companion roster reads without dropping profiles', async (entry) => {+ const [profile] = await h.db!.select().from(botProfiles);+ const [version] = await h.db!.select().from(botProfileVersions);+ for (let i = 2; i <= 20; i++) {+ const id = `roster-${i}`;+ await h.db!.insert(botProfiles).values({ ...profile, id, displayName: id });+ await h.db!.insert(botProfileVersions).values({ ...version, id: `${id}-version`, botId: id });+ }+ const peak = observeBotReadConcurrency();+ const rows = entry === 'resources' ? await listBotRemoteResourceSources()+ : entry === 'remote' ? await runDeviceLinkInvokeContext(+ { controllerDeviceId: 'mobile-roster', channel: 'local-db:bots:list' },+ () => invoke('local-db:bots:list', undefined),+ ) : await invoke('local-db:bots:list', undefined);+ expect(rows).toHaveLength(20);+ expect(new Set(rows.map((row: { id: string }) => row.id)).size).toBe(20);+ expect(peak()).toBe(1);+});++it('reads long companion history snapshots sequentially and keeps every latest snapshot', async () => {+ const created = await invoke('local-db:bots:create-canonical-session', {+ botId: 'bot-1', expectedCanonicalSessionId: null, expectedProfileVersion: 1,+ });+ const [session] = await h.db!.select().from(sessions);+ for (let i = 1; i <= 40; i++) {+ const id = `history-${i}`;+ await h.db!.insert(sessions).values({ ...session, id });+ await h.db!.insert(botSessionLinks).values({ id, botId: 'bot-1', sessionId: id, role: 'history', profileVersion: 1, createdAt: i });+ h.sqlite!.prepare(`INSERT INTO bot_runtime_snapshots+ (id, bot_id, session_id, profile_version, agent_kind, working_dir, status, prepared_at, configured_json)+ VALUES (?, 'bot-1', ?, 1, 'pi', '/workspace', 'prepared', ?, '{}')`).run(id, id, i);+ }+ const peak = observeBotReadConcurrency();+ const profile = await invoke('local-db:bots:get', 'bot-1');+ expect(profile.sessions).toHaveLength(41);+ expect(profile.sessions.filter((row: { runtimeSnapshot?: unknown }) => row.runtimeSnapshot)).toHaveLength(40);+ expect(profile.sessions.some((row: { id: string }) => row.id === created.session.id)).toBe(true);+ expect(peak()).toBe(1);+});diff --git a/apps/desktop/src/main/localDb/ipc/__tests__/botRemoteSessionAccess.db.test.ts b/apps/desktop/src/main/localDb/ipc/__tests__/botRemoteSessionAccess.db.test.tsnew file mode 100644index 0000000000..501b8faf6b--- /dev/null+++ b/apps/desktop/src/main/localDb/ipc/__tests__/botRemoteSessionAccess.db.test.ts@@ -0,0 +1,97 @@+import Database from 'better-sqlite3';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';+import { createDrizzleProxy } from '../../client/drizzleProxy';+import type { DbTransport } from '../../client/DbTransport';+import { currentDbRpcAdmissionClass, runAsBackgroundDbRpc } from '../../client/rpcAdmission';+import { projectRemoteSessionResult, setRemoteBotSessionLookup } from '../../../device-link/remoteBotSessionBoundary';++const state = vi.hoisted(() => ({ db: null as unknown, owner: 1 }));+vi.mock('../../client/current.js', () => ({ getDbClient: () => ({ drizzle: state.db }) }));+vi.mock('../../../device-link/broadcast-tap.js', () => ({+ captureDataOwnerBroadcastScope: () => state.owner,+ isDataOwnerBroadcastScopeCurrent: (owner: number) => owner === state.owner,+}));+import { readRemoteBotSessionAccess, readRemoteBotSessionAccessBatch } from '../botRemoteSessionAccess';++let sqlite: Database.Database;+let calls: Array<{ params: unknown[]; admission: string }>;+let active: number;+let peak: number;+let beforeReply: (() => void) | undefined;++beforeEach(() => {+ state.owner = 1;+ active = peak = 0;+ calls = [];+ beforeReply = undefined;+ sqlite = new Database(':memory:');+ sqlite.exec(`+ CREATE TABLE sessions (id TEXT PRIMARY KEY, source TEXT);+ CREATE TABLE bot_profiles (id TEXT PRIMARY KEY, hidden_at INTEGER, status TEXT);+ CREATE TABLE bot_session_links (session_id TEXT UNIQUE, bot_id TEXT);+ INSERT INTO bot_profiles VALUES ('visible', NULL, 'active'), ('hidden', 1, 'active'), ('archived', NULL, 'archived');+ INSERT INTO sessions VALUES ('ordinary', 'desktop'), ('visible', 'bot'), ('hidden', 'bot'), ('archived', 'bot'), ('orphan', 'bot');+ INSERT INTO bot_session_links VALUES ('visible', 'visible'), ('hidden', 'hidden'), ('archived', 'archived');+ `);+ const transport: DbTransport = {+ async send<R>(_op: string, args: unknown): Promise<R> {+ const { sql, params } = args as { sql: string; params: unknown[] };+ calls.push({ params, admission: currentDbRpcAdmissionClass() });+ active++;+ peak = Math.max(peak, active);+ try {+ // Real SQL through the production async Drizzle proxy, with a delayed RPC reply.+ const rows = sqlite.prepare(sql).raw().all(...params);+ await new Promise<void>((resolve) => setImmediate(resolve));+ beforeReply?.();+ return rows as R;+ } finally { active--; }+ },+ on() {}, onTerminated() {}, async close() {},+ };+ state.db = createDrizzleProxy(transport);+ setRemoteBotSessionLookup(readRemoteBotSessionAccess, readRemoteBotSessionAccessBatch);+});+afterEach(() => { setRemoteBotSessionLookup(null); sqlite.close(); });++describe('remote visibility batch SQL', () => {+ it('preserves ordinary, visible, hidden, archived, orphan and missing access semantics', async () => {+ const ids = ['ordinary', 'visible', 'hidden', 'archived', 'orphan', 'missing'];+ expect(Object.fromEntries(await readRemoteBotSessionAccessBatch(ids))).toEqual({+ ordinary: 'ordinary', visible: 'visible', hidden: 'hidden', archived: 'hidden', orphan: 'hidden', missing: 'missing',+ });+ expect(Object.fromEntries(await readRemoteBotSessionAccessBatch(ids, 'bot'))).toEqual({+ ordinary: 'missing', visible: 'visible', hidden: 'hidden', archived: 'hidden', orphan: 'missing', missing: 'missing',+ });+ expect(await readRemoteBotSessionAccess('hidden')).toBe('hidden');+ });++ it('returns 1,000 authorized tasks in order without a per-row RPC burst or interactive admission', async () => {+ const rows = Array.from({ length: 1000 }, (_, i) => ({ id: `task-${i}` }));+ const insert = sqlite.prepare("INSERT INTO sessions VALUES (?, 'desktop')");+ sqlite.transaction(() => { for (const row of rows) insert.run(row.id); })();+ const input = [...rows, { id: 'hidden' }, rows[0]];+ expect(await runAsBackgroundDbRpc(() => projectRemoteSessionResult('local-db:sessions:list', input)))+ .toEqual([...rows, rows[0]]);+ expect(calls).toHaveLength(6);+ expect(peak).toBe(1);+ expect(calls.every((call) => call.params.length <= 200 && call.admission === 'background')).toBe(true);+ });++ it('discards already read chunks and stops issuing SQL if the data owner changes', async () => {+ const ids = Array.from({ length: 450 }, (_, i) => `task-${i}`);+ beforeReply = () => { if (calls.length === 2) state.owner++; };+ const result = await readRemoteBotSessionAccessBatch(ids);+ expect(result.size).toBe(450);+ expect([...result.values()].every((access) => access === 'hidden')).toBe(true);+ expect(calls).toHaveLength(2);+ });++ it('does not cache visibility across separate deliveries and propagates DB failures', async () => {+ expect(await projectRemoteSessionResult('local-db:bots:list', [{ id: 'visible' }])).toEqual([{ id: 'visible' }]);+ sqlite.exec("UPDATE bot_profiles SET hidden_at=10 WHERE id='visible'");+ expect(await projectRemoteSessionResult('local-db:bots:list', [{ id: 'visible' }])).toEqual([]);+ sqlite.exec('DROP TABLE sessions');+ await expect(projectRemoteSessionResult('local-db:sessions:list', [{ id: 'ordinary' }])).rejects.toThrow();+ });+});diff --git a/apps/desktop/src/main/localDb/ipc/botRemoteSessionAccess.ts b/apps/desktop/src/main/localDb/ipc/botRemoteSessionAccess.tsindex 4064476b02..674a0a46a2 100644--- a/apps/desktop/src/main/localDb/ipc/botRemoteSessionAccess.ts+++ b/apps/desktop/src/main/localDb/ipc/botRemoteSessionAccess.ts@@ -1,30 +1,57 @@-import { eq } from 'drizzle-orm';+import { eq, inArray } from 'drizzle-orm';import { getDbClient } from '../client/current.js';import { botProfiles, botSessionLinks, sessions } from '../schema.js';import { isBotVisibleRemotely } from './botRemoteVisibility.js';import { captureDataOwnerBroadcastScope, isDataOwnerBroadcastScopeCurrent } from '../../device-link/broadcast-tap.js';+import type { RemoteBotSessionAccess } from '../../device-link/remoteBotSessionBoundary.js';-/** Never infer companion authority from a controller's cached Session or resource link. */-export async function readRemoteBotSessionAccess(sessionId: string, kind: 'session' | 'bot' = 'session'): Promise<'ordinary' | 'visible' | 'hidden' | 'missing'> {+/** Keep bind counts bounded and issue only one query at a time, even for huge rosters. */+const ACCESS_BATCH_SIZE = 200;++/** Fresh checks only: cached replies and replay must observe newly hidden companions. */+export async function readRemoteBotSessionAccessBatch(+ ids: readonly string[], kind: 'session' | 'bot' = 'session',+): Promise<Map<string, RemoteBotSessionAccess>> {+ const uniqueIds = [...new Set(ids)];+ const access = new Map<string, RemoteBotSessionAccess>();+ if (!uniqueIds.length) return access;const owner = captureDataOwnerBroadcastScope();- if (kind === 'bot') {- const [profile] = await getDbClient().drizzle.select({ hiddenAt: botProfiles.hiddenAt, status: botProfiles.status })- .from(botProfiles).where(eq(botProfiles.id, sessionId)).limit(1);- if (!isDataOwnerBroadcastScopeCurrent(owner)) return 'hidden';- return profile ? (isBotVisibleRemotely(profile) ? 'visible' : 'hidden') : 'missing';+ const db = getDbClient().drizzle;+ const denyAll = () => new Map(uniqueIds.map((id) => [id, 'hidden' as const]));+ for (let offset = 0; offset < uniqueIds.length; offset += ACCESS_BATCH_SIZE) {+ if (!isDataOwnerBroadcastScopeCurrent(owner)) return denyAll();+ const chunk = uniqueIds.slice(offset, offset + ACCESS_BATCH_SIZE);+ for (const id of chunk) access.set(id, 'missing');+ if (kind === 'bot') {+ const profiles = await db.select({ id: botProfiles.id, hiddenAt: botProfiles.hiddenAt, status: botProfiles.status })+ .from(botProfiles).where(inArray(botProfiles.id, chunk));+ if (!isDataOwnerBroadcastScopeCurrent(owner)) return denyAll();+ for (const profile of profiles) {+ access.set(profile.id, isBotVisibleRemotely(profile) ? 'visible' : 'hidden');+ }+ } else {+ const rows = await db.select({+ id: sessions.id,+ source: sessions.source,+ botId: botProfiles.id,+ hiddenAt: botProfiles.hiddenAt,+ status: botProfiles.status,+ }).from(sessions)+ .leftJoin(botSessionLinks, eq(botSessionLinks.sessionId, sessions.id))+ .leftJoin(botProfiles, eq(botProfiles.id, botSessionLinks.botId))+ .where(inArray(sessions.id, chunk));+ if (!isDataOwnerBroadcastScopeCurrent(owner)) return denyAll();+ for (const row of rows) {+ access.set(row.id, row.source !== 'bot' ? 'ordinary'+ : row.botId && row.status && isBotVisibleRemotely({ hiddenAt: row.hiddenAt, status: row.status })+ ? 'visible' : 'hidden');+ }+ }}- const [row] = await getDbClient().drizzle.select({- source: sessions.source,- botId: botProfiles.id,- hiddenAt: botProfiles.hiddenAt,- status: botProfiles.status,- }).from(sessions)- .leftJoin(botSessionLinks, eq(botSessionLinks.sessionId, sessions.id))- .leftJoin(botProfiles, eq(botProfiles.id, botSessionLinks.botId))- .where(eq(sessions.id, sessionId)).limit(1);- if (!isDataOwnerBroadcastScopeCurrent(owner)) return 'hidden';- if (!row) return 'missing';- if (row.source !== 'bot') return 'ordinary';- return row.botId && row.status && isBotVisibleRemotely({ hiddenAt: row.hiddenAt, status: row.status })- ? 'visible' : 'hidden';+ return access;+}++/** Never infer companion authority from a controller's cached Session or resource link. */+export async function readRemoteBotSessionAccess(sessionId: string, kind: 'session' | 'bot' = 'session'): Promise<RemoteBotSessionAccess> {+ return (await readRemoteBotSessionAccessBatch([sessionId], kind)).get(sessionId) ?? 'hidden';}diff --git a/apps/desktop/src/main/localDb/ipc/bots.ts b/apps/desktop/src/main/localDb/ipc/bots.tsindex b2dc5876ea..d13f04a3c7 100644--- a/apps/desktop/src/main/localDb/ipc/bots.ts+++ b/apps/desktop/src/main/localDb/ipc/bots.ts@@ -30,7 +30,7 @@ import { assertTrustedAppRendererEvent } from '../../security/trustedAppRendererimport { isDeviceLinkInvoke } from '../../device-link/invoke-context.js';import { requireString, throwIpcError } from '../../utils/ipcValidate.js';import { isBotVisibleRemotely } from './botRemoteVisibility.js';-import { readRemoteBotSessionAccess } from './botRemoteSessionAccess.js';+import { readRemoteBotSessionAccess, readRemoteBotSessionAccessBatch } from './botRemoteSessionAccess.js';import { setRemoteBotSessionLookup } from '../../device-link/remoteBotSessionBoundary.js';import { resolveBusinessSessionId } from '../../sessionIds.js';import { ensureProjectGitInitialized } from '../../git-snapshot/projectGitBootstrap.js';@@ -502,13 +502,14 @@ async function readProfile(const byId = new Map(sessionRows.map((row) => [row.id, row]));// Runtime snapshots are durable history. The list needs only the latest per// task; fetching every frozen capability JSON grows with months of restarts.- const runtimeRows = (await Promise.all(links.map((link) => db- .select()- .from(botRuntimeSnapshots)- .where(eq(botRuntimeSnapshots.sessionId, link.sessionId))- .orderBy(desc(botRuntimeSnapshots.preparedAt), desc(botRuntimeSnapshots.appliedAt))- .limit(1),- ))).flat();+ const runtimeRows: (typeof botRuntimeSnapshots.$inferSelect)[] = [];+ for (const link of links) {+ owner.assertCurrent();+ runtimeRows.push(...await db.select().from(botRuntimeSnapshots)+ .where(eq(botRuntimeSnapshots.sessionId, link.sessionId))+ .orderBy(desc(botRuntimeSnapshots.preparedAt), desc(botRuntimeSnapshots.appliedAt))+ .limit(1));+ }const runtimeBySession = new Map<string, (typeof runtimeRows)[number]>();for (const row of runtimeRows) {if (!runtimeBySession.has(row.sessionId)) runtimeBySession.set(row.sessionId, row);@@ -805,7 +806,12 @@ export async function listBotRemoteResourceSources(): Promise<BotRemoteResourceSowner.assertCurrent();// The roster reads only identity + canonical preview, never runtime snapshots// or every historical task attached to a long-lived companion.- const sources = await Promise.all(profiles.map(({ id }) => readBotRemoteResourceSource(client, id)));+ const sources: BotRemoteResourceSource[] = [];+ // A large roster must not occupy one DB RPC slot per companion at once.+ for (const { id } of profiles) {+ owner.assertCurrent();+ sources.push(await readBotRemoteResourceSource(client, id));+ }owner.assertCurrent();return sources;}@@ -1244,7 +1250,7 @@ export function registerBotIpc(): void {owner.assertCurrent();return generateBotCreationDraft(raw, profiles.map(p => p.name));});- setRemoteBotSessionLookup(readRemoteBotSessionAccess);+ setRemoteBotSessionLookup(readRemoteBotSessionAccess, readRemoteBotSessionAccessBatch);ipcMain.handle('local-db:bots:model-chain-settings-get', async (event) => {assertTrustedAppRendererEvent(event);const state = await readBotModelChainSettingsState();@@ -1298,13 +1304,14 @@ export function registerBotIpc(): void {if (status !== 'archived') queueBotInvitation(id);}}- const results = await Promise.all(- profiles.map(({ id }) =>- remote- ? readRemoteBotProfile(client, id)- : readProfile(client, id, lastReadAtByBotId.get(id) ?? null),- ),- );+ const results = [];+ for (const { id } of profiles) {+ owner.assertCurrent();+ results.push(await (remote+ ? readRemoteBotProfile(client, id)+ : readProfile(client, id, lastReadAtByBotId.get(id) ?? null)));+ }+ owner.assertCurrent();return results.filter((profile) => profile !== null);});diff --git a/apps/desktop/src/main/scheduler-host/__tests__/storage.db.test.ts b/apps/desktop/src/main/scheduler-host/__tests__/storage.db.test.tsindex 3edb88eac8..eab3107d90 100644--- a/apps/desktop/src/main/scheduler-host/__tests__/storage.db.test.ts+++ b/apps/desktop/src/main/scheduler-host/__tests__/storage.db.test.ts@@ -1,3 +1,5 @@+import { createDrizzleProxy } from '../../localDb/client/drizzleProxy';+import type { DbTransport } from '../../localDb/client/DbTransport';/*** scheduler storage DB-tier smoke。*@@ -1797,3 +1799,52 @@ it('excludes internal routine history from public indexes, unread counts and delharness.close();}});+++it('bounds history chunk reads while preserving run costs and schedule totals', async () => {+ const harness = createStorageHarness();+ try {+ await harness.storage.insert(baseSchedule());+ const session = harness.sqlite.prepare("INSERT INTO sessions (id,title,source) VALUES (?, 'scheduled', 'scheduler')");+ const run = harness.sqlite.prepare("INSERT INTO schedule_runs (id,schedule_id,session_id,fired_at,status,cost_attribution) VALUES (?, 'sch-1', ?, ?, 'success', 'exact')");+ const message = harness.sqlite.prepare("INSERT INTO messages (id,client_id,session_id,role,content,agent_meta,created_at) VALUES (?, ?, ?, 'assistant', '{}', ?, 1)");+ harness.sqlite.transaction(() => {+ for (let i = 0; i < 1001; i++) {+ const id = `s-${i}`;+ const runId = `r-${i}`;+ session.run(id);+ run.run(runId, id, i);+ message.run(id, id, id, JSON.stringify({ origin: { kind: 'scheduler', scheduleId: 'sch-1', runId }, turnCostUsd: 1 }));+ }+ })();+ let active = 0;+ let peak = 0;+ let messageReads = 0;+ const transport: DbTransport = {+ async send<R>(op: string, args: unknown): Promise<R> {+ if (op !== 'rawAll') throw new Error(`unexpected read operation: ${op}`);+ const { sql, params } = args as { sql: string; params: unknown[] };+ peak = Math.max(peak, ++active);+ if (sql.includes('from "messages"')) messageReads++;+ try {+ const rows = harness.sqlite.prepare(sql).raw().all(...params);+ await new Promise<void>((resolve) => setImmediate(resolve));+ return rows as R;+ } finally { active--; }+ },+ on() {}, onTerminated() {}, async close() {},+ };+ const storage = new DrizzleScheduleStorage(() => createDrizzleProxy(transport) as SchedulerDrizzleDb);+ const runs = await storage.listRuns('sch-1', 1001);+ expect(runs).toHaveLength(1001);+ for (const run of runs) expect(run.costMoney).toEqual(actualMoneyFromLegacyUsd(1));+ expect(messageReads).toBe(3);+ expect(peak).toBe(1);+ peak = messageReads = 0;+ const summaries = await storage.listCostSummaries();+ expect(summaries).toHaveLength(1);+ expect(summaries[0]).toMatchObject({ sessionCount: 1001, totalMoney: actualMoneyFromLegacyUsd(1001) });+ expect(messageReads).toBe(3);+ expect(peak).toBe(1);+ } finally { harness.close(); }+});diff --git a/apps/desktop/src/main/scheduler-host/storage.ts b/apps/desktop/src/main/scheduler-host/storage.tsindex 097d996851..9d4b1f7dfe 100644--- a/apps/desktop/src/main/scheduler-host/storage.ts+++ b/apps/desktop/src/main/scheduler-host/storage.ts@@ -612,18 +612,12 @@ export class DrizzleScheduleStorage implements ScheduleStorage {return runs;}- const rows = (- await Promise.all(- chunkArray([...sessionIds], SQLITE_IN_CHUNK_SIZE).map((sessionIdChunk) =>- db- .select({ agentMeta: messages.agentMeta })- .from(messages)- .where(- and(inArray(messages.sessionId, sessionIdChunk), eq(messages.role, 'assistant')),- ),- ),- )- ).flat();+ const rows: { agentMeta: string | null }[] = [];+ // Chunking bind parameters must also bound RPC fan-out for large histories.+ for (const sessionIdChunk of chunkArray([...sessionIds], SQLITE_IN_CHUNK_SIZE)) {+ rows.push(...await db.select({ agentMeta: messages.agentMeta }).from(messages)+ .where(and(inArray(messages.sessionId, sessionIdChunk), eq(messages.role, 'assistant'))));+ }const ledger = new Map<string,{ costValues: RegionalMoney[]; estimatedValues: RegionalMoney[]; totalTokens: number }@@ -897,38 +891,25 @@ export class DrizzleScheduleStorage implements ScheduleStorage {linkedScheduleIds.add(schedule.id);}- const messageRows =- scanSessionIds.size === 0- ? []- : (- await Promise.all(- chunkArray([...scanSessionIds], SQLITE_IN_CHUNK_SIZE).map((sessionIdChunk) =>- db- .select({- sessionId: messages.sessionId,- role: messages.role,- agentMeta: messages.agentMeta,- createdAt: messages.createdAt,- id: messages.id,- })- .from(messages)- .where(- and(- inArray(messages.sessionId, sessionIdChunk),- inArray(messages.role, ['user', 'assistant']),- ),- )- .orderBy(- messages.sessionId,- messages.createdAt,- // If a scheduler user message and its assistant result land in the- // same millisecond, the user row must establish activeScheduleId first.- sql`case ${messages.role} when 'user' then 0 else 1 end`,- messages.id,- ),- ),- )- ).flat();+ const messageRows: Pick<typeof messages.$inferSelect, 'sessionId' | 'role' | 'agentMeta' | 'createdAt' | 'id'>[] = [];+ for (const sessionIdChunk of chunkArray([...scanSessionIds], SQLITE_IN_CHUNK_SIZE)) {+ messageRows.push(...await db.select({+ sessionId: messages.sessionId,+ role: messages.role,+ agentMeta: messages.agentMeta,+ createdAt: messages.createdAt,+ id: messages.id,+ }).from(messages).where(and(+ inArray(messages.sessionId, sessionIdChunk),+ inArray(messages.role, ['user', 'assistant']),+ )).orderBy(+ messages.sessionId,+ messages.createdAt,+ // Same-millisecond user rows establish attribution before assistant costs.+ sql`case ${messages.role} when 'user' then 0 else 1 end`,+ messages.id,+ ));+ }let activeSessionId: string | null = null;let activeScheduleId: string | null = null;diff --git a/packages/device-link/src/__tests__/invokePolicy.test.ts b/packages/device-link/src/__tests__/invokePolicy.test.tsindex 578badd3c7..70a874d44e 100644--- a/packages/device-link/src/__tests__/invokePolicy.test.ts+++ b/packages/device-link/src/__tests__/invokePolicy.test.ts@@ -2,10 +2,27 @@ import { describe, expect, it } from 'vitest';import {canCoalesceRemoteListing,isPeerResetRetryableReadChannel,+ isCompletedInvokeRetryableReadChannel,resolveRemoteInvokeTimeoutMs,} from '../index.js';describe('remote invoke policy boundaries', () => {+ it.each(['local-db:sessions:list', 'local-db:sessions:get', 'local-db:sessions:interrupted-pending', 'maker:list-active'])(+ 'explicitly permits retrying completed %s reads', (channel) => {+ expect(isCompletedInvokeRetryableReadChannel(channel)).toBe(true);+ },+ );++ it.each([+ 'maker:remote-resources:list', 'maker:remote-resources:get',+ 'local-db:bots:list', 'local-db:bots:get',+ 'maker:send', 'maker:input:enqueue',+ 'maker:get-capabilities', 'maker:provider:list', 'maker:git-safety:get',+ 'maker:schedule:list-sidebar-index-runs', 'unknown:list', undefined,+ ])('does not infer completed retry safety for %s from listing, peer-reset or background classification', (channel) => {+ expect(isCompletedInvokeRetryableReadChannel(channel)).toBe(false);+ });+it('allows retrying a read without sharing a snapshot from before a write', () => {expect(isPeerResetRetryableReadChannel('local-db:sessions:get')).toBe(true);expect(canCoalesceRemoteListing({ channel: 'local-db:sessions:get', args: ['id'] })).toBe(false);diff --git a/packages/device-link/src/invokePolicy.ts b/packages/device-link/src/invokePolicy.tsindex dc1514f55e..3f136329aa 100644--- a/packages/device-link/src/invokePolicy.ts+++ b/packages/device-link/src/invokePolicy.ts@@ -113,6 +113,25 @@ export function isPeerResetRetryableReadChannel(channel: string): boolean {return PEER_RESET_RETRYABLE_READ_CHANNELS.has(channel);}+/**+ * A completed invocation whose result authorization failed is a separate retry+ * boundary from peer reset, snapshot coalescing, and Host DB admission. Keep+ * this list explicit: list/get names do not prove the handler is read-only.+ * Bot and remote-resource reads can provision, migrate, or reconcile data.+ * The session list's derived backfills are guarded/idempotent; the other+ * entries only read stored session state or the active runtime snapshot.+ */+const COMPLETED_INVOKE_RETRYABLE_READ_CHANNELS: ReadonlySet<string> = new Set([+ 'local-db:sessions:list',+ 'local-db:sessions:get',+ 'local-db:sessions:interrupted-pending',+ 'maker:list-active',+]);++export function isCompletedInvokeRetryableReadChannel(channel: string | undefined): boolean {+ return channel !== undefined && COMPLETED_INVOKE_RETRYABLE_READ_CHANNELS.has(channel);+}+const COALESCIBLE_LISTING_CHANNELS: ReadonlySet<string> = new Set(['local-db:sessions:list','maker:get-capabilities',