1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
//! Knowledge Graph REST handlers.
//!
//! Why: The KG Explorer UI and MCP tool surface both rely on these endpoints
//! to browse, assert, and retract triples. Keeping them in one file makes
//! the KG REST surface easy to audit and extend.
//! What: All `/api/v1/palaces/{id}/kg/*` endpoints plus the dream-cycle
//! status/run endpoints, and the opaque triple-id encode/decode helpers.
//! Test: `kg_list_subjects_*`, `kg_list_all_*`, `kg_graph_*`,
//! `decode_triple_id_*`, `dream_status_*`, `dream_run_*` in `web::tests`.
use ;
use Deserialize;
use ;
// #4670: ExpandDirection backs the `direction` param on `kg/graph/neighbors`.
use ;
use crateAppState;
// #4776: the page-size bounds moved to the service layer so the MCP
// `kg_list_subjects` tool reads the same two literals these routes do; `tools`
// is compiled without the `axum-server` feature that gates this module.
use crate;
use ApiError;
// ---------------------------------------------------------------------------
// KG query + assert
// ---------------------------------------------------------------------------
/// Query parameters for `GET /api/v1/palaces/{id}/kg`.
///
/// Why: Requires a `subject` filter so the handler does not accidentally
/// return the full graph, which can be unbounded.
/// What: Single required `subject` string.
/// Test: Covered by integration.
pub
/// `GET /api/v1/palaces/{id}/kg?subject=<s>` — query active triples for a subject.
///
/// Why: The KG Explorer detail view and external tooling need a fast subject
/// lookup without fetching the whole graph.
/// What: Delegates to `MemoryService::kg_query`.
/// Test: Covered by integration.
pub async
pub use crateKgAssertBody;
/// `POST /api/v1/palaces/{id}/kg` — assert a new triple.
///
/// Why: HTTP counterpart to the MCP `kg_assert` tool.
/// What: Delegates to `MemoryService::kg_assert`; returns `204 No Content`.
/// Test: Covered via `http_create_drawer_runs_auto_kg_extraction`.
pub async
// ---------------------------------------------------------------------------
// KG list helpers
// ---------------------------------------------------------------------------
/// Query parameters for `GET /api/v1/palaces/{id}/kg/subjects`.
///
/// Why: The KG Explorer's left panel asks for a bounded subject list; `limit`
/// is clamped server-side so the SPA cannot accidentally pull the whole graph.
/// What: `limit` defaults to [`DEFAULT_KG_LIST_LIMIT`] and is clamped to
/// `[1, MAX_KG_LIST_LIMIT]` in the handler.
/// Test: `kg_list_subjects_returns_distinct`.
pub
/// `GET /api/v1/palaces/{id}/kg/subjects?limit=N` — list distinct active subjects.
///
/// Why: The KG Explorer needs to browse subjects without a prior query (the
/// existing `kg_query` endpoint requires one). Surfacing this read on the
/// daemon avoids the SPA having to know how to issue SQL.
/// What: clamps `limit` to `[1, MAX_KG_LIST_LIMIT]` and delegates to
/// `KnowledgeGraph::list_subjects`. Returns a JSON array of strings.
/// Test: `kg_list_subjects_returns_distinct`.
pub async
/// `GET /api/v1/palaces/{id}/kg/subjects_with_counts?limit=N` — list distinct
/// active subjects with their active-triple counts.
///
/// Why: The KG Explorer's subject list shows a count badge per subject and
/// supports sort-by-count. Returning the grouped counts in a single SQL pass
/// is cheaper than issuing one query per subject from the SPA.
/// What: clamps `limit` to `[1, MAX_KG_LIST_LIMIT]` and delegates to
/// `KnowledgeGraph::list_subjects_with_counts`. Returns a JSON array of
/// `{subject, count}` objects ordered alphabetically.
/// Test: indirectly via the KG Explorer UI.
pub async
/// Query parameters for `GET /api/v1/palaces/{id}/kg/all`.
///
/// Why: The KG Explorer's "All" mode pages through every active triple;
/// `limit`+`offset` give the SPA stable prev/next controls.
/// What: defaults match `kg_list_subjects` for limit; `offset` defaults to 0.
/// Test: `kg_list_all_returns_paginated_triples`.
pub
/// `GET /api/v1/palaces/{id}/kg/all?limit=N&offset=N` — list all active triples.
///
/// Why: The KG Explorer's "All" mode wants a paged view across every active
/// triple regardless of subject. The existing `kg_query` requires a subject.
/// What: clamps `limit` to `[1, MAX_KG_LIST_LIMIT]` and delegates to
/// `KnowledgeGraph::list_active`. Returns a JSON array of `Triple` objects.
/// Test: `kg_list_all_returns_paginated_triples`.
pub async
/// `GET /api/v1/palaces/{id}/kg/count` — count of currently-active triples.
///
/// Why: The KG Explorer header shows a quick "N triples" badge; computing the
/// count server-side avoids fetching every triple to count them.
/// What: returns `{ "active": N }` where N is `count_active_triples()` on the
/// palace's KG. A failed store read is a 500 (#5384): the badge cannot tell
/// `{"active": 0}` apart from a count that was never read.
/// Test: indirectly via the same palace counts surfaced on `/api/v1/status`.
pub async
// ---------------------------------------------------------------------------
// Triple encode/decode + delete
// ---------------------------------------------------------------------------
/// Separator byte sequence used inside a URL-safe base64 triple ID.
///
/// Why: The triple primary key is `(subject, predicate)`. Encoding them as a
/// single opaque ID lets the REST path look like `/kg/triples/<id>` (a
/// resource identifier) rather than carrying both parts in the URL path, which
/// would require double-escaping arbitrary strings. A `\0` separator is safe
/// because neither subjects nor predicates ever contain null bytes.
/// What: Used by [`encode_triple_id`] and [`decode_triple_id`].
/// Test: `decode_triple_id_round_trips`.
const TRIPLE_ID_SEPARATOR: u8 = 0x00;
/// Encode a `(subject, predicate)` pair as a URL-safe base64 triple ID.
///
/// Why: Produces a single opaque string that can travel as a URL path segment
/// without percent-encoding. The null-byte separator ensures the encoding is
/// injective (no two distinct pairs can produce the same encoded string) —
/// but only when neither component itself contains a null byte. Prior to
/// issue #1102 the function silently produced an ambiguous (corrupt) id when
/// the subject or predicate contained `\0`; now it returns an error so
/// callers cannot persist an undecodable id.
/// What: Validates that neither `subject` nor `predicate` contains
/// `TRIPLE_ID_SEPARATOR` (`\0`). On success returns
/// `Ok(base64url(subject_bytes + "\0" + predicate_bytes))`, no padding.
/// On validation failure returns `Err` with a descriptive message.
/// Test: `decode_triple_id_round_trips`, `encode_triple_id_rejects_null_byte`.
// Only called from tests (round-trip + null-byte rejection); suppress the
// dead_code lint that fires in non-test builds.
pub
/// Decode a URL-safe base64 triple ID back to `(subject, predicate)`.
///
/// Why: The handler for `DELETE /kg/triples/<id>` needs to recover the
/// `(subject, predicate)` pair from the opaque path segment to call the
/// service layer.
/// What: Decodes base64url, splits on the first null byte. Returns `None`
/// when the input is not valid base64url or contains no null separator.
/// Test: `decode_triple_id_round_trips`.
pub
/// `DELETE /api/v1/palaces/{id}/kg/triples/{triple_id}` — surgically remove
/// one active triple by its opaque base64url-encoded `(subject, predicate)` ID.
///
/// Why: Issue #278 — the existing `(subject, predicate)` retract via
/// `/kg/prompt-facts` is scope-wide (retract across all palaces). This
/// endpoint targets exactly one triple in exactly one palace, giving callers
/// a surgical way to delete a specific edge without affecting other palaces
/// or other predicates for the same subject.
/// What: Decodes `triple_id` (base64url of `subject\0predicate`) back into
/// `(subject, predicate)`, retracts the active interval via
/// `MemoryService::kg_retract_triple`, and returns:
/// - `204 No Content` on success
/// - `404 Not Found` when the triple_id is malformed or no active triple
/// matched
///
/// Test: `kg_delete_triple_returns_204_on_success` and
/// `kg_delete_triple_returns_404_for_missing`.
pub async : ,
)
pub use crateKgGraphPayload;
/// `GET /api/v1/palaces/{id}/kg/graph` — full graph for visualisation.
///
/// Why: The KG Explorer graph-view's explicit "load everything" mode needs the
/// full active triple set. As of issue #4670 this is no longer the view's
/// default load — see [`kg_graph_seed`] — because the payload is capped at
/// `KG_GRAPH_MAX_TRIPLES` and the response now says so.
/// What: Delegates to `MemoryService::kg_graph`; returns `KgGraphPayload`,
/// which carries `returned_triple_count` / `active_triple_count` / `truncated`.
/// Test: `kg_graph_returns_active_triples`, `kg_graph_signals_truncation`,
/// `kg_graph_meets_perf_budget_for_500_triples`.
pub async
// ---------------------------------------------------------------------------
// Progressive graph exploration (issue #4670)
// ---------------------------------------------------------------------------
pub use crate;
/// Default seed size for `GET /kg/graph/seed` when the caller omits `limit`.
///
/// Why (issue #4670): the graph view's layout is an O(n²) force simulation run
/// for ~200 ticks. At 75 nodes that is ~2,775 pair computations per tick
/// (~555K total) — imperceptible; at the palace's full 9,311 nodes it is ~43M
/// per tick (~8.7B total), which is the freeze this endpoint exists to avoid.
/// 75 sits deliberately above the sibling list endpoints' 50: measured degree
/// distribution on the live palace is 90.2% degree-1 leaves with top degrees of
/// 48/46/45/44/32, so a 50-node seed stops right where the mid-tier hubs
/// (degree 5–10, ~7% of nodes) begin. 75 reaches into that tier while leaving
/// ~2.5× headroom under the max before layout cost becomes noticeable, so a
/// few click-expansions do not need a page reload to stay smooth.
const DEFAULT_KG_SEED_LIMIT: usize = 75;
/// Hard ceiling on the seed size.
///
/// Why: mirrors `MAX_KG_LIST_LIMIT` so every bounded KG read in this file
/// shares one ceiling. 200 nodes is ~4M layout operations — still interactive,
/// and past it the hairball is unreadable regardless of performance.
const MAX_KG_SEED_LIMIT: usize = 200;
/// Query parameters for `GET /api/v1/palaces/{id}/kg/graph/seed`.
pub
/// `GET /api/v1/palaces/{id}/kg/graph/seed?limit=N` — top-N nodes by degree.
///
/// Why (issue #4670): first paint of the graph view. Returns the structurally
/// important slice of the graph plus the palace-wide totals, so the header can
/// state "75 of 9,311 nodes shown" instead of implying it rendered everything.
/// What: clamps `limit` to `[1, MAX_KG_SEED_LIMIT]` and delegates to
/// `MemoryService::kg_graph_seed`. The returned `triples` use the same wire
/// shape as `/kg/graph`, so the client merges seed and expansion results with
/// one code path.
/// Test: `kg_graph_seed_ranks_by_degree`, `kg_graph_seed_clamps_limit`.
pub async
/// Maximum BFS depth for `GET /kg/graph/neighbors`.
///
/// Why: matches `trusty-search`'s `graph_neighbors_handler`, which clamps
/// `max_hops` to `1..=4` — keeping the two crates' traversal contracts
/// identical means an operator who learned one already knows the other.
const MAX_KG_NEIGHBOR_HOPS: usize = 4;
/// Query parameters for `GET /api/v1/palaces/{id}/kg/graph/neighbors`.
///
/// Why: parameter names (`node`, `direction`, `max_hops`) deliberately mirror
/// `trusty-search`'s contributed-graph neighbors endpoint.
/// What: `node` is required; `direction` defaults to `both`; `max_hops`
/// defaults to 1 and is clamped to `[1, MAX_KG_NEIGHBOR_HOPS]`.
/// Test: `kg_neighbors_returns_incoming_edges`, `kg_neighbors_clamps_max_hops`,
/// `kg_neighbors_rejects_bad_direction`.
pub
/// `GET /api/v1/palaces/{id}/kg/graph/neighbors?node=X&direction=…&max_hops=N`
///
/// Why (issue #4670): click-to-expand. Crucially this is the first endpoint
/// that can answer "what points AT this node" — `kg_query` is a subject prefix
/// scan (`store/kg_redb/read_ops.rs:29-67`) and never reads the object side,
/// so the incoming half of every palace graph was unreachable over HTTP.
/// What: parses `direction` (`in` | `out` | `both`, 400 on anything else),
/// clamps `max_hops`, and delegates to `MemoryService::kg_neighbors`, which
/// BFSes the resident adjacency — no disk I/O.
/// Test: `kg_neighbors_returns_incoming_edges`, `kg_neighbors_clamps_max_hops`,
/// `kg_neighbors_rejects_bad_direction`.
pub async
// ---------------------------------------------------------------------------
// Dream cycle status + on-demand run
// ---------------------------------------------------------------------------
pub use crateDreamStatusPayload;
/// `GET /api/v1/dream/status` — aggregate dream cycle status across all palaces.
///
/// Why: The admin UI dashboard shows whether the last dream cycle succeeded.
/// What: Delegates to `MemoryService::dream_status_aggregate`.
/// Test: `dream_status_empty_returns_nulls`, `dream_status_aggregates_across_palaces`.
pub async
/// `GET /api/v1/palaces/{id}/dream/status` — dream cycle status for one palace.
///
/// Why: Per-palace dream status lets the UI show which palace is stale.
/// What: Delegates to `MemoryService::dream_status_for_palace`.
/// Test: Covered implicitly by `dream_status_aggregates_across_palaces`.
pub async
/// `POST /api/v1/dream/run` — trigger an on-demand dream cycle.
///
/// Why: Operators and tests need a way to trigger consolidation without
/// waiting for the scheduled background timer.
/// What: Delegates to `MemoryService::dream_run`; returns the aggregate
/// status after the run completes.
/// Test: `dream_run_aggregates_stats`.
pub async