Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(server): fork Codex threads at native turn boundary #9169

ClosedYusuf007R(GitHub profile, opens in a new tab)wants to merge 250+ commits intot3code/codex-turn-mapping fromrefs/pull/9169/headopened View on GitHub

Codex 0.151 makes durable threads paginated by default, but T3's retry/edit fork path still truncates a completed native fork with the deprecated thread/rollback endpoint. Codex rejects that endpoint for paginated threads, leaving the provider-turn start failed and the T3 run stuck in its starting state.

What Changed

  • Resolve the selected durable provider turn to its native Codex turn ID before creating the fork.
  • Pass that ID as lastTurnId in the original thread/fork request so Codex omits later turns atomically.
  • Skip the follow-up rollback whenever the native boundary is available.
  • Preserve the existing rollback-count path only as a compatibility fallback for historical projected turns without a native turn reference.
  • Cover the native-boundary mapping with a focused adapter test.

Why

The existing flow first creates a complete native fork. If the user selected an earlier turn, T3 counts the terminal turns after that boundary and calls thread/rollback on the newly created thread. Forking at the current head works because the count is zero; retrying or editing an earlier turn reaches the rollback call.

Paginated Codex threads reject that second request with paginated threads do not support thread/rollback. Although Codex has already created the fork, T3 receives an error and fails the durable provider-turn.start effect, so the run appears unresponsive.

Codex already supports the intended operation directly: thread/fork.lastTurnId creates a fork through the specified turn, inclusive. Supplying the known native turn ID removes the incompatible second request without adding recovery, persistence, or provider-version detection machinery.

UI Changes

None. This changes only the Codex provider adapter's native fork request.

Validation

  • vp test run apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts
  • vp run --filter t3 typecheck
  • vp fmt apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts --check

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • Screenshots are not applicable because there are no UI changes
  • Video is not applicable because there are no animation or interaction changes

Implemented with GPT-5.6 in the Codex desktop harness.


Note

Medium Risk Changes native fork semantics for retry/edit-at-earlier-turn flows; incorrect boundary mapping could fork with wrong history, though fallback rollback preserves legacy behavior when native ids are missing.

Overview Codex same-provider forks now cut history at the selected turn in one thread/fork call instead of forking at the head and calling thread/rollback, which paginated Codex threads reject.

The adapter adds resolveCodexForkBoundary, which maps the orchestrator’s source provider turn to its native Codex turn id when nativeTurnRef is present. forkThread includes that id as lastTurnId on thread/fork and sets rollback to zero so the post-fork thread/rollback path is skipped. When there is no native turn reference, behavior stays the same: fork latest state and roll back by the count of later terminal provider turns.

Unit tests cover native-boundary vs identityless fallback. Replay fixtures and orchestration docs were updated to expect lastTurnId and no rollback in the prior-turn fork scenario.

Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 8d034c858ea6a9d63cc4121fb14b9d41b89d0a2a. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).

Note

Fork Codex threads at native turn boundary instead of rollback

  • Adds resolveCodexForkBoundary to CodexAdapterV2, which returns the source provider turn's native turn id as the fork boundary when available and resets rollback count to zero
  • Updates the forkThread handler to include the resolved native turn id in the native fork request and skip fork-local rollback in that case; turns without native identity still use the existing latest-state fork plus rollback fallback
  • Updates replay fixtures in thread_fork_native, thread_fork_native_prior_turn, thread_merge_back_continue, and thread_merge_back_siblings to expect native boundary fork requests and removes explicit rollback exchanges where applicable
  • Behavioral Change: Codex forks with a native source turn id now fork directly at that turn; forks without native identity are unaffected and retain rollback fallback
Macroscope summarized 8d034c8.
    1. chatgpt-codex-connector[bot](GitHub profile, opens in a new tab)commented

      Codex Review Summary

      This comment shows the latest Codex review activity on this pull request.

      ReviewStatusCommitReview trigger
      📝 Code Review✅ Completed 2026-09-04T15:39:53.412435Z2822d10Draft marked ready
      ℹ️ About Codex in GitHub

      Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

      • Open a pull request for review
      • Mark a draft as ready
      • Comment "@codex review" or "@codex security review".

      Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

    2. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched

    3. 💡 Codex Review

      Here are some automated review suggestions for this pull request.

      Reviewed commit: d577d2ca43

      ℹ️ About Codex in GitHub

      Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

      • Open a pull request for review
      • Mark a draft as ready
      • Comment "@codex review".

      If Codex has suggestions, it will comment; otherwise it will react with 👍.

      Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

      @@ -5177,7 +5197,11 @@
      51775197 forkThread: (threadInput) =>
      51785198 Effect.gen(function* () {
      51795199 const threadId = yield* getNativeThreadId(threadInput.sourceProviderThread);
      5200+ const forkBoundary = yield* resolveCodexForkBoundary(threadInput);
      51805201 const response = yield* ensureInitialized.pipe(
      51815202 Effect.andThen(
      51825203 client.request("thread/fork", {
      51835204 threadId,
      5205+ ...(forkBoundary.lastTurnId === undefined
      5206+ ? {}
      5207+ : { lastTurnId: forkBoundary.lastTurnId }),

      P1 Badge Update the fork replay fixture for lastTurnId

      When the source turn has a native reference, this now adds lastTurnId to every thread/fork request. The replay harness compares normalized outbound frames exactly and does not remove this field, while testkit/fixtures/thread_fork_native_prior_turn/codex_transcript.ndjson still expects a fork containing only threadId followed by thread/rollback; consequently the focused ThreadFork.integration.test.ts scenario fails before validating the new behavior. Update that transcript to expect the native boundary and remove the obsolete rollback exchange.

      AGENTS.md reference: AGENTS.md:L109-L109

      Useful? React with 👍 / 👎.

    4. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      Approvability

      Verdict: Not approved

      Macroscope's review found this PR not approvable — This is a focused Codex server fix that changes existing fork behavior by using native turn boundaries and retaining a compatibility fallback. Unresolved substantive review comments concern replay validation and null-identity fallback behavior, so the final implementation should receive human review.

      You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    5. Note: GPT-5.6 on behalf of shivam (@shivamhwp).

      Please rebase this onto the latest t3code/codex-turn-mapping branch. The base branch was rewritten, so GitHub currently shows 331 unrelated commits, 948 changed files, and merge conflicts instead of the two-file change described here.

      After rebasing, please update the Codex replay fixtures for the new thread/fork.lastTurnId request. The fork and merge-back integration tests still expect the old rollback sequence, which is causing the server test failures.

      Once the diff is clean and those tests pass, we can review the actual fix.

    6. 💡 Codex Review

      Here are some automated review suggestions for this pull request.

      Reviewed commit: 2822d10f05

      ℹ️ About Codex in GitHub

      Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

      • Open a pull request for review
      • Mark a draft as ready
      • Comment "@codex review".

      If Codex has suggestions, it will comment; otherwise it will react with 👍.

      Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

      @@ -784,3 +784,21 @@
      784784 },
      785785 );
      786786
      787+export const resolveCodexForkBoundary = Effect.fn("CodexAdapterV2.resolveForkBoundary")(function* (
      788+ input: ProviderAdapterV2ForkThreadInput,
      789+) {
      790+ const rollbackTurnCount = yield* resolveCodexForkRollbackTurnCount(input);
      791+ if (input.providerTurnId === undefined || input.sourceProviderTurns === undefined) {
      792+ return { rollbackTurnCount };
      793+ }
      794+
      795+ const boundaryTurn = providerTurnsForThread(
      796+ input.sourceProviderTurns,
      797+ input.sourceProviderThread,
      798+ ).find((turn) => turn.id === input.providerTurnId);
      799+ const nativeTurnRef = boundaryTurn?.nativeTurnRef;
      800+ if (nativeTurnRef === null || nativeTurnRef === undefined) {
      801+ return { rollbackTurnCount };
      802+ }
      803+
      804+ return { lastTurnId: nativeTurnRef.nativeId, rollbackTurnCount: 0 };

      P2 Badge Treat null native IDs as missing fork boundaries

      When a persisted Codex turn has an identity-less provider ref whose nativeId is null (a valid OrchestrationV2ProviderRef), this returns lastTurnId: null and resets rollbackTurnCount to zero. forkThread then sends a null boundary, which is equivalent to omitting the boundary, while also skipping the compatibility rollback; forking from an earlier run therefore retains later turns. Check nativeTurnRef.nativeId for null before disabling rollback.

      Useful? React with 👍 / 👎.

    7. Stale duplicate of #11490 — same native thread/fork boundary fix, rebased with additional fixtures. Thanks!

    8. juliusmarminge closed this

Sign in to join this conversation.