···11+# Tangled CLI: API Analysis and Implementation Feasibility
22+33+This document details the analysis of the `tangled.org` API, based on the lexicon files found in the `tangled.org/core` repository, and assesses the feasibility of implementing the commands outlined in `TODO.md`.
44+55+## 1. API Structure Overview
66+77+The `tangled.org` API leverages the AT Protocol (ATProto), combining custom lexicons (schemas) for specific data types with generic ATProto repository operations (`com.atproto.repo.*`) for fundamental record management (create, get, list). This approach is common in ATProto-based services.
88+99+## 2. Feasibility of `TODO.md` Commands
1010+1111+All commands currently outlined in `TODO.md` appear to be implementable with the existing API structure.
1212+1313+### 2.1. Authentication (`tangled auth login`)
1414+1515+* **Feasible.** This will utilize the standard AT Protocol `com.atproto.server.createSession` procedure. This procedure is part of the core ATProto specification and would be handled by the `@atproto/api` library, not requiring a specific `tangled.org` lexicon.
1616+1717+### 2.2. Git SSH Key Management
1818+1919+* **`tangled ssh-key add <public-key-path>`**:
2020+ * **Feasible (using generic ATProto record creation).** The `core/lexicons/publicKey.json` defines the `sh.tangled.publicKey` record type. To add a user's global SSH public key, the CLI would use the generic ATProto `com.atproto.repo.createRecord` procedure. The `collection` parameter would be set to `sh.tangled.publicKey`, and the public key content (`key`) and a human-readable name (`name`) would be provided as the record data.
2121+* **`tangled ssh-key verify`**:
2222+ * **Feasible.** This command can be implemented by:
2323+ 1. Executing `ssh -T git@tangled.org` to capture the authenticated user's DID from the server response.
2424+ 2. Using the `sh.tangled.knot.listKeys` query (defined in `core/lexicons/knot/listKeys.json`) to fetch a list of public keys known to the knot server. This query returns objects that include the `did` associated with each key.
2525+ 3. Comparing the DID obtained from the SSH output with the DIDs returned by `listKeys` to confirm the key's association.
2626+ 4. Resolving the DID to a human-readable Bluesky handle using the standard AT Protocol `com.atproto.identity.resolveHandle` procedure (part of `@atproto/api`).
2727+2828+### 2.3. Repository Management
2929+3030+* **`tangled repo create <repo-name>`**:
3131+ * **Feasible.** The `sh.tangled.repo.create` procedure (defined in `core/lexicons/repo/create.json`) directly supports this. It requires an `rkey` (repository key/name) and can optionally accept a `defaultBranch` and a `source` URL for forking/importing.
3232+* **`tangled repo view [--json <fields>]`**:
3333+ * **Feasible (using generic ATProto record retrieval).** The `core/lexicons/repo/repo.json` defines the `sh.tangled.repo` record type. To view details of a specific repository, the CLI would use the generic ATProto `com.atproto.repo.getRecord` procedure, specifying `collection: "sh.tangled.repo"` and the appropriate record key (`rkey`).
3434+3535+### 2.4. Issue Management
3636+3737+* **`tangled issue create "<title>" [--body "<body>" | --body-file <file> | -F -]`**:
3838+ * **Feasible (using generic ATProto record creation).** The `core/lexicons/issue/issue.json` defines the `sh.tangled.repo.issue` record type. To create a new issue, the CLI would use the generic ATProto `com.atproto.repo.createRecord` procedure. The `collection` would be `sh.tangled.repo.issue`, and the `record` data would include the `repo` (AT-URI of the repository), `title`, and `body` (if provided).
3939+* **`tangled issue list [--json "id,title"]`**:
4040+ * **Feasible (using generic ATProto record listing).** To list issues for a repository, the CLI would use the generic ATProto `com.atproto.repo.listRecords` procedure, specifying `collection: "sh.tangled.repo.issue"`. Client-side filtering by the `repo` field (which is an AT-URI to the parent repository) might be necessary if the API does not offer server-side filtering for this specific collection.
4141+4242+## 3. API Resources and Actions Missing from Current CLI Plan
4343+4444+The `tangled.org` API, as revealed by its lexicons, exposes numerous capabilities beyond the current scope of the `TODO.md`. These represent potential future enhancements for the CLI:
4545+4646+### 3.1. Pull Requests
4747+4848+A comprehensive set of lexicons (`pulls/pull.json`, `pulls/open.json`, `pulls/merged.json`, `pulls/comment.json`, `pulls/closed.json`, `pulls/state.json`) indicates a fully-fledged Pull Request system.
4949+* **Potential CLI Commands:** `tangled pulls list`, `tangled pulls view <id>`, `tangled pulls create`, `tangled pulls merge`, `tangled pulls close`, `tangled pulls comment`.
5050+5151+### 3.2. CI/CD Pipelines
5252+5353+Lexicons such as `pipeline/pipeline.json`, `pipeline/status.json`, and `pipeline/cancelPipeline.json` suggest integration with a CI/CD system.
5454+* **Potential CLI Commands:** `tangled pipeline status`, `tangled pipeline cancel <id>`.
5555+5656+### 3.3. Repository Secrets
5757+5858+The `repo/addSecret.json`, `repo/listSecrets.json`, and `repo/removeSecret.json` lexicons allow for managing CI/CD secrets specific to a repository.
5959+* **Potential CLI Commands:** `tangled repo secret add <key> <value>`, `tangled repo secret list`, `tangled repo secret remove <key>`.
6060+6161+### 3.4. Advanced Git Operations
6262+6363+Several lexicons expose finer-grained Git operations directly via XRPC, potentially allowing for more integrated Git functionality beyond simple cloning/pushing:
6464+* `repo/log.json` (view commit history)
6565+* `repo/diff.json` (view changes between commits/branches)
6666+* `repo/branches.json`, `repo/branch.json`, `repo/deleteBranch.json` (manage branches)
6767+* `repo/tags.json`, `repo/tag.json` (manage tags)
6868+* `repo/compare.json` (compare two refs)
6969+* `git/refUpdate.json` (update Git references)
7070+* **Potential CLI Commands:** `tangled repo log`, `tangled repo diff`, `tangled branch list`, etc.
7171+7272+### 3.5. Social & Feed Interactions
7373+7474+Lexicons like `graph/follow.json`, `feed/star.json`, and `feed/reaction.json` suggest social networking features common in ATProto applications.
7575+* **Potential CLI Commands:** `tangled follow <did>`, `tangled star <uri>`, `tangled react <uri>`.
7676+7777+### 3.6. Labels
7878+7979+The `label/definition.json` and `label/op.json` lexicons provide mechanisms for defining and applying labels to various resources (e.g., issues, pull requests).
8080+* **Potential CLI Commands:** `tangled label add`, `tangled label list`.
8181+8282+### 3.7. Repository Collaboration
8383+8484+The `repo/collaborator.json` lexicon suggests managing collaborators on a repository.
8585+* **Potential CLI Commands:** `tangled repo collaborator add`, `tangled repo collaborator list`.
8686+8787+### 3.8. Forking and Syncing
8888+8989+`repo/forkSync.json` and `repo/forkStatus.json` point to specific API support for managing repository forks and their synchronization status.
9090+* **Potential CLI Commands:** `tangled repo fork`, `tangled repo fork-sync`.
9191+9292+## 4. Conclusion
9393+9494+The `tangled.org` API provides a solid foundation for building the planned CLI. All current `TODO.md` items are implementable, primarily by leveraging the `@atproto/api` library's generic record operations in conjunction with `tangled.org`'s custom record definitions and specific procedures. The API also offers a rich set of additional functionalities that can significantly extend the CLI's capabilities in future iterations, particularly around pull requests, CI/CD, and advanced Git management.