Calling predicates from source code & backtracking

I’ve encountered some strange behaviour.
I call some predicate from source by using following syntax:

:- my_predicate(parameters).

inside my_predicates uses undo/1 as part of backtrackable assertions. But they are undone immediately after predicate is finished. Using cuts didn’t help me. I assume that prolog engine undoes everything because undo/1 has nothing to do with choicepoints and cuts. It simply cleares of everything that is put in trail. Problem is that my_predicate is supposed to be used in other contexts where undoing asserts are expected, so I can’t just use non-backtrackable version of assert inside it.

Is there a way to manipulate undo records to emulate something like cut for them?
Or I just better use some global flag to supress call to undo/1?

EDIT #1: Another question arised: if undo records are not purged but cuts, does it imply that theese records might accumulate over program working even if by design program should never backtrack past undo/1 calls? Can it possibly lead to memory leaks?

EDIT #2: What is the logic of backtracking after :- or ?- calls in source code?

The control structure of the compiler is failure driven. I.e., there is a predicate that on backtracking produces (expanded) Prolog terms and the next step which adds these to the database and fails until we see end_of_file.

I have little clue what problem you are trying to solve, but this route is not going to work.

I’am writing program which procedurally generates some model, expressed as set of dynamic facts. In that process program backtracks to find consistent solution, therefore I use backtrackable assertions using undo/1. For testing purposes I sometimes call it’s predicates from REPL or from sources directly. REPL can be switched to recursive mode, but with source-code calls it is not possible as I understand.

As a stopgap measure I switched to another b_assert implementation, using ;/2. For now it works. Then I will do some scaffolding code to work around this issue.