Skip to content

Commit 7e2b903

Browse files
georgenijoclaude
andcommitted
test(server): test the delegated-task wake rule directly
Moves the wake rule into delegatedTaskWillWake so its cases are checked with small literals instead of a full run and task event fixture. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent c2139fd commit 7e2b903

3 files changed

Lines changed: 67 additions & 97 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ import * as Option from "effect/Option";
6767
import * as Path from "effect/Path";
6868
import * as PubSub from "effect/PubSub";
6969
import * as Queue from "effect/Queue";
70-
import * as Ref from "effect/Ref";
7170
import * as Schema from "effect/Schema";
7271
import * as Scope from "effect/Scope";
7372
import * as Stream from "effect/Stream";
@@ -17907,7 +17906,6 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1790717906
Effect.gen(function* () {
1790817907
const harness = yield* makeWakeHarness;
1790917908
const mcpSessions = yield* McpProviderSessions.McpProviderSessions;
17910-
const pendingWake = yield* Ref.make(true);
1791117909
yield* mcpSessions.set({
1791217910
environmentId: EnvironmentId.make("environment-goal-wake"),
1791317911
threadId: harness.threadId,
@@ -17916,7 +17914,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1791617914
endpoint: "http://127.0.0.1:43123/mcp",
1791717915
authorizationHeader: "Bearer secret-claude-token",
1791817916
browserToolsAvailable: true,
17919-
hasPendingWake: Ref.get(pendingWake),
17917+
hasPendingWake: Effect.succeed(true),
1792017918
});
1792117919
const stop = Effect.promise(() =>
1792217920
Promise.all(
@@ -17964,9 +17962,6 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1796417962
const terminal = yield* Queue.take(harness.terminalReceipts);
1796517963
assert.equal(terminal.status, "completed");
1796617964
assert.equal(goalStatuses(harness.events).at(-1)?.status, "active");
17967-
// Once nothing will wake the thread, the goal check runs as before.
17968-
yield* Ref.set(pendingWake, false);
17969-
assert.deepEqual(yield* stop, [{}]);
1797017965
}).pipe(
1797117966
Effect.scoped,
1797217967
Effect.provide(

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

Lines changed: 38 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import { assert, it } from "@effect/vitest";
55
import { AgentScope } from "@t3tools/shared/AgentScope";
66
import {
77
EnvironmentId,
8-
EventId,
9-
MessageId,
108
type ModelSelection,
119
type OrchestrationV2AppThread,
1210
type OrchestrationV2DomainEvent,
@@ -18,6 +16,7 @@ import {
1816
ProviderDriverKind,
1917
ProviderInstanceId,
2018
type ProviderSessionId,
19+
RunId,
2120
ThreadId,
2221
} from "@t3tools/contracts";
2322
import * as Cause from "effect/Cause";
@@ -1771,6 +1770,43 @@ it.effect("ProviderSessionManagerV2 drains subscribers when the provider stops",
17711770
}),
17721771
);
17731772

1773+
it("ProviderSessionManagerV2 counts a delegated task as a wake only while its result can be delivered", () => {
1774+
const runId = RunId.make("run-delegating");
1775+
const thread = { archivedAt: null, deletedAt: null };
1776+
const task = {
1777+
origin: "app_owned" as const,
1778+
status: "running" as const,
1779+
runId,
1780+
completionDelivery: undefined,
1781+
};
1782+
const run = (disposition: "open" | "disposed") => ({
1783+
id: runId,
1784+
delegatedCompletion: { disposition, nextGeneration: 1, delivery: null },
1785+
});
1786+
assert.isTrue(
1787+
ProviderSessionManager.delegatedTaskWillWake({
1788+
thread,
1789+
runs: [run("open")],
1790+
subagents: [task],
1791+
}),
1792+
);
1793+
// An interrupted completion wake disposes the cohort, so the result never wakes the thread.
1794+
assert.isFalse(
1795+
ProviderSessionManager.delegatedTaskWillWake({
1796+
thread,
1797+
runs: [run("disposed")],
1798+
subagents: [task],
1799+
}),
1800+
);
1801+
assert.isFalse(
1802+
ProviderSessionManager.delegatedTaskWillWake({
1803+
thread,
1804+
runs: [run("open")],
1805+
subagents: [{ ...task, origin: "provider_native" }],
1806+
}),
1807+
);
1808+
});
1809+
17741810
it.effect(
17751811
"ProviderSessionManagerV2 issues MCP credentials before opening and revokes them on close",
17761812
() =>
@@ -1816,75 +1852,6 @@ it.effect(
18161852
new Set(["preview", "orchestration", "worktree", "pull-requests"]),
18171853
);
18181854

1819-
// A running delegated task wakes the thread only while its completion can still be delivered.
1820-
assert.isFalse(yield* captured!.hasPendingWake!);
1821-
const runId = idAllocator.derive.run({ threadId, ordinal: 1 });
1822-
const run = (
1823-
disposition: "open" | "disposed",
1824-
): OrchestrationV2DomainEvent & { readonly type: "run.created" | "run.updated" } => ({
1825-
id: EventId.make(`event:${runId}:${disposition}`),
1826-
type: disposition === "open" ? "run.created" : "run.updated",
1827-
threadId,
1828-
runId,
1829-
occurredAt: now,
1830-
payload: {
1831-
id: runId,
1832-
threadId,
1833-
ordinal: 1,
1834-
providerInstanceId: modelSelection.instanceId,
1835-
modelSelection,
1836-
providerThreadId: null,
1837-
userMessageId: MessageId.make("message:delegating"),
1838-
rootNodeId: null,
1839-
activeAttemptId: null,
1840-
status: "completed",
1841-
queuePosition: null,
1842-
requestedAt: now,
1843-
startedAt: now,
1844-
completedAt: now,
1845-
checkpointId: null,
1846-
contextHandoffId: null,
1847-
delegatedCompletion: { disposition, nextGeneration: 1, delivery: null },
1848-
},
1849-
});
1850-
yield* eventSink.write({
1851-
events: [
1852-
run("open"),
1853-
{
1854-
id: yield* idAllocator.allocate.event({ threadId }),
1855-
type: "subagent.updated",
1856-
threadId,
1857-
occurredAt: now,
1858-
payload: {
1859-
id: idAllocator.derive.nodeFromProviderItem({
1860-
driver: CODEX_DRIVER,
1861-
nativeItemId: "delegated-task",
1862-
}),
1863-
threadId,
1864-
runId,
1865-
parentNodeId: idAllocator.derive.rootNode({ runId }),
1866-
origin: "app_owned",
1867-
createdBy: "agent",
1868-
driver: CODEX_DRIVER,
1869-
providerInstanceId: modelSelection.instanceId,
1870-
providerThreadId: null,
1871-
childThreadId: null,
1872-
nativeTaskRef: null,
1873-
prompt: "Review the diff",
1874-
title: "Delegated review",
1875-
model: null,
1876-
status: "running",
1877-
result: null,
1878-
startedAt: now,
1879-
completedAt: null,
1880-
updatedAt: now,
1881-
},
1882-
},
1883-
],
1884-
});
1885-
assert.isTrue(yield* captured!.hasPendingWake!);
1886-
// An interrupted completion wake disposes the cohort, so the result never wakes the thread.
1887-
yield* eventSink.write({ events: [run("disposed")] });
18881855
assert.isFalse(yield* captured!.hasPendingWake!);
18891856

18901857
yield* manager.close(providerSessionId);

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

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
44
import {
55
isOrchestrationV2WorkActive,
66
ModelSelection,
7+
type OrchestrationV2AppThread,
78
OrchestrationV2DomainEvent,
89
OrchestrationV2ProviderSession,
910
type OrchestrationV2ProviderThread,
11+
type OrchestrationV2Run,
1012
OrchestrationV2RuntimeRequest,
13+
type OrchestrationV2Subagent,
1114
ProviderInstanceId,
1215
ProviderSessionId,
1316
ThreadId,
@@ -71,6 +74,30 @@ const WORKING_RUN_STATUSES: ReadonlySet<string> = new Set([
7174
"waiting",
7275
]);
7376

77+
/** Matches planDelegatedCompletionDelivery: true while a delegated task's result still wakes the thread. */
78+
export const delegatedTaskWillWake = (records: {
79+
readonly thread: Pick<OrchestrationV2AppThread, "archivedAt" | "deletedAt">;
80+
readonly runs: ReadonlyArray<Pick<OrchestrationV2Run, "id" | "delegatedCompletion">>;
81+
readonly subagents: ReadonlyArray<
82+
Pick<OrchestrationV2Subagent, "origin" | "status" | "runId" | "completionDelivery">
83+
>;
84+
}): boolean =>
85+
records.thread.archivedAt === null &&
86+
records.thread.deletedAt === null &&
87+
records.subagents.some((task) => {
88+
const run = records.runs.find((candidate) => candidate.id === task.runId);
89+
const delivery = task.completionDelivery?.state;
90+
return (
91+
task.origin === "app_owned" &&
92+
isOrchestrationV2WorkActive(task.status) &&
93+
run !== undefined &&
94+
(run.delegatedCompletion?.disposition ?? "open") === "open" &&
95+
delivery !== "acknowledged" &&
96+
delivery !== "delivered" &&
97+
delivery !== "disposed"
98+
);
99+
});
100+
74101
export const ProviderSessionReleaseReason = Schema.Literals([
75102
"idle_timeout",
76103
"runtime_error",
@@ -499,29 +526,10 @@ export const layerWithOptions = (
499526
});
500527
yield* mcpSessions.set({
501528
...credential.config,
502-
// Matches planDelegatedCompletionDelivery: only tasks whose result still wakes this thread.
503529
hasPendingWake: projectionStore
504530
.getThreadRecords(threadId, ["runs", "subagents"])
505531
.pipe(
506-
Effect.map(
507-
({ thread, runs, subagents }) =>
508-
thread.archivedAt === null &&
509-
thread.deletedAt === null &&
510-
subagents.some((task) => {
511-
const run = runs.find((candidate) => candidate.id === task.runId);
512-
const delivery = task.completionDelivery?.state;
513-
return (
514-
task.origin === "app_owned" &&
515-
isOrchestrationV2WorkActive(task.status) &&
516-
run !== undefined &&
517-
(run.delegatedCompletion === undefined ||
518-
run.delegatedCompletion.disposition === "open") &&
519-
delivery !== "acknowledged" &&
520-
delivery !== "delivered" &&
521-
delivery !== "disposed"
522-
);
523-
}),
524-
),
532+
Effect.map(delegatedTaskWillWake),
525533
Effect.orElseSucceed(() => false),
526534
),
527535
});

0 commit comments

Comments
 (0)