An agent re-plans after three saga steps committed. Adopt, amend, compensate or keep each one by intent key, compensations first, so no effect is applied twice.
The problem a re-plan creates
A saga runs a business process as a sequence of local transactions, each with a compensation that semantically undoes it. The orchestrator records which steps have completed, and when a later step fails it runs the compensations of the completed steps in reverse order. That is the classic contract, and it assumes the plan does not change while it runs.
Put an agent in charge of planning and the assumption breaks. At the help desk of Belgavi's Book Store, Geetha's support agent handles CASE-6204: a damaged hardback. Its first plan reserves a replacement copy, schedules a courier pickup for the damaged one, and emails the customer that a replacement is on its way. Three steps commit. Then the customer replies that they would rather have a refund. The agent produces a new plan: schedule the pickup, refund the card, email the customer about the refund.
If the orchestrator simply runs the new plan from the top, the courier pickup is scheduled twice and the reserved replacement stays reserved forever. If it compensates the whole old plan first and then runs the new one, it cancels a courier pickup it is about to schedule again, and it tries to undo an email, which cannot be undone. Neither is correct. What is needed is a reconciliation step between the old plan's completed work and the new plan, decided by the orchestrator, not by the model.
This guide describes that step. The companion example implements it in about sixty lines and runs four scenarios.
What stays deterministic
The agent proposes plans. Everything that decides what has already happened, and what to do about it, stays in code:
- the step catalogue: which steps exist, which compensation each has, and which have none because their effect leaves the system (emails, card refunds, messages to a carrier);
- the record of completed steps, written by the orchestrator in the same transaction as each step's local effect;
- the intent key of every step, built from case facts such as
courier:CASE-6204orrefund:CASE-6204, never from text the model wrote; - the reconciliation rule below, and the order in which compensations and new steps run.
The model never decides whether a courier pickup was already scheduled. That is a fact in the saga log. Asking the model would turn a lookup into a guess.
Reconciling completed steps with the new plan
For each step the old plan completed, compare it with the new plan by intent key. There are four outcomes.
| Completed step | New plan | Decision |
|---|---|---|
| same intent, same step, same arguments | wants it | adopt: mark it done in the new plan, do not run it again |
| same intent, different arguments | wants it | amend: compensate the old one, then run the new one |
| intent not in the new plan | does not want it | compensate |
| no compensation exists | either | keep, and escalate to a person if the new plan changed or dropped it |
Then build one schedule: compensations first, in reverse completion order, then the new plan's steps that were not adopted or kept. Running compensations first matters when they release a shared resource. A released stock reservation can be taken by the new plan, and a cancelled pickup slot can be scheduled again.
The core of the rule is short:
for s in completed:
comp = catalogue[s['step']]['compensation']
w = wanted.get(s['intent'])
if w and w['step'] == s['step'] and w['args'] == s['args']:
decide(s, 'adopt'); skip.add(s['intent'])
elif w and comp is None:
decide(s, 'keep'); skip.add(s['intent']) # changed, but cannot be undone
elif w:
decide(s, 'amend'); to_compensate.append(s)
elif comp is None:
decide(s, 'keep') # dropped, but cannot be undone
else:
decide(s, 'compensate'); to_compensate.append(s)
schedule = [comp_of(s) for s in reversed(to_compensate)]
schedule += [s for s in new_plan if s['intent'] not in skip]Comparing arguments by equality is deliberate. The orchestrator should not judge whether a courier slot on 5 November is "close enough" to one on 6 November. If the arguments differ, the new plan gets a new effect and the old one is reversed.
Worked example
The companion example runs four synthetic cases and counts each intent's net effects after the schedule, where 1 means applied once and 0 means applied and reversed.
CASE-6204, replacement to refund. The reservation is compensated (net 0), the courier pickup is adopted (net 1, not 2), and the replacement email is kept and escalated, because it cannot be recalled and the customer now holds a message that is no longer true. The schedule is release_stock, refund_card, email_customer. The naive re-run would have left the stock reserved and scheduled the pickup twice.
CASE-6211, store credit lowered. The agent re-plans after a policy lookup and lowers the credit from 25.00 to 15.00. The decision is amend: revoke the old credit, issue the new one. The naive re-run issues a second credit, so the customer ends up with 40.00.
CASE-6230, the same plan again. A retry produces an identical plan. The reservation is adopted and only the courier step runs. This is the most common case, and it is the one that silently doubles effects when nothing reconciles.
CASE-6242, an irreversible step changed. A 30.00 card refund already ran, and the new plan says 22.00. A card refund has no compensation in the catalogue, so the step is kept and the case goes to a person. The schedule is empty. Refunding 22.00 more would be wrong, and so would asking the payment provider to claw back 8.00 without a human decision.
| Case | Decisions | Net effects after reconcile | Naive re-run |
|---|---|---|---|
| CASE-6204 | compensate, adopt, keep | stock 0, courier 1 | stock 1, courier 2 |
| CASE-6211 | amend | credit 1 | credit 2 |
| CASE-6230 | adopt | stock 1 | stock 2 |
| CASE-6242 | keep, escalate | refund 1 | refund 2 |
Failure modes
Intent keys built from model output. If the key for the courier step includes the agent's free-text description of it, two plans that mean the same thing get different keys and nothing is ever adopted. Build keys from identifiers the workflow already holds.
Compensating before the new plan is validated. Reconciliation must run only after the new plan has passed the same validation as the first one: catalogue membership, argument schemas, limits. Otherwise a plan that will be rejected has already caused compensations.
A re-plan racing a running step. If a step is in flight when the new plan arrives, its outcome is not yet known. Wait for it to finish or time out, record the result, and only then reconcile. The saga log is the single source of truth, so the reconciler must read it after the step settles.
Compensations that fail. A failed compensation leaves the saga in a state neither plan expected. Retry it with the same idempotency key, and if it keeps failing, stop and escalate rather than starting the new plan on top of it.
Repeated re-planning. An agent that re-plans after every tool response can oscillate between two plans, and each swing compensates and redoes work. Cap the number of re-plans per saga and treat the cap as a reason to hand the case to a person.
Migration steps for an existing orchestrator
- Add an intent key to every step in the catalogue, and mark the steps that have no compensation. Many teams find their catalogue already implies this, but nobody has written it down.
- Record arguments with each completed step, not just its name, so equality checks are possible.
- Add a
replace_planentry point to the orchestrator that validates the new plan, reconciles, and writes the reconciliation decisions to the saga log before running anything. - Route kept-and-changed steps to the existing human review queue, with both plans attached.
- Replay recorded sagas with synthetic re-plans in a test environment and assert that no intent ever has a net effect above one.
When an agent is unnecessary
If the set of possible plan changes is small and known, such as replacement to refund or refund to store credit, write them as explicit transitions in the workflow. The reconciliation rule still applies, but the new plan comes from a table rather than a model. An agent earns its place when customers ask for changes nobody enumerated in advance. Even then, its job ends at proposing the new plan.
Verification
The companion tests assert, for every scenario, that no intent ends with more than one net effect, that compensations come first and in reverse order, and that the reconciler rejects steps outside the catalogue and plans that name one intent twice. Run them after every change to the catalogue, because adding a step without a compensation changes which decisions are possible.