database.proto 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  1. // Smartbotic Database Service
  2. // Document-oriented storage with persistence and replication
  3. syntax = "proto3";
  4. package smartbotic.databasepb;
  5. // Error code range: 6000-6999 for Database errors
  6. // 6000: Generic database error
  7. // 6001: Collection not found
  8. // 6002: Document not found
  9. // 6003: Document already exists
  10. // 6004: Version conflict
  11. // 6005: Invalid query
  12. // 6006: File not found
  13. // 6007: File too large
  14. // 6008: Encryption error
  15. // 6009: Replication error
  16. // 6010: Version not found
  17. // ===== Main Database Service =====
  18. service DatabaseService {
  19. // Document operations
  20. rpc Insert(InsertRequest) returns (InsertResponse);
  21. rpc Get(GetRequest) returns (GetResponse);
  22. rpc Update(UpdateRequest) returns (UpdateResponse);
  23. rpc Upsert(UpsertRequest) returns (UpsertResponse);
  24. rpc Delete(DeleteRequest) returns (DeleteResponse);
  25. rpc PatchDocument(PatchDocumentRequest) returns (PatchDocumentResponse);
  26. rpc Exists(ExistsRequest) returns (ExistsResponse);
  27. // Version history operations
  28. rpc GetVersionHistory(GetVersionHistoryRequest) returns (GetVersionHistoryResponse);
  29. rpc GetDocumentVersion(GetDocumentVersionRequest) returns (GetDocumentVersionResponse);
  30. rpc RestoreVersion(RestoreVersionRequest) returns (RestoreVersionResponse);
  31. rpc RestoreToDate(RestoreToDateRequest) returns (RestoreToDateResponse);
  32. // Batch operations
  33. rpc BatchInsert(BatchInsertRequest) returns (BatchInsertResponse);
  34. rpc BatchGet(BatchGetRequest) returns (BatchGetResponse);
  35. rpc BatchDelete(BatchDeleteRequest) returns (BatchDeleteResponse);
  36. // Query operations
  37. rpc Find(FindRequest) returns (FindResponse);
  38. rpc Count(CountRequest) returns (CountResponse);
  39. rpc SimilaritySearch(SimilaritySearchRequest) returns (SimilaritySearchResponse);
  40. // Set operations (Redis compatibility)
  41. rpc SetAdd(SetAddRequest) returns (SetAddResponse);
  42. rpc SetRemove(SetRemoveRequest) returns (SetRemoveResponse);
  43. rpc SetMembers(SetMembersRequest) returns (SetMembersResponse);
  44. rpc SetIsMember(SetIsMemberRequest) returns (SetIsMemberResponse);
  45. // Collection management
  46. rpc CreateCollection(CreateCollectionRequest) returns (CreateCollectionResponse);
  47. rpc DropCollection(DropCollectionRequest) returns (DropCollectionResponse);
  48. rpc ListCollections(ListCollectionsRequest) returns (ListCollectionsResponse);
  49. rpc GetCollectionInfo(GetCollectionInfoRequest) returns (GetCollectionInfoResponse);
  50. // v2.3 — Project management. Projects are top-level namespaces over
  51. // collections. The "default" project is implicit and always exists.
  52. rpc ListProjects(ListProjectsRequest) returns (ListProjectsResponse);
  53. rpc CreateProject(CreateProjectRequest) returns (CreateProjectResponse);
  54. rpc DropProject(DropProjectRequest) returns (DropProjectResponse);
  55. // View management
  56. rpc CreateView(CreateViewRequest) returns (CreateViewResponse);
  57. rpc DropView(DropViewRequest) returns (DropViewResponse);
  58. rpc ListViews(ListViewsRequest) returns (ListViewsResponse);
  59. rpc GetViewInfo(GetViewInfoRequest) returns (GetViewInfoResponse);
  60. // v2.11.0 T6b — relation (referential integrity) management. Admin-only:
  61. // `_relations` is a system collection and a relation names another
  62. // collection's schema, so declaring one is not ordinary per-collection
  63. // write access.
  64. rpc CreateRelation(CreateRelationRequest) returns (CreateRelationResponse);
  65. rpc DropRelation(DropRelationRequest) returns (DropRelationResponse);
  66. rpc ListRelations(ListRelationsRequest) returns (ListRelationsResponse);
  67. rpc GetRelationInfo(GetRelationInfoRequest) returns (GetRelationInfoResponse);
  68. // v2.11.0 T5 — "what would happen if I deleted this?" without deleting
  69. // it. Read-only and gated as an ordinary per-collection read, not admin.
  70. rpc DescribeDelete(DescribeDeleteRequest) returns (DescribeDeleteResponse);
  71. // v2.11.0 T7 — "does this relation's reverse index actually match live
  72. // data?" A relation declared over a collection that already had rows
  73. // before v2.11.0 T7 shipped (or one whose sub-db was lost/restored from
  74. // an older snapshot) can carry postings for parent ids that no longer
  75. // exist. This walks the reverse index and probes each parent id;
  76. // mutates nothing. ⚠ Cost is proportional to the number of DISTINCT
  77. // parents referenced, not collection size, but on a very large child
  78. // collection that is still a full index walk - treat this as an
  79. // operator/migration tool, not something to call on a hot path.
  80. // Admin-only, like the other relation-management RPCs above (NOT gated
  81. // as a per-collection read on `child`, despite being read-only and
  82. // reporting facts about that collection's data): answering requires
  83. // resolving the relation first to learn its `child`, and gating after
  84. // that lookup would make a nonexistent-relation response distinguishable
  85. // from an existing-but-unauthorized one - a probe-able existence oracle,
  86. // which is exactly what gate() exists to prevent elsewhere.
  87. rpc CheckRelation(CheckRelationRequest) returns (CheckRelationResponse);
  88. // Collection configuration
  89. rpc ConfigureCollection(ConfigureCollectionRequest) returns (ConfigureCollectionResponse);
  90. // v2.9.0 — secondary indexes. Declaration is explicit per collection: an
  91. // index costs write throughput, and indexing every field would build useless
  92. // ones (a low-cardinality field like a status enum is slower through an
  93. // index than a scan).
  94. rpc CreateIndex(CreateIndexRequest) returns (CreateIndexResponse);
  95. rpc DropIndex(DropIndexRequest) returns (DropIndexResponse);
  96. rpc ListIndexes(ListIndexesRequest) returns (ListIndexesResponse);
  97. // v2.10.0 — the distinct values an indexed field holds, with row counts.
  98. // Answered from the index, one step per distinct value rather than per row.
  99. rpc GetIndexValues(GetIndexValuesRequest) returns (GetIndexValuesResponse);
  100. rpc GetCollectionConfig(GetCollectionConfigRequest) returns (GetCollectionConfigResponse);
  101. rpc MigrateCollectionTimestamps(MigrateCollectionTimestampsRequest) returns (MigrateCollectionTimestampsResponse);
  102. // File operations
  103. rpc UploadFile(stream FileChunk) returns (UploadFileResponse);
  104. rpc DownloadFile(DownloadFileRequest) returns (stream FileChunk);
  105. rpc DeleteFile(DeleteFileRequest) returns (DeleteFileResponse);
  106. rpc GetFileInfo(GetFileInfoRequest) returns (FileInfo);
  107. rpc ListFiles(ListFilesRequest) returns (ListFilesResponse);
  108. rpc SetFileTtl(SetFileTtlRequest) returns (SetFileTtlResponse);
  109. // Event subscription
  110. rpc Subscribe(SubscribeRequest) returns (stream DatabaseEvent);
  111. // Health and stats
  112. rpc HealthCheck(HealthCheckRequest) returns (HealthCheckResponse);
  113. rpc GetStats(GetStatsRequest) returns (GetStatsResponse);
  114. rpc GetMemoryStats(GetMemoryStatsRequest) returns (GetMemoryStatsResponse);
  115. // Read-only runtime state control
  116. rpc SetReadOnly(SetReadOnlyRequest) returns (SetReadOnlyResponse);
  117. rpc GetReadOnlyStatus(GetReadOnlyStatusRequest) returns (GetReadOnlyStatusResponse);
  118. }
  119. // ===== Replication Service =====
  120. service DatabaseReplication {
  121. // Bidirectional streaming for real-time sync
  122. rpc SyncStream(stream ReplicationMessage) returns (stream ReplicationMessage);
  123. // Pull missed entries (for recovery)
  124. rpc GetEntriesSince(GetEntriesRequest) returns (stream ReplicationEntry);
  125. // Get current node state
  126. rpc GetNodeState(GetNodeStateRequest) returns (NodeState);
  127. }
  128. // ===== Document Types =====
  129. message Document {
  130. string id = 1;
  131. string collection = 2;
  132. bytes data = 3; // JSON-encoded document data
  133. uint64 version = 4;
  134. uint64 created_at = 5; // Timestamp in milliseconds
  135. uint64 updated_at = 6;
  136. uint64 expires_at = 7; // 0 = no expiration
  137. string node_id = 8; // Origin node for replication
  138. bool encrypted = 9;
  139. repeated string encrypted_fields = 10;
  140. string created_by = 11; // User ID who created the document
  141. string updated_by = 12; // User ID who last updated the document
  142. }
  143. // ===== Document Operations =====
  144. message InsertRequest {
  145. string collection = 1;
  146. bytes data = 2; // JSON document
  147. string id = 3; // Optional, auto-generated if empty
  148. uint32 ttl_seconds = 4; // 0 = use collection default
  149. string actor = 5; // User ID performing the operation (for audit)
  150. }
  151. message InsertResponse {
  152. string id = 1;
  153. uint64 version = 2;
  154. }
  155. message GetRequest {
  156. string collection = 1;
  157. string id = 2;
  158. }
  159. message GetResponse {
  160. Document document = 1;
  161. bool found = 2;
  162. }
  163. message UpdateRequest {
  164. string collection = 1;
  165. string id = 2;
  166. bytes data = 3;
  167. uint64 expected_version = 4; // 0 = no version check (force update)
  168. string actor = 5; // User ID performing the operation (for audit)
  169. // v2.8.0 — change the document's expiry as part of the update.
  170. //
  171. // `optional` gives three distinct meanings, which a plain uint32 cannot:
  172. // absent -> leave the existing expiry alone (the default, and what an
  173. // update did implicitly once it stopped WIPING the expiry)
  174. // 0 -> clear the expiry: the document becomes permanent
  175. // N -> expire N seconds from now
  176. //
  177. // Before this, ttl_seconds existed only on Insert/Upsert/BatchInsert, so
  178. // changing a TTL meant rewriting the whole document via upsert.
  179. optional uint32 ttl_seconds = 6;
  180. }
  181. message UpdateResponse {
  182. uint64 new_version = 1;
  183. bool success = 2;
  184. string error = 3;
  185. }
  186. message UpsertRequest {
  187. string collection = 1;
  188. bytes data = 2;
  189. string id = 3;
  190. uint32 ttl_seconds = 4;
  191. string actor = 5; // User ID performing the operation (for audit)
  192. }
  193. message UpsertResponse {
  194. string id = 1;
  195. uint64 version = 2;
  196. bool inserted = 3; // true if inserted, false if updated
  197. }
  198. message DeleteRequest {
  199. string collection = 1;
  200. string id = 2;
  201. }
  202. message DeleteResponse {
  203. bool deleted = 1;
  204. }
  205. message PatchDocumentRequest {
  206. string collection = 1;
  207. string id = 2;
  208. bytes patch_json = 3; // JSON fields to merge into existing document
  209. string actor = 4; // User ID performing the operation (for audit)
  210. // v2.8.0 — same three-way semantics as UpdateRequest.ttl_seconds: absent
  211. // leaves the expiry alone, 0 clears it, N sets N seconds from now.
  212. optional uint32 ttl_seconds = 5;
  213. }
  214. message PatchDocumentResponse {
  215. uint64 new_version = 1;
  216. bool success = 2;
  217. string error = 3;
  218. }
  219. message ExistsRequest {
  220. string collection = 1;
  221. string id = 2;
  222. }
  223. message ExistsResponse {
  224. bool exists = 1;
  225. }
  226. // ===== Version History Operations =====
  227. message DocumentVersionEntry {
  228. uint64 version = 1;
  229. bytes data = 2; // JSON-encoded document data at this version
  230. uint64 timestamp = 3; // When this version was created (updatedAt)
  231. string updated_by = 4;
  232. bool encrypted = 5;
  233. repeated string encrypted_fields = 6;
  234. }
  235. message GetVersionHistoryRequest {
  236. string collection = 1;
  237. string id = 2;
  238. uint32 limit = 3; // 0 = all versions
  239. uint32 offset = 4;
  240. }
  241. message GetVersionHistoryResponse {
  242. repeated DocumentVersionEntry versions = 1;
  243. uint64 current_version = 2; // 0 if document is deleted
  244. uint64 total_count = 3;
  245. bool document_deleted = 4;
  246. }
  247. message GetDocumentVersionRequest {
  248. string collection = 1;
  249. string id = 2;
  250. uint64 version = 3;
  251. }
  252. message GetDocumentVersionResponse {
  253. DocumentVersionEntry version_entry = 1;
  254. bool found = 2;
  255. }
  256. message RestoreVersionRequest {
  257. string collection = 1;
  258. string id = 2;
  259. uint64 version = 3; // Version number to restore
  260. string actor = 4; // User performing the restore
  261. }
  262. message RestoreVersionResponse {
  263. uint64 new_version = 1; // The newly created version number
  264. bool success = 2;
  265. string error = 3;
  266. }
  267. message RestoreToDateRequest {
  268. string collection = 1;
  269. string id = 2;
  270. uint64 timestamp = 3; // Find version active at this time (ms since epoch)
  271. string actor = 4;
  272. }
  273. message RestoreToDateResponse {
  274. uint64 restored_version = 1; // Which old version was restored from
  275. uint64 new_version = 2; // The newly created version number
  276. bool success = 3;
  277. string error = 4;
  278. }
  279. // ===== Batch Operations =====
  280. message BatchInsertRequest {
  281. string collection = 1;
  282. repeated BatchInsertItem items = 2;
  283. string actor = 3; // User ID performing the operation (for audit)
  284. }
  285. message BatchInsertItem {
  286. bytes data = 1;
  287. string id = 2;
  288. uint32 ttl_seconds = 3;
  289. }
  290. message BatchInsertResponse {
  291. repeated string ids = 1;
  292. uint32 success_count = 2;
  293. uint32 error_count = 3;
  294. }
  295. message BatchGetRequest {
  296. string collection = 1;
  297. repeated string ids = 2;
  298. }
  299. message BatchGetResponse {
  300. repeated Document documents = 1;
  301. }
  302. message BatchDeleteRequest {
  303. string collection = 1;
  304. repeated string ids = 2;
  305. }
  306. message BatchDeleteResponse {
  307. uint64 deleted_count = 1;
  308. }
  309. // ===== Query Operations =====
  310. message FindRequest {
  311. string collection = 1;
  312. repeated Filter filters = 2;
  313. Sort sort = 3;
  314. uint32 limit = 4; // Default 100
  315. uint32 offset = 5;
  316. repeated string projection = 6; // Fields to include (empty = all)
  317. }
  318. message Filter {
  319. string field = 1;
  320. FilterOp op = 2;
  321. bytes value = 3; // JSON-encoded value
  322. }
  323. enum FilterOp {
  324. FILTER_OP_UNSPECIFIED = 0;
  325. FILTER_OP_EQ = 1;
  326. FILTER_OP_NE = 2;
  327. FILTER_OP_GT = 3;
  328. FILTER_OP_GTE = 4;
  329. FILTER_OP_LT = 5;
  330. FILTER_OP_LTE = 6;
  331. FILTER_OP_IN = 7;
  332. FILTER_OP_CONTAINS = 8;
  333. FILTER_OP_EXISTS = 9;
  334. FILTER_OP_REGEX = 10;
  335. FILTER_OP_SEARCH = 11; // Full-text search across ID and string fields
  336. }
  337. message Sort {
  338. string field = 1;
  339. bool descending = 2;
  340. }
  341. message FindResponse {
  342. repeated Document documents = 1;
  343. uint64 total_count = 2;
  344. bool has_more = 3;
  345. // WAL fallback metrics (for queries that include evicted documents)
  346. bool used_wal_fallback = 4; // True if WAL was scanned for evicted docs
  347. uint32 memory_match_count = 5; // Documents matched from memory
  348. uint32 wal_match_count = 6; // Documents matched from WAL
  349. uint64 memory_search_micros = 7; // Time spent searching memory (µs)
  350. uint64 wal_search_micros = 8; // Time spent loading/searching WAL (µs)
  351. }
  352. message CountRequest {
  353. string collection = 1;
  354. repeated Filter filters = 2;
  355. }
  356. message CountResponse {
  357. uint64 count = 1;
  358. }
  359. message SimilaritySearchRequest {
  360. string collection = 1;
  361. repeated float query_vector = 2;
  362. uint32 top_k = 3;
  363. float min_score = 4;
  364. }
  365. message SimilaritySearchResponse {
  366. repeated SimilarityResult results = 1;
  367. }
  368. message SimilarityResult {
  369. string id = 1;
  370. float score = 2;
  371. bytes data = 3;
  372. }
  373. // ===== Set Operations =====
  374. message SetAddRequest {
  375. string collection = 1;
  376. string set_id = 2;
  377. string member = 3;
  378. }
  379. message SetAddResponse {
  380. bool added = 1; // false if already exists
  381. }
  382. message SetRemoveRequest {
  383. string collection = 1;
  384. string set_id = 2;
  385. string member = 3;
  386. }
  387. message SetRemoveResponse {
  388. bool removed = 1;
  389. }
  390. message SetMembersRequest {
  391. string collection = 1;
  392. string set_id = 2;
  393. }
  394. message SetMembersResponse {
  395. repeated string members = 1;
  396. }
  397. message SetIsMemberRequest {
  398. string collection = 1;
  399. string set_id = 2;
  400. string member = 3;
  401. }
  402. message SetIsMemberResponse {
  403. bool is_member = 1;
  404. }
  405. // ===== Collection Management =====
  406. message CreateCollectionRequest {
  407. string name = 1;
  408. CollectionOptions options = 2;
  409. }
  410. message CollectionOptions {
  411. bool auto_create_id = 1;
  412. uint32 default_ttl_seconds = 2;
  413. bool encrypted = 3;
  414. repeated string sensitive_fields = 4;
  415. uint32 max_versions = 5; // Max version history per document (0 = unlimited)
  416. uint32 vector_dimension = 6; // Dimension of vectors stored in this collection (0 = not a vector collection)
  417. // NEW in v1.7.0 — eviction priority for the collection.
  418. // UNSPECIFIED is treated as NORMAL for backward compatibility.
  419. MemoryPriority memory_priority = 7;
  420. }
  421. enum MemoryPriority {
  422. MEMORY_PRIORITY_UNSPECIFIED = 0; // treated as NORMAL
  423. MEMORY_PRIORITY_LOW = 1;
  424. MEMORY_PRIORITY_NORMAL = 2;
  425. MEMORY_PRIORITY_HIGH = 3;
  426. }
  427. message CreateCollectionResponse {
  428. bool created = 1;
  429. }
  430. message DropCollectionRequest {
  431. string name = 1;
  432. }
  433. message DropCollectionResponse {
  434. bool dropped = 1;
  435. }
  436. message ListCollectionsRequest {
  437. // v2.8.0 — restrict the listing to one project namespace, mirroring
  438. // ListViewsRequest. Empty lists every project (operator/CLI use); clients
  439. // always set it, so a workspace never sees another workspace's collection
  440. // names. A collection name is information about the shape of someone else's
  441. // data even when its contents are unreachable.
  442. string project = 1;
  443. }
  444. message ListCollectionsResponse {
  445. repeated string names = 1;
  446. }
  447. message GetCollectionInfoRequest {
  448. string name = 1;
  449. }
  450. message GetCollectionInfoResponse {
  451. CollectionInfo info = 1;
  452. bool found = 2;
  453. }
  454. message CollectionInfo {
  455. string name = 1;
  456. uint64 document_count = 2;
  457. uint64 size_bytes = 3;
  458. CollectionOptions options = 4;
  459. uint64 created_at = 5;
  460. uint64 updated_at = 6;
  461. uint32 vector_dimension = 7; // Dimension of vectors stored in this collection (0 = not a vector collection)
  462. }
  463. // ===== Project Management (v2.3) =====
  464. //
  465. // Projects are top-level namespaces over collections. Collections live
  466. // inside a project; wire-form "my_app:users" addresses collection "users"
  467. // in project "my_app", bare "users" addresses ("default", "users"). The
  468. // "default" project is implicit and always present.
  469. //
  470. // Stage F ships the RPC surface backed by a filesystem-only placeholder
  471. // (`<dataDir>/projects/<name>/env/`). Stage B replaces the placeholder
  472. // with the real ProjectStore-backed implementation.
  473. message ListProjectsRequest {}
  474. message ListProjectsResponse {
  475. // Alphabetical. Always includes "default".
  476. repeated string projects = 1;
  477. }
  478. message CreateProjectRequest {
  479. string name = 1;
  480. }
  481. message CreateProjectResponse {
  482. // true = newly created, false = already existed (idempotent) OR validation error.
  483. bool created = 1;
  484. // Populated on validation failure (e.g. invalid name). Empty on success
  485. // and on the idempotent "already existed" path.
  486. string error = 2;
  487. }
  488. message DropProjectRequest {
  489. string name = 1;
  490. }
  491. message DropProjectResponse {
  492. bool dropped = 1;
  493. // Populated when the drop is refused — e.g. name == "default",
  494. // unknown project, or filesystem error.
  495. string error = 2;
  496. }
  497. // ===== File Operations =====
  498. message FileChunk {
  499. oneof content {
  500. FileMetadata metadata = 1; // First chunk includes metadata
  501. bytes data = 2; // Subsequent chunks are data
  502. }
  503. }
  504. message FileMetadata {
  505. string id = 1; // Set by server on upload response
  506. string name = 2;
  507. string mime_type = 3;
  508. uint64 size = 4;
  509. string file_type = 5; // "plugin", "document", "generated"
  510. string related_id = 6; // plugin_id, conversation_id, etc.
  511. map<string, string> metadata = 7; // Custom metadata
  512. bool is_public = 8; // Visibility flag
  513. // v2.6.0 — owning project. Empty means "default", matching the rule for
  514. // bare collection names. Files are namespaced so that per-project access
  515. // policy has something to attach to.
  516. string project = 9;
  517. // v2.8.0 — relative time-to-live in seconds, applied at upload. Converted
  518. // once to an absolute expiry server-side, so a file's lifetime does not
  519. // restart when it is read or on restart.
  520. //
  521. // `optional` is load-bearing: ABSENT means "inherit the configured default
  522. // for this file type", while an explicitly SET 0 means "never expire" and
  523. // overrides that default. A plain uint32 cannot express the difference,
  524. // which would make it impossible to opt one file out of a retention policy.
  525. optional uint32 ttl_seconds = 10;
  526. }
  527. message UploadFileResponse {
  528. string id = 1;
  529. uint64 size = 2;
  530. string checksum = 3; // SHA-256
  531. bool deduplicated = 4; // True if blob already existed
  532. }
  533. message DownloadFileRequest {
  534. string id = 1;
  535. string project = 2; // v2.6.0 — empty means "default"
  536. }
  537. message DeleteFileRequest {
  538. string id = 1;
  539. string project = 2; // v2.6.0 — empty means "default"
  540. }
  541. message DeleteFileResponse {
  542. bool deleted = 1;
  543. }
  544. message GetFileInfoRequest {
  545. string id = 1;
  546. string project = 2; // v2.6.0 — empty means "default"
  547. }
  548. message FileInfo {
  549. string id = 1;
  550. string name = 2;
  551. string mime_type = 3;
  552. uint64 size = 4;
  553. string file_type = 5;
  554. string related_id = 6;
  555. string checksum = 7;
  556. uint64 created_at = 8;
  557. map<string, string> metadata = 9;
  558. bool is_public = 10; // Visibility flag
  559. uint32 ref_count = 11; // Blob reference count (informational)
  560. string project = 12; // v2.6.0 — owning project
  561. uint64 expires_at = 13; // v2.8.0 — absolute expiry ms, 0 = never
  562. }
  563. message ListFilesRequest {
  564. string file_type = 1; // Filter by type (optional)
  565. string related_id = 2; // Filter by related ID (optional)
  566. uint32 limit = 3;
  567. uint32 offset = 4;
  568. string checksum = 5; // Filter by checksum (for dedup queries)
  569. string name = 6; // Filter by exact filename (optional)
  570. string project = 7; // v2.6.0 — empty means "default"
  571. }
  572. // v2.8.0 — change the expiry of a file that is already stored. A file's TTL was
  573. // otherwise fixed at upload.
  574. message SetFileTtlRequest {
  575. string id = 1;
  576. string project = 2; // empty means "default"
  577. // Absent is invalid here - the call exists to change the expiry, so it must
  578. // say what to. 0 clears the expiry (keep indefinitely); N expires the file N
  579. // seconds from now.
  580. uint32 ttl_seconds = 3;
  581. }
  582. message SetFileTtlResponse {
  583. bool updated = 1; // false if no such file in that project
  584. uint64 expires_at = 2; // the resulting absolute expiry, 0 = never
  585. }
  586. message ListFilesResponse {
  587. repeated FileInfo files = 1;
  588. uint64 total_count = 2;
  589. bool has_more = 3;
  590. }
  591. // ===== Event Subscription =====
  592. message SubscribeRequest {
  593. repeated string collections = 1; // Empty = all collections
  594. repeated string patterns = 2; // Glob patterns (e.g., "assistants:*")
  595. bool include_data = 3; // Include document data in events
  596. }
  597. message DatabaseEvent {
  598. EventType type = 1;
  599. string collection = 2;
  600. string document_id = 3;
  601. uint64 timestamp = 4;
  602. string node_id = 5;
  603. bytes data = 6; // JSON document (if include_data)
  604. }
  605. enum EventType {
  606. EVENT_UNKNOWN = 0;
  607. EVENT_INSERT = 1;
  608. EVENT_UPDATE = 2;
  609. EVENT_DELETE = 3;
  610. EVENT_EXPIRE = 4;
  611. EVENT_INVALIDATE = 5;
  612. // NEW in v1.7.0 — memory-pressure observability events.
  613. EVENT_MEMORY_PRESSURE_HIGH = 6; // Emitted when pressure reaches hard threshold
  614. EVENT_MEMORY_EVICTION_BURST = 7; // Emitted when eviction runs a large burst
  615. }
  616. // ===== Replication =====
  617. message ReplicationEntry {
  618. uint64 sequence = 1;
  619. uint64 global_timestamp = 2;
  620. string node_id = 3;
  621. OperationType op = 4;
  622. string collection = 5;
  623. string document_id = 6;
  624. uint64 document_version = 7;
  625. bytes data = 8;
  626. }
  627. enum OperationType {
  628. OP_UNKNOWN = 0;
  629. OP_INSERT = 1;
  630. OP_UPDATE = 2;
  631. OP_DELETE = 3;
  632. OP_UPSERT = 4;
  633. OP_CREATE_COLLECTION = 5;
  634. OP_DROP_COLLECTION = 6;
  635. }
  636. message ReplicationMessage {
  637. oneof content {
  638. ReplicationEntry entry = 1;
  639. Heartbeat heartbeat = 2;
  640. WatermarkUpdate watermark = 3;
  641. SyncRequest sync_request = 4;
  642. }
  643. }
  644. message Heartbeat {
  645. string node_id = 1;
  646. uint64 timestamp = 2;
  647. uint64 sequence = 3;
  648. }
  649. message WatermarkUpdate {
  650. string node_id = 1;
  651. uint64 sequence = 2;
  652. }
  653. message SyncRequest {
  654. uint64 from_sequence = 1;
  655. }
  656. message GetEntriesRequest {
  657. uint64 from_sequence = 1;
  658. uint32 limit = 2;
  659. }
  660. message GetNodeStateRequest {
  661. }
  662. message NodeState {
  663. string node_id = 1;
  664. uint64 current_sequence = 2;
  665. map<string, uint64> peer_watermarks = 3;
  666. uint64 document_count = 4;
  667. repeated string collections = 5;
  668. bool healthy = 6;
  669. // Per-collection sequence tracking for collection discovery
  670. map<string, uint64> collection_sequences = 7;
  671. }
  672. // ===== Health and Stats =====
  673. message HealthCheckRequest {
  674. }
  675. message HealthCheckResponse {
  676. bool healthy = 1;
  677. uint64 uptime_seconds = 2;
  678. uint64 document_count = 3;
  679. uint64 memory_used_bytes = 4;
  680. uint64 wal_size_bytes = 5;
  681. repeated PeerHealth peers = 6;
  682. }
  683. message PeerHealth {
  684. string node_id = 1;
  685. bool connected = 2;
  686. uint64 latency_ms = 3;
  687. uint64 lag_entries = 4;
  688. }
  689. message GetStatsRequest {
  690. }
  691. message GetStatsResponse {
  692. uint64 total_documents = 1;
  693. uint64 total_collections = 2;
  694. uint64 memory_used_bytes = 3;
  695. uint64 wal_sequence = 4;
  696. uint64 wal_size_bytes = 5;
  697. uint64 snapshot_count = 6;
  698. uint64 last_snapshot_sequence = 7;
  699. uint64 insert_count = 8;
  700. uint64 update_count = 9;
  701. uint64 delete_count = 10;
  702. uint64 query_count = 11;
  703. // Memory eviction stats
  704. uint64 evicted_documents = 12; // Documents currently evicted to disk
  705. uint64 total_evictions = 13; // Total eviction operations performed
  706. uint64 recovery_count = 14; // Documents recovered from eviction
  707. // Memory configuration
  708. uint64 max_memory_bytes = 15; // Configured max memory limit
  709. uint32 eviction_threshold_percent = 16; // Start evicting at this % of max
  710. uint32 eviction_target_percent = 17; // Evict down to this % of max
  711. // Operation timing (microseconds) - for performance monitoring
  712. uint64 get_count = 18; // Number of get operations
  713. uint64 get_total_micros = 19; // Total time spent in get operations
  714. uint64 get_max_micros = 20; // Max single get operation time
  715. uint64 insert_total_micros = 21;
  716. uint64 insert_max_micros = 22;
  717. uint64 update_total_micros = 23;
  718. uint64 update_max_micros = 24;
  719. uint64 query_total_micros = 25;
  720. uint64 query_max_micros = 26;
  721. }
  722. // ===== View Operations =====
  723. message ViewDefinition {
  724. string name = 1; // view name (globally unique, cannot start with _)
  725. string collection = 2; // target real collection (must NOT be another view)
  726. repeated string include = 3; // field paths to include (dot-notation for nested)
  727. repeated string exclude = 4; // field paths to exclude (ignored if include is non-empty)
  728. repeated Filter where = 5; // baked-in filters (AND-merged with caller filters)
  729. Sort default_sort = 6; // baked-in default sort (used when caller doesn't specify)
  730. uint64 created_at = 7;
  731. uint64 updated_at = 8;
  732. }
  733. message CreateViewRequest {
  734. string name = 1;
  735. string collection = 2;
  736. repeated string include = 3;
  737. repeated string exclude = 4;
  738. repeated Filter where = 5;
  739. Sort default_sort = 6;
  740. }
  741. message CreateViewResponse {
  742. bool success = 1;
  743. string error = 2;
  744. }
  745. message DropViewRequest {
  746. string name = 1;
  747. }
  748. message DropViewResponse {
  749. bool success = 1;
  750. string error = 2;
  751. }
  752. message ListViewsRequest {
  753. // Restrict the listing to one project namespace. Empty lists every
  754. // project (operator/CLI use). Clients always set it so a workspace
  755. // never sees another workspace's views.
  756. string project = 1;
  757. }
  758. message ListViewsResponse {
  759. repeated ViewDefinition views = 1;
  760. }
  761. message GetViewInfoRequest {
  762. string name = 1;
  763. }
  764. message GetViewInfoResponse {
  765. ViewDefinition view = 1;
  766. bool found = 2;
  767. }
  768. // ===== Relation (Referential Integrity) Operations — v2.11.0 T6b =====
  769. //
  770. // A relation says: documents in `child` reference documents in `parent`
  771. // through `child_field` (a dot-path that may resolve to a single id or an
  772. // array of ids). Declarations are persisted in the global `_relations`
  773. // system collection (mirrors `_views`) and are project-qualified like any
  774. // other collection-carrying name. Management is admin-only.
  775. message RelationDefinition {
  776. string name = 1; // relation name, project-qualified
  777. string child = 2; // collection holding the reference, project-qualified
  778. string child_field = 3; // dot-path on the child; may resolve to an array of ids
  779. string parent = 4; // collection being referenced, project-qualified
  780. // "restrict" (default) | "cascade" | "set_null" | "no_action".
  781. // Only restrict/no_action are enforced today (v2.11.0 T4/T6a); cascade
  782. // and set_null are accepted and persisted but currently behave as
  783. // permit (Task 12 makes them destructive).
  784. string on_delete = 5;
  785. // Not yet enforced (Task 13). Accepted and persisted.
  786. bool validate_on_write = 6;
  787. uint64 created_at = 7;
  788. uint64 updated_at = 8;
  789. }
  790. message CreateRelationRequest {
  791. string name = 1;
  792. string child = 2;
  793. string child_field = 3;
  794. string parent = 4;
  795. string on_delete = 5; // empty defaults to "restrict"
  796. bool validate_on_write = 6;
  797. }
  798. message CreateRelationResponse {
  799. bool success = 1;
  800. string error = 2;
  801. // v2.11.0 T7 — rows indexed by the bootstrap scan. Declaring a relation
  802. // over a collection that already has rows backfills the reverse index
  803. // immediately (mirrors CreateIndexResponse.rows_indexed), so enforcement
  804. // covers pre-existing children from the moment the relation is declared,
  805. // not only writes made afterwards.
  806. uint64 rows_indexed = 3;
  807. }
  808. message DropRelationRequest {
  809. string name = 1;
  810. }
  811. message DropRelationResponse {
  812. bool success = 1;
  813. string error = 2;
  814. }
  815. message ListRelationsRequest {
  816. // Restrict the listing to one project namespace. Empty lists every
  817. // project (operator/CLI use). Clients always send their own project so
  818. // one workspace never enumerates another's relations.
  819. string project = 1;
  820. }
  821. message ListRelationsResponse {
  822. repeated RelationDefinition relations = 1;
  823. }
  824. message GetRelationInfoRequest {
  825. string name = 1;
  826. }
  827. message GetRelationInfoResponse {
  828. RelationDefinition relation = 1;
  829. bool found = 2;
  830. }
  831. // v2.11.0 T5 — "what would happen if I deleted this?", answered without
  832. // deleting anything. A relational database exposes its constraints through
  833. // DDL you can read; a document store has none, so this has to be a query.
  834. // Gated as an ordinary READ on `collection` (it changes nothing), unlike
  835. // the relation-management RPCs above which are admin-only.
  836. message DescribeDeleteRequest {
  837. string collection = 1;
  838. string id = 2;
  839. }
  840. message RelationImpact {
  841. string relation = 1;
  842. string child_collection = 2;
  843. string child_field = 3;
  844. string on_delete = 4; // restrict | cascade | set_null | no_action
  845. uint64 child_count = 5;
  846. repeated string sample_child_ids = 6; // at most five
  847. bool blocks = 7;
  848. }
  849. message DescribeDeleteResponse {
  850. bool success = 1;
  851. string error = 2;
  852. bool would_be_blocked = 3;
  853. repeated RelationImpact impacts = 4;
  854. }
  855. // v2.11.0 T7 — the bootstrap-scan gap. Declaring a relation with T3 alone
  856. // only maintains the reverse index going FORWARD from writes made after
  857. // declaration; rows already in the child collection were never indexed, so
  858. // enforcement silently missed them. This RPC answers "is that still true
  859. // right now, for this relation" without changing anything.
  860. message CheckRelationRequest {
  861. string name = 1; // relation name, project-qualified like the others
  862. }
  863. // One parent id referenced by the child collection's data, whose parent
  864. // document does not exist.
  865. message DanglingReference {
  866. string parent_id = 1;
  867. uint64 child_count = 2; // children of this parent, from the index
  868. repeated string sample_child_ids = 3; // at most five
  869. }
  870. message CheckRelationResponse {
  871. bool success = 1;
  872. string error = 2;
  873. // Every dangling parent id found, not just the reported sample - so a
  874. // capped `dangling` list below does not silently understate the problem.
  875. uint64 total_dangling = 3;
  876. // Capped (see the server implementation) so a badly out-of-sync relation
  877. // cannot blow up the response size; total_dangling is the true count.
  878. repeated DanglingReference dangling = 4;
  879. }
  880. // ===== Collection Configuration =====
  881. // Per-collection configuration for timestamp precision and other runtime knobs.
  882. // Stored in the _collection_meta system collection.
  883. message CollectionConfig {
  884. // "ms" (default) — _created_at/_updated_at stamped in milliseconds since epoch
  885. // "ns" — _created_at/_updated_at stamped in nanoseconds since epoch
  886. // Empty on a ConfigureCollection request means "leave unchanged".
  887. string timestamp_precision = 1;
  888. // v2.4.5 — version history on/off for an EXISTING collection.
  889. //
  890. // `optional` (proto3 field presence) is load-bearing, not decoration: a
  891. // plain bool defaults to false, so any ConfigureCollection call that meant
  892. // to set only timestamp_precision would silently switch versioning OFF.
  893. // With presence, an absent field means "leave unchanged" and the server
  894. // overlays only what the caller actually set.
  895. //
  896. // Disabling stops NEW versions being recorded; history already on disk
  897. // stays readable, so this is reversible. Distinct from max_versions=0,
  898. // which means "unlimited", not "off".
  899. optional bool versioning_enabled = 2;
  900. // v2.11.0 T8 — whether declared relations are enforced for this collection.
  901. //
  902. // `optional` for the same reason versioning_enabled is: a plain bool
  903. // defaults to false, so a ConfigureCollection call that meant to set
  904. // only timestamp_precision would silently switch relation enforcement
  905. // OFF. With presence, an absent field means "leave unchanged".
  906. //
  907. // Defaults to true (enforced) when never explicitly configured -
  908. // declaring a relation names its child and parent explicitly, so the
  909. // declaration IS the opt-in.
  910. optional bool relations_enforced = 3;
  911. // Room for future per-collection knobs
  912. }
  913. message ConfigureCollectionRequest {
  914. string collection = 1;
  915. CollectionConfig config = 2;
  916. }
  917. message ConfigureCollectionResponse {
  918. bool success = 1;
  919. string error = 2;
  920. }
  921. // v2.9.0 — secondary index management.
  922. message CreateIndexRequest {
  923. // Project-qualified collection name, as every other collection-carrying
  924. // request takes.
  925. string collection = 1;
  926. // Field to index. Dotted paths address nested fields ("nest.deep"), and the
  927. // document metadata fields (_id, _created_at, _updated_at, _version) are
  928. // addressable too.
  929. string field = 2;
  930. }
  931. message CreateIndexResponse {
  932. bool success = 1;
  933. string error = 2;
  934. // Rows indexed by the backfill. Creating an index over an existing
  935. // collection populates it, so it is usable immediately rather than only for
  936. // rows written afterwards.
  937. uint64 rows_indexed = 3;
  938. // True when the index already existed - creation is idempotent.
  939. bool already_existed = 4;
  940. }
  941. message DropIndexRequest {
  942. string collection = 1;
  943. string field = 2;
  944. }
  945. message DropIndexResponse {
  946. bool success = 1;
  947. string error = 2;
  948. }
  949. message ListIndexesRequest {
  950. string collection = 1;
  951. }
  952. message IndexInfo {
  953. string field = 1;
  954. // Distinct values held, and total postings. Both come from the index itself
  955. // rather than a scan, so an operator can judge selectivity - a field whose
  956. // postings are concentrated in few values will not be used by the planner.
  957. uint64 distinct_values = 2;
  958. uint64 entries = 3;
  959. }
  960. message ListIndexesResponse {
  961. bool success = 1;
  962. string error = 2;
  963. repeated IndexInfo indexes = 3;
  964. }
  965. message GetIndexValuesRequest {
  966. string collection = 1;
  967. string field = 2;
  968. // Bounded on purpose: reading the values costs one document read each.
  969. uint32 limit = 3;
  970. // false walks from the highest value down, so limit=1 gives the maximum.
  971. bool ascending = 4;
  972. }
  973. message IndexValueCount {
  974. // The value as stored, JSON-encoded.
  975. string value = 1;
  976. uint64 count = 2;
  977. }
  978. message GetIndexValuesResponse {
  979. bool success = 1;
  980. string error = 2;
  981. repeated IndexValueCount values = 3;
  982. }
  983. message GetCollectionConfigRequest {
  984. string collection = 1;
  985. }
  986. message GetCollectionConfigResponse {
  987. CollectionConfig config = 1;
  988. bool found = 2;
  989. }
  990. message MigrateCollectionTimestampsRequest {
  991. string collection = 1;
  992. string from_precision = 2; // "ms" or "ns"
  993. string to_precision = 3; // "ms" or "ns"
  994. }
  995. message MigrateCollectionTimestampsResponse {
  996. bool success = 1;
  997. string error = 2;
  998. uint64 rows_migrated = 3; // how many documents had their timestamps updated
  999. uint64 rows_skipped = 4; // already in target range (idempotent resume)
  1000. }
  1001. // ===== Read-Only Control =====
  1002. message SetReadOnlyRequest {
  1003. bool read_only = 1;
  1004. }
  1005. message SetReadOnlyResponse {
  1006. bool success = 1;
  1007. bool was_read_only = 2; // previous state
  1008. string error = 3;
  1009. }
  1010. message GetReadOnlyStatusRequest {}
  1011. message GetReadOnlyStatusResponse {
  1012. bool read_only = 1;
  1013. string reason = 2;
  1014. // Recovery outcome details (populated when auto-readonly triggered)
  1015. string recovery_outcome = 3; // "trivial_success" | "fresh_install" | "snapshot_fell_back" | "wal_only_replay" | "forced_empty" | "failed"
  1016. string expected_snapshot = 4;
  1017. string snapshot_used = 5;
  1018. string failure_reason = 6;
  1019. uint64 wal_entries_replayed = 7;
  1020. uint32 snapshots_attempted = 8;
  1021. }
  1022. // ===== Memory Stats =====
  1023. // Added in v1.7.0. Wire-compatible — server-side implementation lands in T9.
  1024. message GetMemoryStatsRequest {}
  1025. message CollectionMemoryStats {
  1026. string collection = 1;
  1027. uint64 document_count = 2;
  1028. uint64 estimated_bytes = 3;
  1029. uint64 evicted_stub_count = 4;
  1030. MemoryPriority priority = 5;
  1031. }
  1032. message GetMemoryStatsResponse {
  1033. uint64 total_memory_bytes = 1;
  1034. uint64 max_memory_bytes = 2;
  1035. uint32 pressure_percent = 3; // current usage / max × 100
  1036. string pressure_level = 4; // "normal" | "soft" | "hard" | "emergency"
  1037. repeated CollectionMemoryStats collections = 5;
  1038. // Most recent eviction
  1039. uint64 last_eviction_timestamp = 6; // ms since epoch, 0 if none
  1040. uint64 last_eviction_docs = 7;
  1041. uint64 last_eviction_bytes_freed = 8;
  1042. }