Interprompt prompt engine
Overview
src/interprompt is the prompt templating package Serena vendors for loading YAML prompt definitions, rendering them with Jinja, and generating a typed-ish prompt factory module for Serena. The public package entry point is src/…/__init__.py, which exports only autogenerate_prompt_factory_module from src/…/prompt_factory.py.
Template rendering is handled by src/…/jinja_template.py. JinjaTemplate wraps a Jinja SandboxedEnvironment, created lazily through the _JinjaEnvProvider singleton from src/…/class_decorators.py, and compiles each template string with from_string. At construction time it parses the template and stores the sorted undeclared variable names from jinja2.meta.find_undeclared_variables; render(**params) later renders with caller-provided values. The lightweight ParameterizedTemplateInterface is shared by renderable prompt objects so callers can inspect required parameter names before rendering.
Multilingual prompt loading lives in src/…/multilang_prompt.py. MultiLangPromptCollection reads every .yml/.yaml file directly inside one or more prompt directories with yaml.safe_load, expects a top-level prompts map, and treats an optional top-level lang as the language code, defaulting to default. String values become PromptTemplate instances backed by JinjaTemplate; list values become PromptList instances whose to_string() formats bullet lists while preserving multiline indentation. MultiLangPromptTemplate groups language variants for a prompt name and rejects variants whose Jinja parameter sets differ from the already-registered variant. LanguageFallbackMode controls missing language behavior for templates: raise, use any registered item, or use the default language.
Directory precedence is part of the runtime behavior. MultiLangPromptCollection.__init__ accepts either one directory or a list; the first directory is loaded with duplicate name/language collisions treated as errors, while subsequent fallback directories are loaded with collisions skipped. This gives earlier directories override priority without mutating the fallback source. Serena uses this in src/…/prompt_factory.py: SerenaPromptFactory creates the user prompt-template directory from SerenaPaths().user_prompt_templates_dir, then initializes the generated factory with [user_templates_dir, PROMPT_TEMPLATES_DIR_INTERNAL], so user YAML files override built-in templates from src/…/constants.py's PROMPT_TEMPLATES_DIR_INTERNAL.
src/…/prompt_factory.py provides both the runtime base and code generator. PromptFactoryBase owns a MultiLangPromptCollection, a selected lang_code, and helpers for retrieving prompt names, template strings, rendered prompts, and prompt lists. _render_prompt() deletes the self entry from the locals() dict passed by generated methods before forwarding parameters to the underlying template. autogenerate_prompt_factory_module() loads the prompt collection, then writes a Python module containing a subclass of PromptFactoryBase: for each prompt template it emits get_<name>_template_string() and create_<name>(..., *, <template params>: Any), and for each prompt list it emits get_list_<name>(). The generated file is marked # ruff: noqa, # black: skip, and # mypy: ignore-errors, and the target module path is overwritten after creating its parent directory.
Serena's generator entry point is scripts/gen_prompt_factory.py. It calls interprompt.autogenerate_prompt_factory_module() with PROMPT_TEMPLATES_DIR_INTERNAL and writes src/…/generated_prompt_factory.py, then re-adds the GPL header because the vendored generator template does not include one. The generated Serena factory is what src/…/prompt_factory.py subclasses; src/…/agent.py calls methods such as SerenaPromptFactory().create_system_prompt(...) to render the active system prompt, and src/…/cli.py exposes prompt commands that list overrides, copy built-in YAML files into the user prompt directory, and print the effective unrendered template.
The .syncCommitId.this and .syncCommitId.remote files under src/interprompt record the vendored sync state for this package; they are not read by the runtime prompt engine.
Coupling
- If a prompt YAML under Serena's built-in prompt-template directory adds, removes, or renames a Jinja variable, regenerate src/…/generated_prompt_factory.py through scripts/gen_prompt_factory.py; the generated
create_<prompt>()signatures come fromMultiLangPromptCollection.get_prompt_template_parameters()in src/…/prompt_factory.py, while runtime rendering in src/…/jinja_template.py still depends on the template's actual undeclared variables. - If multiple language variants of a prompt are introduced, keep the Jinja variable names identical across those YAML entries;
MultiLangPromptTemplate.add_prompt_template()in src/…/multilang_prompt.py raises when language variants have different parameter lists. - If Serena's prompt override precedence changes in src/…/prompt_factory.py, verify
MultiLangPromptCollection.__init__in src/…/multilang_prompt.py; the first directory is strict and wins, while later directories silently skip colliding prompt names. - If the generated factory method style changes in src/…/prompt_factory.py, verify callers that pass
locals()-derived parameters andPromptFactoryBase._render_prompt(), because_render_prompt()currently assumes aselfkey exists and deletes it before rendering.
Risks
- src/…/prompt_factory.py overwrites
target_module_pathunconditionally inautogenerate_prompt_factory_module(). If scripts/gen_prompt_factory.py points at the wrong path, it can replace a source module with generated code. - src/…/multilang_prompt.py only applies
fallback_modeto prompt templates throughget_prompt_template().get_prompt_list()callsget_item(lang_code)without passing the collection fallback mode, so multilingual prompt lists will raise on a missing requested language even when template fallback is configured.