Ralph Autonomous AI Agent Loop

repository·main·Indexed 12 days ago

https://github.com/snarktank/ralph

An autonomous AI agent loop designed to execute software development tasks by repeatedly running AI coding tools like Amp or Claude Code until a Product Requirements Document (PRD) is fully implemented. Ralph uses a 'fresh context' approach for each iteration, preserving memory via Git history, a progress.txt log, and a prd.json status file to maintain high quality and reliability during the implementation of user stories.

Tokens
6.8K
Snippets
16
Records
26
Agent score
98%

What's inside Ralph

  1. Best practices for PRD stories and tasks

    main

    To ensure Ralph succeeds, follow these guidelines for task sizing and verification:

    Task Sizing

    Each story in prd.json must be small enough to fit within a single LLM context window.

    • Good (Small): Adding a single database column, creating one UI component, or updating one server action.
    • Bad (Too Large): "Build the entire dashboard" or "Add authentication". These should be split into multiple smaller stories.

    Verification

    • Feedback Loops: Ensure your project has automated typechecking and tests. Ralph relies on these to verify work.
    • UI Stories: For frontend tasks, include "Verify in browser using dev-browser skill" in the acceptance criteria so Ralph can interactively confirm changes.
  2. How to size and order user stories for Ralph

    main

    To ensure successful autonomous execution, follow these two critical rules for user stories:

    1. Story Size (The One-Iteration Rule)

    Each story must be completable in ONE Ralph iteration. Ralph spawns a fresh instance per iteration with no memory of previous work. If a story is too large, the LLM will run out of context and produce broken code.

    • Good size: "Add a database column", "Update a server action", "Add a UI component".
    • Too big: "Build the entire dashboard" (Split into schema, queries, and UI components).
    • Rule of thumb: If you cannot describe the change in 2-3 sentences, it is too big.

    2. Story Ordering (Dependencies First)

    Stories execute in priority order. You must order them so that earlier stories provide the foundation for later ones:

    1. Schema/database changes (migrations)
    2. Server actions / backend logic
    3. UI components that use the backend
    4. Dashboard/summary views that aggregate data
  3. How Ralph manages context and memory

    main

    Ralph operates on the principle that each iteration is a fresh instance. To prevent context bloat and ensure reliability, every iteration starts with a clean context window.

    Memory is preserved across these fresh instances via three specific mechanisms:

    1. Git History: Commits from previous iterations provide context on what was changed.
    2. progress.txt: An append-only file containing learnings and context discovered during previous runs.
    3. prd.json: Tracks the status of user stories (the passes field).
  4. PRD Structure and Requirements

    main

    A generated PRD follows a strict structure to ensure clarity for implementation. When reviewing or manually creating a PRD using this pattern, ensure the following sections are present:

    1. Introduction/Overview: Brief description of the feature and the problem.
    2. Goals: Specific, measurable objectives.
    3. User Stories: Formatted as ### US-XXX: [Title]. Each must include a Description ("As a [user], I want [feature] so that [benefit]") and Acceptance Criteria (a verifiable checklist).
      • Note: For any story involving UI changes, the acceptance criteria must include: Verify in browser using dev-browser skill.
    4. Functional Requirements: A numbered list (e.g., FR-1: ...) of specific, unambiguous functionalities.
    5. Non-Goals (Out of Scope): Explicitly defines what the feature will NOT include to manage scope.
    6. Design Considerations (Optional): UI/UX requirements or component reuse.
    7. Technical Considerations (Optional): Constraints, dependencies, or integration points.
    8. Success Metrics: How success is measured (e.g., "Reduce time to complete X by 50%").
    9. Open Questions: Areas needing further clarification.
    ### US-001: [Title]
    **Description:** As a [user], I want [feature] so that [benefit].
    
    **Acceptance Criteria:**
    - [ ] Specific verifiable criterion
    - [ ] Typecheck/lint passes
    - [ ] **[UI stories only]** Verify in browser using dev-browser skill
  5. How to consolidate Codebase Patterns in progress.txt

    main

    To prevent future iterations from repeating mistakes, maintain a ## Codebase Patterns section at the TOP of progress.txt.

    Only add patterns that are general and reusable. Do not include story-specific implementation details or temporary debugging notes.

    Examples of patterns to include:

    • Specific template usages (e.g., sql<number> for aggregations).
    • Migration requirements (e.g., IF NOT EXISTS).
    • Export conventions (e.g., exporting types from actions.ts for UI components).
    ## Codebase Patterns
    - Example: Use `sql<number>` template for aggregations
    - Example: Always use `IF NOT EXISTS` for migrations
    - Example: Export types from actions.ts for UI components
  6. Enable type-aware ESLint rules for production

    main

    For production applications, it is recommended to enable type-aware lint rules by replacing standard tseslint configurations with type-checked versions. You must also configure parserOptions to point to your tsconfig files so the linter can access type information.

    export default defineConfig([
      globalIgnores(['dist']),
      {
        files: ['**/*.{ts,tsx}'],
        extends: [
          // Replace tseslint.configs.recommended with one of these:
          tseslint.configs.recommendedTypeChecked,
          // Or for stricter rules:
          tseslint.configs.strictTypeChecked,
          // Or for stylistic rules:
          tseslint.configs.stylisticTypeChecked,
        ],
        languageOptions: {
          parserOptions: {
            project: ['./tsconfig.node.json', './tsconfig.app.json'],
            tsconfigRootDir: import.meta.dirname,
          },
        },
      },
    ])
  7. Convert PRDs to Ralph prd.json format

    main

    Use the ralph skill to convert existing Product Requirement Documents (PRDs) in markdown or text format into the prd.json format required for Ralph's autonomous execution.

    Trigger phrases:

    • convert this prd
    • turn this into ralph format
    • create prd.json from this
    • ralph json
  8. Use the PRD Generator skill

    main

    The prd skill is used to generate a detailed Product Requirements Document (PRD) for new features or projects. It is designed to create clear, actionable documentation suitable for implementation by developers or AI agents.

    Trigger Phrases:

    • create a prd
    • write prd for [feature]
    • plan this feature
    • requirements for [feature]
    • spec out [feature]

    Workflow:

    1. Input: Provide a description of the feature.
    2. Clarification: The tool will ask 3-5 essential clarifying questions (using lettered options like A, B, C) to resolve ambiguities regarding goals, functionality, scope, or success criteria.
    3. Generation: Once questions are answered, a structured PRD is generated.
    4. Output: The final document is saved as a Markdown file in the tasks/ directory using the naming convention prd-[feature-name].md (kebab-case).
  9. Use Ralph via Claude Code Marketplace

    main

    You can add Ralph as a plugin to Claude Code using the marketplace commands. This provides two specific skills:

    • /prd: Generates Product Requirements Documents.
    • /ralph: Converts PRDs to the prd.json format required for autonomous execution.

    These skills are automatically triggered by natural language prompts like "create a prd" or "turn into ralph format".

    /plugin marketplace add snarktank/ralph
    /plugin install ralph-skills@ralph-marketplace
  10. Define verifiable Acceptance Criteria

    main

    Acceptance criteria must be specific and checkable by an agent. Avoid vague language like "Good UX" or "Works correctly".

    Required Criteria:

    • Every story: Must include "Typecheck passes" as the final criterion.
    • Testable logic: Include "Tests pass".
    • UI changes: Must include "Verify in browser using dev-browser skill". Frontend stories are not considered complete until visually verified via the dev-browser skill.

    Examples:

    • Good: "Add status column to tasks table with default 'pending'"
    • Bad: "Handles edge cases"
  11. Add React-specific lint rules to ESLint

    main

    To enforce React-specific best practices, install and configure eslint-plugin-react-x and eslint-plugin-react-dom. These plugins provide recommended configurations for both React logic and React DOM usage.

    // eslint.config.js
    import reactX from 'eslint-plugin-react-x'
    import reactDom from 'eslint-plugin-react-dom'
    
    export default defineConfig([
      globalIgnores(['dist']),
      {
        files: ['**/*.{ts,tsx}'],
        extends: [
          // Enable lint rules for React
          reactX.configs['recommended-typescript'],
          // Enable lint rules for React DOM
          reactDom.configs.recommended,
        ],
        languageOptions: {
          parserOptions: {
            project: ['./tsconfig.node.json', './tsconfig.app.json'],
            tsconfigRootDir: import.meta.dirname,
          },
        },
      },
    ])
  12. Install Ralph skills globally for Amp

    main

    You can install Ralph's skills globally so they are available to Amp across all your projects by copying the skill directories to your Amp configuration folder.

    cp -r skills/prd ~/.config/amp/skills/
    cp -r skills/ralph ~/.config/amp/skills/