@fern-api/replay 0.11.0 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -210,6 +210,31 @@ declare class ReplayDetector {
210
210
  readonly warnings: string[];
211
211
  constructor(git: GitClient, lockManager: LockfileManager, sdkOutputDir: string);
212
212
  detectNewPatches(): Promise<DetectionResult>;
213
+ /**
214
+ * FER-10201 — Non-linear-history detection via `merge-base(prevGen, HEAD)`.
215
+ *
216
+ * After a squash-merge of a Fern-bot PR, `lastGen.commit_sha` (the previous
217
+ * `[fern-generated]`) is orphaned but still in git's object database. The
218
+ * merge-base is the commit on main from which the bot's branch was created —
219
+ * a reachable ancestor of HEAD. Walking that range and classifying each
220
+ * commit via `isGenerationCommit` mirrors the linear path and eliminates
221
+ * the bug class where pipeline-driven changes (autoversion, replay,
222
+ * generation) get bundled as customer customizations.
223
+ *
224
+ * Falls through to the legacy composite path when no common ancestor exists
225
+ * (truly disjoint history — orphan branches with no shared root).
226
+ */
227
+ private detectPatchesViaMergeBase;
228
+ /**
229
+ * FER-10201 — Walk `${rangeStart}..HEAD`, classify each commit, emit one
230
+ * `StoredPatch` per customer commit. Body shared by the linear path
231
+ * (rangeStart = lastGen.commit_sha) and the merge-base path.
232
+ *
233
+ * Patch `base_generation` is always `lastGen.commit_sha` — the
234
+ * lockfile-tracked reference the applicator uses to fetch base file
235
+ * content, regardless of where the walk actually started.
236
+ */
237
+ private detectPatchesInRange;
213
238
  /**
214
239
  * Compute content hash for deduplication.
215
240
  *
@@ -434,6 +459,72 @@ interface ReplayOptions {
434
459
  /** SHA of the base branch HEAD before replay runs. Stored in lockfile for squash merge recovery. */
435
460
  baseBranchHead?: string;
436
461
  }
462
+ /**
463
+ * Options honored by `applyPreparedReplay()`. `cliVersion`, `generatorVersions`,
464
+ * `baseBranchHead`, `dryRun`, and `skipApplication` were all captured in phase 1
465
+ * and re-supplying them here is meaningless.
466
+ */
467
+ interface ApplyOptions {
468
+ /** Write files and stage changes, but don't commit */
469
+ stageOnly?: boolean;
470
+ }
471
+ /**
472
+ * Opaque handle returned by `prepareReplay()`. Consumed by `applyPreparedReplay()`
473
+ * to complete the run.
474
+ *
475
+ * Contract for consumers (e.g., generator-cli pipeline inserting AutoVersionStep):
476
+ *
477
+ * - Between `prepareReplay` and `applyPreparedReplay`, HEAD is at the new
478
+ * `[fern-generated]` commit (unless `_prepared.terminal === true`, e.g. dry-run).
479
+ * - Consumers MAY land additional commits on HEAD between phases. `applyPatches`
480
+ * runs against current HEAD at phase-2 call time, so mid-phase commits are
481
+ * respected. `rebasePatches` uses `currentGenerationSha` (the pure `[fern-generated]`
482
+ * SHA), NOT HEAD — mid-phase commits do not retarget patches' `base_generation`.
483
+ * - The service instance is single-use across the phase boundary: do not call
484
+ * other service methods (`syncFromDivergentMerge`, another `prepareReplay`, etc.)
485
+ * between phases. In-memory lockfile and warning state would be corrupted.
486
+ * - If the caller's mid-phase work throws or `applyPreparedReplay` is never called,
487
+ * the repo is left with the new `[fern-generated]` commit but a stale lockfile.
488
+ * Consumers MUST either (a) still call `applyPreparedReplay` so the lockfile
489
+ * advances, or (b) reset HEAD to `previousGenerationSha` and surface the failure.
490
+ *
491
+ * To decide whether to run autoversion between phases, use:
492
+ * prep.previousGenerationSha != null && prep.flow !== "skip-application"
493
+ */
494
+ interface ReplayPreparation {
495
+ flow: "first-generation" | "no-patches" | "normal-regeneration" | "skip-application";
496
+ /** lockfile.current_generation from BEFORE this run; null iff no prior generation exists */
497
+ previousGenerationSha: string | null;
498
+ /** SHA of the new [fern-generated] commit created by prepareReplay (or HEAD for dry-run terminals) */
499
+ currentGenerationSha: string;
500
+ /** options.baseBranchHead passed through */
501
+ baseBranchHead: string | null;
502
+ /** @internal Opaque state consumed by applyPreparedReplay */
503
+ readonly _prepared: InternalPreparationState;
504
+ }
505
+ /** @internal */
506
+ interface InternalPreparationState {
507
+ flow: "first-generation" | "no-patches" | "normal-regeneration" | "skip-application";
508
+ /** true → applyPreparedReplay returns preparedReport without disk/git work (dry-run path) */
509
+ terminal: boolean;
510
+ preparedReport?: ReplayReport;
511
+ /** post-preGenRebase, post-detection, post-absorption (references, not copies) */
512
+ patchesToApply: StoredPatch[];
513
+ /** subset of patchesToApply that were freshly detected this cycle (vs already in the lockfile) */
514
+ newPatchIds: Set<string>;
515
+ preRebaseCounts?: {
516
+ conflictResolved: number;
517
+ conflictAbsorbed: number;
518
+ contentRefreshed: number;
519
+ };
520
+ /** detector warnings snapshotted at end of phase 1 */
521
+ detectorWarnings: string[];
522
+ revertedCount: number;
523
+ /** [fern-generated] SHA; passed to rebasePatches as currentGenSha */
524
+ genSha: string;
525
+ /** cached phase-1 options for constructing the final ReplayReport in phase 2 */
526
+ optionsSnapshot?: ReplayOptions;
527
+ }
437
528
  declare class ReplayService {
438
529
  private git;
439
530
  private detector;
@@ -465,6 +556,33 @@ declare class ReplayService {
465
556
  */
466
557
  getLastResults(): ReadonlyArray<ReplayResult>;
467
558
  constructor(outputDir: string, _config: ReplayConfig);
559
+ /**
560
+ * Phase 1 of the two-phase replay flow. Runs preGenerationRebase (when applicable),
561
+ * detects new patches, and creates the `[fern-generated]` commit. Leaves HEAD at
562
+ * the generation commit (or unchanged for dry-run).
563
+ *
564
+ * Does NOT apply patches — the returned handle must be passed to
565
+ * `applyPreparedReplay()` to complete the run. No disk writes to the lockfile
566
+ * happen here; lockfile persistence is deferred to phase 2.
567
+ *
568
+ * Callers may land additional commits on HEAD between `prepareReplay` and
569
+ * `applyPreparedReplay` (e.g., `[fern-autoversion]`).
570
+ */
571
+ prepareReplay(options?: ReplayOptions): Promise<ReplayPreparation>;
572
+ /**
573
+ * Phase 2 of the two-phase replay flow. Applies patches, rebases them against
574
+ * the generation, saves the lockfile, and commits `[fern-replay]` (or stages
575
+ * under `stageOnly`). Operates on HEAD at call time — mid-phase commits are
576
+ * respected.
577
+ *
578
+ * For terminal handles (dry-run, first-gen has no patch work, skip-app does
579
+ * its own post-commit logic), this returns the precomputed report.
580
+ */
581
+ applyPreparedReplay(prep: ReplayPreparation, options?: ApplyOptions): Promise<ReplayReport>;
582
+ /**
583
+ * Backwards-compatible atomic entry point. Composes `prepareReplay` + `applyPreparedReplay`.
584
+ * Behavior is byte-identical to the pre-split implementation for all existing callers.
585
+ */
468
586
  runReplay(options?: ReplayOptions): Promise<ReplayReport>;
469
587
  /**
470
588
  * Sync the lockfile after a divergent PR was squash-merged.
@@ -482,15 +600,22 @@ declare class ReplayService {
482
600
  baseBranchHead?: string;
483
601
  }): Promise<void>;
484
602
  private determineFlow;
485
- private handleFirstGeneration;
603
+ private firstGenerationReport;
604
+ private _prepareFirstGeneration;
605
+ private _applyFirstGeneration;
486
606
  /**
487
- * Skip-application mode: commit the generation and update the lockfile
488
- * but don't detect or apply patches. Sets a marker so the next normal
489
- * run skips revert detection in preGenerationRebase().
607
+ * Skip-application mode phase 1: commit the generation and update the
608
+ * in-memory lockfile, but don't detect or apply patches. Sets a marker
609
+ * so the next normal run skips revert detection in preGenerationRebase().
610
+ *
611
+ * Disk save is deferred to `_applySkipApplication`.
490
612
  */
491
- private handleSkipApplication;
492
- private handleNoPatchesRegeneration;
493
- private handleNormalRegeneration;
613
+ private _prepareSkipApplication;
614
+ private _applySkipApplication;
615
+ private _prepareNoPatchesRegeneration;
616
+ private _applyNoPatchesRegeneration;
617
+ private _prepareNormalRegeneration;
618
+ private _applyNormalRegeneration;
494
619
  /**
495
620
  * Rebase cleanly applied patches so they are relative to the current generation.
496
621
  * This prevents recurring conflicts on subsequent regenerations.
@@ -761,4 +886,4 @@ interface StatusGeneration {
761
886
  }
762
887
  declare function status(outputDir: string): StatusResult;
763
888
 
764
- export { type BootstrapOptions, type BootstrapResult, CREDENTIAL_PATTERNS, type CommitInfo, type CommitOptions, type ConflictDetail, type ConflictMetadata, type ConflictReason, type ConflictRegion, type CredentialPattern, type CustomizationsConfig, type DetectionResult, type DiffStat, FERN_BOT_EMAIL, FERN_BOT_LOGIN, FERN_BOT_NAME, FernignoreMigrator, type FileResult, type ForgetOptions, type ForgetResult, type GenerationLock, type GenerationRecord, GitClient, LockfileManager, LockfileNotFoundError, type MatchedPatch, type MergeResult, type MigrationAnalysis, type MigrationResult, type MoveDeclaration, ReplayApplicator, ReplayCommitter, type ReplayConfig, ReplayDetector, type ReplayOptions, type ReplayReport, type ReplayResult, ReplayService, type ResetOptions, type ResetResult, type ResolveOptions, type ResolveResult, SENSITIVE_FILE_PATTERNS, type StatusGeneration, type StatusPatch, type StatusResult, type StoredPatch, type UnresolvedPatchInfo, bootstrap, forget, isGenerationCommit, isReplayCommit, isRevertCommit, isSensitiveFile, parseRevertedMessage, parseRevertedSha, reset, resolve, scanForCredentials, status, threeWayMerge };
889
+ export { type ApplyOptions, type BootstrapOptions, type BootstrapResult, CREDENTIAL_PATTERNS, type CommitInfo, type CommitOptions, type ConflictDetail, type ConflictMetadata, type ConflictReason, type ConflictRegion, type CredentialPattern, type CustomizationsConfig, type DetectionResult, type DiffStat, FERN_BOT_EMAIL, FERN_BOT_LOGIN, FERN_BOT_NAME, FernignoreMigrator, type FileResult, type ForgetOptions, type ForgetResult, type GenerationLock, type GenerationRecord, GitClient, LockfileManager, LockfileNotFoundError, type MatchedPatch, type MergeResult, type MigrationAnalysis, type MigrationResult, type MoveDeclaration, ReplayApplicator, ReplayCommitter, type ReplayConfig, ReplayDetector, type ReplayOptions, type ReplayPreparation, type ReplayReport, type ReplayResult, ReplayService, type ResetOptions, type ResetResult, type ResolveOptions, type ResolveResult, SENSITIVE_FILE_PATTERNS, type StatusGeneration, type StatusPatch, type StatusResult, type StoredPatch, type UnresolvedPatchInfo, bootstrap, forget, isGenerationCommit, isReplayCommit, isRevertCommit, isSensitiveFile, parseRevertedMessage, parseRevertedSha, reset, resolve, scanForCredentials, status, threeWayMerge };
package/dist/index.d.ts CHANGED
@@ -210,6 +210,31 @@ declare class ReplayDetector {
210
210
  readonly warnings: string[];
211
211
  constructor(git: GitClient, lockManager: LockfileManager, sdkOutputDir: string);
212
212
  detectNewPatches(): Promise<DetectionResult>;
213
+ /**
214
+ * FER-10201 — Non-linear-history detection via `merge-base(prevGen, HEAD)`.
215
+ *
216
+ * After a squash-merge of a Fern-bot PR, `lastGen.commit_sha` (the previous
217
+ * `[fern-generated]`) is orphaned but still in git's object database. The
218
+ * merge-base is the commit on main from which the bot's branch was created —
219
+ * a reachable ancestor of HEAD. Walking that range and classifying each
220
+ * commit via `isGenerationCommit` mirrors the linear path and eliminates
221
+ * the bug class where pipeline-driven changes (autoversion, replay,
222
+ * generation) get bundled as customer customizations.
223
+ *
224
+ * Falls through to the legacy composite path when no common ancestor exists
225
+ * (truly disjoint history — orphan branches with no shared root).
226
+ */
227
+ private detectPatchesViaMergeBase;
228
+ /**
229
+ * FER-10201 — Walk `${rangeStart}..HEAD`, classify each commit, emit one
230
+ * `StoredPatch` per customer commit. Body shared by the linear path
231
+ * (rangeStart = lastGen.commit_sha) and the merge-base path.
232
+ *
233
+ * Patch `base_generation` is always `lastGen.commit_sha` — the
234
+ * lockfile-tracked reference the applicator uses to fetch base file
235
+ * content, regardless of where the walk actually started.
236
+ */
237
+ private detectPatchesInRange;
213
238
  /**
214
239
  * Compute content hash for deduplication.
215
240
  *
@@ -434,6 +459,72 @@ interface ReplayOptions {
434
459
  /** SHA of the base branch HEAD before replay runs. Stored in lockfile for squash merge recovery. */
435
460
  baseBranchHead?: string;
436
461
  }
462
+ /**
463
+ * Options honored by `applyPreparedReplay()`. `cliVersion`, `generatorVersions`,
464
+ * `baseBranchHead`, `dryRun`, and `skipApplication` were all captured in phase 1
465
+ * and re-supplying them here is meaningless.
466
+ */
467
+ interface ApplyOptions {
468
+ /** Write files and stage changes, but don't commit */
469
+ stageOnly?: boolean;
470
+ }
471
+ /**
472
+ * Opaque handle returned by `prepareReplay()`. Consumed by `applyPreparedReplay()`
473
+ * to complete the run.
474
+ *
475
+ * Contract for consumers (e.g., generator-cli pipeline inserting AutoVersionStep):
476
+ *
477
+ * - Between `prepareReplay` and `applyPreparedReplay`, HEAD is at the new
478
+ * `[fern-generated]` commit (unless `_prepared.terminal === true`, e.g. dry-run).
479
+ * - Consumers MAY land additional commits on HEAD between phases. `applyPatches`
480
+ * runs against current HEAD at phase-2 call time, so mid-phase commits are
481
+ * respected. `rebasePatches` uses `currentGenerationSha` (the pure `[fern-generated]`
482
+ * SHA), NOT HEAD — mid-phase commits do not retarget patches' `base_generation`.
483
+ * - The service instance is single-use across the phase boundary: do not call
484
+ * other service methods (`syncFromDivergentMerge`, another `prepareReplay`, etc.)
485
+ * between phases. In-memory lockfile and warning state would be corrupted.
486
+ * - If the caller's mid-phase work throws or `applyPreparedReplay` is never called,
487
+ * the repo is left with the new `[fern-generated]` commit but a stale lockfile.
488
+ * Consumers MUST either (a) still call `applyPreparedReplay` so the lockfile
489
+ * advances, or (b) reset HEAD to `previousGenerationSha` and surface the failure.
490
+ *
491
+ * To decide whether to run autoversion between phases, use:
492
+ * prep.previousGenerationSha != null && prep.flow !== "skip-application"
493
+ */
494
+ interface ReplayPreparation {
495
+ flow: "first-generation" | "no-patches" | "normal-regeneration" | "skip-application";
496
+ /** lockfile.current_generation from BEFORE this run; null iff no prior generation exists */
497
+ previousGenerationSha: string | null;
498
+ /** SHA of the new [fern-generated] commit created by prepareReplay (or HEAD for dry-run terminals) */
499
+ currentGenerationSha: string;
500
+ /** options.baseBranchHead passed through */
501
+ baseBranchHead: string | null;
502
+ /** @internal Opaque state consumed by applyPreparedReplay */
503
+ readonly _prepared: InternalPreparationState;
504
+ }
505
+ /** @internal */
506
+ interface InternalPreparationState {
507
+ flow: "first-generation" | "no-patches" | "normal-regeneration" | "skip-application";
508
+ /** true → applyPreparedReplay returns preparedReport without disk/git work (dry-run path) */
509
+ terminal: boolean;
510
+ preparedReport?: ReplayReport;
511
+ /** post-preGenRebase, post-detection, post-absorption (references, not copies) */
512
+ patchesToApply: StoredPatch[];
513
+ /** subset of patchesToApply that were freshly detected this cycle (vs already in the lockfile) */
514
+ newPatchIds: Set<string>;
515
+ preRebaseCounts?: {
516
+ conflictResolved: number;
517
+ conflictAbsorbed: number;
518
+ contentRefreshed: number;
519
+ };
520
+ /** detector warnings snapshotted at end of phase 1 */
521
+ detectorWarnings: string[];
522
+ revertedCount: number;
523
+ /** [fern-generated] SHA; passed to rebasePatches as currentGenSha */
524
+ genSha: string;
525
+ /** cached phase-1 options for constructing the final ReplayReport in phase 2 */
526
+ optionsSnapshot?: ReplayOptions;
527
+ }
437
528
  declare class ReplayService {
438
529
  private git;
439
530
  private detector;
@@ -465,6 +556,33 @@ declare class ReplayService {
465
556
  */
466
557
  getLastResults(): ReadonlyArray<ReplayResult>;
467
558
  constructor(outputDir: string, _config: ReplayConfig);
559
+ /**
560
+ * Phase 1 of the two-phase replay flow. Runs preGenerationRebase (when applicable),
561
+ * detects new patches, and creates the `[fern-generated]` commit. Leaves HEAD at
562
+ * the generation commit (or unchanged for dry-run).
563
+ *
564
+ * Does NOT apply patches — the returned handle must be passed to
565
+ * `applyPreparedReplay()` to complete the run. No disk writes to the lockfile
566
+ * happen here; lockfile persistence is deferred to phase 2.
567
+ *
568
+ * Callers may land additional commits on HEAD between `prepareReplay` and
569
+ * `applyPreparedReplay` (e.g., `[fern-autoversion]`).
570
+ */
571
+ prepareReplay(options?: ReplayOptions): Promise<ReplayPreparation>;
572
+ /**
573
+ * Phase 2 of the two-phase replay flow. Applies patches, rebases them against
574
+ * the generation, saves the lockfile, and commits `[fern-replay]` (or stages
575
+ * under `stageOnly`). Operates on HEAD at call time — mid-phase commits are
576
+ * respected.
577
+ *
578
+ * For terminal handles (dry-run, first-gen has no patch work, skip-app does
579
+ * its own post-commit logic), this returns the precomputed report.
580
+ */
581
+ applyPreparedReplay(prep: ReplayPreparation, options?: ApplyOptions): Promise<ReplayReport>;
582
+ /**
583
+ * Backwards-compatible atomic entry point. Composes `prepareReplay` + `applyPreparedReplay`.
584
+ * Behavior is byte-identical to the pre-split implementation for all existing callers.
585
+ */
468
586
  runReplay(options?: ReplayOptions): Promise<ReplayReport>;
469
587
  /**
470
588
  * Sync the lockfile after a divergent PR was squash-merged.
@@ -482,15 +600,22 @@ declare class ReplayService {
482
600
  baseBranchHead?: string;
483
601
  }): Promise<void>;
484
602
  private determineFlow;
485
- private handleFirstGeneration;
603
+ private firstGenerationReport;
604
+ private _prepareFirstGeneration;
605
+ private _applyFirstGeneration;
486
606
  /**
487
- * Skip-application mode: commit the generation and update the lockfile
488
- * but don't detect or apply patches. Sets a marker so the next normal
489
- * run skips revert detection in preGenerationRebase().
607
+ * Skip-application mode phase 1: commit the generation and update the
608
+ * in-memory lockfile, but don't detect or apply patches. Sets a marker
609
+ * so the next normal run skips revert detection in preGenerationRebase().
610
+ *
611
+ * Disk save is deferred to `_applySkipApplication`.
490
612
  */
491
- private handleSkipApplication;
492
- private handleNoPatchesRegeneration;
493
- private handleNormalRegeneration;
613
+ private _prepareSkipApplication;
614
+ private _applySkipApplication;
615
+ private _prepareNoPatchesRegeneration;
616
+ private _applyNoPatchesRegeneration;
617
+ private _prepareNormalRegeneration;
618
+ private _applyNormalRegeneration;
494
619
  /**
495
620
  * Rebase cleanly applied patches so they are relative to the current generation.
496
621
  * This prevents recurring conflicts on subsequent regenerations.
@@ -761,4 +886,4 @@ interface StatusGeneration {
761
886
  }
762
887
  declare function status(outputDir: string): StatusResult;
763
888
 
764
- export { type BootstrapOptions, type BootstrapResult, CREDENTIAL_PATTERNS, type CommitInfo, type CommitOptions, type ConflictDetail, type ConflictMetadata, type ConflictReason, type ConflictRegion, type CredentialPattern, type CustomizationsConfig, type DetectionResult, type DiffStat, FERN_BOT_EMAIL, FERN_BOT_LOGIN, FERN_BOT_NAME, FernignoreMigrator, type FileResult, type ForgetOptions, type ForgetResult, type GenerationLock, type GenerationRecord, GitClient, LockfileManager, LockfileNotFoundError, type MatchedPatch, type MergeResult, type MigrationAnalysis, type MigrationResult, type MoveDeclaration, ReplayApplicator, ReplayCommitter, type ReplayConfig, ReplayDetector, type ReplayOptions, type ReplayReport, type ReplayResult, ReplayService, type ResetOptions, type ResetResult, type ResolveOptions, type ResolveResult, SENSITIVE_FILE_PATTERNS, type StatusGeneration, type StatusPatch, type StatusResult, type StoredPatch, type UnresolvedPatchInfo, bootstrap, forget, isGenerationCommit, isReplayCommit, isRevertCommit, isSensitiveFile, parseRevertedMessage, parseRevertedSha, reset, resolve, scanForCredentials, status, threeWayMerge };
889
+ export { type ApplyOptions, type BootstrapOptions, type BootstrapResult, CREDENTIAL_PATTERNS, type CommitInfo, type CommitOptions, type ConflictDetail, type ConflictMetadata, type ConflictReason, type ConflictRegion, type CredentialPattern, type CustomizationsConfig, type DetectionResult, type DiffStat, FERN_BOT_EMAIL, FERN_BOT_LOGIN, FERN_BOT_NAME, FernignoreMigrator, type FileResult, type ForgetOptions, type ForgetResult, type GenerationLock, type GenerationRecord, GitClient, LockfileManager, LockfileNotFoundError, type MatchedPatch, type MergeResult, type MigrationAnalysis, type MigrationResult, type MoveDeclaration, ReplayApplicator, ReplayCommitter, type ReplayConfig, ReplayDetector, type ReplayOptions, type ReplayPreparation, type ReplayReport, type ReplayResult, ReplayService, type ResetOptions, type ResetResult, type ResolveOptions, type ResolveResult, SENSITIVE_FILE_PATTERNS, type StatusGeneration, type StatusPatch, type StatusResult, type StoredPatch, type UnresolvedPatchInfo, bootstrap, forget, isGenerationCommit, isReplayCommit, isRevertCommit, isSensitiveFile, parseRevertedMessage, parseRevertedSha, reset, resolve, scanForCredentials, status, threeWayMerge };