loc bengaluru, ist | local --:-- srijanshukla18@gmail.com →
[post]/tech/feature-end-extension

splitting a multi-step cleanup prompt into gated turns in Pi

/ 3 min read· ai

I wanted one cleanup workflow after each feature and tried building a Pi extension for it. Packing all three steps into a single prompt failed quietly, so the extension now runs them as sequential gated steps.

Every time I finish coding a feature with an agent, I run the same 3 cleanup steps:

  1. remove fallback code
  2. remove dead code
  3. update docs

I got tired of typing those three prompts by hand every time, so I wrote a Pi extension to run the whole cleanup with a single slash command.

Pi initially suggested making 3 separate slash commands. I didn’t want that - the whole point was having one command to run at the end of a feature.

So for v1, I just stuffed all 3 instructions into one prompt:

Review the codebase and complete the following:
1) Remove fallback code
2) Remove dead code
3) Update all .md files in the repo

And it failed in the most annoying way possible: the model would do step 1, do half of step 2, completely ignore step 3 (updating the .md files), and say “Done!”.

No error, no warning. If you weren’t paying close attention to the diff, you’d think the docs were updated when the model just got lazy at the end of the turn.

The fix: 3 separate turns with an explicit gate

Instead of sending one big prompt, I changed the Pi extension to run fallback, dead-code, and docs as three separate sequential turns.

At the end of each turn, the prompt forces the model to output a strict status token:

Reply with exactly one of:
- STEP_DONE - if you completed the task above
- STEP_SKIPPED - if you skipped or did nothing

Then a brief one-line reason.

And the extension checks the reply before moving on to the next task. If the model doesn’t output STEP_DONE or STEP_SKIPPED, it retries that step (up to 3 times):

async function runTask(pi, ctx, task, attempt = 1) {
  await ctx.waitForIdle();

  const seenEntryIds = new Set(
    ctx.sessionManager.getBranch().map((entry) => entry.id),
  );

  pi.sendUserMessage(buildInstruction(task, attempt));

  await waitForTurnToStart(ctx, seenEntryIds);
  await ctx.waitForIdle();

  const reply = getNewAssistantReply(ctx, seenEntryIds);
  const result = parseTaskResult(reply);

  if (result === "done" || result === "skipped") {
    return result;
  }

  if (attempt >= MAX_ATTEMPTS) {
    return "failed";
  }

  return runTask(pi, ctx, task, attempt + 1);
}

Why STEP_SKIPPED is a valid state

Sometimes there genuinely isn’t any fallback code to delete, or the docs are already up to date.

The problem with the single-prompt version wasn’t that docs didn’t change - it was that I couldn’t tell the difference between “I checked the docs and nothing needed updating” vs “I forgot to check the docs”.

By giving docs its own dedicated turn and requiring STEP_DONE or STEP_SKIPPED + a 1-line reason, the model actually inspects the .md files every time and tells me why it skipped them if no edits were needed.

Costs slightly more tokens coz it’s 3 turns instead of 1, but it never silently drops the last step anymore.