fix(server): working timers no longer reset on every background wake (#15029)

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
Theo BrowneandClaude Opus 5.5 authored and GitHub committed 2026-10-02 21:55:57 -07:00
1 parent 9333509c91
commit 736130c2fd
9 files changed
+245 -11

No files matched your search

+4 -1
View File
@@ -296,7 +296,10 @@ export function resolveThreadProviderStack(
return [...previous.slice(-(THREAD_PROVIDER_STACK_LIMIT - 1)), current];
}
/** Both shell and detail timers use the activity-owning run, never last activity. */
/**
* Both shell and detail timers count from the activity-owning run's work
* start, never last activity. A wake keeps the start of the work it continues.
*/
export function resolveThreadWorkingStartedAt(input: {
readonly latestRun: Pick<
ThreadRunSummary,
@@ -96,6 +96,19 @@ describe("thread execution presentation", () => {
).toMatchObject({ status: "running", lastError: null, lastErrorClass: null });
});
it("counts a wake run's activity from the start of the work it continues", () => {
const workStartedAt = DateTime.makeUnsafe("2026-07-28T09:20:00.000Z");
const wake = { ...run("wake", 2, "running"), workStartedAt };
expect(
deriveThreadRuntime({ ...v2Projection, runs: [run("prompt", 1, "completed"), wake] })
?.activityStartedAt,
).toBe("2026-07-28T09:20:00.000Z");
expect(
deriveThreadRuntime({ ...v2Projection, runs: [run("prompt", 1, "running")] })
?.activityStartedAt,
).toBe("2026-07-28T10:00:00.000Z");
});
it("keeps a subscription limit visible while later messages stay queued", () => {
const failed = {
...run("limited", 1, "failed"),
@@ -13,6 +13,7 @@ import {
type ServerProviderModel,
type OrchestrationV2ExecutionNode,
type OrchestrationV2ThreadProjection,
orchestrationV2RunWorkStartedAt,
type ThreadId,
} from "@t3tools/contracts";
import { derivePendingBackgroundWork } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
@@ -213,6 +214,9 @@ export function deriveThreadRuntime(
const usageLimitedRun = presentedUsageLimitRun(projection);
const latestRunProjection = presentedLatestRun(projection);
const activityRun = deriveThreadActivityRun(projection);
const liveActivityRun = latestMatchingRun(projection, (run) =>
ACTIVITY_RUN_STATUSES.has(run.status),
);
if (latestRun === null && projection.thread.activeProviderThreadId === null) return null;
const activeRunId =
latestMatchingRun(projection, (run) => INTERRUPTIBLE_RUN_STATUSES.has(run.status))?.id ?? null;
@@ -232,9 +236,9 @@ export function deriveThreadRuntime(
: (activityRun?.status ?? "idle"),
activeRunId,
activityStartedAt:
activityRun !== null && ACTIVITY_RUN_STATUSES.has(activityRun.status)
? (activityRun.startedAt ?? activityRun.requestedAt)
: null,
liveActivityRun === null
? null
: DateTime.formatIso(orchestrationV2RunWorkStartedAt(liveActivityRun)),
providerInstanceId: projection.thread.providerInstanceId,
providerName: providerSession?.driver ?? null,
...threadErrorSummary(