Treat Per-Task Model Switching... Note

Treat Per-Task Model Switching as a Concurrency Protocol

Changing the AI model for a running task is a distributed operation, not a simple settings update. It involves reading the current task, preparing credentials, requesting a restart, receiving the result, and persisting the active model. When multiple model switch requests overlap, the order of completion can differ from the order of requests, necessitating a rule to determine which intent wins. The MonkeyCode system records model switch attempts with details like model IDs and request IDs. A typical workflow involves creating a switch record, asking the taskflow to restart, and then completing the switch record. However, explicit compare-and-swap generation or per-task serialization contracts around overlapping requests were not established in a source review.The instability of the "last completion wins" approach is demonstrated by scenarios where a later, successful completion can overwrite an earlier one due to network timing. A companion simulator visualizes this order dependence, showing that the caller's latest intent is not inherently considered. To address this, a monotonic generation is proposed, assigning a unique generation number to each request. The system should only update the active model if the completion's generation matches the task's current requested generation. This generation guard ensures that stale operations are not applied, even if they complete later.The generation guard is only one part of a comprehensive protocol that needs to define contracts for duplicate requests, competing requests, late successes, restart failures, process crashes, session loading, and credential binding. Serialization, such as using per-task locks, is an alternative but introduces complexities like lease expiry and fairness. Unit tests should validate this protocol with controlled interleavings of operations at various stages. The invariant is that the active model should always correspond to the successful result for the greatest non-superseded generation. Treating model switching as a protocol ensures consistency across UI, audit records, retries, and persistence.