This document implements the Loop Contract plan by making expected_output the primary execution contract across CrewAI task completion, task chaining, runtime state, and UI rendering.
Use one shared structured output contract named LoopOutput.
hookgrippayoffreinforceloopLoopOutput.loop_contract_version to each task config.1.0.0.Use one reusable expected_output template for all narrative tasks and bind each task to output_pydantic=LoopOutput.
Use this exact order and wording:
Every narrative task must define:
descriptionexpected_output (template above)output_pydantic=LoopOutputloop_contract_versionChain narrative tasks using explicit state transitions.
next.hook must be derived from prior.loop.LoopOutput.prior.loop -> next.hook and pass prior task in context=[prior_task].Persist per handoff:
transition_source_task_idtransition_target_task_idtransition_mode (direct|repair|fallback)transition_reasonTreat Loop Contract fields as first-class runtime state.
{
"run_id": "string",
"task_id": "string",
"loop_contract_version": "1.0.0",
"loop_mode": "hard",
"validation": {
"status": "valid",
"errors": []
},
"loop_output": {
"hook": "...",
"grip": "...",
"payoff": "...",
"reinforce": "...",
"loop": "..."
},
"routing": {
"next_agent": "string",
"next_tool": "string",
"next_workflow": "string"
}
}
loop as primary routing hint for next task selection.hook to shape retrieval query intent.payoff to shape summarization output priorities.Render Loop Contract fields as separate UI blocks.
valid, partial, failed.loop as Next recommended question/action.valid: all fields pass schema.partial: schema fails but at least 3 fields are present.failed: schema fails with fewer than 3 valid fields.Add contract checks to prevent drift and breakage.
Per narrative task type, test:
expected_output section order must match canonical template.loop_contract_version must be present.output_pydantic must equal LoopOutput for narrative tasks.Track at minimum:
loop_schema_pass_rateloop_follow_ratedownstream_action_ratesession_depthrepair_ratefallback_rateAdd workflow-level loop_mode.
none: no narrative sections, machine-first output.soft: narrative sections with flexible strictness.hard: strict contract, all checks enforced.Default policy:
hardnone or softUse these interfaces/events in MarsRAG to operationalize the contract.
validation.status, normalized loop_output, error listloop_output, target task configtransition_mode, reasonEmit events per run:
loop.contract.validatedloop.contract.failedloop.transition.plannedloop.transition.fallback_usedloop.ui.renderedloop.action.followedEvent payload fields:
run_idtask_idloop_contract_versionloop_modevalidation_statustransition_modetimestampv1.0.0).prior.loop -> next.hook) with repair/fallback.loop_mode by workflow based on measured outcomes.Implementation is complete when:
expected_output and LoopOutput.prior.loop -> next.hook with fallback.