Skip to content

Add /foreach command for deterministic batch iteration and isolated prompt execution #744

Closed

Description

@philippeback

Problem

When running repetitive or batch tasks across many files (e.g., generating documentation, migrating syntax, running code audits, or batch-refactoring dozens of files), relying on an LLM supervisor loop to iterate over file lists is fragile and inefficient:

  1. Supervisor Fatigue & Cognitive Drift: Across 20+ files, agent supervisors frequently lose track of their position, re-process already completed files, or terminate prematurely.
  2. Excessive Round-Trip Latency: A supervisory agent delegating to worker subagents incurs multi-turn coordination overhead (delegating -> checking worker status -> re-reading the written file to verify -> deciding next file), turning a 2-turn job into 7+ round-trips (~90s per file on local models).
  3. Context Accumulation & State Leaks: Without deterministic boundaries, conversation messages, cached file chunks (MessageTag.READONLY_FILES), and internal resource sets (abs_read_only_fnames) accumulate across iterations, causing prompt sizes to balloon into context exhaustion.

Proposed Solution

Introduce a native /foreach slash command for deterministic, crash-resilient batch execution:

/foreach [options] <glob_pattern> [::] <prompt_template>

Key Capabilities:

  1. Glob & Path Resolution:
    • Matches filesystem paths using glob expressions (e.g. src/**/*.py or pine_sources/PBK_*.pine).
    • Populates template placeholders: {file}, {path}, {filename}, {stem}, {dir}, {index}, and {total}.
    • Uses safe_substitute() to allow JSON strings containing literal curly braces in prompts (e.g. {"read_only": ["{file}"]}) without formatting errors.
  2. Zero-Overhead Idempotency (-u / --unless-exists <path>):
    • Checks if evaluated output path already exists before initiating an inference turn. If the target exists, it skips execution in microseconds.
    • Makes batch workloads completely crash-resilient: re-running resumes immediately from the first incomplete item.
  3. Holistic Context & Cache Isolation (-c / --clear):
    • Ensures an isolated slate for every iteration.
    • Clears message tags (CUR, DONE, DIFFS, FILE_CONTEXTS, READONLY_FILES, CHAT_FILES, EDIT_FILES, LINT).
    • Wipes tracked resource sets (abs_fnames, abs_read_only_fnames, abs_read_only_stubs_fnames, files_added_in_exploration) and flushes chunk removal buffers so prompt sizes remain lean and constant.
  4. Safety & Control:
    • -n <limit>: Process at most N items for testing.
    • --dry-run: Preview candidate matches and substituted prompt templates without executing.
    • --delay <seconds>: Optional pause between iterations to allow GPU cooling or rate limit compliance.
    • Clean console formatting without escaped markup or terminal artifacts.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions