Skip to content

Commit 7ac31ee

Browse files
fix(server): preserve Claude transcript order during wake cleanup
1 parent f2313e9 commit 7ac31ee

2 files changed

Lines changed: 155 additions & 12 deletions

File tree

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3768,24 +3768,18 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
37683768
const emitProviderEvent = Effect.fnUntraced(function* (
37693769
event: ProviderAdapter.ProviderAdapterV2Event,
37703770
) {
3771-
// Persist child artifacts in session order, even while their original
3772-
// run still subscribes. A queued running update must not overwrite a
3773-
// terminal outcome persisted by a dropped wake.
3771+
// Live turn items share the run lane so text and cards allocate display
3772+
// positions in emission order. Other child lifecycle rows stay ordered
3773+
// with terminal outcomes persisted by a dropped wake.
37743774
const artifactThreadId =
37753775
event.type === "subagent.updated"
37763776
? event.subagent.threadId
37773777
: event.type === "node.updated" &&
37783778
(event.node.kind === "subagent" || event.node.runId === null)
37793779
? event.node.threadId
3780-
: event.type === "turn_item.updated" &&
3781-
(event.turnItem.type === "subagent" ||
3782-
(event.turnItem.runId === null &&
3783-
event.turnItem.type !== "assistant_message" &&
3784-
event.turnItem.type !== "reasoning"))
3785-
? event.turnItem.threadId
3786-
: event.type === "app_thread.created"
3787-
? event.appThread.id
3788-
: undefined;
3780+
: event.type === "app_thread.created"
3781+
? event.appThread.id
3782+
: undefined;
37893783
const sessionThreadId = (yield* sessionEventThread) ?? artifactThreadId;
37903784
yield* Queue.offer(
37913785
events,

‎apps/server/src/orchestration-v2/ClaudeAutomaticDelivery.integration.test.ts‎

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,155 @@ it.effect.each(deliveries)(
482482
),
483483
);
484484

485+
it.effect("persists root assistant text before the Agent card it introduces", () =>
486+
Effect.scoped(
487+
Effect.gen(function* () {
488+
const cwd = yield* checkpointWorkspace("claude-transcript-order");
489+
const sdkMessages = yield* Queue.unbounded<SDKMessage>();
490+
const adapter = yield* ClaudeAdapterV2.makeClaudeAdapterV2({
491+
instanceId: modelSelection.instanceId,
492+
settings,
493+
environment: {},
494+
attachmentsDir: cwd,
495+
fileSystem: yield* FileSystem.FileSystem,
496+
path: yield* Path.Path,
497+
crypto: yield* Crypto.Crypto,
498+
idAllocator: yield* IdAllocator.IdAllocatorV2,
499+
queryRunner: {
500+
allocateSessionId: Effect.succeed(sessionId),
501+
open: () =>
502+
Effect.succeed({
503+
messages: Stream.fromQueue(sdkMessages),
504+
offer: () => Effect.void,
505+
setModel: () => Effect.void,
506+
applyFlagSettings: () => Effect.void,
507+
setPermissionMode: () => Effect.void,
508+
backgroundTasks: () => Effect.succeed(false),
509+
stopTask: () => Effect.void,
510+
interrupt: Effect.void,
511+
close: Effect.void,
512+
}),
513+
forkSession: () => Effect.die("unused"),
514+
subagentLaunchToolUseId: () => Effect.succeed(null),
515+
assertComplete: Effect.void,
516+
},
517+
});
518+
yield* Effect.gen(function* () {
519+
const orchestrator = yield* Orchestrator.OrchestratorV2;
520+
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
521+
const threadId = ThreadId.make("thread:transcript-order");
522+
const watch = (predicate: (event: OrchestrationV2DomainEvent) => boolean) =>
523+
orchestrator.streamDomainEvents.pipe(
524+
Stream.filter(predicate),
525+
Stream.take(1),
526+
Stream.runDrain,
527+
Effect.forkScoped,
528+
);
529+
yield* orchestrator.dispatch({
530+
type: "thread.create",
531+
commandId: CommandId.make("create-transcript-order"),
532+
threadId,
533+
projectId: ProjectId.make("project:transcript-order"),
534+
title: "Transcript order",
535+
modelSelection,
536+
runtimeMode: "full-access",
537+
interactionMode: "default",
538+
branch: null,
539+
worktreePath: cwd,
540+
createdBy: "user",
541+
creationSource: "web",
542+
});
543+
const running = yield* watch(
544+
(event) => event.type === "provider-turn.updated" && event.payload.status === "running",
545+
);
546+
yield* orchestrator.dispatch({
547+
type: "message.dispatch",
548+
commandId: CommandId.make("start-transcript-order"),
549+
threadId,
550+
messageId: MessageId.make("start-transcript-order"),
551+
text: "Start background research.",
552+
attachments: [],
553+
dispatchMode: { type: "start_immediately" },
554+
createdBy: "user",
555+
creationSource: "web",
556+
});
557+
yield* worker.drain();
558+
yield* Fiber.join(running);
559+
const settled = yield* watch(
560+
(event) => event.type === "run.updated" && event.payload.status === "waiting",
561+
);
562+
const introduction = "I will start a research agent.";
563+
yield* Queue.offerAll(sdkMessages, [
564+
frame({
565+
type: "assistant",
566+
uuid: "research-introduction",
567+
session_id: sessionId,
568+
parent_tool_use_id: null,
569+
message: {
570+
id: "research-introduction",
571+
role: "assistant",
572+
type: "message",
573+
model: modelSelection.model,
574+
content: [
575+
{ type: "text", text: introduction },
576+
{
577+
type: "tool_use",
578+
id: "research-launch",
579+
name: "Agent",
580+
input: {
581+
description: "Research",
582+
prompt: "Research the task.",
583+
subagent_type: "general-purpose",
584+
run_in_background: true,
585+
},
586+
},
587+
],
588+
stop_reason: "tool_use",
589+
stop_sequence: null,
590+
usage: { input_tokens: 1, output_tokens: 1 },
591+
},
592+
}),
593+
frame({
594+
type: "system",
595+
subtype: "task_started",
596+
task_id: "research",
597+
tool_use_id: "research-launch",
598+
description: "Research",
599+
task_type: "local_agent",
600+
subagent_type: "general-purpose",
601+
is_backgrounded: true,
602+
uuid: "research-started",
603+
session_id: sessionId,
604+
}),
605+
result("research-root-finished", false),
606+
]);
607+
yield* Fiber.join(settled);
608+
yield* worker.drain();
609+
const projection = yield* orchestrator.getThreadProjection(threadId);
610+
const text = projection.turnItems.find(
611+
(item) => item.type === "assistant_message" && item.text === introduction,
612+
);
613+
const card = projection.turnItems.find((item) => item.type === "subagent");
614+
assert.ok(text, "the root introduction is persisted");
615+
assert.ok(card, "the Agent card is persisted");
616+
assert.isBelow(text.ordinal, card.ordinal, "the introduction precedes its Agent card");
617+
}).pipe(
618+
Effect.provide(
619+
ProviderReplayHarness.layerWithRegistry(
620+
{ name: "claude-transcript-order" },
621+
ProviderAdapterRegistry.layerSingle(adapter),
622+
{ runEffectWorker: false },
623+
),
624+
),
625+
);
626+
}).pipe(
627+
Effect.provide(
628+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
629+
),
630+
),
631+
),
632+
);
633+
485634
it.effect("persists a resumed subagent outcome when its buffered wake is dropped", () =>
486635
Effect.scoped(
487636
Effect.gen(function* () {

0 commit comments

Comments
 (0)