Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(rooms): each room deadline entry reads the clock once

laijsg1ccf69785e4ee
diff --git a/server/room-turn-timeout.test.ts b/server/room-turn-timeout.test.ts
index fab57d1474..dc7c43a550 100644
--- a/server/room-turn-timeout.test.ts
+++ b/server/room-turn-timeout.test.ts
@@ -27,7 +27,7 @@ const reply: RuntimeEvent = { ...base, type: "content.delta", streamKind: "assis
* `wake` runs what came due meanwhile, the kind named first. */
function sleepingClock() {
type Timer = { at: number; every?: number; run: () => void };
- let now = 0, nextId = 0;
+ let now = 0, nextId = 0, sleepAfterRead = 0;
const timers = new Map<number, Timer>();
const add = (timer: Timer) => {
const id = nextId++;
@@ -35,7 +35,12 @@ function sleepingClock() {
return () => { timers.delete(id); };
};
const clock: RoomTurnClock = {
- now: () => now,
+ now: () => {
+ const at = now;
+ now += sleepAfterRead;
+ sleepAfterRead = 0;
+ return at;
+ },
after: (ms, run) => add({ at: now + ms, run }),
every: (ms, run) => add({ at: now + ms, every: ms, run }),
};
@@ -59,6 +64,10 @@ function sleepingClock() {
sleep(ms: number) {
now += ms;
},
+ /** The next reading of the clock is the last before a sleep of `ms`. */
+ sleepAfterNextRead(ms: number) {
+ sleepAfterRead = ms;
+ },
wake(first: "after" | "every") {
for (const kind of [first, first === "after" ? "every" : "after"] as const) {
for (const [id, timer] of due(now, kind)) if (timers.get(id) === timer) fire(id, timer);
@@ -205,6 +214,26 @@ describe("room turn timeout", () => {
});
}
+ it("keeps the idle time from before a wait when the computer sleeps right as the wait ends", () => {
+ const time = sleepingClock();
+ const onTimeout = vi.fn();
+ const deadline = new RoomTurnDeadline(1, onTimeout, time.clock);
+ deadline.start();
+
+ time.advance(50_000);
+ deadline.setWaiting(true);
+ time.advance(10_000);
+ time.sleepAfterNextRead(2 * 60 * 60_000);
+ deadline.setWaiting(false);
+ time.wake("every");
+ expect(onTimeout).not.toHaveBeenCalled();
+
+ time.advance(10_000 - 1);
+ expect(onTimeout).not.toHaveBeenCalled();
+ time.advance(1);
+ expect(onTimeout).toHaveBeenCalledOnce();
+ });
+
it("counts the time awake between many short wakes", () => {
const time = sleepingClock();
const onTimeout = vi.fn();
diff --git a/server/room-turn-timeout.ts b/server/room-turn-timeout.ts
index 949d8cf07d..5afcd2b6de 100644
--- a/server/room-turn-timeout.ts
+++ b/server/room-turn-timeout.ts
@@ -105,56 +105,64 @@ export class RoomTurnDeadline {
start(): void {
if (this.stopped || this.fired || this.stopLooking !== null) return;
- this.progressAt = this.clock.now();
- this.lookedAt = this.progressAt;
+ const at = this.clock.now();
+ this.progressAt = at;
+ this.lookedAt = at;
this.stopLooking = this.clock.every(ROOM_TURN_CLOCK_LOOK_MS, () => this.discountSleep());
- this.arm();
+ this.arm(at);
}
observe(e: RuntimeEvent): void {
- this.discountSleep();
- if (ROOM_TURN_PROGRESS.has(e.type)) this.progress();
- if (e.type === "request.opened" || e.type === "turn.wait_started") this.setWaiting(true);
- else if (e.type === "request.resolved" || e.type === "turn.wait_ended") this.setWaiting(false);
+ const at = this.discountSleep();
+ if (ROOM_TURN_PROGRESS.has(e.type)) this.progressed(at);
+ if (e.type === "request.opened" || e.type === "turn.wait_started") this.waiting(true, at);
+ else if (e.type === "request.resolved" || e.type === "turn.wait_ended") this.waiting(false, at);
else if ((e.type === "item.started" || e.type === "item.completed") && e.itemType === "tool" && e.itemId) {
if (e.type === "item.started") this.runningTools.add(e.itemId);
else this.runningTools.delete(e.itemId);
- this.rearm();
+ this.rearm(at);
}
}
progress(): void {
+ this.progressed(this.discountSleep());
+ }
+
+ setWaiting(waiting: boolean): void {
+ this.waiting(waiting, this.discountSleep());
+ }
+
+ stop(): void {
+ this.stopped = true;
+ this.release();
+ }
+
+ private progressed(at: number): void {
// A delayed event loop can deliver progress after the limit, before the timer ran: it must not revive an idle turn.
- if (this.cancelTimer !== null && this.idleMs() >= this.quietLimitMs()) {
+ if (this.cancelTimer !== null && at - this.progressAt >= this.quietLimitMs()) {
this.expired();
return;
}
- this.progressAt = this.clock.now();
+ this.progressAt = at;
}
- setWaiting(waiting: boolean): void {
+ private waiting(waiting: boolean, at: number): void {
if (this.stopped || this.fired) return;
- this.discountSleep();
if (waiting) {
const wasIdle = this.openWaits === 0;
this.openWaits += 1;
- if (wasIdle) this.hold();
+ if (wasIdle) this.hold(at);
} else if (this.openWaits > 0) {
this.openWaits -= 1;
if (this.openWaits === 0) {
// The wait paused the clock: idle time from before it still counts, progress during it counts as now.
const idleBefore = this.progressAt >= this.heldAt ? 0 : this.heldAt - this.progressAt;
- this.progressAt = this.clock.now() - idleBefore;
- this.arm();
+ this.progressAt = at - idleBefore;
+ this.arm(at);
}
}
}
- stop(): void {
- this.stopped = true;
- this.release();
- }
-
private release(): void {
this.stopLooking?.();
this.stopLooking = null;
@@ -162,52 +170,47 @@ export class RoomTurnDeadline {
this.cancelTimer = null;
}
- /** A look late enough moves the turn's progress, and a wait's start, on by the whole gap. */
- private discountSleep(): void {
- if (this.stopLooking === null) return;
+ private discountSleep(): number {
const at = this.clock.now();
+ if (this.stopLooking === null) return at;
const gapMs = at - this.lookedAt;
this.lookedAt = at;
- if (gapMs <= SUSPENDED_LOOKS * ROOM_TURN_CLOCK_LOOK_MS) return;
+ if (gapMs <= SUSPENDED_LOOKS * ROOM_TURN_CLOCK_LOOK_MS) return at;
this.progressAt = Math.min(at, this.progressAt + gapMs);
if (this.openWaits > 0) this.heldAt = Math.min(at, this.heldAt + gapMs);
- }
-
- private idleMs(): number {
- this.discountSleep();
- return this.clock.now() - this.progressAt;
+ return at;
}
private quietLimitMs(): number {
return this.runningTools.size > 0 ? Math.max(this.limitMs, ROOM_TOOL_QUIET_LIMIT_MS) : this.limitMs;
}
- private hold(): void {
- this.heldAt = this.clock.now();
+ private hold(at: number): void {
+ this.heldAt = at;
if (this.cancelTimer === null) return;
this.cancelTimer();
this.cancelTimer = null;
// A delayed event loop can deliver the wait after the deadline: a wait must not revive an idle turn.
- if (this.idleMs() >= this.quietLimitMs()) this.expired();
+ if (at - this.progressAt >= this.quietLimitMs()) this.expired();
}
- private rearm(): void {
+ private rearm(at: number): void {
if (this.cancelTimer === null) return;
this.cancelTimer();
this.cancelTimer = null;
- this.arm();
+ this.arm(at);
}
- private arm(): void {
+ private arm(at: number): void {
if (this.stopped || this.fired || this.openWaits > 0) return;
- const leftMs = this.quietLimitMs() - this.idleMs();
+ const leftMs = this.quietLimitMs() - (at - this.progressAt);
if (leftMs <= 0) {
this.expired();
return;
}
this.cancelTimer = this.clock.after(leftMs, () => {
this.cancelTimer = null;
- this.arm();
+ this.arm(this.discountSleep());
});
}