feat(x): graceful wrap-up when a chat turn hits its model-call limit

Interactive turns no longer die with a raw 'Model call limit of N
reached' error. The final budgeted model call is composed as a wrap-up:
the durable request records wrapUp: true, and the composer strips tools
and appends a notice telling the model to answer with what it has,
state what's unfinished, and mention the limit is configurable in
Settings -> Advanced. The turn then completes normally with that answer.

Headless turns (and a wrap-up call that still emits tool calls) keep
the hard model-call-limit failure so automation retains the
machine-readable outcome; sub-agents already surface partial results.
If a hard limit failure still reaches the chat UI, the error bubble now
explains the limit and points at the setting instead of the raw error.
This commit is contained in:
Gagan 2026-07-21 15:50:04 +05:30
parent 47453fc333
commit f270f39e35
6 changed files with 129 additions and 2 deletions

View file

@ -233,8 +233,26 @@ export const ModelRequest = z.object({
contextRef: TurnContextRef.optional(),
messages: z.array(ModelRequestMessageRef),
parameters: z.record(z.string(), z.json()),
// Final budgeted call of an interactive turn: the composer strips tools
// and appends wrapUpNotice() so the model ends with a real answer
// instead of the turn failing with model-call-limit (issue #768).
wrapUp: z.literal(true).optional(),
});
// The message appended to a wrap-up call's request. A pure function of the
// turn's durable config, so replaying the log reproduces the wire bytes.
export function wrapUpNotice(
maxModelCalls: number,
): z.infer<typeof ConversationMessage> {
return {
role: "user",
content:
`[system notice] You are on the final model call this turn's budget allows (model-call limit: ${maxModelCalls}); tools are no longer available. ` +
"Give your best final answer using only the work above: present what you completed, state clearly what remains unfinished, " +
"and let the user know the turn was cut short by the model-call limit, which they can raise in Settings → Advanced.",
};
}
export const ModelCallRequested = z.object({
type: z.literal("model_call_requested"),
turnId: z.string(),