@devries-cameron20 / t3code Mirror · read-only / commits / 1c18c16e93
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 14:45 UTC

perf(server): ACP subagent replies no longer rewrite the database on every chunk (#29422)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Mokhles elheni committed Nov 14, 2026 at 16:00 UTC 1c18c16e931de582677a5efdf77b76d7be0fe3cb
4 files changed +227 -14
apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts
+122 -4
@@ -3578,14 +3578,14 @@ describe("AcpAdapterV2", () => {
3578 "cognition.ai/subagent_context": { parentAgentId: "child-a" },
3579 },
3580 },
3581 - {
3582 - sessionUpdate: "agent_message_chunk",
3583 - content: { type: "text", text: "ONE" },
3581 + ...["O", "N", "E"].map((text) => ({
3582 + sessionUpdate: "agent_message_chunk" as const,
3583 + content: { type: "text" as const, text },
3584 _meta: {
3585 "cognition.ai/streamingMessageId": "child-result",
3586 "cognition.ai/subagent_context": { parentAgentId: "child-a" },
3587 },
3588 - },
3588 + })),
3589 {
3590 sessionUpdate: "tool_call",
3591 toolCallId: "child-later",
@@ -3738,6 +3738,15 @@ describe("AcpAdapterV2", () => {
3738 (item) => item.threadId === task?.childThreadId && item.type === "assistant_message",
3739 );
3740 assert.equal(new Set(childAnswers.map((item) => item.ordinal)).size, 2);
3741 + // Later chunks coalesce; the subagent's completion flushes the final text.
3742 + assert.deepEqual(
3743 + childAnswers.flatMap((item) =>
3744 + item.type === "assistant_message" && item.text !== "Checking the code."
3745 + ? [item.text]
3746 + : [],
3747 + ),
3748 + ["O", "ONE"],
3749 + );
3750 const parentTools = items.filter(
3751 (item) => item.threadId === threadId && item.type === "dynamic_tool",
3752 );
@@ -3746,6 +3755,115 @@ describe("AcpAdapterV2", () => {
3755 }).pipe(Effect.provide(layerTest)),
3756 );
3757
3758 + it.effect("emits coalesced subagent text before a failed turn's terminal", () =>
3759 + Effect.gen(function* () {
3760 + const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
3761 + const path = yield* Path.Path;
3762 + const mockAgentPath = yield* path.fromFileUrl(
3763 + new URL("../../../scripts/acp-mock-agent.ts", import.meta.url),
3764 + );
3765 + type Runtime = AcpSessionRuntime.AcpSessionRuntime["Service"];
3766 + let handler: Parameters<Runtime["handleSessionUpdate"]>[0] | undefined;
3767 + const instanceId = ProviderInstanceId.make("devin-failed-flush");
3768 + const adapter = yield* makeAcpAdapterV2({
3769 + instanceId,
3770 + selfInvocation: yield* resolveSelfInvocation(),
3771 + flavor: {
3772 + driver: ACP_TEST_DRIVER,
3773 + capabilities: AcpProviderCapabilitiesV2,
3774 + normalizeSessionUpdate: normalizeDevinSessionUpdate,
3775 + normalizeToolCall: normalizeDevinToolCall,
3776 + extractSubagentUpdate: extractDevinSubagentUpdate,
3777 + makeRuntime: makeMockRuntime({
3778 + childProcessSpawner,
3779 + mockAgentPath,
3780 + wrapRuntime: (runtime) => ({
3781 + ...runtime,
3782 + handleSessionUpdate: (next) =>
3783 + Effect.sync(() => {
3784 + handler = next;
3785 + }).pipe(Effect.andThen(runtime.handleSessionUpdate(next))),
3786 + prompt: () =>
3787 + Effect.gen(function* () {
3788 + const updates = [
3789 + {
3790 + sessionUpdate: "tool_call_update",
3791 + toolCallId: "child-a",
3792 + status: "in_progress",
3793 + _meta: {
3794 + "cognition.ai/subagent_started": {
3795 + agentId: "child-a",
3796 + title: "Answer",
3797 + task: "Reply ONE.",
3798 + },
3799 + },
3800 + },
3801 + ...["O", "N", "E"].map((text) => ({
3802 + sessionUpdate: "agent_message_chunk" as const,
3803 + content: { type: "text" as const, text },
3804 + _meta: {
3805 + "cognition.ai/streamingMessageId": "child-result",
3806 + "cognition.ai/subagent_context": { parentAgentId: "child-a" },
3807 + },
3808 + })),
3809 + ] satisfies Array<EffectAcpSchema.SessionUpdate>;
3810 + for (const update of updates)
3811 + yield* handler!({ sessionId: "mock-session-1", update });
3812 + return yield* new EffectAcpErrors.AcpRequestError({
3813 + code: -32603,
3814 + errorMessage: "prompt failed",
3815 + });
3816 + }),
3817 + }),
3818 + }),
3819 + },
3820 + });
3821 + const threadId = ThreadId.make("devin-failed-flush-parent");
3822 + const modelSelection = { instanceId, model: "default" };
3823 + const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
3824 + runtimeMode: "full-access",
3825 + interactionMode: "default",
3826 + cwd: process.cwd(),
3827 + });
3828 + const runtime = yield* adapter.openSession({
3829 + threadId,
3830 + providerSessionId: ProviderSessionId.make("devin-failed-flush-session"),
3831 + modelSelection,
3832 + runtimePolicy,
3833 + });
3834 + const providerThread = yield* runtime.ensureThread({
3835 + threadId,
3836 + modelSelection,
3837 + runtimePolicy,
3838 + });
3839 + yield* runtime.startTurn(
3840 + makeTurnInput({
3841 + threadId,
3842 + providerThread,
3843 + instanceId,
3844 + runtimePolicy,
3845 + now: yield* DateTime.now,
3846 + }),
3847 + );
3848 + const events = Array.from(
3849 + yield* runtime.events.pipe(
3850 + Stream.takeUntil((event) => event.type === "turn.terminal"),
3851 + Stream.runCollect,
3852 + ),
3853 + );
3854 + const terminal = events.at(-1);
3855 + assert.equal(terminal?.type === "turn.terminal" ? terminal.status : undefined, "failed");
3856 + const childText = events.flatMap((event) =>
3857 + event.type === "turn_item.updated" &&
3858 + event.turnItem.type === "assistant_message" &&
3859 + event.turnItem.threadId !== threadId
3860 + ? [event.turnItem.text]
3861 + : [],
3862 + );
3863 + assert.equal(childText.at(-1), "ONE");
3864 + }).pipe(Effect.provide(layerTest), Effect.scoped),
3865 + );
3866 +
3867 it.effect("routes interleaved Copilot subagent output into subagent threads", () =>
3868 Effect.gen(function* () {
3869 const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.test.ts
+36
@@ -2685,6 +2685,42 @@ describe("AcpRegistryAdapterV2", () => {
2685 }).pipe(Effect.provide(layerTest)),
2686 );
2687
2688 + it.effect("writes a child's coalesced text before a restart resets it", () =>
2689 + Effect.gen(function* () {
2690 + const { update, childState, idle, prompt } = claudeFrames;
2691 + const reply = (text: string) =>
2692 + update("child-1", {
2693 + sessionUpdate: "agent_message_chunk",
2694 + messageId: "reply",
2695 + content: { type: "text", text },
2696 + });
2697 + const session = yield* openClaudeReplaySession([
2698 + ...claudeFrames.open,
2699 + ...prompt(1),
2700 + ...claudeChild.slice(0, -1),
2701 + reply("Review "),
2702 + reply("done."),
2703 + // A working grandchild keeps the idle child from publishing (and flushing) its status.
2704 + childState("child-1", "grandchild", "running"),
2705 + childState(CLAUDE_ROOT_SESSION, "child-1", "idle"),
2706 + childState(CLAUDE_ROOT_SESSION, "child-1", "running"),
2707 + idle("end_turn"),
2708 + claudeFrames.closeRequest,
2709 + claudeFrames.closed,
2710 + ]);
2711 + yield* session.runtime.startTurn(session.turnInput(1));
2712 + const events = yield* session.until((event) => event.type === "turn.terminal");
2713 + assert.isTrue(
2714 + events.some(
2715 + (event) =>
2716 + event.type === "message.updated" &&
2717 + event.message.role === "assistant" &&
2718 + event.message.text === "Review done.",
2719 + ),
2720 + );
2721 + }).pipe(Effect.provide(layerTest), Effect.scoped),
2722 + );
2723 +
2724 it.effect("fails active Claude work and releases its children when the process exits", () =>
2725 Effect.gen(function* () {
2726 const session = yield* openClaudeReplaySession([
apps/server/src/orchestration-v2/messageSearchIndex.ts
+1 -1
@@ -18,7 +18,7 @@ export const nonPlaceholderMessageCondition = (sql: SqlClient.SqlClient, row = "
18
19 /**
20 * Exclude attachment placeholders, generated notices and subagent threads. ACP subagents
21 - * rewrite their finished answer on every streamed chunk.
21 + * rewrite their finished answer while it streams.
22 * Migration triggers, backfill, rebuild and scans share this predicate; `row` may be `new`.
23 */
24 export const indexedMessageCondition = (sql: SqlClient.SqlClient, row = "messages") =>
packages/provider-acp/src/server/adapter.ts
+68 -9
@@ -179,6 +179,9 @@ function sendPromptWhenIdle<A>(
179 * This avoids quadratic output; scheduled flushes can run late under host load.
180 */
181 const ACP_TEXT_FLUSH_INTERVAL: Duration.Input = "50 millis";
182 +// Subagent messages are stored as finished, so they skip the server's paragraph
183 +// throttle and every snapshot is persisted. Match that throttle's 400 ms cadence.
184 +const ACP_SUBAGENT_TEXT_FLUSH_INTERVAL: Duration.Input = "400 millis";
185
186 // Output of the running prompt. Session metadata and unknown updates do not reset its timeout.
187 const ACP_PROMPT_ACTIVITY_UPDATES: ReadonlySet<string> = new Set([
@@ -1679,6 +1682,8 @@ interface ActiveAcpSubagent {
1682 childSessionId: string | null;
1683 assistantText: string;
1684 readonly assistantMessages: Map<string, string>;
1685 + // Messages whose latest text waits for a delayed flush.
1686 + readonly pendingAssistantMessages: Set<string>;
1687 readonly childItemOrdinals: Map<string, number>;
1688 nextChildOrdinal: number;
1689 terminalStatusProjected: boolean;
@@ -3097,10 +3102,53 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
3102 ) {
3103 if (text.length === 0 && mode === "append") return;
3104 const nativeItemId = `${subagent.task.nativeTaskRef?.nativeId ?? subagent.task.id}:message:${messageId ?? "result"}`;
3100 - const previous = subagent.assistantMessages.get(nativeItemId) ?? "";
3101 - const messageText = mode === "replace" ? text : `${previous}${text}`;
3105 + const previous = subagent.assistantMessages.get(nativeItemId);
3106 + const messageText = mode === "replace" ? text : `${previous ?? ""}${text}`;
3107 subagent.assistantMessages.set(nativeItemId, messageText);
3108 subagent.assistantText = messageText;
3109 + if (mode === "append" && previous !== undefined) {
3110 + if (subagent.pendingAssistantMessages.has(nativeItemId)) return;
3111 + subagent.pendingAssistantMessages.add(nativeItemId);
3112 + yield* writeSubagentAssistant(subagent, nativeItemId, true).pipe(
3113 + Effect.delay(ACP_SUBAGENT_TEXT_FLUSH_INTERVAL),
3114 + Effect.forkIn(sessionScope),
3115 + );
3116 + return;
3117 + }
3118 + subagent.pendingAssistantMessages.delete(nativeItemId);
3119 + yield* writeSubagentAssistant(subagent, nativeItemId, false);
3120 + });
3121 +
3122 + // Reads the pending set under the permit, so it also waits for a write in progress.
3123 + const flushPendingSubagentAssistant = (subagent: ActiveAcpSubagent) =>
3124 + Effect.suspend(() =>
3125 + Effect.forEach(
3126 + [...subagent.pendingAssistantMessages],
3127 + (nativeItemId) => writeSubagentAssistantUnlocked(subagent, nativeItemId, true),
3128 + { discard: true },
3129 + ),
3130 + );
3131 +
3132 + const flushSubagentAssistant = (subagent: ActiveAcpSubagent) =>
3133 + textEmissionPermit.withPermit(flushPendingSubagentAssistant(subagent));
3134 +
3135 + const writeSubagentAssistant = (
3136 + subagent: ActiveAcpSubagent,
3137 + nativeItemId: string,
3138 + onlyIfPending: boolean,
3139 + ) =>
3140 + textEmissionPermit.withPermit(
3141 + writeSubagentAssistantUnlocked(subagent, nativeItemId, onlyIfPending),
3142 + );
3143 +
3144 + const writeSubagentAssistantUnlocked = Effect.fnUntraced(function* (
3145 + subagent: ActiveAcpSubagent,
3146 + nativeItemId: string,
3147 + onlyIfPending: boolean,
3148 + ) {
3149 + if (onlyIfPending && !subagent.pendingAssistantMessages.delete(nativeItemId)) return;
3150 + const text = subagent.assistantMessages.get(nativeItemId);
3151 + if (text === undefined) return;
3152 const now = yield* DateTime.now;
3153 const ordinal = resolveSubagentChildOrdinal(subagent, nativeItemId);
3154 const artifacts = makeSubagentConversationArtifacts({
@@ -3112,7 +3160,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
3160 providerTurnId: null,
3161 nativeItemRef: { driver, nativeId: nativeItemId, strength: "weak" },
3162 role: "assistant",
3115 - text: subagent.assistantText,
3163 + text,
3164 ordinal,
3165 now,
3166 });
@@ -3355,6 +3403,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
3403 childSessionId: null,
3404 assistantText: "",
3405 assistantMessages: new Map(),
3406 + pendingAssistantMessages: new Set(),
3407 childItemOrdinals: new Map(),
3408 nextChildOrdinal: 101,
3409 terminalStatusProjected: false,
@@ -3457,6 +3506,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
3506 );
3507 }
3508
3509 + yield* flushSubagentAssistant(subagent);
3510 // A failed child's result explains the failure; it is not the child's reply.
3511 if (
3512 taskStatus !== "running" &&
@@ -3569,20 +3619,22 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
3619 return child;
3620 };
3621
3572 - const setNativeChildState = (
3622 + const setNativeChildState = Effect.fnUntraced(function* (
3623 child: AcpNativeChildSession,
3624 status: AcpNativeChildSession["status"],
3625 result: string | null,
3576 - ) => {
3626 + ) {
3627 if (child.status !== "running" && status === "running" && child.subagent !== undefined) {
3628 + yield* flushSubagentAssistant(child.subagent);
3629 // A new execution replaces its result; live ownership transfers keep their progress.
3630 child.subagent.task = { ...child.subagent.task, result: null };
3631 child.subagent.assistantText = "";
3632 child.subagent.assistantMessages.clear();
3633 + child.subagent.pendingAssistantMessages.clear();
3634 }
3635 child.status = status;
3636 child.result = result;
3585 - };
3637 + });
3638
3639 const nativeChildOwner = (sessionId: string): AcpNativeChildSession | undefined => {
3640 const visited = new Set<string>();
@@ -3657,7 +3709,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
3709 child.delegationId !== subagent.task.nativeTaskRef?.nativeId &&
3710 !subagent.childItemOrdinals.has(`${sessionId}:prompt:${child.delegationId}`);
3711 if (newDelegation && child.status !== "running")
3660 - setNativeChildState(child, "running", null);
3712 + yield* setNativeChildState(child, "running", null);
3713 const hasNativeWork = child.status === "running" || hasWorkingDescendant(sessionId);
3714 if (
3715 (hasNativeWork && acpSubagentStatusIsTerminal(subagent.task.status)) ||
@@ -3803,7 +3855,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
3855 const child = nativeChild(event.childSessionId);
3856 child.parentSessionId = event.parentSessionId ?? child.parentSessionId;
3857 if (event.type === "state") {
3806 - setNativeChildState(child, event.status, event.result);
3858 + yield* setNativeChildState(child, event.status, event.result);
3859 } else {
3860 child.delegationId = event.delegationId;
3861 child.prompt = event.prompt;
@@ -3832,7 +3884,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
3884 const child = nativeChild(notification.sessionId);
3885 const unfinishedMessage =
3886 update.state === "idle" ? acpUnfinishedStopMessage(update.stopReason) : undefined;
3835 - setNativeChildState(
3887 + yield* setNativeChildState(
3888 child,
3889 update.state !== "idle"
3890 ? "running"
@@ -6152,6 +6204,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
6204 status: OrchestrationV2Subagent["status"],
6205 resultOverride?: string | null,
6206 ) {
6207 + yield* flushSubagentAssistant(subagent);
6208 yield* mutateCarryoverSubagentStatus(subagent, status, resultOverride);
6209 const now = subagent.task.updatedAt;
6210 const nativeTaskId = subagent.task.nativeTaskRef?.nativeId ?? subagent.task.id;
@@ -7879,6 +7932,12 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
7932 });
7933 }
7934 yield* closeTextStreams(context);
7935 + // Ingestion may stop at turn.terminal, before a delayed flush would run.
7936 + yield* textEmissionPermit.withPermit(
7937 + Effect.forEach(context.subagents.values(), flushPendingSubagentAssistant, {
7938 + discard: true,
7939 + }),
7940 + );
7941 if (settledStatus !== "failed") {
7942 yield* emitProviderRetry(context, settledStatus);
7943 }