/**
 * PersistStage - State persistence after execution.
 *
 * If persistState is enabled and the execution succeeded (exit code 0),
 * commits the execution state (cwd, env, functions, options) back to the
 * parent Bash instance.
 */
export class PersistStage {
    name = "persist";
    defaultPersistState;
    persistCallback;
    constructor(defaultPersistState, persistCallback) {
        this.defaultPersistState = defaultPersistState;
        this.persistCallback = persistCallback;
    }
    async execute(context) {
        const { options, execState, result } = context;
        if (result === undefined) {
            return { continue: true, context };
        }
        const shouldPersist = options?.persistState ?? this.defaultPersistState;
        if (shouldPersist && result.exitCode === 0) {
            this.persistCallback(execState, result.exitCode);
        }
        return { continue: true, context };
    }
}
