Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(perf): 处理 review 反馈 —— 队列后的 push 到队头再校验、额度回放全部 limitId、终态事件须完整、远端离线不被整点复核掩盖

Dashfdf11ea19db4
diff --git a/apps/desktop/src/main/device-link/__tests__/dispatchSendSafety.test.ts b/apps/desktop/src/main/device-link/__tests__/dispatchSendSafety.test.ts
index f98b7a5932..ee44cb82f9 100644
--- a/apps/desktop/src/main/device-link/__tests__/dispatchSendSafety.test.ts
+++ b/apps/desktop/src/main/device-link/__tests__/dispatchSendSafety.test.ts
@@ -1020,9 +1020,9 @@ describe('remote companion Session visibility at the device-link boundary', () =
});
// 测试价值:forwardPush 扇出到多台控制端时,同一轮只读一次授权(实测 3 台 peer 下每条 push
- // 每台各读一次 sessions,rawAll 堆积);同时守住「读取发出后到达的 push 必须重新读库」,
- // 轮内合并不能变成缓存,隐藏之后的 push 不得沿用旧结论。unit,进程内 fake。
- it('shares one authorization read per fan-out pass and never reuses it for later pushes', async () => {
+ // 每台各读一次 sessions,rawAll 堆积);同时守住「排在慢 push 后面的 push 到队头才读库」:
+ // 提前读到的 visible 结论不能越过排队等待,等待期间被隐藏的伙伴事件不得发出。unit,进程内 fake。
+ it('shares one authorization read per fan-out and reads a queued push only when it reaches the head', async () => {
let hidden = false;
let release!: () => void;
let gate: Promise<void> = Promise.resolve();
@@ -1037,24 +1037,23 @@ describe('remote companion Session visibility at the device-link boundary', () =
const peers = ['ctrl-1', 'ctrl-2', 'ctrl-3'];
for (const peer of peers) subscriptions.subscribe(peer, ['session:s1']);
__testing.forwardPush('maker:event', { sessionId: 's1', seq: 0 });
- __testing.forwardPush('maker:event', { sessionId: 's1', seq: 1 });
- await vi.waitFor(() => expect(client.sendPush).toHaveBeenCalledTimes(6));
+ await vi.waitFor(() => expect(client.sendPush).toHaveBeenCalledTimes(3));
+ // Three peers, one push: a single shared read, not one per peer.
expect(batch).toHaveBeenCalledTimes(1);
expect(batch).toHaveBeenCalledWith(['s1'], 'session');
- for (const peer of peers) {
- expect(client.sendPush.mock.calls.filter(([dst]) => dst === peer).map((call) => call[2].seq)).toEqual([0, 1]);
- }
- // A read already issued is not joined: the push after the hide reads again.
+ // The first push's read is slow; the second queues behind it and must not carry a
+ // verdict taken before the companion is hidden.
gate = new Promise<void>((resolve) => { release = resolve; });
__testing.forwardPush('maker:event', { sessionId: 's1', seq: 2 });
await vi.waitFor(() => expect(batch).toHaveBeenCalledTimes(2));
- hidden = true;
__testing.forwardPush('maker:event', { sessionId: 's1', seq: 3 });
- await vi.waitFor(() => expect(batch).toHaveBeenCalledTimes(3));
+ await new Promise((resolve) => setTimeout(resolve, 10));
+ expect(batch).toHaveBeenCalledTimes(2);
+ hidden = true;
release();
- await vi.waitFor(() => expect(client.sendPush).toHaveBeenCalledTimes(9));
- await new Promise((resolve) => setTimeout(resolve, 0));
+ await vi.waitFor(() => expect(batch.mock.calls.length).toBeGreaterThanOrEqual(3));
+ await new Promise((resolve) => setTimeout(resolve, 10));
expect(client.sendPush.mock.calls.map((call) => call[2].seq).filter((seq) => seq >= 2)).toEqual([2, 2, 2]);
});
diff --git a/apps/desktop/src/main/device-link/dispatch.ts b/apps/desktop/src/main/device-link/dispatch.ts
index 718caf5ca0..c6eccb467f 100644
--- a/apps/desktop/src/main/device-link/dispatch.ts
+++ b/apps/desktop/src/main/device-link/dispatch.ts
@@ -213,8 +213,8 @@ const offlinePushQueue = createOfflinePushQueue({ maxBytes: 2 * 1024 * 1024 });
// Start the async DB check at the final wire boundary and deliver in per-peer
// order even when replies arrive out of order. Checks issued in the same pass
-// (all peers of one fan-out, every item of one flush) share one batched read
-// that is issued after all of them arrived; see projectRemoteBotPush. Bounds
+// (all peers of one fan-out) share one batched read that is issued after all of
+// them arrived; see projectRemoteBotPush. Bounds
// match best-effort push: dropped frames recover through the Session snapshot.
const botPushChecks = new Map<string, { tail: Promise<void>; bytes: number; count: number }>();
function sendBotCheckedPush(
@@ -236,12 +236,16 @@ function sendBotCheckedPush(
const client = activeClient;
const linkEpoch = remoteInvokeLinkEpoch.get(dst) ?? 0;
const owner = broadcastTap.captureDataOwnerBroadcastScope();
+ // A push that is already at the head of an idle queue starts its read now, so all peers of
+ // one fan-out share a round. One queued behind another reads only when it reaches the head:
+ // an earlier read would be a visibility verdict taken before waiting out the slower pushes.
+ const atHead = queue.count === 0;
queue.count += 1; queue.bytes += size;
- const authorized = projectRemoteBotPush(payload, channel);
+ const early = atHead ? projectRemoteBotPush(payload, channel) : null;
// Settled below in per-peer order; do not surface as unhandled meanwhile.
- authorized.catch(() => undefined);
+ early?.catch(() => undefined);
queue.tail = queue.tail.then(async () => {
- const projected = await authorized;
+ const projected = await (early ?? projectRemoteBotPush(payload, channel));
if (projected !== null && activeClient === client && (remoteInvokeLinkEpoch.get(dst) ?? 0) === linkEpoch && !isControllerRevoked(dst) && broadcastTap.isDataOwnerBroadcastScopeCurrent(owner)) send(projected);
}).catch(failed).finally(() => {
queue.count -= 1; queue.bytes -= size;
diff --git a/apps/desktop/src/main/maker-host/__tests__/session-archive-sync.test.ts b/apps/desktop/src/main/maker-host/__tests__/session-archive-sync.test.ts
index dc6d13391a..f531243521 100644
--- a/apps/desktop/src/main/maker-host/__tests__/session-archive-sync.test.ts
+++ b/apps/desktop/src/main/maker-host/__tests__/session-archive-sync.test.ts
@@ -131,6 +131,20 @@ describe('task archive projection', () => {
expect(f.sync).toHaveBeenLastCalledWith(expect.objectContaining({ threadId: 'thread', archived: true, remoteHostId: 'remote' }));
});
+ it('does not let an hourly pass that skipped an offline remote hide it until the next hour', async () => {
+ vi.useFakeTimers();
+ const f = fixture();
+ f.insert('c', 'codex', 'archived', 'thread', 'remote');
+ await f.coordinator.request();
+ expect(f.sync).toHaveBeenCalledTimes(1);
+ f.canUseRemote.mockReturnValue(false);
+ await vi.advanceTimersByTimeAsync(61 * 60_000);
+ expect(f.sync).toHaveBeenCalledTimes(1);
+ f.canUseRemote.mockReturnValue(true);
+ await vi.advanceTimersByTimeAsync(60_000);
+ expect(f.sync).toHaveBeenCalledTimes(2);
+ });
+
it('stops a backfill when the owner changes', async () => {
const f = fixture(); f.insert('a'); f.insert('b');
f.sync.mockImplementationOnce(async () => { f.assertCurrent.mockImplementation(() => { throw new Error('changed'); }); });
diff --git a/apps/desktop/src/main/maker-host/session-archive-sync.ts b/apps/desktop/src/main/maker-host/session-archive-sync.ts
index 8de10d7a75..b9f25dd5c0 100644
--- a/apps/desktop/src/main/maker-host/session-archive-sync.ts
+++ b/apps/desktop/src/main/maker-host/session-archive-sync.ts
@@ -91,7 +91,11 @@ export function createSessionArchiveSync(deps: {
if (backoff?.signature === signature && Date.now() < backoff.retryAt) continue;
}
const first = group[0]!;
- if (first.remoteHostId && !deps.canUseRemote(first.remoteHostId)) continue;
+ if (first.remoteHostId && !deps.canUseRemote(first.remoteHostId)) {
+ // 这一轮没能核验: 不让整点复核的标记掩盖它, 远端恢复后下一次 tick 就补做。
+ applied.delete(key);
+ continue;
+ }
try {
await withSessionRouteLocks(group.map(row => row.id), async () => {
const current = await scope.client.query<ArchiveSessionRow>(
diff --git a/packages/anthropic-compat-proxy/src/server.test.ts b/packages/anthropic-compat-proxy/src/server.test.ts
index 96ece08e55..4b6bf1881f 100644
--- a/packages/anthropic-compat-proxy/src/server.test.ts
+++ b/packages/anthropic-compat-proxy/src/server.test.ts
@@ -3691,6 +3691,15 @@ describe('anthropic-compat-proxy 客户端中断传播', () => {
observerEnds: 1,
disconnectLogs: 0,
},
+ {
+ name: 'terminal event only half sent (no blank line yet) → client-aborted',
+ events: 'event: response.created\ndata: {"type":"response.created"}\n\n'
+ + 'event: response.completed\n',
+ waitFor: 'response.completed',
+ expected: { type: 'failure', failure: 'client-aborted' },
+ observerEnds: 0,
+ disconnectLogs: 1,
+ },
{
name: 'no terminal event yet → client-aborted',
events: 'event: response.created\ndata: {"type":"response.created"}\n\n',
diff --git a/packages/anthropic-compat-proxy/src/server.ts b/packages/anthropic-compat-proxy/src/server.ts
index 0510a2e0e9..69663d937f 100644
--- a/packages/anthropic-compat-proxy/src/server.ts
+++ b/packages/anthropic-compat-proxy/src/server.ts
@@ -137,6 +137,8 @@ const ERROR_RESPONSE_DUMP_MAX_BYTES = 16 * 1024;
const SSE_TERMINAL_EVENT_RE =
/\nevent:[ \t]*(?:response\.(?:completed|failed|incomplete)|message_stop)[ \t]*(?:\r?\n|\r?$)|"type"[ \t]*:[ \t]*"(?:response\.(?:completed|failed|incomplete)|message_stop)"|\ndata:[ \t]*\[DONE\]/;
const SSE_TERMINAL_SCAN_TAIL_CHARS = 64;
+/** SSE 事件结束符:空行。 */
+const SSE_EVENT_END_RE = /\r?\n\r?\n/;
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value);
@@ -1685,18 +1687,32 @@ function forward(
// 匹配 `event: <终态>` 行或 data JSON 的顶层 `"type":"<终态>"`(JSON 字符串内的引号
// 必然被转义,正文内容不会误命中);保留上一 chunk 的尾巴以覆盖跨 chunk 截断。
const scanTerminalSseEvent = (isSse || canInferSse) && !isCompressed && status >= 200 && status < 300;
+ // terminalSseEventSeen 仅在终态事件完整经过(其后出现空行结束符)后为真:
+ // 事件被拆成两段发送时,第一段命中标记不代表事件已转发完,此时取消仍按断开处理。
let terminalSseEventSeen = false;
+ let terminalSseMarkerSeen = false;
// 以换行起头:首个 chunk 开头即视为行首。
let terminalSseScanTail = '\n';
const scanChunkForTerminalSseEvent = (chunk: Buffer): void => {
if (!scanTerminalSseEvent || terminalSseEventSeen) return;
const text = terminalSseScanTail + chunk.toString('latin1');
- if (SSE_TERMINAL_EVENT_RE.test(text)) {
+ let rest = text;
+ if (!terminalSseMarkerSeen) {
+ const match = SSE_TERMINAL_EVENT_RE.exec(text);
+ if (!match) {
+ terminalSseScanTail = text.slice(-SSE_TERMINAL_SCAN_TAIL_CHARS);
+ return;
+ }
+ terminalSseMarkerSeen = true;
+ rest = text.slice(match.index + match[0].length);
+ }
+ if (SSE_EVENT_END_RE.test(rest)) {
terminalSseEventSeen = true;
terminalSseScanTail = '';
return;
}
- terminalSseScanTail = text.slice(-SSE_TERMINAL_SCAN_TAIL_CHARS);
+ // 空行结束符可能跨 chunk,保留最后 3 个字符。
+ terminalSseScanTail = rest.slice(-3);
};
completeAfterTerminalSseEvent = () => {
if (!terminalSseEventSeen || !streamGateCommitted || upstreamResponseTerminal !== null) return false;
diff --git a/packages/maker-core/src/agents/codex/app-server/host.test.ts b/packages/maker-core/src/agents/codex/app-server/host.test.ts
index 35e0c0ade9..3bc3bd8433 100644
--- a/packages/maker-core/src/agents/codex/app-server/host.test.ts
+++ b/packages/maker-core/src/agents/codex/app-server/host.test.ts
@@ -291,8 +291,10 @@ describe('AppServerHost account rate limit fan-out', () => {
transport.emit(snapshot('codex', 20));
const late = vi.fn();
const lateSubscription = host.subscribeThread('thread-2', { accountRateLimitsUpdated: late });
- expect(late).toHaveBeenCalledOnce();
- expect(late.mock.calls[0][0].rateLimits).toMatchObject({ limitId: 'codex', primary: { usedPercent: 20 } });
+ expect(late.mock.calls.map(([p]) => [p.rateLimits.limitId, p.rateLimits.primary.usedPercent])).toEqual([
+ ['codex', 20],
+ ['codex_spark', 10],
+ ]);
expect(early).toHaveBeenCalledTimes(3);
await earlySubscription.release();
diff --git a/packages/maker-core/src/agents/codex/app-server/host.ts b/packages/maker-core/src/agents/codex/app-server/host.ts
index 1e6c2cc186..f3537356fb 100644
--- a/packages/maker-core/src/agents/codex/app-server/host.ts
+++ b/packages/maker-core/src/agents/codex/app-server/host.ts
@@ -379,7 +379,7 @@ export class AppServerHost {
* 账号配额最近一次 snapshot, 给新 subscribeThread 立即重放 — 用户打开新 codex
* session 时不必等下次 turn 完成才看到 chip 数据。整个 host 生命周期共享一份 (账号级)。
*/
- private lastAccountRateLimits: AccountRateLimitsUpdatedNotification['params'] | null = null;
+ private readonly lastAccountRateLimits = new Map<string, AccountRateLimitsUpdatedNotification['params']>();
/**
* 按 limitId 记录最近一次已 fan-out 的配额 payload (JSON)。app-server 会以秒级间隔
* 重复推送内容不变的快照 (实测 ~64% 与上一条相同), 相同内容不再重复广播与记日志。
@@ -1152,9 +1152,12 @@ export class AppServerHost {
this.replayBufferedDescendantThreadStarts(threadId);
// 账号配额 snapshot replay — 让新 session 立即看到当前账号配额, 不必等下次 turn。
- if (this.lastAccountRateLimits && handlers.accountRateLimitsUpdated) {
+ if (this.lastAccountRateLimits.size > 0 && handlers.accountRateLimitsUpdated) {
+ // 按 limitId 各回放最新一份: 去重后不变的快照不会再被推送, 只回放一条会让新会话缺其余 limit。
try {
- handlers.accountRateLimitsUpdated(this.lastAccountRateLimits);
+ for (const snapshot of this.lastAccountRateLimits.values()) {
+ handlers.accountRateLimitsUpdated(snapshot);
+ }
} catch (e) {
this.logger.error('accountRateLimitsUpdated replay threw', { message: (e as Error).message });
}
@@ -1205,9 +1208,9 @@ export class AppServerHost {
if (method === 'account/rateLimits/updated') {
const p = params as AccountRateLimitsUpdatedNotification['params'];
// replay 快照始终跟随最新一条 (哪怕内容未变), 新 subscriber 拿到的与旧行为一致。
- this.lastAccountRateLimits = p;
- const rawJson = JSON.stringify(p.rateLimits ?? null);
const limitKey = p.rateLimits?.limitId ?? '';
+ this.lastAccountRateLimits.set(limitKey, p);
+ const rawJson = JSON.stringify(p.rateLimits ?? null);
if (this.lastFannedOutRateLimitsJson.get(limitKey) === rawJson) return;
this.lastFannedOutRateLimitsJson.set(limitKey, rawJson);
this.logger.debug('accountRateLimits fan-out', {