···11-# **Tangled CLI: Architecture & Implementation Plan**
11+# Tangled CLI: Architecture & Implementation Plan
2233-## **1\. Project Overview**
33+## 1. Project Overview
4455**Goal:** Create a context-aware CLI for tangled.org that bridges the gap between the AT Protocol (XRPC) and standard Git. **Philosophy:** Follow the **GitHub CLI (gh)** standard: act as a wrapper that creates a seamless experience where the API and local Git repo feel like one unified tool.
6677-## **2\. Prior Art Analysis: GitHub CLI (gh) vs. Tangled CLI**
77+## 2. Prior Art Analysis: GitHub CLI (gh) vs. Tangled CLI
8899| Feature | GitHub CLI (gh) Approach | Tangled CLI Strategy |
1010| :------------- | :--------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------- |
···1414| **Filtering** | \--json name,url (filters fields). | **Plan:** Support basic \--json flag first. Add field filtering (--json "cloneUrl,did") to save LLM context window tokens. |
1515| **Extensions** | Allows custom subcommands. | _Out of Scope for V1._ |
16161717-## **3\. High-Level Architecture (Refined)**
1717+## 3. High-Level Architecture (Refined)
18181919The CLI acts as a "Context Engine" before it even hits the API.
2020`graph TD`
···3333 `API --> Output`
3434 `Shell --> Output`
35353636-## **4\. Tech Stack (TypeScript)**
3636+## 4. Tech Stack (TypeScript)
37373838| Component | Library | Purpose |
3939| :---------------- | :-------------------- | :----------------------------------------------------------- |
···4545| **Interactivity** | **@inquirer/prompts** | Modern prompts for humans. |
4646| **Formatting** | **cli-table3** | **New:** For gh-style pretty tables in Human Mode. |
47474848-## **5\. Agent Integration (The "LLM Friendly" Layer)**
4848+## 5. Agent Integration (The "LLM Friendly" Layer)
49495050To make this tool accessible to Claude Code/Gemini, we adopt gh's best patterns:
51515252-### **Rule 1: Context is King**
5252+### Rule 1: Context is King
53535454LLMs often hallucinate repo IDs.
55555656- **Design:** If the user/LLM runs tangled issue list inside a folder, **do not** ask for the repo DID. Infer it.
5757- **Fallback:** Only error if no git remote is found.
58585959-### **Rule 2: Precision JSON (--json \<fields\>)**
5959+### Rule 2: Precision JSON (--json \<fields\>)
60606161LLMs have token limits. Returning a 50KB repo object is wasteful.
62626363- **Feature:** tangled repo view \--json name,cloneUrl,description
6464- **Implementation:** Use lodash/pick to filter the API response before printing to stdout.
65656666-### **Rule 3: The CLAUDE.md Context**
6666+### Rule 3: Fail Fast, Fail Loud
67676868-Include this file in the repo so Claude knows how to drive the CLI.
6969-`# Tangled CLI Cheatsheet`
7070-7171-`## Principles`
7272-``1. **Context Aware:** Run commands inside the repo directory. The CLI infers the repo DID from `git remote`.``
7373-``2. **Flags:** Always use `--json` and `--no-input`.``
7474-7575-`## Common Tasks`
7676-``- **Setup:** `tangled auth login --username <handle> --password <app-password>` (One time)``
7777-`` - **Init:** `tangled repo create <name> --json` -> `git remote add tangled <url>` ``
7878-``- **Issues:** `tangled issue list --json title,number` (Uses current dir context)``
6868+LLMs can't read error messages buried in HTML or long stack traces. Provide a `--no-input` flag that forces the CLI to error if it can't resolve context or if required flags are missing.
79698080-## **6\. Implementation Roadmap**
7070+## 6. Implementation Roadmap
81718282-### **Phase 1: Context & Auth (The "Plumbing")**
7272+### Phase 1: Context & Auth (The "Plumbing")
837384741. **Auth Storage:** Store the AT Proto session in conf.
85752. **Context Resolver:**
···8878 - Regex matches the Tangled DID from the URL.
8979 - Returns the repo param required by XRPC.
90809191-### **Phase 2: CRUD Commands (The "Porcelain")**
8181+### Phase 2: CRUD Commands (The "Porcelain")
928293831. **repo create:**
9484 - Creates record via XRPC.
···96862. **issue list/create:**
9787 - Uses getRepoFromContext() so the user just types tangled issue create.
98889999-### **Phase 3: The "Machine" View**
8989+### Phase 3: The "Machine" View
10090101911. Implement a generic Formatter class.
10292 - Formatter.render(data, { json: true, fields: \['id', 'name'\] })
10393 - If json: pick fields, JSON.stringify.
10494 - If human: use cli-table3.
10595106106-## **7\. Example: The Context Pattern**
9696+## 7. Example: The Context Pattern
1079710898This is how we solve the usability problem (and the LLM hallucination problem).
9999+109100`// src/lib/context.ts`
110101`import simpleGit from 'simple-git';`
111102···132123`throw new Error("Could not infer repository context. Please use --repo <did> or run this inside a Tangled repository.");`
133124`}`
134125135135-### **Summary of Improvements**
126126+### Summary of Improvements
136127137128- **Context Inference:** This is the "killer feature" of gh that we are copying. It makes the tool usable for humans and safer for LLMs (less typing \= fewer errors).
138129- **Filtered JSON:** Saves tokens for LLM context windows.
139130- **Git Config Integration:** Treats the local .git folder as a database of configuration, reducing the need for environment variables or complex flags.
131131+132132+### Examples Tangled CLI Usage
133133+134134+```bash
135135+tangled auth login
136136+tangled repo create my-new-repo
137137+cd my-new-repo
138138+tangled issue create "Bug: Something is broken"
139139+tangled issue list --json "id,title"
140140+```
141141+142142+### Outstanding Issues
143143+144144+1. Can we allow auth through the web browser, rather than just CLI username/password? This would be more secure and user-friendly.
145145+2. The GitHub CLI manages the private keys allowing you to authenticate git operations. Can we do something similar, or will users have to manage SSH keys separately? Currently, I store my SSH keys in 1Password which signs requests for me. It would be great if tangled CLI could detect this and use it seamlessly, itentifying the user by the signed ssh key.
146146+3. How should we handle storing the AT Proto session securely? The GitHub CLI uses the OS keychain. We could do something similar.
147147+4. How are settings resolved (e.g. local config file, home folder, command-line flags)? We should define a clear precedence order.