loop: pauses interPromptPause seconds after the last prompt even when the generator is exhausted

Open Beginner friendly
#42 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
84/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
typescript
Domain
tooling

Research direction

Start in src/loop.ts around lines 154-161 and inspect the loop entry point used by the one-prompt generator reproduction. Move the pause so it occurs only before non-initial prompts, then verify that a single yielded prompt completes without a trailing delay while multiple prompts remain spaced apart.

Written by the indexing model from the issue text.

Description

bug S4

Observed behavior

In src/loop.ts, the interPromptPause sleep is placed at the bottom of the for await body (lines 154-161):

completed++;
if (completed >= maxPrompts) {
  logger.state(`Reached limit of ${maxPrompts} prompts`);
  return `Done (reached limit of ${maxPrompts} prompts)`;
}

// istanbul ignore else
if (interPromptPause !== 0) {
  logger.info(`Pausing ${interPromptPause}s before next prompt`);
  console.log(`Pause (${interPromptPause}s) before starting next prompt`);
  await new Promise(resolve => {
    setTimeout(resolve, interPromptPause * 1_000);
  });
}

When the prompt generator yields its final prompt and is then exhausted (i.e. the run is ending naturally, not because maxPrompts was hit), the loop body still runs the pause after the final prompt before the for await advances, sees the generator is done, and exits with 'Done'. The user waits interPromptPause seconds for nothing.

The early-return for maxPrompts correctly skips the pause (because it returns before reaching it), but there is no equivalent check for "no more prompts coming". The bug is invisible at the default PAUSE_SECS = 5 but becomes obvious when users tune the pause up to manage rate limits (60s+).

Expected behavior

After the last prompt yielded by the generator, the loop should exit immediately rather than sleeping interPromptPause seconds first. The pause is supposed to space out between prompts; there is nothing to space against once the generator is exhausted.

Minimal reproduction

import { loop } from 'loop-the-loop';

class OnePrompt {
  async *generate() {
    yield { id: 'only', prompt: 'hi' };
  }
}

console.time('loop');
await loop({
  name: 'demo',
  agent: /* any agent that returns success quickly */,
  promptGenerator: new OnePrompt(),
  interPromptPause: 30,
});
console.timeEnd('loop');

Observed: loop: ~30s (one quick agent call + a 30 s tail pause).
Expected: loop: <1s (one quick agent call, no trailing pause).

Suggested fix

Move the pause to the top of the loop body and skip it on the first iteration, so it pauses before each non-initial prompt rather than after each one:

let completed = 0;
let glitchCount = 0;
let first = true;
for await (const prompt of promptGenerator.generate(loopState)) {
  if (!first && interPromptPause !== 0) {
    logger.info(`Pausing ${interPromptPause}s before next prompt`);
    console.log(`Pause (${interPromptPause}s) before starting next prompt`);
    await new Promise(resolve => {
      setTimeout(resolve, interPromptPause * 1_000);
    });
  }
  first = false;
  // ... process prompt ...
}

This preserves the inter-prompt spacing semantics and naturally avoids the trailing pause.

Dominant language
TypeScript
Stars
2
Forks
1
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from joewalker/loop-the-loop

All issues in joewalker/loop-the-loop

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.