xfiles 0.4.5

Persistence of data using X, formerly known as Twitter, as a public filesystem.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
<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)

<!-- TABLE OF CONTENTS -->
<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**