A small service prepares labels for a batch of events and stores them in a temporary log. After a recent change, the results started differing between identical calls: some labels seem to “stick” to neighboring objects, and the temporary file is not always closed on normal completion.
build_event_labels(records, prefix='EV') must return a list of strings in the format PREFIX-<index>:<name>.1 and depends only on the order of input records in the current call.records is any iterable object, including a one-shot generator.write_label_report(records, path) must write one line for each label and return the list of written lines.build_event_labels() twice with different event lists.build_event_labels() and verify that it is processed once without losing data.write_label_report() on a successful input and check that the temporary file is closed after return.Use line comments to explain the problem, its impact, the fix and how to test it.