PerFilePromptGenerator: prompt template includes are re-read from disk once per file
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 75/100
- Tipo de issue
- Refactorización
- Claridad
- Bien especificado
- Estado de actividad
- Tranquilo
- Stack tecnológico
- typescript
- Área
- performance
Línea de trabajo
Empieza en src/prompt-generators/per-file.ts y src/util/expand-prompt.ts; después ejecuta la reproducción de dry-run proporcionada o una reproducción del generador a nivel de unidad contando las llamadas a readFile. Mueve la expansión de includes fuera del bucle por archivo, manteniendo la sustitución de archivos por archivo; se considera terminado cuando los prompts generados no cambian y el archivo include se lee una vez por cada llamada a generate().
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Observed behavior
PerFilePromptGenerator.generate() calls expandPrompt(template, basePath, { file }) inside the per-file loop. expandPrompt runs expandIncludes first (which recursively readFiles every {{include:path}} target) and only then substitutes the variables.
Source: src/prompt-generators/per-file.ts:67-78
async *generate(loopState: LoopState): AsyncIterable<Prompt> {
const { filePattern, excludePatterns } = this.#task;
const allFiles = await resolveFiles(filePattern, excludePatterns);
for (const file of allFiles) {
if (loopState.isOutstanding(file)) {
const template = this.#task.promptTemplate;
const prompt = await expandPrompt(template, this.#basePath, { file });
yield { id: file, prompt };
}
}
}
And src/util/expand-prompt.ts:17-29:
export async function expandPrompt(
template: string,
basePath: string,
variables: Readonly<Record<string, string>>,
): Promise<string> {
let result = await expandIncludes(template, basePath);
for (const [key, value] of Object.entries(variables)) {
result = result.replaceAll(`{{${key}}}`, value);
}
return result;
}
The only per-file variable is file. Includes do not, and cannot, depend on it: expandIncludes runs strictly before variable substitution. So the resolved-includes form of the template is identical across every iteration of the loop, yet it is recomputed (and the include files re-read) for every file.
For the project's own src/examples/quality-check/quality-check.json config, which uses {{include:prompt.md}} against filePattern: src/**/*.ts, every prompt re-reads prompt.md even though its contents never change between iterations. Larger include trees and bigger file sets amplify the wasted I/O linearly. With recursive includes the multiplier compounds, since expandIncludes walks the include graph again from scratch on every call.
Expected behavior
Expand {{include:...}} macros once per generate() call, cache the result, and only run the {{file}} substitution per iteration. The include graph is fixed for the lifetime of a run, so this is a safe optimization with the same observable output.
Minimal reproduction
mkdir -p /tmp/llt-include-repro/src
for i in $(seq 1 200); do touch /tmp/llt-include-repro/src/f$i.ts; done
# A non-trivial include file
cat > /tmp/llt-include-repro/prompt.md <<'MD'
Review {{file}} for issues.
MD
cat > /tmp/llt-include-repro/config.json <<'JSON'
{
"name": "include-perf",
"agent": "claude-sdk",
"promptGenerator": [
"per-file",
{
"filePattern": "src/**/*.ts",
"promptTemplate": "{{include:prompt.md}}"
}
]
}
JSON
# Use --dry-run so we exercise only prompt generation, then strace/instrument the
# readFile calls to prompt.md. With 200 files there are 200 readFile calls for
# prompt.md; one is sufficient.
cd /tmp/llt-include-repro
pnpx loop-the-loop --dry-run config.json
Equivalently, a unit-level repro: wrap node:fs/promises.readFile with a counter, run PerFilePromptGenerator.generate() over N files where the template is {{include:prompt.md}}, and observe N reads of prompt.md instead of 1.
Suggested fix
Move the include expansion out of the loop:
async *generate(loopState: LoopState): AsyncIterable<Prompt> {
const { filePattern, excludePatterns, promptTemplate } = this.#task;
const allFiles = await resolveFiles(filePattern, excludePatterns);
const expandedTemplate = await expandIncludes(promptTemplate, this.#basePath);
for (const file of allFiles) {
if (loopState.isOutstanding(file)) {
const prompt = expandedTemplate.replaceAll('{{file}}', file);
yield { id: file, prompt };
}
}
}
This preserves the existing behavior for templates with no includes (one cheap expandIncludes call up front instead of N), and avoids re-reading any included files for every iteration. The same optimization applies to other generators that call expandPrompt in a loop (json, bugzilla, github, gitlab) and is worth considering uniformly, but per-file is the one this issue is about. A unified alternative would be to add an expandPromptWithCachedIncludes (or an opt-in precomputeIncludes) helper in src/util/expand-prompt.ts and use it from all looping generators.
Note this is purely a performance/maintainability fix: callers see the same expanded text either way, so no behavioral migration is required.
- Lenguaje dominante
- TypeScript
- Estrellas
- 2
- Forks
- 1
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Preparar el entorno
Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de joewalker/loop-the-loop
-
bug S4
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
joewalker/loop-the-loop#88 ·
-
Git exec(): child killed by a signal rejects with new Error('') and loses the signal informationAbiertobug S3
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
joewalker/loop-the-loop#84 ·
-
bug S3
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
joewalker/loop-the-loop#83 ·
-
bug S3
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
joewalker/loop-the-loop#82 ·
-
bug S4
Dificultad 1/5 Menos de una hora Aptitud para principiantes 90/100
joewalker/loop-the-loop#79 ·
Todos los issues de joewalker/loop-the-loop
Issues similares
-
[Bug]: [MCP/CLI] Bare loopback IP addresses (127.0.0.1:port) and hosts with ports fail to navigate due to erroneous scheme inferencePosiblemente ocupada @alok-108 la tomó hoy. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
microsoft/playwright#43263 ·
Los mantenedores suelen responder en 1 día
-
bug priority:medium
Dificultad 2/5 1-3 horas Aptitud para principiantes 65/100
Los mantenedores suelen responder en 1 día
-
community first-timers-only good first issue hacktoberfest help wanted low hanging fruit up-for-grabs
Dificultad 1/5 Menos de una hora Aptitud para principiantes 75/100
lingdojo/kana-dojo#32018 · 1 comentario · 5 reacciones ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
paperclipai/paperclip#15751 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
BuilderIO/agent-native#7275 ·
Los mantenedores suelen responder en 1 día