{
  "schema_version": 1,
  "diagram_type": "architecture",
  "meta": {
    "title": "terminal-error-agent — Inline Failure Diagnoser",
    "output": "terminal-error-agent.architecture.html",
    "locale": "en",
    "quality_profile": "showcase",
    "repository": {
      "url": "https://github.com/himanshu07Rai/terminal-error-agent",
      "revision": "69e5ad7f59e406a4ac84ddda86238284fd25a20f",
      "provider": "github"
    },
    "views": [
      {
        "id": "fail-path",
        "label": "Fail a command",
        "focus": [
          "terminal",
          "preexec",
          "tee",
          "precmd",
          "log",
          "analyze"
        ],
        "note": "A non-zero exit hands the captured stderr to Claude and prints the diagnosis just before your next prompt."
      },
      {
        "id": "diagnose",
        "label": "Ask Claude",
        "focus": [
          "log",
          "analyze",
          "claude"
        ],
        "note": "claude -p receives the command, the exit code and the last 4 KB of stderr, and answers in 2-3 sentences."
      },
      {
        "id": "guardrails",
        "label": "Guardrails",
        "focus": [
          "precmd",
          "analyze",
          "log"
        ],
        "note": "Analysis runs only while TEA_ENABLED=1 — off by default, toggled with tea on, and never persisted across tabs."
      }
    ]
  },
  "components": [
    {
      "id": "terminal",
      "type": "external",
      "label": "Your terminal",
      "sublabel": "any tab · bash or zsh",
      "pos": [
        20,
        300
      ],
      "size": [
        150,
        64
      ]
    },
    {
      "id": "preexec",
      "type": "backend",
      "label": "_tea_preexec()",
      "sublabel": "remembers the command",
      "tag": "trap DEBUG · preexec hook",
      "pos": [
        250,
        140
      ],
      "size": [
        180,
        76
      ],
      "sources": [
        {
          "path": "terminal-agent-error.sh",
          "line": 89,
          "end_line": 93,
          "label": "bash: trap DEBUG"
        },
        {
          "path": "terminal-agent-error.sh",
          "line": 76,
          "label": "zsh: preexec"
        }
      ]
    },
    {
      "id": "tee",
      "type": "messagebus",
      "label": "stderr tee",
      "sublabel": "exec 2> >(tee -a …)",
      "tag": "source-time redirect",
      "pos": [
        540,
        140
      ],
      "size": [
        180,
        64
      ],
      "sources": [
        {
          "path": "terminal-agent-error.sh",
          "line": 18,
          "label": "stderr tee"
        }
      ]
    },
    {
      "id": "log",
      "type": "database",
      "label": ".tea_stderr_<pid>.log",
      "sublabel": "/tmp · per shell PID",
      "tag": "last 4 KB only",
      "pos": [
        830,
        140
      ],
      "size": [
        180,
        72
      ],
      "sources": [
        {
          "path": "terminal-agent-error.sh",
          "line": 13,
          "label": "log path"
        },
        {
          "path": "terminal-agent-error.sh",
          "line": 48,
          "label": "tail -c 4000"
        }
      ]
    },
    {
      "id": "precmd",
      "type": "backend",
      "label": "_tea_precmd()",
      "sublabel": "runs at prompt return",
      "tag": "skips exit 0 and 130",
      "pos": [
        250,
        400
      ],
      "size": [
        180,
        76
      ],
      "sources": [
        {
          "path": "terminal-agent-error.sh",
          "line": 95,
          "end_line": 102,
          "label": "bash: PROMPT_COMMAND"
        },
        {
          "path": "terminal-agent-error.sh",
          "line": 77,
          "end_line": 83,
          "label": "zsh: precmd"
        },
        {
          "path": "terminal-agent-error.sh",
          "line": 21,
          "label": "skip codes"
        }
      ]
    },
    {
      "id": "analyze",
      "type": "backend",
      "label": "_tea_analyze()",
      "sublabel": "guard · read · ask Claude",
      "tag": "one call per failure",
      "pos": [
        580,
        400
      ],
      "size": [
        180,
        76
      ],
      "sources": [
        {
          "path": "terminal-agent-error.sh",
          "line": 43,
          "end_line": 70,
          "label": "_tea_analyze"
        },
        {
          "path": "terminal-agent-error.sh",
          "line": 53,
          "label": "claude guard"
        },
        {
          "path": "terminal-agent-error.sh",
          "line": 59,
          "end_line": 65,
          "label": "claude -p prompt"
        }
      ]
    },
    {
      "id": "claude",
      "type": "external",
      "label": "claude CLI",
      "sublabel": "claude -p",
      "tag": "your Claude Code login",
      "pos": [
        1080,
        400
      ],
      "size": [
        170,
        72
      ]
    }
  ],
  "boundaries": [
    {
      "kind": "region",
      "label": "One shell session · bash or zsh",
      "wraps": [
        "preexec",
        "tee",
        "log",
        "precmd",
        "analyze"
      ],
      "pad": 20
    }
  ],
  "connections": [
    {
      "id": "cmd",
      "from": "terminal",
      "to": "preexec",
      "label": "every command",
      "variant": "emphasis"
    },
    {
      "id": "stderr",
      "from": "preexec",
      "to": "tee",
      "label": "command stderr"
    },
    {
      "id": "tee-log",
      "from": "tee",
      "to": "log",
      "label": "tee -a append"
    },
    {
      "id": "exit",
      "from": "precmd",
      "to": "analyze",
      "label": "exit ≠ 0, 130",
      "variant": "emphasis"
    },
    {
      "id": "guard",
      "from": "precmd",
      "to": "analyze",
      "label": "only while tea on",
      "variant": "dashed",
      "labelDy": 26
    },
    {
      "id": "read",
      "from": "log",
      "to": "analyze",
      "label": "tail -c 4000",
      "fromSide": "bottom",
      "toSide": "top"
    },
    {
      "id": "ask",
      "from": "analyze",
      "to": "claude",
      "label": "claude -p prompt",
      "variant": "emphasis",
      "labelAt": [
        784,
        396
      ]
    },
    {
      "id": "reply",
      "from": "claude",
      "to": "analyze",
      "label": "2-3 sentences",
      "variant": "dashed",
      "labelAt": [
        1056,
        486
      ]
    },
    {
      "id": "print",
      "from": "analyze",
      "to": "terminal",
      "label": "🤖 printed to stderr",
      "variant": "dashed",
      "fromSide": "bottom",
      "toSide": "bottom"
    }
  ],
  "cards": [
    {
      "dot": "cyan",
      "title": "Trigger",
      "items": [
        "preexec stores the last command line — bash via `trap … DEBUG`, zsh via add-zsh-hook preexec",
        "precmd reads $? at prompt return and skips 0 and 130, so Ctrl-C never triggers a call",
        "One claude call per failed line: pipelines and && chains analyze only the last simple command"
      ]
    },
    {
      "dot": "emerald",
      "title": "Capture",
      "items": [
        "exec 2> >(tee -a \"$_tea_log\" >&2) mirrors stderr to /tmp/.tea_stderr_<pid>.log while still showing it",
        "Only the last 4000 bytes are read, then the log is truncated so the next command starts clean",
        "A child that redirects its own stderr (2>/dev/null) never reaches the log — the tee only sees the shell's stream"
      ]
    },
    {
      "dot": "violet",
      "title": "Guardrails",
      "items": [
        "Off by default: TEA_ENABLED stays 0 until you run `tea on` in that shell — nothing persists between tabs",
        "Silently skips when `claude` is not on PATH, so machines without the CLI are unaffected",
        "Costs ~1-2 s per failure before the next prompt, and a failed claude call prints that error with no retry"
      ]
    }
  ]
}
