run<P> static method
Proposes, previews, and keeps or discards one proposal as a unit.
propose runs inside atomically and may write. refuse answers
non-null when a proposal cannot be kept, and then confirm is not asked.
confirm answers whether the user keeps it. apply writes an accepted
proposal, inside the same scope.
Never throws: a failure is AgentProposalStatus.failed with the AgentProposalOutcome.error, so a caller has one value to read whatever happened.
Implementation
static Future<AgentProposalOutcome<P>> run<P>({
required AgentAtomicScope atomically,
required String label,
required Future<P> Function() propose,
required Future<bool> Function(P proposal) confirm,
FutureOr<void> Function(P proposal)? apply,
Object? Function(P proposal)? refuse,
}) async {
P? proposal;
Object? refusal;
var status = AgentProposalStatus.accepted;
Object? error;
try {
await atomically(label, () async {
final made = await propose();
proposal = made;
refusal = refuse?.call(made);
if (refusal != null) throw const _Discarded();
if (!await confirm(made)) throw const _Discarded();
await apply?.call(made);
});
} on _Discarded {
status = refusal == null
? AgentProposalStatus.rejected
: AgentProposalStatus.failed;
} on Object catch (e) {
status = AgentProposalStatus.failed;
error = e;
}
return AgentProposalOutcome(
status: status,
proposal: proposal,
refusal: refusal,
error: error,
);
}