Skip to content
Source
Sign inSign up
pingdotgg/t3code

perf(server): trim stale context-window rows and drop dead replay RPC #4791

Mergedt3dotgg(GitHub profile, opens in a new tab)merged 3 commits intomain fromrefs/pull/4791/headmerged by t3dotgg(GitHub profile, opens in a new tab) View on GitHub

Showing 6 changed files with 49 additions and 77 deletions (loading 4).

x
0 of 6 viewed, 6 left
49 additions and 1 deletionapps/server/src/orchestration/ActivityPayloadProjection.tsModified
View file
@@ -201,6 +201,52 @@
​
+/**
+ * Matches the validity rule in the web client's
+ * `deriveLatestContextWindowSnapshot`: rows without a finite, non-negative
+ * `usedTokens` are skipped during its backward walk, so they must not shadow
+ * an earlier resolvable row here.
+ */
+function isResolvableContextWindowActivity(activity: OrchestrationThreadActivity): boolean {
+ if (activity.kind !== "context-window.updated") {
+ return false;
+ }
+ const payload = asRecord(activity.payload);
+ const usedTokens = payload?.usedTokens;
+ return typeof usedTokens === "number" && Number.isFinite(usedTokens) && usedTokens >= 0;
+}
+​
+/**
+ * Drops all but the last resolvable context-window activity per turn from a
+ * snapshot. Clients only ever read the latest usage value (walking the array
+ * backwards), so shipping the full history — often thousands of rows on long
+ * threads — buys nothing. Retention is per turn rather than per thread because
+ * a live `thread.reverted` makes the client discard whole turns; keeping each
+ * turn's latest row means the meter can still resolve a value from the turns
+ * that survive. Malformed rows pass through untouched rather than shadowing a
+ * valid earlier row. Live `thread.activity-appended` events are untouched:
+ * newer updates still stream through and supersede the retained rows on the
+ * client.