1
>
/*---------------------------------------------------------------------------------------------
codexForkPlan.ts
2
>
* Copyright (c) Microsoft Corporation. All rights reserved.
3
>
* Licensed under the MIT License. See License.txt in the project root for license information.
4
>
*--------------------------------------------------------------------------------------------*/
5
>
6
>
/**
7
>
* Pure decision helpers for the Codex `thread/fork` path, extracted so they can
8
>
* be unit-tested without standing up the whole {@link CodexAgent} (mirrors the
9
>
* `codexShellCommand.ts` extraction). No protocol or service imports.
10
>
*/
11
>
12
>
/**
13
>
* Outcome of locating where a fork should branch within the source thread.
14
>
* `resolved: false` means neither the mapped codex turn id nor the caller's
15
>
* fallback index identified a turn, and the caller must reject the fork instead
16
>
* of silently branching from the tip.
17
>
*/
18
>
export type ForkBoundaryResolution =
19
>
| { readonly resolved: true; readonly keepThroughIndex: number; readonly numTurnsToDrop: number }
20
>
| { readonly resolved: false };
21
>
22
>
/**
23
>
* Resolve the fork boundary from the source thread's ordered turn ids.
24
>
*
25
>
* `thread/fork` copies the full source history; the returned `numTurnsToDrop`
26
>
* is how many trailing turns must be rolled back so the fork ends at (and
27
>
* includes) the requested turn.
28
>
*
29
>
* @param sourceTurnIds Codex turn ids of the source thread, in order.
30
>
* @param codexTurnId The resolved codex turn id of the requested fork point.
31
>
* @param fallbackTurnIndex Zero-based index used when `codexTurnId` isn't found.
32
>
*/
33
>
export function resolveForkBoundary(sourceTurnIds: readonly string[], codexTurnId: string, fallbackTurnIndex: number): ForkBoundaryResolution {
34
const total = sourceTurnIds.length;
35
let keepThroughIndex = sourceTurnIds.findIndex(id => id === codexTurnId);