Skip to content

Commit a79834b

Browse files
shivamhwpclaude
andcommitted
fix(server): usage-limit auto-resume is sent by T3 Code, not the user
The "Resume at reset" message was stored as createdBy user, so clients showed it as the user's own message and it entered the user message search index and user request analytics. Send it as agent/server like restart continuation. A subagent child's auto-resume still starts a follow-up the parent tracks: mayStartSubagentFollowup and the follow-up sender check now accept a usage-limit continuation instead of keying on createdBy user. Closes #29746 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f6503f0 commit a79834b

4 files changed

Lines changed: 121 additions & 2 deletions

File tree

‎apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import * as Fiber from "effect/Fiber";
2727
import * as Layer from "effect/Layer";
2828
import * as Stream from "effect/Stream";
2929
import * as Tracer from "effect/Tracer";
30+
import * as TestClock from "effect/testing/TestClock";
3031

3132
import * as CheckpointStore from "../checkpointing/CheckpointStore.ts";
3233
import * as ServerConfig from "../config.ts";
@@ -46,6 +47,7 @@ import * as Orchestrator from "./Orchestrator.ts";
4647
import { continueRestartedRun } from "./RestartContinuation.ts";
4748
import * as RuntimeLayer from "./runtimeLayer.ts";
4849
import * as ThreadCommandExecutor from "./ThreadCommandExecutor.ts";
50+
import { limitRecoveryCommand } from "./UsageLimitRecoveryWorker.ts";
4951
import * as ProviderTurnStartServiceTestkit from "./ProviderTurnStartService.testkit.ts";
5052
import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
5153
import * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests";
@@ -2120,6 +2122,111 @@ it.layer(Layer.merge(layerTest, ProviderContinuationRequests.layer))(
21202122
}),
21212123
);
21222124

2125+
it.effect("tracks a child's usage-limit auto-resume as a follow-up", () =>
2126+
Effect.gen(function* () {
2127+
const orchestrator = yield* Orchestrator.OrchestratorV2;
2128+
const eventSink = yield* EventSink.EventSinkV2;
2129+
const now = yield* DateTime.now;
2130+
const parentThreadId = ThreadId.make("thread:limit-resume-parent");
2131+
const projectId = ProjectId.make("project:limit-resume");
2132+
const parentRunId = RunId.make("run:limit-resume-parent");
2133+
const rootNodeId = NodeId.make("node:limit-resume-parent");
2134+
yield* seedParentWithTerminalTask({
2135+
threadId: parentThreadId,
2136+
projectId,
2137+
runId: parentRunId,
2138+
rootNodeId,
2139+
taskId: NodeId.make("node:limit-resume-seed"),
2140+
deliveryState: "acknowledged",
2141+
now,
2142+
});
2143+
const child = yield* seedRestartCancelledChild({
2144+
parentThreadId,
2145+
projectId,
2146+
parentRunId,
2147+
rootNodeId,
2148+
name: "limit-resume-child",
2149+
completionWake: "always",
2150+
continuationPending: false,
2151+
runStatus: "completed",
2152+
now,
2153+
});
2154+
const childRun = (yield* orchestrator.getThreadProjection(child.childThreadId)).runs[0]!;
2155+
yield* eventSink.write({
2156+
commandId: reconcileCommandId("limit-resume-child-limited"),
2157+
events: [
2158+
{
2159+
id: EventId.make("event:limit-resume-child-failed"),
2160+
type: "run.updated",
2161+
threadId: child.childThreadId,
2162+
runId: childRun.id,
2163+
occurredAt: now,
2164+
payload: { ...childRun, status: "failed", completedAt: now },
2165+
},
2166+
{
2167+
id: EventId.make("event:limit-resume-child-error"),
2168+
type: "turn-item.updated",
2169+
threadId: child.childThreadId,
2170+
occurredAt: now,
2171+
payload: {
2172+
id: TurnItemId.make("turn-item:limit-resume-child-error"),
2173+
type: "error",
2174+
threadId: child.childThreadId,
2175+
runId: childRun.id,
2176+
nodeId: childRun.rootNodeId,
2177+
providerThreadId: null,
2178+
providerTurnId: null,
2179+
nativeItemRef: null,
2180+
parentItemId: null,
2181+
ordinal: 1,
2182+
status: "failed",
2183+
title: "Usage limit reached",
2184+
startedAt: now,
2185+
completedAt: now,
2186+
updatedAt: now,
2187+
failure: {
2188+
class: "usage_limit",
2189+
message: "Plan limit reached.",
2190+
code: "usageLimitExceeded",
2191+
retryable: null,
2192+
resetAt: DateTime.formatIso(DateTime.add(now, { minutes: 1 })),
2193+
},
2194+
},
2195+
},
2196+
],
2197+
});
2198+
yield* orchestrator.recoverDelegatedTasks;
2199+
const childShell = Effect.map(orchestrator.getShellSnapshot(), (snapshot) =>
2200+
snapshot.threads.find((thread) => thread.id === child.childThreadId),
2201+
);
2202+
const arm = limitRecoveryCommand((yield* childShell)!, true, DateTime.toEpochMillis(now));
2203+
assert(arm?.type === "thread.metadata.update" && arm.limitRecovery != null);
2204+
yield* orchestrator.autoArmLimitRecovery({
2205+
commandId: arm.commandId,
2206+
threadId: arm.threadId,
2207+
limitRecovery: arm.limitRecovery,
2208+
});
2209+
yield* TestClock.adjust("1 minute");
2210+
const resume = limitRecoveryCommand(
2211+
(yield* childShell)!,
2212+
true,
2213+
DateTime.toEpochMillis(yield* DateTime.now),
2214+
);
2215+
assert(resume?.type === "message.dispatch");
2216+
yield* orchestrator.dispatch(resume);
2217+
2218+
const resumed = (yield* orchestrator.getThreadProjection(child.childThreadId)).runs.find(
2219+
(run) => run.userMessageId === resume.messageId,
2220+
);
2221+
assert.isDefined(resumed?.delegatedTaskId);
2222+
const followup = (yield* orchestrator.getThreadProjection(parentThreadId)).subagents.find(
2223+
(task) => task.id === resumed?.delegatedTaskId,
2224+
);
2225+
assert.equal(followup?.childThreadId, child.childThreadId);
2226+
assert.isNull(followup?.result);
2227+
}),
2228+
);
2229+
21232230
it.effect("serializes a recovered child answer with its parent and tracks the follow-up", () =>
21242231
Effect.gen(function* () {
21252232
const orchestrator = yield* Orchestrator.OrchestratorV2;

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,11 @@ function mayStartSubagentFollowup(
685685
if (command.scheduledTaskId !== undefined || command.restartContinuationOfRunId !== undefined)
686686
return false;
687687
if (command.notification !== undefined) return command.notification.source.kind !== "monitor";
688-
return command.createdBy === "user" || command.creationSource === "mcp";
688+
return (
689+
command.createdBy === "user" ||
690+
command.creationSource === "mcp" ||
691+
command.usageLimitContinuationOfRunId !== undefined
692+
);
689693
}
690694

691695
/** Waiting runs only drain post-terminal output; their agent turn is over and still needs a wake. */
@@ -7902,6 +7906,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
79027906
parentThreadId === null ||
79037907
(command.notification === undefined &&
79047908
command.createdBy !== "user" &&
7909+
command.usageLimitContinuationOfRunId === undefined &&
79057910
command.senderThreadId !== parentThreadId)
79067911
)
79077912
return;

‎apps/server/src/orchestration-v2/UsageLimitRecoveryWorker.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export function limitRecoveryCommand(
7373
text: "Continue where you left off.",
7474
attachments: [],
7575
dispatchMode: { type: "start_immediately" },
76-
createdBy: "user",
76+
createdBy: "agent",
7777
creationSource: "server",
7878
};
7979
}

‎apps/server/src/orchestration-v2/runtimeLayer.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8447,6 +8447,13 @@ it.layer(layerTest)("usage-limit recovery", (it) => {
84478447
yield* orchestrator.dispatch(resume);
84488448
}
84498449
const after = yield* orchestrator.getThreadProjection(threadId);
8450+
if (scenario === "resume") {
8451+
assert.deepInclude(after.messages.at(-1), {
8452+
text: "Continue where you left off.",
8453+
createdBy: "agent",
8454+
creationSource: "server",
8455+
});
8456+
}
84508457
assert.lengthOf(
84518458
after.runs,
84528459
before.runs.length +

0 commit comments

Comments
 (0)