Skip to content

Commit a5eb0c6

Browse files
perf(client): skip idle inbox placement serialization
1 parent 5ef5685 commit a5eb0c6

2 files changed

Lines changed: 17 additions & 12 deletions

File tree

‎packages/client-runtime/src/state/threadInbox.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ describe("createInboxReturnTracker", () => {
4545
});
4646
it("stamps a thread when it stops working, but never on the first observation", () => {
4747
const tracker = createInboxReturnTracker();
48+
using stringify = vi.spyOn(JSON, "stringify");
4849
const first = tracker.observe([thread("a", true), thread("b", false)]);
50+
expect(stringify).toHaveBeenCalledTimes(1);
4951
expect(first.returnedAt(thread("a", true))).toBeUndefined();
5052
expect(first.returnedAt(thread("b", false))).toBeUndefined();
5153

‎packages/client-runtime/src/state/threadInbox.ts‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export function createInboxReturnTracker() {
123123
const keyOf = (thread: ThreadKeyInput) => `${thread.environmentId}:${thread.id}`;
124124
const states = new Map<
125125
string,
126-
{ working: boolean; deadline: number | null; placement: string }
126+
{ working: boolean; deadline: number | null; placement: string | null }
127127
>();
128128
const returns = new Map<string, number>();
129129
const listeners = new Set<() => void>();
@@ -184,17 +184,21 @@ export function createInboxReturnTracker() {
184184
for (const thread of threads) {
185185
const key = keyOf(thread);
186186
present.add(key);
187-
const placement = JSON.stringify([
188-
thread.archivedAt,
189-
thread.deletedAt,
190-
thread.pinnedAt,
191-
thread.settledOverride,
192-
thread.settledAt,
193-
thread.snoozedAt,
194-
thread.snoozedUntil,
195-
thread.activeOrderKey,
196-
]);
197187
const previous = states.get(key);
188+
const actual = isThreadWorking(thread);
189+
const placement =
190+
actual || previous?.working
191+
? JSON.stringify([
192+
thread.archivedAt,
193+
thread.deletedAt,
194+
thread.pinnedAt,
195+
thread.settledOverride,
196+
thread.settledAt,
197+
thread.snoozedAt,
198+
thread.snoozedUntil,
199+
thread.activeOrderKey,
200+
])
201+
: null;
198202
const actionable =
199203
thread.hasPendingApprovals ||
200204
thread.hasPendingUserInput ||
@@ -210,7 +214,6 @@ export function createInboxReturnTracker() {
210214
thread.pinnedAt != null ||
211215
thread.settledOverride === "settled" ||
212216
effectiveSnoozed(thread, { now: observedAt });
213-
const actual = isThreadWorking(thread);
214217
// Only shelf placement is held: execution state and attention requests stay authoritative.
215218
const deadline =
216219
!actual && previous?.working && !actionable && !moved && !excluded

0 commit comments

Comments
 (0)