database.proto 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  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. // v2.11.0 T11 — when true, the field carries a UNIQUE constraint: two
  931. // documents may never hold the same value for it. Declaring this over a
  932. // collection that already contains duplicate values is REFUSED (see
  933. // CreateIndexResponse.duplicate_examples) rather than accepted and left
  934. // silently unenforced against the data that already violates it.
  935. bool unique = 3;
  936. }
  937. message CreateIndexResponse {
  938. bool success = 1;
  939. string error = 2;
  940. // Rows indexed by the backfill. Creating an index over an existing
  941. // collection populates it, so it is usable immediately rather than only for
  942. // rows written afterwards.
  943. uint64 rows_indexed = 3;
  944. // True when the index already existed - creation is idempotent.
  945. bool already_existed = 4;
  946. // v2.11.0 T11 — set when a `unique=true` request was refused because the
  947. // field already holds duplicate values. `error` explains the refusal;
  948. // these are up to five example document ids that collide, one entry per
  949. // colliding value, so the caller can go fix the data rather than guess.
  950. repeated string duplicate_examples = 5;
  951. }
  952. message DropIndexRequest {
  953. string collection = 1;
  954. string field = 2;
  955. }
  956. message DropIndexResponse {
  957. bool success = 1;
  958. string error = 2;
  959. }
  960. message ListIndexesRequest {
  961. string collection = 1;
  962. }
  963. message IndexInfo {
  964. string field = 1;
  965. // Distinct values held, and total postings. Both come from the index itself
  966. // rather than a scan, so an operator can judge selectivity - a field whose
  967. // postings are concentrated in few values will not be used by the planner.
  968. uint64 distinct_values = 2;
  969. uint64 entries = 3;
  970. // v2.11.0 T11 — true when this index also carries a UNIQUE constraint.
  971. bool unique = 4;
  972. }
  973. message ListIndexesResponse {
  974. bool success = 1;
  975. string error = 2;
  976. repeated IndexInfo indexes = 3;
  977. }
  978. message GetIndexValuesRequest {
  979. string collection = 1;
  980. string field = 2;
  981. // Bounded on purpose: reading the values costs one document read each.
  982. uint32 limit = 3;
  983. // false walks from the highest value down, so limit=1 gives the maximum.
  984. bool ascending = 4;
  985. }
  986. message IndexValueCount {
  987. // The value as stored, JSON-encoded.
  988. string value = 1;
  989. uint64 count = 2;
  990. }
  991. message GetIndexValuesResponse {
  992. bool success = 1;
  993. string error = 2;
  994. repeated IndexValueCount values = 3;
  995. }
  996. message GetCollectionConfigRequest {
  997. string collection = 1;
  998. }
  999. message GetCollectionConfigResponse {
  1000. CollectionConfig config = 1;
  1001. bool found = 2;
  1002. }
  1003. message MigrateCollectionTimestampsRequest {
  1004. string collection = 1;
  1005. string from_precision = 2; // "ms" or "ns"
  1006. string to_precision = 3; // "ms" or "ns"
  1007. }
  1008. message MigrateCollectionTimestampsResponse {
  1009. bool success = 1;
  1010. string error = 2;
  1011. uint64 rows_migrated = 3; // how many documents had their timestamps updated
  1012. uint64 rows_skipped = 4; // already in target range (idempotent resume)
  1013. }
  1014. // ===== Read-Only Control =====
  1015. message SetReadOnlyRequest {
  1016. bool read_only = 1;
  1017. }
  1018. message SetReadOnlyResponse {
  1019. bool success = 1;
  1020. bool was_read_only = 2; // previous state
  1021. string error = 3;
  1022. }
  1023. message GetReadOnlyStatusRequest {}
  1024. message GetReadOnlyStatusResponse {
  1025. bool read_only = 1;
  1026. string reason = 2;
  1027. // Recovery outcome details (populated when auto-readonly triggered)
  1028. string recovery_outcome = 3; // "trivial_success" | "fresh_install" | "snapshot_fell_back" | "wal_only_replay" | "forced_empty" | "failed"
  1029. string expected_snapshot = 4;
  1030. string snapshot_used = 5;
  1031. string failure_reason = 6;
  1032. uint64 wal_entries_replayed = 7;
  1033. uint32 snapshots_attempted = 8;
  1034. }
  1035. // ===== Memory Stats =====
  1036. // Added in v1.7.0. Wire-compatible — server-side implementation lands in T9.
  1037. message GetMemoryStatsRequest {}
  1038. message CollectionMemoryStats {
  1039. string collection = 1;
  1040. uint64 document_count = 2;
  1041. uint64 estimated_bytes = 3;
  1042. uint64 evicted_stub_count = 4;
  1043. MemoryPriority priority = 5;
  1044. }
  1045. message GetMemoryStatsResponse {
  1046. uint64 total_memory_bytes = 1;
  1047. uint64 max_memory_bytes = 2;
  1048. uint32 pressure_percent = 3; // current usage / max × 100
  1049. string pressure_level = 4; // "normal" | "soft" | "hard" | "emergency"
  1050. repeated CollectionMemoryStats collections = 5;
  1051. // Most recent eviction
  1052. uint64 last_eviction_timestamp = 6; // ms since epoch, 0 if none
  1053. uint64 last_eviction_docs = 7;
  1054. uint64 last_eviction_bytes_freed = 8;
  1055. }