Tangled CLI – Agent Handoff (Massive Context)#
This document is a complete handoff for the next Codex instance working on the Tangled CLI (Rust). It explains what exists, what to build next, where to edit, how to call the APIs, how to persist sessions, how to print output, and how to validate success.
Primary focus for this session: implement authentication (auth login/status/logout) and repository listing (repo list).
0) TL;DR – Immediate Actions#
-
Implement
auth loginusing AT Protocolcom.atproto.server.createSession.- Prompt for handle/password if flags aren’t provided.
- POST to
/xrpc/com.atproto.server.createSessionat the configured PDS (defaulthttps://bsky.social). - Persist
{accessJwt, refreshJwt, did, handle}viaSessionManager(keyring-backed). auth statusreads keyring and prints handle + did;auth logoutclears keyring.
-
Implement
repo listusing Tangled’s repo list method (tentativesh.tangled.repo.list).- GET
/xrpc/sh.tangled.repo.listwith optional params:user,knot,starred. - Include
Authorization: Bearer <accessJwt>if required. - Print results as table (default) or JSON (
--format json).
- GET
Keep edits minimal and scoped to these features.
1) Repository Map (Paths You Will Touch)#
-
CLI (binary):
tangled/crates/tangled-cli/src/commands/auth.rs→ implement login/status/logout.tangled/crates/tangled-cli/src/commands/repo.rs→ implement list.tangled/crates/tangled-cli/src/cli.rs→ already contains arguments and subcommands; no structural changes needed.tangled/crates/tangled-cli/src/main.rs→ no change.
-
Config + session:
tangled/crates/tangled-config/src/session.rs→ already providesSession+SessionManager(keyring).tangled/crates/tangled-config/src/config.rs→ optional use for PDS/base URL (MVP can use CLI flags/env vars).
-
API client:
tangled/crates/tangled-api/src/client.rs→ add XRPC helpers and implementlogin_with_passwordandlist_repos.
2) Current State Snapshot#
- Workspace is scaffolded and compiles after wiring dependencies (network needed to fetch crates):
tangled-cli: clap CLI with subcommands; commands currently log stubs.tangled-config: TOML config loader/saver; keyring-backed session store.tangled-api: client struct with placeholder methods.tangled-git: stubs for future.
- Placeholder lexicons in
tangled/lexicons/sh.tangled/*are not authoritative; use AT Protocol docs and inspect real endpoints later.
Goal: replace CLI stubs with real API calls for auth + repo list.
3) Endpoints & Data Shapes#
3.1 AT Protocol – Create Session#
- Method:
com.atproto.server.createSession - HTTP:
POST /xrpc/com.atproto.server.createSession - Request JSON:
identifier: string→ user handle or email (e.g.,alice.bsky.social).password: string→ password or app password.
- Response JSON (subset used):
accessJwt: stringrefreshJwt: stringdid: string(e.g.,did:plc:...)handle: string
Persist to keyring using SessionManager.
3.2 Tangled – Repo List (tentative)#
- Method:
sh.tangled.repo.list(subject to change; wire in a constant to adjust easily). - HTTP:
GET /xrpc/sh.tangled.repo.list?user=<..>&knot=<..>&starred=<true|false> - Auth: likely required; include
Authorization: Bearer <accessJwt>. - Response JSON (envelope):
{ "repos": [{ "name": string, "knot": string, "private": bool, ... }] }
If method name or response shape differs, adapt the client code; keep CLI interface stable.
4) Implementation Plan#
4.1 Add XRPC helpers and methods in tangled-api#
File: tangled/crates/tangled-api/src/client.rs
-
Extend
TangledClientwith:fn xrpc_url(&self, method: &str) -> String→ combinesbase_url+/xrpc/+method.async fn post_json<TReq: Serialize, TRes: DeserializeOwned>(&self, method, req, bearer) -> Result<TRes>.async fn get_json<TRes: DeserializeOwned>(&self, method, params, bearer) -> Result<TRes>.- Include
Authorization: Bearer <token>whenbeareris provided.
-
Implement:
pub async fn login_with_password(&self, handle: &str, password: &str, pds: &str) -> Result<Session>- POST to
com.atproto.server.createSessionatself.base_url(which should be the PDS base). - Map response to
tangled_config::session::Sessionand return it (caller will persist).
- POST to
pub async fn list_repos(&self, user: Option<&str>, knot: Option<&str>, starred: bool, bearer: Option<&str>) -> Result<Vec<Repository>>- GET
sh.tangled.repo.listwith params present only if set. - Return parsed
Vec<Repository>from an envelope{ repos: [...] }.
- GET
Error handling: For non-2xx, read the response body, return anyhow!("{status}: {body}").
4.2 Wire CLI auth commands#
File: tangled/crates/tangled-cli/src/commands/auth.rs
-
login:- Determine PDS: use
--pdsarg if provided, else defaulthttps://bsky.social(later from config/env). - Prompt for missing handle/password.
let client = tangled_api::TangledClient::new(&pds);let session = client.login_with_password(&handle, &password, &pds).await?;tangled_config::session::SessionManager::default().save(&session)?;- Print:
Logged in as '{handle}' ({did}).
- Determine PDS: use
-
status:- Load
SessionManager::default().load()?. - If Some: print
Logged in as '{handle}' ({did}). - Else: print
Not logged in. Run: tangled auth login.
- Load
-
logout:SessionManager::default().clear()?.- Print
Logged outif something was cleared; otherwiseNo session foundis acceptable.
4.3 Wire CLI repo list#
File: tangled/crates/tangled-cli/src/commands/repo.rs
- Load session; if absent, print
Please login first: tangled auth loginand exit 1 (or 0 with friendly message; choose one and be consistent). - Build a client for Tangled API base (for now, default to
https://tangled.orgor allowTANGLED_API_BASEenv var to override):let base = std::env::var("TANGLED_API_BASE").unwrap_or_else(|_| "https://tangled.org".into());let client = tangled_api::TangledClient::new(base);
- Call
client.list_repos(args.user.as_deref(), args.knot.as_deref(), args.starred, Some(session.access_jwt.as_str())).await?. - Print:
- If
Cli.format == OutputFormat::Json:serde_json::to_string_pretty(&repos). - Else: simple columns
NAME KNOT PRIVATEusingprintln!formatting for now.
- If
5) Code Snippets (Copy/Paste Friendly)#
5.1 In tangled-api/src/client.rs#
use anyhow::{anyhow, bail, Result};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use tangled_config::session::Session;
#[derive(Clone, Debug)]
pub struct TangledClient { pub(crate) base_url: String }
impl TangledClient {
pub fn new(base_url: impl Into<String>) -> Self { Self { base_url: base_url.into() } }
pub fn default() -> Self { Self::new("https://tangled.org") }
fn xrpc_url(&self, method: &str) -> String {
format!("{}/xrpc/{}", self.base_url.trim_end_matches('/'), method)
}
async fn post_json<TReq: Serialize, TRes: DeserializeOwned>(
&self,
method: &str,
req: &TReq,
bearer: Option<&str>,
) -> Result<TRes> {
let url = self.xrpc_url(method);
let client = reqwest::Client::new();
let mut reqb = client.post(url).header(reqwest::header::CONTENT_TYPE, "application/json");
if let Some(token) = bearer { reqb = reqb.header(reqwest::header::AUTHORIZATION, format!("Bearer {}", token)); }
let res = reqb.json(req).send().await?;
let status = res.status();
if !status.is_success() {
let body = res.text().await.unwrap_or_default();
return Err(anyhow!("{}: {}", status, body));
}
Ok(res.json::<TRes>().await?)
}
async fn get_json<TRes: DeserializeOwned>(
&self,
method: &str,
params: &[(&str, String)],
bearer: Option<&str>,
) -> Result<TRes> {
let url = self.xrpc_url(method);
let client = reqwest::Client::new();
let mut reqb = client.get(url).query(¶ms);
if let Some(token) = bearer { reqb = reqb.header(reqwest::header::AUTHORIZATION, format!("Bearer {}", token)); }
let res = reqb.send().await?;
let status = res.status();
if !status.is_success() {
let body = res.text().await.unwrap_or_default();
return Err(anyhow!("{}: {}", status, body));
}
Ok(res.json::<TRes>().await?)
}
pub async fn login_with_password(&self, handle: &str, password: &str, _pds: &str) -> Result<Session> {
#[derive(Serialize)]
struct Req<'a> { #[serde(rename = "identifier")] identifier: &'a str, #[serde(rename = "password")] password: &'a str }
#[derive(Deserialize)]
struct Res { #[serde(rename = "accessJwt")] access_jwt: String, #[serde(rename = "refreshJwt")] refresh_jwt: String, did: String, handle: String }
let body = Req { identifier: handle, password };
let res: Res = self.post_json("com.atproto.server.createSession", &body, None).await?;
Ok(Session { access_jwt: res.access_jwt, refresh_jwt: res.refresh_jwt, did: res.did, handle: res.handle, ..Default::default() })
}
pub async fn list_repos(&self, user: Option<&str>, knot: Option<&str>, starred: bool, bearer: Option<&str>) -> Result<Vec<Repository>> {
#[derive(Deserialize)]
struct Envelope { repos: Vec<Repository> }
let mut q = vec![];
if let Some(u) = user { q.push(("user", u.to_string())); }
if let Some(k) = knot { q.push(("knot", k.to_string())); }
if starred { q.push(("starred", true.to_string())); }
let env: Envelope = self.get_json("sh.tangled.repo.list", &q, bearer).await?;
Ok(env.repos)
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Repository { pub did: Option<String>, pub rkey: Option<String>, pub name: String, pub knot: Option<String>, pub description: Option<String>, pub private: bool }
5.2 In tangled-cli/src/commands/auth.rs#
use anyhow::Result;
use dialoguer::{Input, Password};
use tangled_config::session::SessionManager;
use crate::cli::{AuthCommand, AuthLoginArgs, Cli};
pub async fn run(_cli: &Cli, cmd: AuthCommand) -> Result<()> {
match cmd {
AuthCommand::Login(args) => login(args).await,
AuthCommand::Status => status().await,
AuthCommand::Logout => logout().await,
}
}
async fn login(mut args: AuthLoginArgs) -> Result<()> {
let handle: String = match args.handle.take() { Some(h) => h, None => Input::new().with_prompt("Handle").interact_text()? };
let password: String = match args.password.take() { Some(p) => p, None => Password::new().with_prompt("Password").interact()? };
let pds = args.pds.unwrap_or_else(|| "https://bsky.social".to_string());
let client = tangled_api::TangledClient::new(&pds);
let session = client.login_with_password(&handle, &password, &pds).await?;
SessionManager::default().save(&session)?;
println!("Logged in as '{}' ({})", session.handle, session.did);
Ok(())
}
async fn status() -> Result<()> {
let mgr = SessionManager::default();
match mgr.load()? {
Some(s) => println!("Logged in as '{}' ({})", s.handle, s.did),
None => println!("Not logged in. Run: tangled auth login"),
}
Ok(())
}
async fn logout() -> Result<()> {
let mgr = SessionManager::default();
if mgr.load()?.is_some() { mgr.clear()?; println!("Logged out"); } else { println!("No session found"); }
Ok(())
}
5.3 In tangled-cli/src/commands/repo.rs#
use anyhow::{anyhow, Result};
use tangled_config::session::SessionManager;
use crate::cli::{Cli, RepoCommand, RepoListArgs};
pub async fn run(_cli: &Cli, cmd: RepoCommand) -> Result<()> {
match cmd { RepoCommand::List(args) => list(args).await, _ => Ok(println!("not implemented")) }
}
async fn list(args: RepoListArgs) -> Result<()> {
let mgr = SessionManager::default();
let session = mgr.load()?.ok_or_else(|| anyhow!("Please login first: tangled auth login"))?;
let base = std::env::var("TANGLED_API_BASE").unwrap_or_else(|_| "https://tangled.org".into());
let client = tangled_api::TangledClient::new(base);
let repos = client.list_repos(args.user.as_deref(), args.knot.as_deref(), args.starred, Some(session.access_jwt.as_str())).await?;
// Simple output: table or JSON to be improved later
println!("NAME\tKNOT\tPRIVATE");
for r in repos { println!("{}\t{}\t{}", r.name, r.knot.unwrap_or_default(), r.private); }
Ok(())
}
6) Configuration, Env Vars, and Security#
- PDS base (auth): default
https://bsky.social. Accept CLI flag--pds; later read from config. - Tangled API base (repo list): default
https://tangled.org; allow override viaTANGLED_API_BASEenv var. - Do not log passwords or tokens.
- Store tokens only in keyring (already implemented).
7) Testing Plan (MVP)#
- Client unit tests with
mockitoforcreateSessionandrepo listendpoints; simulate expected JSON. - CLI smoke tests optional for this pass. If added, use
assert_cmdto check printed output strings. - Avoid live network calls in tests.
8) Acceptance Criteria#
tangled auth login:- Prompts or uses flags; successful call saves session and prints
Logged in as .... - On failure, shows HTTP status and short message.
- Prompts or uses flags; successful call saves session and prints
tangled auth status:- Shows handle + did if session exists; otherwise says not logged in.
tangled auth logout:- Clears keyring; prints confirmation.
tangled repo list:- Performs authenticated GET and prints a list (even if empty) without panicking.
- JSON output possible later; table output acceptable for now.
9) Troubleshooting Notes#
- Keyring errors on Linux may indicate no secret service running; suggest enabling GNOME Keyring or KWallet.
- If
repo listreturns 404, the method name or base URL may be wrong; adjustsh.tangled.repo.listorTANGLED_API_BASE. - If 401, session may be missing/expired; run
auth loginagain.
10) Non‑Goals for This Pass#
- Refresh token flow, device code, OAuth.
- PRs, issues, knots, spindle implementation.
- Advanced formatting, paging, completions.
11) Future Follow‑ups#
- Refresh flow (
com.atproto.server.refreshSession) and retry once on 401. - Persist base URLs and profiles in config; add
tangled configcommands. - Proper table/json formatting and shell completions.
12) Quick Operator Commands#
- Build CLI:
cargo build -p tangled-cli - Help:
cargo run -p tangled-cli -- --help - Login:
cargo run -p tangled-cli -- auth login --handle <handle> - Status:
cargo run -p tangled-cli -- auth status - Repo list:
TANGLED_API_BASE=https://tangled.org cargo run -p tangled-cli -- repo list --user <handle>
End of handoff. Implement auth login and repo list as described, keeping changes focused and testable.
13) Tangled Core (../tangled-core) – Practical Notes#
This workspace often needs to peek at the Tangled monorepo to confirm XRPC endpoints and shapes. Here are concise tips and findings that informed this CLI implementation.
Where To Look#
- Lexicons (authoritative NSIDs and shapes):
../tangled-core/lexicons/**- Repo create:
../tangled-core/lexicons/repo/create.json→sh.tangled.repo.create - Repo record schema:
../tangled-core/lexicons/repo/repo.json→sh.tangled.repo - Misc repo queries (tree, log, tags, etc.) under
../tangled-core/lexicons/repo/ - Note: there is no
sh.tangled.repo.listlexicon in the core right now; listing is done via ATproto records.
- Repo create:
- Knotserver XRPC routes (what requires auth vs open):
../tangled-core/knotserver/xrpc/xrpc.go- Mutating repo ops (e.g.,
sh.tangled.repo.create) are behind ServiceAuth middleware. - Read-only repo queries (tree, log, etc.) are open.
- Mutating repo ops (e.g.,
- Create repo handler (server-side flow):
../tangled-core/knotserver/xrpc/create_repo.go- Validates ServiceAuth; expects rkey for the
sh.tangled.reporecord that already exists on the user's PDS.
- Validates ServiceAuth; expects rkey for the
- ServiceAuth middleware (how Bearer is validated):
../tangled-core/xrpc/serviceauth/service_auth.go- Validates a ServiceAuth token with Audience =
did:web:<knot-or-service-host>.
- Validates a ServiceAuth token with Audience =
- Appview client for ServiceAuth:
../tangled-core/appview/xrpcclient/xrpc.go(method:ServerGetServiceAuth).
How To Search Quickly (rg examples)#
- Find a specific NSID across the repo:
rg -n "sh\.tangled\.repo\.create" ../tangled-core
- See which endpoints are routed and whether they’re behind ServiceAuth:
rg -n "chi\..*Get\(|chi\..*Post\(" ../tangled-core/knotserver/xrpc- Then open
xrpc.goand respective handlers.
- Discover ServiceAuth usage and audience DID:
rg -n "ServerGetServiceAuth|VerifyServiceAuth|serviceauth" ../tangled-core
- List lexicons by area:
ls ../tangled-core/lexicons/repoorrg -n "\bid\": \"sh\.tangled\..*\"" ../tangled-core/lexicons
Repo Listing (client-side pattern)#
- There is no
sh.tangled.repo.listin core. To list a user’s repos:- Resolve handle → DID if needed via PDS:
GET com.atproto.identity.resolveHandle. - List records from the user’s PDS:
GET com.atproto.repo.listRecordswithcollection=sh.tangled.repo. - Filter client-side (e.g., by
knot). “Starred” filtering is not currently defined in core.
- Resolve handle → DID if needed via PDS:
Repo Creation (two-step flow)#
- Step 1 (PDS): create the
sh.tangled.reporecord in the user’s repo:POST com.atproto.repo.createRecordwith{ repo: <did>, collection: "sh.tangled.repo", record: { name, knot, description?, createdAt } }.- Extract
rkeyfrom the returneduri(at://<did>/<collection>/<rkey>).
- Step 2 (Tangled API base): call the server to initialize the bare repo on the knot:
- Obtain ServiceAuth:
GET com.atproto.server.getServiceAuthfrom PDS withaud=did:web:<tngl.sh or target-host>. POST sh.tangled.repo.createon the Tangled API base with{ rkey, defaultBranch?, source? }andAuthorization: Bearer <serviceAuth>.- Server validates token via
xrpc/serviceauth, confirms actor permissions, and creates the git repo.
- Obtain ServiceAuth:
Base URLs, DIDs, and Defaults#
- Tangled API base (server): default is
https://tngl.sh. Do not use the marketing/landing site. - PDS base (auth + record ops): default
https://bsky.socialunless a different PDS was chosen on login. - ServiceAuth audience DID is
did:web:<host>where<host>is the Tangled API base hostname. - CLI stores the PDS URL in the session to keep the CLI stateful.
Common Errors and Fixes#
InvalidTokenwhen listing repos: listing should use the PDS (com.atproto.repo.listRecords), not the Tangled API base.- 404 on
repo.create: verify ServiceAuth audience matches the target host and that the rkey exists on the PDS. - Keychain issues on Linux: ensure a Secret Service (e.g., GNOME Keyring or KWallet) is running.
Implementation Pointers (CLI)#
- Auth
com.atproto.server.createSessionagainst the PDS, save{accessJwt, refreshJwt, did, handle, pds}in keyring.
- List repos
- Use session.handle by default; resolve to DID, then
com.atproto.repo.listRecordson PDS.
- Use session.handle by default; resolve to DID, then
- Create repo
- Build the PDS record first; then ServiceAuth →
sh.tangled.repo.createontngl.sh.
- Build the PDS record first; then ServiceAuth →
Testing Hints#
- Avoid live calls; use
mockitoto stub both PDS and Tangled API base endpoints. - Unit test decoding with minimal JSON envelopes: record lists, createRecord
uri, and repo.create (empty body or simple ack).