Repository navigation
fix(server): a command id reused for two threads at once no longer succeeds for both #18385
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
maria-rcks
merged 6 commits into
pingdotgg:main
from
saphid:t3code/sweep/receipt-collision
Nov 6, 2026
+241
−45
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
074e067
fix(server): a command id reused for two threads at once no longer su…
saphid d8cabdb
fix: integrate current main while preserving PR 18385 behavior
saphid 776e537
Merge commit 'e5909e53c32a9d5b51caf6352d1f17fd73e86db7' into HEAD
saphid 91f9d97
fix(server): preserve current receipt thread matching after commit
saphid ce5c2c1
fix(server): validate concurrent no-op command receipts
maria-rcks e12d2c9
Merge remote-tracking branch 'upstream/main' into t3code/sweep/receip…
saphid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
168 changes: 168 additions & 0 deletions
168
apps/server/src/orchestration-v2/Orchestrator.receipt-race.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| import { vi } from "vite-plus/test"; | ||
| import { assert, it } from "@effect/vitest"; | ||
| import { | ||
| CommandId, | ||
| ProjectId, | ||
| ProviderDriverKind, | ||
| ProviderInstanceId, | ||
| ThreadId, | ||
| } from "@t3tools/contracts"; | ||
| import * as Cause from "effect/Cause"; | ||
| import * as Deferred from "effect/Deferred"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as Exit from "effect/Exit"; | ||
| import * as Layer from "effect/Layer"; | ||
| import * as Ref from "effect/Ref"; | ||
| import * as SqlitePersistence from "../persistence/Sqlite.ts"; | ||
| import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts"; | ||
| import * as Orchestrator from "./Orchestrator.ts"; | ||
| import * as ProjectionStore from "./ProjectionStore.ts"; | ||
| import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts"; | ||
| import * as ProviderReplayHarness from "./testkit/ProviderReplayHarness.ts"; | ||
| import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter"; | ||
|
|
||
| const instanceId = ProviderInstanceId.make("codex"); | ||
| const modelSelection = { instanceId, model: "gpt-5.1-codex" }; | ||
| // Holds model-selection planning until both racing commands have read no receipt. | ||
| let capabilityGate: Effect.Effect<void> | undefined; | ||
| const adapter = { | ||
| instanceId, | ||
| driver: ProviderDriverKind.make("codex"), | ||
| getCapabilities: () => | ||
| (capabilityGate ?? Effect.void).pipe(Effect.as(CodexProviderCapabilitiesV2)), | ||
| planSelectionTransition: () => Effect.succeed({ type: "apply_on_next_turn" as const }), | ||
| openSession: () => Effect.die("No provider process needed for metadata controls"), | ||
| } as ProviderAdapter.ProviderAdapterV2["Service"]; | ||
| const layerDatabase = SqlitePersistence.layerMemory; | ||
| const layerTest = Layer.mergeAll( | ||
| layerDatabase, | ||
| ProjectionStore.layer.pipe(Layer.provide(layerDatabase)), | ||
| ProviderReplayHarness.layerWithRegistry( | ||
| { name: "receipt-race" }, | ||
| ProviderAdapterRegistry.layerFromAdapters([adapter]), | ||
| { databaseLayer: layerDatabase, runEffectWorker: false }, | ||
| ), | ||
| ); | ||
|
|
||
| it.effect("a command id reused concurrently for two threads succeeds for only one", () => | ||
| Effect.gen(function* () { | ||
| const orchestrator = yield* Orchestrator.OrchestratorV2; | ||
| const projections = yield* ProjectionStore.ProjectionStoreV2; | ||
| const threadIds = [ThreadId.make("thread:race-a"), ThreadId.make("thread:race-b")]; | ||
| for (const threadId of threadIds) { | ||
| yield* orchestrator.dispatch({ | ||
| type: "thread.create", | ||
| commandId: CommandId.make(`create:${threadId}`), | ||
| threadId, | ||
| projectId: ProjectId.make("project:receipt-race"), | ||
| title: threadId, | ||
| modelSelection, | ||
| runtimeMode: "full-access", | ||
| interactionMode: "default", | ||
| branch: null, | ||
| worktreePath: null, | ||
| createdBy: "user", | ||
| creationSource: "web", | ||
| }); | ||
| } | ||
|
|
||
| const arrived = yield* Ref.make(0); | ||
| const bothArrived = yield* Deferred.make<void>(); | ||
| capabilityGate = Effect.gen(function* () { | ||
| if ((yield* Ref.updateAndGet(arrived, (count) => count + 1)) === 2) { | ||
| yield* Deferred.succeed(bothArrived, undefined); | ||
| } | ||
| yield* Deferred.await(bothArrived); | ||
| }); | ||
| const setModel = (threadId: ThreadId) => | ||
| orchestrator.dispatch({ | ||
| type: "thread.model-selection.set", | ||
| commandId: CommandId.make("command:shared"), | ||
| threadId, | ||
| modelSelection: { ...modelSelection, model: "gpt-6" }, | ||
| }); | ||
| const exits = yield* Effect.forEach(threadIds, (threadId) => Effect.exit(setModel(threadId)), { | ||
| concurrency: "unbounded", | ||
| }).pipe(Effect.ensuring(Effect.sync(() => (capabilityGate = undefined)))); | ||
|
|
||
| const winner = exits.findIndex(Exit.isSuccess); | ||
| const loser = exits.findIndex(Exit.isFailure); | ||
| assert.isAtLeast(winner, 0); | ||
| assert.isAtLeast(loser, 0); | ||
| const winnerExit = exits[winner]!; | ||
| const loserExit = exits[loser]!; | ||
| assert.isTrue(Exit.isSuccess(winnerExit) && Exit.isFailure(loserExit)); | ||
| if (Exit.isFailure(loserExit)) { | ||
| const error = Cause.findErrorOption(loserExit.cause); | ||
| assert.isTrue(error._tag === "Some"); | ||
| if (error._tag === "Some") { | ||
| assert.equal(error.value._tag, "OrchestratorCommandIdConflictError"); | ||
| if (error.value._tag === "OrchestratorCommandIdConflictError") { | ||
| assert.equal(error.value.receiptThreadId, threadIds[winner]); | ||
| assert.equal(error.value.commandThreadId, threadIds[loser]); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const models = yield* Effect.forEach(threadIds, (threadId) => | ||
| projections.getThreadShell(threadId).pipe(Effect.map((shell) => shell?.modelSelection.model)), | ||
| ); | ||
| assert.equal(models[winner], "gpt-6"); | ||
| assert.equal(models[loser], "gpt-5.1-codex"); | ||
|
|
||
| const noOpArrived = yield* Ref.make(0); | ||
| const bothNoOpsArrived = yield* Deferred.make<void>(); | ||
| const getThreadRecords = projections.getThreadRecords; | ||
| using _records = vi.spyOn(projections, "getThreadRecords").mockImplementation((...args) => | ||
| Effect.gen(function* () { | ||
| if ((yield* Ref.updateAndGet(noOpArrived, (count) => count + 1)) === 2) { | ||
| yield* Deferred.succeed(bothNoOpsArrived, undefined); | ||
| } | ||
| yield* Deferred.await(bothNoOpsArrived); | ||
| return yield* getThreadRecords(...args); | ||
| }), | ||
| ); | ||
| const closeShare = (threadId: ThreadId) => | ||
| orchestrator.dispatch({ | ||
| type: "thread.guest-share.close", | ||
| commandId: CommandId.make("command:shared-noop"), | ||
| threadId, | ||
| }); | ||
| const noOpExits = yield* Effect.forEach( | ||
| threadIds, | ||
| (threadId) => Effect.exit(closeShare(threadId)), | ||
| { | ||
| concurrency: "unbounded", | ||
| }, | ||
| ); | ||
| const noOpWinner = noOpExits.findIndex(Exit.isSuccess); | ||
| const noOpLoser = noOpExits.findIndex(Exit.isFailure); | ||
| assert.isAtLeast(noOpWinner, 0); | ||
| assert.isAtLeast(noOpLoser, 0); | ||
| const noOpFailure = noOpExits[noOpLoser]!; | ||
| if (Exit.isFailure(noOpFailure)) { | ||
| const error = Cause.findErrorOption(noOpFailure.cause); | ||
| assert.isTrue(error._tag === "Some"); | ||
| if (error._tag === "Some") { | ||
| assert.equal(error.value._tag, "OrchestratorCommandIdConflictError"); | ||
| } | ||
| } | ||
| const noOpReplay = yield* closeShare(threadIds[noOpWinner]!); | ||
| const noOpSuccess = noOpExits[noOpWinner]!; | ||
| if (Exit.isSuccess(noOpSuccess)) { | ||
| assert.equal(noOpReplay.sequence, noOpSuccess.value.sequence); | ||
| assert.deepEqual(noOpReplay.storedEvents, []); | ||
| } | ||
|
|
||
| // The winner's own thread still replays its receipt. | ||
| const replay = yield* setModel(threadIds[winner]!); | ||
| if (Exit.isSuccess(winnerExit)) { | ||
| assert.equal(replay.sequence, winnerExit.value.sequence); | ||
| assert.isAbove(replay.storedEvents.length, 0); | ||
| assert.deepEqual( | ||
| replay.storedEvents.map((stored) => stored.sequence), | ||
| winnerExit.value.storedEvents.map((stored) => stored.sequence), | ||
| ); | ||
| } | ||
| }).pipe(Effect.provide(layerTest)), | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
Repository: pingdotgg/t3code
Length of output: 5281
Script executed:
Repository: pingdotgg/t3code
Length of output: 45670
Script executed:
Repository: pingdotgg/t3code
Length of output: 20503
Script executed:
Repository: pingdotgg/t3code
Length of output: 12008
Script executed:
Repository: pingdotgg/t3code
Length of output: 8590
Script executed:
sed -n '1,150p' apps/server/src/orchestration-v2/UserFacingErrors.tsRepository: pingdotgg/t3code
Length of output: 7068
Check thread ownership before replaying rejection details.
When the zero-event path loses
insertIfAbsent,replayCommandReceiptreturns the storedreceipt.errorbefore checkingreceipt.threadId. A rejected receipt from another thread can therefore expose that thread's rejection detail through the websocket response and returnOrchestratorCommandPreviouslyRejectedErrorinstead ofOrchestratorCommandIdConflictError. Check thread ownership first. Preserve rejection details only for same-thread replays.Suggested fix
) { + const dispatchThreadId = commandThreadId(command); + if (receipt.threadId !== dispatchThreadId) { + return yield* new OrchestratorCommandIdConflictError({ + commandId: command.commandId, + commandType: command.type, + receiptThreadId: receipt.threadId, + commandThreadId: dispatchThreadId, + }); + } if (receipt.status === "rejected") { return yield* new OrchestratorCommandPreviouslyRejectedError({ commandId: command.commandId, commandType: command.type, detail: receipt.error ?? "Previously rejected.", }); } - const dispatchThreadId = commandThreadId(command); - // Receipts only prove execution for the thread they were recorded against. - if (receipt.threadId !== dispatchThreadId) { - return yield* new OrchestratorCommandIdConflictError({ - commandId: command.commandId, - commandType: command.type, - receiptThreadId: receipt.threadId, - commandThreadId: dispatchThreadId, - }); - }