Skip to content

Commit fcb7338

Browse files
committed
Merge pull request #117214 from openclaw/fix/talk-transcript-persistence-bound-clean
2 parents 80b0e78 + a947b73 commit fcb7338

7 files changed

Lines changed: 810 additions & 104 deletions
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
import { BoundedSerialQueue } from "./bounded-serial-queue.js";
3+
4+
function deferred<T = void>(): {
5+
promise: Promise<T>;
6+
resolve: (value: T) => void;
7+
reject: (reason: unknown) => void;
8+
} {
9+
let resolve!: (value: T) => void;
10+
let reject!: (reason: unknown) => void;
11+
const promise = new Promise<T>((accept, fail) => {
12+
resolve = accept;
13+
reject = fail;
14+
});
15+
return { promise, resolve, reject };
16+
}
17+
18+
describe("BoundedSerialQueue", () => {
19+
it("runs one task at a time in FIFO order and continues after failures", async () => {
20+
const first = deferred();
21+
const order: string[] = [];
22+
const queue = new BoundedSerialQueue({ maxPendingCount: 4, maxPendingWeight: 4 });
23+
24+
const one = queue.enqueue(async () => {
25+
order.push("one:start");
26+
await first.promise;
27+
order.push("one:end");
28+
});
29+
const failure = new Error("second failed");
30+
const two = queue.enqueue(async () => {
31+
order.push("two");
32+
throw failure;
33+
});
34+
const three = queue.enqueue(async () => {
35+
order.push("three");
36+
return 3;
37+
});
38+
39+
expect(one.accepted && two.accepted && three.accepted).toBe(true);
40+
expect(order).toEqual(["one:start"]);
41+
const oneCompletion = expect(
42+
one.accepted ? one.completion : Promise.reject(new Error("first task was not admitted")),
43+
).resolves.toBeUndefined();
44+
const twoCompletion = expect(two.accepted ? two.completion : Promise.resolve()).rejects.toBe(
45+
failure,
46+
);
47+
const threeCompletion = expect(
48+
three.accepted ? three.completion : Promise.reject(new Error("third task was not admitted")),
49+
).resolves.toBe(3);
50+
first.resolve();
51+
52+
await Promise.all([oneCompletion, twoCompletion, threeCompletion]);
53+
expect(order).toEqual(["one:start", "one:end", "two", "three"]);
54+
expect(queue.isIdle).toBe(true);
55+
});
56+
57+
it("bounds waiting count and weight, then seals on the first overflow", () => {
58+
const first = deferred();
59+
const queue = new BoundedSerialQueue({ maxPendingCount: 2, maxPendingWeight: 5 });
60+
const active = queue.enqueue(async () => await first.promise, { weight: 100 });
61+
const waiting = queue.enqueue(async () => undefined, { weight: 3 });
62+
const overflow = queue.enqueue(async () => undefined, { weight: 3 });
63+
const late = queue.enqueue(async () => undefined);
64+
65+
expect(active.accepted).toBe(true);
66+
expect(waiting.accepted).toBe(true);
67+
expect(overflow).toEqual({ accepted: false, reason: "overflow" });
68+
expect(late).toEqual({ accepted: false, reason: "sealed" });
69+
expect(queue.didOverflow).toBe(true);
70+
first.resolve();
71+
});
72+
73+
it("can reject at capacity without sealing admission", async () => {
74+
const first = deferred();
75+
const queue = new BoundedSerialQueue({ maxPendingCount: 1, maxPendingWeight: 1 });
76+
const active = queue.enqueue(async () => await first.promise);
77+
const waiting = queue.enqueue(async () => undefined);
78+
79+
expect(
80+
queue.enqueue(async () => undefined, {
81+
sealOnOverflow: false,
82+
}),
83+
).toEqual({ accepted: false, reason: "capacity" });
84+
expect(queue.didOverflow).toBe(false);
85+
86+
first.resolve();
87+
await queue.flush();
88+
const afterDrain = queue.enqueue(async () => "accepted");
89+
expect(afterDrain.accepted).toBe(true);
90+
if (afterDrain.accepted) {
91+
await expect(afterDrain.completion).resolves.toBe("accepted");
92+
}
93+
if (active.accepted) {
94+
await active.completion;
95+
}
96+
if (waiting.accepted) {
97+
await waiting.completion;
98+
}
99+
});
100+
101+
it("flushes only the accepted prefix visible at call time", async () => {
102+
const first = deferred();
103+
const second = deferred();
104+
const queue = new BoundedSerialQueue({ maxPendingCount: 2, maxPendingWeight: 2 });
105+
const active = queue.enqueue(async () => await first.promise);
106+
const flush = queue.flush();
107+
const repeatedFlushes = Array.from({ length: 10_000 }, () => queue.flush());
108+
expect(new Set([flush, ...repeatedFlushes]).size).toBe(1);
109+
const late = queue.enqueue(async () => await second.promise);
110+
const flushed = vi.fn();
111+
void flush.then(flushed);
112+
113+
first.resolve();
114+
await flush;
115+
116+
expect(flushed).toHaveBeenCalledOnce();
117+
expect(late.accepted && queue.isIdle).toBe(false);
118+
second.resolve();
119+
await queue.flush();
120+
expect(queue.isIdle).toBe(true);
121+
if (active.accepted) {
122+
await active.completion;
123+
}
124+
if (late.accepted) {
125+
await late.completion;
126+
}
127+
});
128+
129+
it("seals idempotently while preserving accepted work", async () => {
130+
const first = deferred();
131+
const queue = new BoundedSerialQueue({ maxPendingCount: 1, maxPendingWeight: 1 });
132+
const active = queue.enqueue(async () => await first.promise);
133+
const waiting = queue.enqueue(async () => "done");
134+
135+
queue.seal();
136+
queue.seal();
137+
expect(queue.enqueue(async () => undefined)).toEqual({ accepted: false, reason: "sealed" });
138+
first.resolve();
139+
await queue.flush();
140+
141+
expect(active.accepted && waiting.accepted).toBe(true);
142+
if (waiting.accepted) {
143+
await expect(waiting.completion).resolves.toBe("done");
144+
}
145+
});
146+
});

‎src/shared/bounded-serial-queue.ts‎

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
type BoundedSerialQueueAdmission<T> =
2+
| { accepted: true; completion: Promise<T> }
3+
| { accepted: false; reason: "capacity" | "overflow" | "sealed" };
4+
5+
type BoundedSerialQueueTask = {
6+
weight: number;
7+
run: () => unknown;
8+
resolve: (value: unknown) => void;
9+
reject: (reason: unknown) => void;
10+
};
11+
12+
/**
13+
* Single-worker FIFO with bounded waiting work.
14+
*
15+
* The active task is owned separately from the waiting budget. Overflow seals
16+
* admission but preserves the already accepted prefix for flush and close.
17+
*/
18+
export class BoundedSerialQueue {
19+
private readonly pending: BoundedSerialQueueTask[] = [];
20+
private pendingWeight = 0;
21+
private active = false;
22+
private sealed = false;
23+
private overflowed = false;
24+
private settledPrefix: Promise<void> = Promise.resolve();
25+
26+
constructor(
27+
private readonly options: {
28+
maxPendingCount: number;
29+
maxPendingWeight: number;
30+
},
31+
) {
32+
if (!Number.isSafeInteger(options.maxPendingCount) || options.maxPendingCount < 0) {
33+
throw new Error("maxPendingCount must be a non-negative safe integer");
34+
}
35+
if (!Number.isFinite(options.maxPendingWeight) || options.maxPendingWeight < 0) {
36+
throw new Error("maxPendingWeight must be a non-negative finite number");
37+
}
38+
}
39+
40+
get isIdle(): boolean {
41+
return !this.active && this.pending.length === 0;
42+
}
43+
44+
get didOverflow(): boolean {
45+
return this.overflowed;
46+
}
47+
48+
enqueue<T>(
49+
run: () => T | Promise<T>,
50+
options: { weight?: number; sealOnOverflow?: boolean } = {},
51+
): BoundedSerialQueueAdmission<T> {
52+
if (this.sealed) {
53+
return { accepted: false, reason: "sealed" };
54+
}
55+
const weight = options.weight ?? 1;
56+
if (!Number.isFinite(weight) || weight < 0) {
57+
throw new Error("queue task weight must be a non-negative finite number");
58+
}
59+
if (
60+
this.active &&
61+
(this.pending.length >= this.options.maxPendingCount ||
62+
this.pendingWeight + weight > this.options.maxPendingWeight)
63+
) {
64+
if (options.sealOnOverflow === false) {
65+
return { accepted: false, reason: "capacity" };
66+
}
67+
this.sealed = true;
68+
this.overflowed = true;
69+
return { accepted: false, reason: "overflow" };
70+
}
71+
72+
let resolve!: (value: T | PromiseLike<T>) => void;
73+
let reject!: (reason: unknown) => void;
74+
const completion = new Promise<T>((accept, fail) => {
75+
resolve = accept;
76+
reject = fail;
77+
});
78+
const task: BoundedSerialQueueTask = {
79+
weight,
80+
run,
81+
resolve: (value) => resolve(value as T),
82+
reject,
83+
};
84+
this.settledPrefix = completion.then(
85+
() => undefined,
86+
() => undefined,
87+
);
88+
if (this.active) {
89+
this.pending.push(task);
90+
this.pendingWeight += weight;
91+
} else {
92+
this.active = true;
93+
this.startTask(task);
94+
}
95+
return { accepted: true, completion };
96+
}
97+
98+
seal(): void {
99+
this.sealed = true;
100+
}
101+
102+
/**
103+
* Waits for the accepted prefix visible at call time.
104+
*
105+
* Later admissions do not extend this barrier, which keeps consult flushes
106+
* finite while close can seal first to drain the entire accepted prefix.
107+
*/
108+
flush(): Promise<void> {
109+
return this.settledPrefix;
110+
}
111+
112+
private startTask(task: BoundedSerialQueueTask): void {
113+
void this.runTask(task);
114+
}
115+
116+
private async runTask(task: BoundedSerialQueueTask): Promise<void> {
117+
try {
118+
task.resolve(await task.run());
119+
} catch (error) {
120+
task.reject(error);
121+
} finally {
122+
const next = this.pending.shift();
123+
if (next) {
124+
this.pendingWeight -= next.weight;
125+
queueMicrotask(() => this.startTask(next));
126+
} else {
127+
this.active = false;
128+
}
129+
}
130+
}
131+
}

‎src/talk/client-voice-session-store.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66

77
export const VOICE_SESSION_CACHE_SCOPE = "talk-client-voice-sessions";
88
export const VOICE_SESSION_RECORD_VERSION = 1;
9-
export const VOICE_SESSION_MAX_TRANSCRIPT_CHARS = 8_000;
109
export const VOICE_SESSION_STALE_AFTER_MS = 6 * 60 * 60_000;
1110

1211
export type ClientVoiceToolEffect = {

0 commit comments

Comments
 (0)