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(
+21 -1
View File
@@ -528,6 +528,12 @@ export const OrchestrationV2Run = Schema.Struct({
contextHandoffId: Schema.NullOr(ContextHandoffId),
/** Links server-generated restart continuations to the interrupted run. */
restartContinuationOfRunId: Schema.optional(RunId),
/**
* Set on wake runs (background notifications, delegated task results,
* restart continuations): when the work they continue started. Read it
* through orchestrationV2RunWorkStartedAt.
*/
workStartedAt: Schema.optional(Schema.DateTimeUtc),
/**
* Set by restart recovery on the thread's latest started run. Delivered to
* the provider with the first later run that reaches a provider turn.
@@ -545,6 +551,16 @@ export const OrchestrationV2Run = Schema.Struct({
});
export type OrchestrationV2Run = typeof OrchestrationV2Run.Type;
/**
* When the work a run belongs to started. A wake does not start new work, so
* working timers count from the prompt that did, not from the latest wake.
*/
export function orchestrationV2RunWorkStartedAt(
run: Pick<OrchestrationV2Run, "workStartedAt" | "startedAt" | "requestedAt">,
): OrchestrationV2Run["requestedAt"] {
return run.workStartedAt ?? run.startedAt ?? run.requestedAt;
}
export const OrchestrationV2RunAttempt = Schema.Struct({
id: RunAttemptId,
// Provider-thread rows can be reused after recovery; retain the native input destination.
@@ -1688,7 +1704,10 @@ export const OrchestrationV2ThreadShell = Schema.Struct({
latestRunStartedAt: Schema.optional(Schema.NullOr(Schema.DateTimeUtc)),
latestRunCompletedAt: Schema.optional(Schema.NullOr(Schema.DateTimeUtc)),
activeRunId: Schema.NullOr(RunId),
/** Start of the activity-owning run; request time while it is preparing. */
/**
* orchestrationV2RunWorkStartedAt of the activity-owning run: a wake keeps
* the start of the work it continues; request time while preparing.
*/
activityRunStartedAt: Schema.optional(Schema.NullOr(Schema.DateTimeUtc)),
activityRunStatus: Schema.optional(
Schema.NullOr(Schema.Literals(["preparing", "starting", "running", "waiting"])),
@@ -1841,6 +1860,7 @@ export const OrchestrationV2RunJson = OrchestrationV2Run.mapFields((fields) => (
requestedAt: Schema.DateTimeUtcFromString,
startedAt: Schema.NullOr(Schema.DateTimeUtcFromString),
completedAt: Schema.NullOr(Schema.DateTimeUtcFromString),
workStartedAt: Schema.optional(Schema.DateTimeUtcFromString),
}));
export type OrchestrationV2RunJson = typeof OrchestrationV2RunJson.Type;