@@ -29,6 +29,7 @@ import {
2929 writeLegacyOpenaiHistoryRecovery ,
3030 type HistoryWriteTarget ,
3131} from "./internal/history-writer" ;
32+ import { readCodexTransitionStateAtHome } from "./transition-state" ;
3233
3334/**
3435 * The durable operation, mirrored into the request for diagnostics only.
@@ -106,23 +107,38 @@ export function isHistoryWorkerRunMessage(data: unknown): data is HistoryWorkerR
106107export function runHistoryUnitUnderLock (
107108 message : HistoryWorkerRunMessage ,
108109) : HistoryWorkerResult {
109- const { requestId, jobId, operation } = message ;
110+ const { requestId, jobId } = message ;
110111 const target : HistoryWriteTarget = {
111112 canonicalStateDbPath : message . canonicalStateDbPath ,
112113 canonicalBackupPath : message . canonicalBackupPath ,
113114 } ;
114115
115- if ( operation === "skip" ) {
116+ if ( message . operation === "skip" ) {
116117 return { type : "done" , requestId, jobId, outcome : "skipped" , rows : 0 , files : 0 } ;
117118 }
118119
119120 const acquired = withHistoryWriteSerialization (
120121 message . canonicalCodexHome ,
121122 message . canonicalStateDbPath ,
122123 permit => {
123- if ( operation === "recover-legacy-openai" ) {
124+ // Legacy recovery is an explicit maintenance command, not a scheduled
125+ // convergence transition. It remains distinct from generic restore.
126+ if ( message . operation === "recover-legacy-openai" ) {
124127 return writeLegacyOpenaiHistoryRecovery ( permit , target ) ;
125128 }
129+ const durable = readCodexTransitionStateAtHome ( message . canonicalCodexHome ) ;
130+ if ( durable . kind !== "ready" ) return { refused : "history_schedule_unavailable" } as const ;
131+ const { state } = durable ;
132+ if ( state . currentTxId !== jobId || state . history . txId !== jobId || ! state . historySchedule ) {
133+ return { refused : "history_job_overtaken" } as const ;
134+ }
135+ const operation = message . operation ;
136+ const expectedDirection = operation === "apply-opencodex" || operation === "migrate-openai"
137+ ? "apply"
138+ : operation === "restore-openai" ? "remove" : null ;
139+ if ( expectedDirection === null || state . historySchedule . direction !== expectedDirection ) {
140+ return { refused : "history_operation_mismatch" } as const ;
141+ }
126142 // apply-opencodex routes history to opencodex; migrate/restore return it to
127143 // native. The provider is derived from the operation, never from a caller.
128144 const provider = operation === "apply-opencodex" ? "opencodex" : "openai" ;
@@ -134,6 +150,9 @@ export function runHistoryUnitUnderLock(
134150 return { type : "blocked" , requestId, jobId, reason : acquired . reason } ;
135151 }
136152 const result = acquired . value ;
153+ if ( "refused" in result ) {
154+ return { type : "error" , requestId, jobId, message : result . refused } ;
155+ }
137156 if ( result . failed === true ) {
138157 return { type : "error" , requestId, jobId, message : "history_transition_failed" } ;
139158 }
0 commit comments