Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(ios-simulator): 本地停止 H.264 流后容忍一帧在途,避免 sidecar 被 protocol-error 终止 (#5253)
FicoWang1e916bff7aab
diff --git a/packages/ios-simulator-runtime/src/native-sidecar/channel.test.ts b/packages/ios-simulator-runtime/src/native-sidecar/channel.test.tsindex 920c9bdf53..b59a844740 100644--- a/packages/ios-simulator-runtime/src/native-sidecar/channel.test.ts+++ b/packages/ios-simulator-runtime/src/native-sidecar/channel.test.ts@@ -674,6 +674,119 @@ describe("IOSSimulatorNativeSidecarChannel", () => {await channel.stop();});+ it("tolerates one in-flight frame after a local stop without terminating the sidecar (#5250)", async () => {+ const { channel, processes } = harness();+ await channel.start();+ const controller = new AbortController();+ const streamPromise = channel.streamFrames(command("startStream"), {+ signal: controller.signal,+ acknowledgeFrames: true,+ onFrame: () => undefined,+ });+ processes[0]!.stdout.write(reply("sidecar-1", { streamId: "stream-1" }));+ controller.abort();+ await expect(streamPromise).resolves.toMatchObject({+ endReason: "aborted",+ });++ // The Helper produces frames on a background queue; a frame that crossed+ // stdio before it observed stopStream lands after the Host tombstone.+ processes[0]!.stdout.write(streamFrame(1));+ await Promise.resolve();+ expect(channel.state).toBe("running");+ expect(channel.crashCount).toBe(0);++ // The Helper's terminal end for the stopped stream is still accepted.+ processes[0]!.stdout.write(streamEnd("aborted"));+ await Promise.resolve();+ expect(channel.state).toBe("running");++ const next = channel.request(command("availability"));+ await vi.waitFor(() => expect(processes[0]!.writes).toHaveLength(3));+ processes[0]!.stdout.write(reply("sidecar-3", { ready: true }));+ await expect(next).resolves.toEqual({ ready: true });++ // Once the Helper confirmed the end, further frames are unsolicited+ // protocol traffic and keep the fail-closed behavior.+ processes[0]!.stdout.write(streamFrame(2));+ await vi.waitFor(() => expect(channel.state).toBe("failed"));+ expect(channel.crashCount).toBe(1);+ await channel.stop();+ });++ it("closes the late-frame window when the Helper end lands behind a local stop in the frame queue (#5253)", async () => {+ const { channel, processes } = harness();+ await channel.start();+ const controller = new AbortController();+ let releaseFrame!: () => void;+ const frameGate = new Promise<void>((resolve) => {+ releaseFrame = resolve;+ });+ const streamPromise = channel.streamFrames(command("startStream"), {+ signal: controller.signal,+ acknowledgeFrames: true,+ onFrame: () => frameGate,+ });+ processes[0]!.stdout.write(reply("sidecar-1", { streamId: "stream-1" }));+ // A frame callback is still running when the local stop and then the+ // Helper's own end are both queued behind it.+ processes[0]!.stdout.write(streamFrame(1));+ controller.abort();+ processes[0]!.stdout.write(streamEnd("aborted"));+ releaseFrame();+ await expect(streamPromise).resolves.toMatchObject({+ endReason: "aborted",+ });++ // The Helper already confirmed the end, so a further frame is unsolicited.+ processes[0]!.stdout.write(streamFrame(2));+ await vi.waitFor(() => expect(channel.state).toBe("failed"));+ expect(channel.crashCount).toBe(1);+ await channel.stop();+ });++ it("fails closed when a locally stopped stream keeps emitting beyond the late-frame budget (#5250)", async () => {+ const { channel, processes } = harness();+ await channel.start();+ const controller = new AbortController();+ const streamPromise = channel.streamFrames(command("startStream"), {+ signal: controller.signal,+ acknowledgeFrames: true,+ onFrame: () => undefined,+ });+ processes[0]!.stdout.write(reply("sidecar-1", { streamId: "stream-1" }));+ controller.abort();+ await expect(streamPromise).resolves.toMatchObject({+ endReason: "aborted",+ });++ // Acknowledged streams have at most one frame in flight, so a second+ // late frame means the Helper ignored stopStream.+ processes[0]!.stdout.write(streamFrame(1));+ await Promise.resolve();+ expect(channel.state).toBe("running");+ processes[0]!.stdout.write(streamFrame(2));+ await vi.waitFor(() => expect(channel.state).toBe("failed"));+ expect(channel.crashCount).toBe(1);+ await channel.stop();+ });++ it("keeps rejecting late frames for streams the Helper already ended", async () => {+ const { channel, processes } = harness();+ await channel.start();+ const streamPromise = channel.streamFrames(command("startStream"), {+ onFrame: () => undefined,+ });+ processes[0]!.stdout.write(reply("sidecar-1", { streamId: "stream-1" }));+ processes[0]!.stdout.write(streamEnd("eof"));+ await expect(streamPromise).resolves.toMatchObject({ endReason: "eof" });++ processes[0]!.stdout.write(streamFrame(1));+ await vi.waitFor(() => expect(channel.state).toBe("failed"));+ expect(channel.crashCount).toBe(1);+ await channel.stop();+ });+it("preserves a sanitized producer message on stream errors", async () => {const { channel, processes } = harness();await channel.start();diff --git a/packages/ios-simulator-runtime/src/native-sidecar/channel.ts b/packages/ios-simulator-runtime/src/native-sidecar/channel.tsindex f2299a4e93..7abf23753a 100644--- a/packages/ios-simulator-runtime/src/native-sidecar/channel.ts+++ b/packages/ios-simulator-runtime/src/native-sidecar/channel.ts@@ -31,6 +31,12 @@ const MAX_EARLY_STREAM_EVENTS_PER_STREAM = 4;const MAX_EARLY_STREAMS = 8;const MAX_EARLY_STREAM_BYTES = 32 * 1024 * 1024;const MAX_CLOSED_STREAMS = 64;+// Frames the Helper may still deliver after the Host stopped a stream locally+// and before the Helper's own terminal `streamEnd` arrives. Acknowledged+// streams are back-pressured one frame at a time, so a single frame can be in+// flight; unacknowledged streams get a small fixed allowance instead.+const MAX_LATE_FRAMES_AFTER_LOCAL_STOP_ACKNOWLEDGED = 1;+const MAX_LATE_FRAMES_AFTER_LOCAL_STOP_UNACKNOWLEDGED = 8;const MAX_LOCALLY_SETTLED_REQUESTS = 256;export type IOSSimulatorNativeSidecarChannelState =@@ -194,6 +200,12 @@ interface PendingRequest {removeAbortListener: () => void;}+interface ClosedStreamState {+ simulatorUdid: string;+ generation: number;+ lateFrameAllowance: number;+}+interface StreamState {streamId: string;command: IOSSimulatorNativeSidecarCommand;@@ -311,10 +323,7 @@ export class IOSSimulatorNativeSidecarChannel implements IOSSimulatorNativeSidecreadonly #locallySettledRequestIds = new Set<string>();readonly #streams = new Map<string, StreamState>();readonly #earlyStreamEvents = new Map<string, EarlyStreamEvent[]>();- readonly #closedStreams = new Map<- string,- { simulatorUdid: string; generation: number }- >();+ readonly #closedStreams = new Map<string, ClosedStreamState>();#process: IOSSimulatorNativeSidecarManagedProcess | null = null;#state: IOSSimulatorNativeSidecarChannelState = "idle";#requestSequence = 0;@@ -852,7 +861,9 @@ export class IOSSimulatorNativeSidecarChannel implements IOSSimulatorNativeSidecconst decoded = decodeIOSSimulatorNativeSidecarStreamFrame(frame);const state = this.#streams.get(decoded.metadata.streamId);if (!state) {- if (this.#closedStreams.has(decoded.metadata.streamId)) {+ const closed = this.#closedStreams.get(decoded.metadata.streamId);+ if (closed) {+ if (this.#consumeLateFrameAllowance(closed, decoded)) return;throw new IOSSimulatorNativeSidecarProtocolError(`Native sidecar emitted a frame for closed stream ${decoded.metadata.streamId}`,);@@ -884,6 +895,7 @@ export class IOSSimulatorNativeSidecarChannel implements IOSSimulatorNativeSidec"Native sidecar closed-stream identity does not match",);}+ this.#markHelperEnded(end.streamId);return;}this.#bufferEarlyStreamEvent(end.streamId, { kind: "end", end }, 0);@@ -1001,7 +1013,9 @@ export class IOSSimulatorNativeSidecarChannel implements IOSSimulatorNativeSidec) {if (state.awaitStreamEndAfterMaxFrames) return;this.#sendStopStream(state);- this.#finishStream(state, "max-frames");+ this.#finishStream(state, "max-frames", undefined, {+ helperEnded: false,+ });}}).catch((error) => {@@ -1024,7 +1038,15 @@ export class IOSSimulatorNativeSidecarChannel implements IOSSimulatorNativeSidec);}state.tail = state.tail.then(() => {- this.#finishStream(state, end.reason, end.message);+ if (state.finished) {+ // A local stop already settled the stream while a frame callback was+ // still running; the Helper's end still closes the late-frame window.+ this.#markHelperEnded(state.streamId);+ return;+ }+ this.#finishStream(state, end.reason, end.message, {+ helperEnded: true,+ });});}@@ -1075,7 +1097,7 @@ export class IOSSimulatorNativeSidecarChannel implements IOSSimulatorNativeSidecstate.stopping = true;this.#sendStopStream(state);state.tail = state.tail.then(() => {- this.#finishStream(state, reason);+ this.#finishStream(state, reason, undefined, { helperEnded: false });});}@@ -1092,13 +1114,14 @@ export class IOSSimulatorNativeSidecarChannel implements IOSSimulatorNativeSidec#finishStream(state: StreamState,reason: IOSSimulatorStreamStats["endReason"],- message?: string,+ message: string | undefined,+ options: { helperEnded: boolean },): void {if (state.finished) return;state.finished = true;state.removeAbortListener();this.#streams.delete(state.streamId);- this.#rememberClosedStream(state);+ this.#rememberClosedStream(state, options);state.resolve({frameCount: state.frameCount,byteCount: state.byteCount,@@ -1115,15 +1138,30 @@ export class IOSSimulatorNativeSidecarChannel implements IOSSimulatorNativeSidecstate.finished = true;state.removeAbortListener();this.#streams.delete(state.streamId);- this.#rememberClosedStream(state);+ this.#rememberClosedStream(state, { helperEnded: false });state.reject(error);}- #rememberClosedStream(state: StreamState): void {+ /**+ * Records a closed-stream tombstone. When the Host ended the stream itself+ * (abort, max-frames, local error) the Helper cannot take back frames that+ * already crossed stdio, so a bounded number of late frames are tolerated+ * until the Helper's own `streamEnd` arrives. Streams the Helper ended stay+ * strictly fail-closed.+ */+ #rememberClosedStream(+ state: StreamState,+ options: { helperEnded: boolean },+ ): void {this.#closedStreams.delete(state.streamId);this.#closedStreams.set(state.streamId, {simulatorUdid: state.command.simulatorUdid,generation: state.command.generation,+ lateFrameAllowance: options.helperEnded+ ? 0+ : state.acknowledgeFrames+ ? MAX_LATE_FRAMES_AFTER_LOCAL_STOP_ACKNOWLEDGED+ : MAX_LATE_FRAMES_AFTER_LOCAL_STOP_UNACKNOWLEDGED,});while (this.#closedStreams.size > MAX_CLOSED_STREAMS) {const oldest = this.#closedStreams.keys().next().value as@@ -1133,6 +1171,30 @@ export class IOSSimulatorNativeSidecarChannel implements IOSSimulatorNativeSidec}}+ /**+ * The Helper confirmed the stream is over; anything after this is+ * unsolicited traffic again.+ */+ #markHelperEnded(streamId: string): void {+ const closed = this.#closedStreams.get(streamId);+ if (closed) closed.lateFrameAllowance = 0;+ }++ #consumeLateFrameAllowance(+ closed: ClosedStreamState,+ decoded: DecodedStreamFrame,+ ): boolean {+ if (closed.lateFrameAllowance <= 0) return false;+ if (+ decoded.metadata.simulatorUdid !== closed.simulatorUdid ||+ decoded.metadata.generation !== closed.generation+ ) {+ return false;+ }+ closed.lateFrameAllowance -= 1;+ return true;+ }+#recordCrash(): void {this.#crashCount += 1;this.#state =