name: ideal-pseudocode
Clean up one function at a time by writing the pseudocode it should read as, naming every delta between that and the real code, and closing only the gaps the user approves.
One function per round. Never touch code before the user picks a direction.
ts-fenced code block — TypeScript-flavored for syntax highlighting, but pseudocode: comments over mechanics, one line per idea, every arm of a loop visible as an arm. For a dense or unfamiliar function, first show the current structure as pseudocode, then the ideal.if chains for control flow; matchers are for producing values.Every round should read like this (abridged from a real one, on an Effect step-runner):
runStepit is. The ideal pseudocode:// One logical step: one settled model call, however many attempts it takes. // An attempt can end without settling in two ways: // transient provider failure -> retry the same call, same assistant message // compaction rewrote history -> rebuild the request and call again function runStep(session, promotable, step) { while (true) { const result = callModel(session, promotable, step) if (result.completed) return result if (result.retryable) { wait(retrySchedule) // exhausted -> publish Step.Failed, fail drain promotable = none // never re-promote on a second attempt continue } // compaction restarted the step: fresh request from rewritten history promotable = none step = result.step } }Comparing against the real thing, three deltas:
- The retry arm doesn't live in the loop. In the pseudocode, retry is just another loop arm. In the real code it's smuggled through the error channel:
Effect.tapErrormutating loop variables viaEffect.syncclosures, thenEffect.retryOrElsere-running a suspended call. Four mutable variables mutated from three different places — the state flow is invisible unless you already know the trick.recoverOverflow: typeof compaction.compact | undefined— a function-typed parameter used as a one-shot armed/disarmed flag. The callee already closes overcompaction; a boolean says what it is.assistantMessageIDexists solely to resume the same durable message across retries — fine, but only legible once the retry arm is visible in the loop.The unifying move for (1): a schedule driver, so a retryable failure becomes a caught value handled in the loop body exactly like the compaction arm. One honest trade: it's more manual than the blessed combinator — but the manual version is the loop the combinator was hiding, and the loop already exists for restarts. Two restart mechanisms, one control structure.
Want me to apply it — unified loop, simplified schedule input, boolean
recoverOverflow?