@devries-cameron20 / t3code Mirror · read-only / commits / 0804c60a9c
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 22, 12:00 UTC

fix(server): reject busy merge-back targets before blocking their queue (#23554)

Afonso Ferreira committed Oct 30, 2026 at 20:52 UTC 0804c60a9cd0a120092a565b5c422a19fc6c5612
2 files changed +101 -2
apps/server/src/orchestration-v2/Orchestrator.ts
+12 -1
@@ -4340,7 +4340,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
4340 // The source is unlocked; validate the modes copied here, not an earlier read.
4341 yield* refuseAboveDispatchModeLimit(command, command.sourceThreadId, sourceProjection.thread);
4342 const targetProjection = yield* projectionStore
4343 - .getThreadRecords(command.targetThreadId, ["contextTransfers"])
4343 + .getThreadRecords(command.targetThreadId, ["runs", "contextTransfers"])
4344 .pipe(
4345 Effect.mapError(
4346 (cause) =>
@@ -4392,6 +4392,17 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
4392 });
4393 }
4394
4395 + // Queued runs cannot consume merge-back context. Refuse under the target's
4396 + // command lock before a pending transfer can block its ordinary messages.
4397 + if (targetProjection.runs.some((run) => isBlockingRun(run) || run.status === "queued")) {
4398 + return yield* new OrchestratorDispatchError({
4399 + commandId: command.commandId,
4400 + commandType: command.type,
4401 + cause:
4402 + "Finish or stop the target thread's active work and clear its queue before merging back.",
4403 + });
4404 + }
4405 +
4406 const sourceProviderThread = providerThreadForRun(sourceProjection, sourceRun);
4407 const now = command.createdAt ?? (yield* DateTime.now);
4408 const emitEvent = emit(events, command);
apps/server/src/orchestration-v2/runtimeLayer.test.ts
+89 -1
@@ -1726,7 +1726,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive", (it) => {
1726 }),
1727 );
1728
1729 - it.effect("merges an explicit provider-finished run while checkpoint capture is pending", () =>
1729 + it.effect("refuses busy merge-back targets without blocking the queue", () =>
1730 Effect.gen(function* () {
1731 const orchestrator = yield* Orchestrator.OrchestratorV2;
1732 const eventSink = yield* EventSink.EventSinkV2;
@@ -1741,6 +1741,13 @@ it.layer(layerTest)("OrchestrationV2LayerLive", (it) => {
1741 );
1742 const forkTransferId = ContextTransferId.make("runtime-layer-waiting-merge-fork-transfer");
1743
1744 + yield* seedProject({
1745 + projectId,
1746 + title: "Merge-back project",
1747 + workspaceRoot: process.cwd(),
1748 + defaultModelSelection: modelSelection,
1749 + createdAt: DateTime.formatIso(now),
1750 + });
1751 yield* orchestrator.dispatch({
1752 type: "thread.create",
1753 createdBy: "user",
@@ -1868,6 +1875,87 @@ it.layer(layerTest)("OrchestrationV2LayerLive", (it) => {
1875 ],
1876 });
1877
1878 + yield* orchestrator.dispatch({
1879 + type: "message.dispatch",
1880 + createdBy: "user",
1881 + creationSource: "web",
1882 + commandId: CommandId.make("runtime-layer-merge-target-start"),
1883 + threadId: targetThreadId,
1884 + messageId: MessageId.make("runtime-layer-merge-target-message"),
1885 + text: "Keep working in the parent.",
1886 + attachments: [],
1887 + dispatchMode: { type: "start_immediately" },
1888 + });
1889 + const activeTarget = yield* orchestrator.getThreadProjection(targetThreadId);
1890 + const targetRun = activeTarget.runs[0]!;
1891 + for (const status of ["preparing", "starting", "waiting", "queued", "running"] as const) {
1892 + yield* eventSink.write({
1893 + commandId: CommandId.make(`runtime-layer-merge-target-${status}`),
1894 + events: [
1895 + {
1896 + id: EventId.make(`runtime-layer-merge-target-${status}-event`),
1897 + type: "run.updated",
1898 + threadId: targetThreadId,
1899 + runId: targetRun.id,
1900 + occurredAt: now,
1901 + payload: { ...targetRun, status },
1902 + },
1903 + ],
1904 + });
1905 + const error = yield* orchestrator
1906 + .dispatch({
1907 + type: "thread.merge_back",
1908 + createdBy: "user",
1909 + creationSource: "mobile",
1910 + commandId: CommandId.make(`runtime-layer-merge-refused-${status}`),
1911 + sourceThreadId,
1912 + targetThreadId,
1913 + sourcePoint: { type: "run", runId: sourceRunId },
1914 + })
1915 + .pipe(Effect.flip);
1916 + assert.instanceOf(error, Orchestrator.OrchestratorDispatchError);
1917 + assert.equal(
1918 + error.cause,
1919 + "Finish or stop the target thread's active work and clear its queue before merging back.",
1920 + );
1921 + const refusedTarget = yield* orchestrator.getThreadProjection(targetThreadId);
1922 + assert.deepEqual(refusedTarget.contextTransfers, activeTarget.contextTransfers);
1923 + assert.equal(refusedTarget.runs[0]?.status, status);
1924 + }
1925 + yield* orchestrator.dispatch({
1926 + type: "message.dispatch",
1927 + createdBy: "user",
1928 + creationSource: "web",
1929 + commandId: CommandId.make("runtime-layer-merge-target-queue"),
1930 + threadId: targetThreadId,
1931 + messageId: MessageId.make("runtime-layer-merge-target-queued-message"),
1932 + text: "Run this after the current turn.",
1933 + attachments: [],
1934 + dispatchMode: { type: "queue_after_active" },
1935 + });
1936 + const queuedTarget = yield* orchestrator.getThreadProjection(targetThreadId);
1937 + const queuedRun = queuedTarget.runs.find((run) => run.status === "queued");
1938 + assert.isDefined(queuedRun);
1939 + yield* orchestrator.dispatch({
1940 + type: "queued-run.cancel",
1941 + commandId: CommandId.make("runtime-layer-merge-target-cancel-queue"),
1942 + threadId: targetThreadId,
1943 + runId: queuedRun.id,
1944 + });
1945 + yield* eventSink.write({
1946 + commandId: CommandId.make("runtime-layer-merge-target-complete"),
1947 + events: [
1948 + {
1949 + id: EventId.make("runtime-layer-merge-target-completed-event"),
1950 + type: "run.updated",
1951 + threadId: targetThreadId,
1952 + runId: targetRun.id,
1953 + occurredAt: now,
1954 + payload: { ...targetRun, status: "completed", completedAt: now },
1955 + },
1956 + ],
1957 + });
1958 +
1959 yield* orchestrator.dispatch({
1960 type: "thread.merge_back",
1961 createdBy: "user",