<h1 align="center">
<br>
<img
src="https://github.com/cryptopatrick/factory/blob/master/img/100days/xfiles.png"
alt="xfiles"
width="200"
/>
<br>
XFILES
<br>
</h1>
<h4 align="center">
Twitter (X) as a public filesystem for AI agents.
</h4>
<p align="center">
<a href="https://crates.io/crates/xfiles" target="_blank">
<img src="https://img.shields.io/crates/v/xfiles" alt="Crates.io"/>
</a>
<a href="https://crates.io/crates/xfiles" target="_blank">
<img src="https://img.shields.io/crates/d/xfiles" alt="Downloads"/>
</a>
<a href="https://docs.rs/xfiles" target="_blank">
<img src="https://docs.rs/xfiles/badge.svg" alt="Documentation"/>
</a>
<a href="LICENSE" target="_blank">
<img src="https://img.shields.io/github/license/sulu/sulu.svg" alt="GitHub license"/>
</a>
</p>
<b>Author's bio:</b> ๐๐ Hi, I'm CryptoPatrick! I'm currently enrolled as an
Undergraduate student in Mathematics, at Chalmers & the University of Gothenburg, Sweden. <br>
If you have any questions or need more info, then please <a href="https://discord.gg/T8EWmJZpCB">join my Discord Channel: AiMath</a>
---
<p align="center">
<a href="#-what-is-xfiles">What is xfiles</a> โข
<a href="#-features">Features</a> โข
<a href="#-how-to-use">How To Use</a> โข
<a href="#-documentation">Documentation</a> โข
<a href="#-license">License</a>
</p>
## ๐ Important Notices
* **Twitter API Required**: Requires Twitter API v2 Bearer Token for production use
* **Storage**: Uses Twitter threads as remote storage, SQLite for local indexing
* **Experimental**: v0.1 is a proof-of-concept suitable for research and creative projects
* **Public by Default**: All data is visible on Twitter (encryption coming in v0.2)
<h2 id="table-of-contents"> :pushpin: Table of Contents</h2>
<details open="open">
<summary>Table of Contents</summary>
<ol>
<li><a href="#-what-is-xfiles">What is xfiles</a></li>
<li><a href="#-features">Features</a></li>
<ul>
<li><a href="#-core-functionality">Core Functionality</a></li>
<li><a href="#-filesystem-operations">Filesystem Operations</a></li>
<li><a href="#-twitter-integration">Twitter Integration</a></li>
<li><a href="#-persistence">Persistence</a></li>
</ul>
<li><a href="#-architecture">Architecture</a></li>
<li><a href="#-how-to-use">How to Use</a></li>
<li><a href="#-examples">Examples</a></li>
<li><a href="#-testing">Testing</a></li>
<li><a href="#-documentation">Documentation</a></li>
<li><a href="#-license">License</a>
</ol>
</details>
## ๐ค What is xfiles
`xfiles` is a Rust library that treats Twitter as a public, append-only, log-structured filesystem. Tweets become "files", replies become "commits", and a local SQLite index keeps traversal fast.
**Why?** For transparent AI agents, public verifiability, distributed state, and creative experiments where Twitter serves as a globally verifiable shared memory bus.
### Core Concept
```
Tweet (root) โ File
Reply โ Commit
Thread โ Version history
SQLite โ Local index/cache
```
### Use Cases
- **AI Agent Memory**: Agents persist state to Twitter for transparency and recovery
- **Multi-Agent Collaboration**: Agents coordinate through shared Twitter threads
- **Public Audit Trails**: All operations are publicly visible and timestamped
- **Distributed State**: No single party controls the substrate
- **Creative Experiments**: Explore novel uses of social platforms as infrastructure
## ๐ท Features
`xfiles` provides a complete filesystem abstraction over Twitter with persistent local caching:
### ๐ง Core Functionality
- **Tweet as File Root**: Each file starts with a root tweet
- **Reply as Commit**: Updates are posted as replies, forming a version chain
- **Append-Only DAG**: Git-like directed acyclic graph for version history
- **SQLite Indexing**: Fast local queries without hitting Twitter API
### ๐ Filesystem Operations
- **File Creation**: `open(path, Create)` posts a root tweet
- **Reading**: `read()` fetches content from Twitter (cached locally)
- **Writing**: `write(content)` posts reply commits
- **History**: `history(path)` retrieves full commit chain
- **Listing**: `list(dir)` shows all files in a directory
- **Existence Checks**: `exists(path)` queries local index
### ๐ฆ Twitter Integration
- **Twitter API v2**: Full integration with modern Twitter API
- **Bearer Token Auth**: Simple authentication with Bearer Tokens
- **Rate Limiting**: Automatic backoff and retry logic
- **Chunking**: Transparent splitting of content >280 characters
- **Error Handling**: Robust error handling for API failures
### ๐พ Persistence
- **SQLite Storage**: Reliable file-based persistence
- **Commit Tracking**: DAG of all commits with timestamps
- **Path Mapping**: Files map to Twitter thread roots
- **Content Caching**: Avoid redundant API calls
- **Session Continuity**: Resume operations across restarts
## ๐ Architecture
1. ๐ Overall Architecture
```diagram
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ User Application (Agent/CLI/Backend) โ
โ Single call: fs.open().write() โ
โโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ XFS Component โ
โ โข Open files (create root tweets) โ
โ โข Read content (fetch from Twitter) โ
โ โข Write updates (post reply tweets) โ
โ โข List files (query SQLite index) โ
โ โข Track history (traverse DAG) โ
โโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโ
โ โ
โโโโโโโโโผโโโโโโโโโ โโโโโโโโโโผโโโโโโโโโโ
โ Twitter Adapterโ โ SQLite Store โ
โ โข API calls โ โ โข Commit index โ
โ โข Rate limit โ โ โข File mapping โ
โ โข Chunking โ โ โข Cache layer โ
โโโโโโโโโโฌโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโผโโโโโโโโโ
โ Twitter API โ
โ โข GET tweet โ
โ โข POST tweet โ
โ โข GET replies โ
โโโโโโโโโโโโโโโโโโ
```
User โ XFS โ SQLite Index + Twitter API โ Remote Storage
2. ๐ Data Flow Diagram
```diagram
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ file.write("Agent state v2") โ
โโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโผโโโโโโโโโ
โ 1. Compute โ
โ Hash โ
โ (blake3) โ
โโโโโโโโโโโฌโโโโโโโโ
โ
โ content hash
โผ
โโโโโโโโโโโโโโโโโโโโโโ
โ 2. Chunk โ
โ Content โ
โ (if >280 chars) โ
โโโโโโโโโโโฌโโโโโโโโโโโ
โ
โ chunks[]
โผ
โโโโโโโโโโโโโโโโโโโโโโ
โ 3. Post Reply โ
โ to Twitter โ
โ (TwitterAdapter) โ
โโโโโโโโโโโฌโโโโโโโโโโโ
โ
โ tweet_id
โผ
โโโโโโโโโโโโโโโโโโโโโโ
โ 4. Create Commit โ
โ โข id = tweet_id โ
โ โข parent = head โ
โ โข hash, timestamp โ
โโโโโโโโโโโฌโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโ
โ 5. Store Commit โ
โ in SQLite โ
โ โข Update index โ
โ โข Mark as head โ
โ โข Cache content โ
โโโโโโโโโโโโโโโโโโโโโโ
```
3. ๐พ Storage Layer Architecture
```diagram
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ XFS โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Public API โ โ
โ โ โข open(path, mode) โ XFile โ โ
โ โ โข list(dir) โ Vec<String> โ โ
โ โ โข history(path) โ Vec<Commit> โ โ
โ โ โข exists(path) โ bool โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ XFile Operations โโ
โ โ โข read() โ Vec<u8> โโ
โ โ โข write(content) โ Result<()> โโ
โ โ โข delete() โ Result<()> โโ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโ
โ SQLite Database (file.db) โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ files โ โ
โ โ - path (PK) โ โ
โ โ - root_tweet_id โ โ
โ โ - created_at โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ commits โ โ
โ โ - tweet_id (PK) โ โ
โ โ - parent_id (JSON array) โ โ
โ โ - timestamp โ โ
โ โ - author โ โ
โ โ - hash (blake3) โ โ
โ โ - mime โ โ
โ โ - size โ โ
โ โ - head (boolean) โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ chunks โ โ
โ โ - tweet_id (PK) โ โ
โ โ - parent_commit (FK) โ โ
โ โ - idx (chunk order) โ โ
โ โ - size, hash โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
```
4. โณ Commit Lifecycle
```diagram
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ User calls file.write(content) โ
โโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 1. Chunk Content โ
โ โข Split into 280-byte chunks if needed โ
โ โข Compute blake3 hash of full content โ
โโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 2. Post to Twitter API โ
โ โข Post first chunk as reply to current head โ
โ โข Post remaining chunks as reply chain โ
โ โข Receive tweet_id for each chunk โ
โโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 3. Create Commit Object โ
โ โข id = first chunk tweet_id โ
โ โข parents = [current_head] โ
โ โข hash = content hash โ
โ โข timestamp = now() โ
โ โข author = username โ
โโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 4. Persist to SQLite โ
โ โข INSERT INTO commits โ
โ โข UPDATE files SET head โ
โ โข Cache content for fast reads โ
โโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 5. Publicly Visible on Twitter โ
โ โข Tweet URL: https://twitter.com/i/web/status/{id} โ
โ โข Timestamped by Twitter โ
โ โข Immutable and auditable โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
```
## ๐ How to Use
### Installation
Add `xfiles` to your `Cargo.toml`:
```toml
[dependencies]
xfiles = "0.1"
tokio = { version = "1", features = ["full"] }
```
Or install with cargo:
```bash
cargo add xfiles
```
### Twitter API Setup
Before using xfiles with real Twitter, you need API credentials:
1. Go to https://developer.twitter.com/en/portal/dashboard
2. Create a project and app
3. Generate Bearer Token under "Keys and tokens"
4. See [docs/TWITTER_SETUP.md](docs/TWITTER_SETUP.md) for detailed instructions
```bash
export TWITTER_BEARER_TOKEN="your_bearer_token_here"
export TWITTER_USERNAME="your_username"
```
### Basic Example (Mock Adapter)
```rust
use xfiles::*;
use std::sync::Arc;
#[tokio::main]
async fn main() -> Result<()> {
// Use mock adapter for testing (no Twitter API needed)
let adapter = Arc::new(MockAdapter::new());
let mut fs = XFS::with_adapter("agent", adapter, Some(":memory:")).await?;
// Create and write to file
let mut file = fs.open("memory.txt", OpenMode::Create).await?;
file.write(b"Agent state v1").await?;
// Read it back
let content = file.read().await?;
println!("{}", String::from_utf8_lossy(&content));
// Multiple writes create commit chain
file.write(b"Agent state v2").await?;
file.write(b"Agent state v3").await?;
// Get history
let history = fs.history("memory.txt").await?;
println!("Total commits: {}", history.len());
Ok(())
}
```
### Real Twitter API Example
```rust
use xfiles::*;
#[tokio::main]
async fn main() -> Result<()> {
let bearer_token = std::env::var("TWITTER_BEARER_TOKEN")?;
// Connect with real Twitter API
let mut fs = XFS::connect("@myagent", &bearer_token).await?;
// Create file (posts root tweet)
let mut file = fs.open("agent_memory.txt", OpenMode::Create).await?;
// Write content (posts reply tweet)
file.write(b"Day 1: Agent initialized").await?;
// Read it back (fetches from Twitter)
let content = file.read().await?;
println!("Content: {}", String::from_utf8_lossy(&content));
println!("Tweet URL: https://twitter.com/i/web/status/{}", file.head());
Ok(())
}
```
### Advanced Usage
```rust
use xfiles::*;
#[tokio::main]
async fn main() -> Result<()> {
let bearer_token = std::env::var("TWITTER_BEARER_TOKEN")?;
let mut fs = XFS::connect("@myagent", &bearer_token).await?;
// Create multiple files
let mut memory = fs.open("agent/memory.json", OpenMode::Create).await?;
let mut log = fs.open("agent/debug.log", OpenMode::Create).await?;
// Write to different files
memory.write(br#"{"state": "active", "version": 1}"#).await?;
log.write(b"[INFO] Agent started").await?;
// List all files
let files = fs.list("agent").await?;
println!("Files in agent/: {:?}", files);
// Get full history of a file
let history = fs.history("agent/memory.json").await?;
for (i, commit) in history.iter().enumerate() {
println!("Commit {}: {} at {}",
i + 1,
commit.id,
commit.timestamp
);
}
// Check file existence
if fs.exists("agent/config.toml").await? {
println!("Config file exists");
}
Ok(())
}
```
## ๐งช Examples
The repository includes several examples demonstrating different features:
```bash
# Mock adapter example (no Twitter API needed)
cargo run --example basic
# Real Twitter API example (requires credentials)
export TWITTER_BEARER_TOKEN="your_token"
cargo run --example twitter_real
```
### Example Output
```
=== xfiles Basic Example ===
1. Creating a new file...
โ Created file: memory.txt
2. Writing to file...
โ Wrote initial content
3. Reading file content...
Content: Day 1: Agent initialized
4. Writing multiple updates...
โ Created commit chain
5. Reading latest version...
Latest: Day 3: Successfully stored memory
6. Getting file history...
Total commits: 4
Commit 1: mock_tweet_1 (2026-01-16 12:28:15 UTC)
Commit 2: mock_tweet_2 (2026-01-16 12:28:15 UTC)
Commit 3: mock_tweet_3 (2026-01-16 12:28:15 UTC)
Commit 4: mock_tweet_4 (2026-01-16 12:28:15 UTC)
```
## ๐งช Testing
Run the test suite:
```bash
# Run all tests (uses mock adapter)
cargo test
# Run specific test suite
cargo test --test tests
# Run with output
cargo test -- --nocapture
```
All tests use the MockAdapter, so no Twitter API credentials are needed for testing.
## ๐ Documentation
Comprehensive documentation is available:
- **[API Documentation](https://docs.rs/xfiles)** - Full API reference
- **[Twitter Setup Guide](docs/TWITTER_SETUP.md)** - Get Twitter API credentials and setup
- **[Contributing Guide](CONTRIBUTING.md)** - Development guidelines and workflow
### Key Concepts
- **Files**: Mapped to Twitter thread roots, tracked in SQLite
- **Commits**: Each write creates a new tweet reply
- **DAG**: Git-like directed acyclic graph for version history
- **Chunking**: Content >280 chars automatically split across multiple tweets
- **Caching**: SQLite caches content to minimize API calls
### Performance Notes
- **Reads**: Cached in SQLite (no API call on cache hit)
- **Writes**: Rate-limited by Twitter API (300 tweets/15min)
- **Chunking**: Transparent for content > 280 chars
- **Rate Limiting**: Automatic exponential backoff
Twitter API v2 Free Tier limits:
- 50 tweet reads / 15 min
- 300 tweet posts / 15 min
## ๐ Author
<a href="https://x.com/cryptopatrick">CryptoPatrick</a>
Keybase Verification:
https://keybase.io/cryptopatrick/sigs/8epNh5h2FtIX1UNNmf8YQ-k33M8J-Md4LnAN
## ๐ฃ Support
Leave a โญ if you think this project is cool.
## ๐ License
This project is licensed under MIT. See [LICENSE](LICENSE) for details.
---
**Inspired by:**
- Git (DAG commits, history)
- IPFS (content-addressed chunks)
- CRDTs (distributed updates)
- Blockchains (timestamped logs)
**Made with โ for transparent AI agents**