# Copyright 2026 The RPent Authors.
# SPDX-License-Identifier: Apache-2.0

"""Opt-in access to RPent memory during coded insertion evaluation."""

import os

from robots.robodojo import insertion_control


def enabled() -> bool:
    """Return whether staged insertion may use the existing memory file tools."""
    return (
        insertion_control.enabled()
        and os.environ.get("ROBODOJO_INSERTION_MEMORY", "0") == "1"
    )


PROMPT = """CROSS-RUN INSERTION MEMORY:
Before the first motion, use read_text_file to read {{memory_dir}}/MEMORY.md,
then one to three relevant leaves for {{task_name}}. Published memory is read-only.
Current observations, motion guards and the native judge override historical notes.
Carry applicable facts and rejected approaches in STATE when older turns leave context.
After a new verified milestone or a changed failure, use write_text_file to update
{{output_dir}}/insertion_memory_draft.md. Keep it brief: task, controller version,
native step, observed outcome, failed approach, and next hypothesis. Distinguish
observations from hypotheses; a completed stage is not whole-task success. Never
write a successful recipe unless the native judge reports success. These episode
drafts require evidence review before publication to shared memory. Do not overwrite
published leaves. Do not repeat a rejected unchanged geometry because memory says
it might work. Memory does not authorize extra motion tools or bypass controller stages.
"""
