Skip to content

Commit 0dc69be

Browse files
fix(provider): keep Droid backlog limits strict (PR-7993)
Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
1 parent 6f8c5ac commit 0dc69be

2 files changed

Lines changed: 33 additions & 31 deletions

File tree

‎apps/server/src/provider/droid/DroidRpcProtocol.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,25 @@ describe("DroidRpcProtocol", () => {
361361
}),
362362
);
363363

364+
it.effect("keeps prefetched lossless deliveries inside the backlog limit", () =>
365+
Effect.gen(function* () {
366+
const protocol = yield* makeDroidRpcProtocol({ maxBacklogItems: 2 });
367+
const frames = ["first", "second", "third", "fourth"].map((id) =>
368+
notification(assistantDelta(id, "x")),
369+
);
370+
371+
yield* protocol.acceptChunk(`${frames[0]}\n${frames[1]}\n`);
372+
assert.equal((yield* take(protocol.notifications)).notification.type, "assistant_text_delta");
373+
yield* protocol.acceptChunk(`${frames[2]}\n`);
374+
375+
const overflow = yield* Effect.result(protocol.acceptChunk(`${frames[3]}\n`));
376+
assert.equal(overflow._tag, "Failure");
377+
if (overflow._tag === "Failure") {
378+
assert.equal(overflow.failure.kind, "backlog-overflow");
379+
}
380+
}),
381+
);
382+
364383
it.effect("bounds UTF-8 remainders and outbound encoded frames", () =>
365384
Effect.gen(function* () {
366385
const inbound = yield* makeDroidRpcProtocol({ maxMessageBytes: 8 });

‎apps/server/src/provider/droid/DroidRpcProtocol.ts‎

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -975,53 +975,36 @@ export const makeDroidRpcProtocol = (
975975
backlogState: Ref.Ref<LosslessBacklogState>,
976976
backlogMutex: Semaphore.Semaphore,
977977
) => {
978-
const takeDeliveries: Effect.Effect<
979-
Arr.NonEmptyReadonlyArray<QueuedDelivery<A>>,
980-
Cause.Done<void>
981-
> = Effect.suspend(() =>
978+
const takeDelivery: Effect.Effect<QueuedDelivery<A>, Cause.Done<void>> = Effect.suspend(() =>
982979
Queue.peek(queue).pipe(
983980
Effect.andThen(
984981
backlogMutex.withPermits(1)(
985982
Effect.uninterruptible(
986-
Queue.clear(queue).pipe(
987-
Effect.flatMap((deliveries) => {
988-
const first = deliveries[0];
989-
if (first === undefined) {
990-
return Effect.succeed(
991-
Option.none<Arr.NonEmptyReadonlyArray<QueuedDelivery<A>>>(),
992-
);
993-
}
994-
const batch: Arr.NonEmptyArray<QueuedDelivery<A>> = [
995-
first,
996-
...deliveries.slice(1),
997-
];
998-
const releasedBytes = batch.reduce(
999-
(total, delivery) => total + delivery.encodedBytes,
1000-
0,
1001-
);
1002-
return Ref.update(backlogState, (current) => ({
1003-
items: Math.max(0, current.items - batch.length),
1004-
bytes: Math.max(0, current.bytes - releasedBytes),
1005-
})).pipe(Effect.as(Option.some(batch)));
1006-
}),
983+
Queue.poll(queue).pipe(
984+
Effect.flatMap(
985+
Option.match({
986+
onNone: () => Effect.succeed(Option.none<QueuedDelivery<A>>()),
987+
onSome: (delivery) =>
988+
Ref.update(backlogState, (current) => ({
989+
items: Math.max(0, current.items - 1),
990+
bytes: Math.max(0, current.bytes - delivery.encodedBytes),
991+
})).pipe(Effect.as(Option.some(delivery))),
992+
}),
993+
),
1007994
),
1008995
),
1009996
),
1010997
),
1011998
Effect.flatMap(
1012999
Option.match({
1013-
onNone: () => takeDeliveries,
1000+
onNone: () => takeDelivery,
10141001
onSome: Effect.succeed,
10151002
}),
10161003
),
10171004
),
10181005
);
10191006
return Stream.fromPull(
1020-
Effect.succeed(
1021-
takeDeliveries.pipe(
1022-
Effect.map((deliveries) => Arr.map(deliveries, (item) => item.value)),
1023-
),
1024-
),
1007+
Effect.succeed(takeDelivery.pipe(Effect.map((delivery) => Arr.of(delivery.value)))),
10251008
);
10261009
};
10271010

0 commit comments

Comments
 (0)