GmMars1

MarsRAG Loop Contract Implementation

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.

1) Canonical Contract

Use one shared structured output contract named LoopOutput.

Required fields

Validation rules

Contract completion rule

Contract versioning

2) CrewAI Task Contract Standardization

Use one reusable expected_output template for all narrative tasks and bind each task to output_pydantic=LoopOutput.

Standard template

Use this exact order and wording:

  1. HOOK: Open with one clear tension or stakes statement anchored to the user goal.
  2. GRIP: Provide the key insight, mechanism, or diagnosis that keeps attention.
  3. PAYOFF: Deliver specific value, recommendation, or resolution.
  4. REINFORCE: Strengthen confidence with evidence, constraints, or practical framing.
  5. LOOP: End with the best next question or action that naturally advances the flow.

Task definition requirements

Every narrative task must define:

3) Contract-Native Chaining

Chain narrative tasks using explicit state transitions.

Required transition rule

Transition behavior

  1. Validate prior LoopOutput.
  2. If valid, map prior.loop -> next.hook and pass prior task in context=[prior_task].
  3. If invalid/missing, run one repair attempt.
  4. If repair fails, use safe fallback hook:
    • “What is the single most important next step to resolve the current uncertainty?”

Transition metadata

Persist per handoff:

4) Runtime State Integration (MarsRAG Orchestrator)

Treat Loop Contract fields as first-class runtime state.

Runtime state object

{
  "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"
  }
}

Runtime usage rules

5) UI Contract Rendering

Render Loop Contract fields as separate UI blocks.

UI requirements

UI state mapping

6) Reliability Safeguards

Add contract checks to prevent drift and breakage.

Regression tests

Per narrative task type, test:

  1. Schema conformance success path.
  2. Missing field failure.
  3. Empty string failure.
  4. Over-length field failure.
  5. Chaining transition from prior loop.
  6. Fallback transition path.

Contract lint checks

Required metrics

Track at minimum:

7) Loop Intensity Controls

Add workflow-level loop_mode.

Default policy:

8) Reference Interfaces and Events

Use these interfaces/events in MarsRAG to operationalize the contract.

Interface: Contract validation

Interface: Transition planner

Interface: UI view model mapper

Event model

Emit events per run:

Event payload fields:

9) Rollout Sequence

  1. Ship schema and canonical task template (v1.0.0).
  2. Enable contract lint in CI for narrative tasks.
  3. Enable chaining rule (prior.loop -> next.hook) with repair/fallback.
  4. Ship UI section rendering + status badges.
  5. Start metrics collection and weekly review.
  6. Tune loop_mode by workflow based on measured outcomes.

10) Definition of Done

Implementation is complete when: