database.proto 38 KB

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