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
//! Wire types shared between the trusty-memory HTTP handlers and the service
//! layer.
//!
//! Why: both the axum handlers and the `MemoryService` business layer need the
//! same serializable request/response shapes; hosting them in one submodule
//! keeps the wire contract single-source (split out of the former monolithic
//! `service.rs`, issue #607).
//! What: the `PalaceInfo`/`*Body`/`*Query`/`*Payload` structs plus the
//! `ServiceError` enum and `ServiceResult` alias, moved verbatim.
//! Test: covered indirectly via the HTTP tests in `web::tests` and
//! `service::tests`.
use ;
use PersistedDreamStats;
use Triple;
/// Serializable palace summary used by `GET /api/v1/palaces` and
/// `GET /api/v1/palaces/{id}`.
///
/// Why: Both endpoints return the same enriched shape; centralising the
/// type in the service layer keeps the wire contract single-source.
/// What: Mirrors the legacy `PalaceInfo` struct verbatim — counts, timestamps,
/// graph stats, and the `is_compacting` flag.
/// Test: `palace_list_includes_richer_counts`, `palace_list_includes_graph_counts`.
/// Dream statistics wire shape used by both per-palace and aggregate endpoints.
///
/// Why: Lifted out of `web.rs` so the service layer owns the type the chat
/// dispatcher and HTTP handlers both serialise. Stays identical to the
/// pre-refactor shape.
/// What: All fields are saturating sums across one or more palaces; the
/// `last_run_at` is the max across them (or `None` when no palace has run).
/// Test: `dream_status_aggregates_across_palaces`, `dream_run_aggregates_stats`.
/// `POST /api/v1/palaces` body — service-facing version.
///
/// Why: Change 2 — the optional `cwd` field lets HTTP callers pass the
/// filesystem path of the project they are operating from. When present,
/// `validate_palace_name` uses it as the `start` for pin-file-based
/// validation instead of the daemon's own cwd (which is `~` or `/` and
/// rarely meaningful). When absent the existing daemon-cwd fallback applies
/// so older clients continue to work.
/// What: `name` is required; `description`, `cwd`, and `force` are optional.
/// Test: `create_palace_accepts_pinned_slug_via_cwd`,
/// `create_palace_rejects_mismatch_when_cwd_given`,
/// `create_palace_force_bypasses_validation`.
/// `POST /api/v1/palaces/{id}/drawers` body — service-facing version.
///
/// Why: `content` normally goes through `PalaceHandle::remember`'s signal/
/// noise quality gate (noise patterns, short-content, non-alphabetic ratio —
/// see `trusty_common::memory_core::filter`), which is correct for
/// human/LLM-authored prose but rejects legitimate structured payloads (a
/// caller storing `{"score":0.42,"tier":3}`-shaped content is mostly
/// punctuation/digits, so `non_alphabetic_ratio` trips the gate). Issue
/// #3225 (trusty-agents `TrustyMemoryClient` writing JSON-serialized
/// payloads as `content` to preserve losslessness) needed a bypass that
/// mirrors the QUALITY-gate-only `force` semantics `RememberOptions`
/// already exposes internally — MCP/CLI callers never had a way to reach
/// it over HTTP.
/// What: `force` mirrors `RememberOptions::force` (issue #61/#2520):
/// `Some(true)` skips the noise/short-content/non-alphabetic QUALITY gates
/// only — the secret-detection gate (`check_secret`) still runs
/// unconditionally, so this can never be used to smuggle credential-shaped
/// content past screening. Defaults to `false` (`None`/absent), preserving
/// the existing gate for ordinary prose callers (the admin UI, MCP
/// `memory_remember`, `memory_note`).
/// Test: `create_drawer_rejects_json_content_without_force`,
/// `create_drawer_force_bypasses_quality_gate_for_json_content`.
/// `GET /api/v1/palaces/{id}/drawers` query — service-facing version.
///
/// Why: the TUI activity panel (#184) needs paged access to a palace's
/// drawers in newest-first order. Adding `offset` and `sort` to the existing
/// query struct keeps the surface compatible (both fields default to absent)
/// while letting the panel walk through arbitrarily many drawers.
/// What: optional `room` / `tag` filters, a `limit` (default 50 in the
/// handler), an `offset` for pagination, and a `sort` selector — `importance`
/// (the legacy default, descending) or `created_desc` (newest first).
/// Test: `list_drawers_creates_desc_paginates` in `service::tests`.
/// `POST /api/v1/palaces/{id}/kg` body — service-facing version.
/// Knowledge-graph "graph payload" used by `GET /api/v1/palaces/{id}/kg/graph`.
///
/// Why: the count fields are computed over the FULL in-memory adjacency while
/// `triples` is capped at `KG_GRAPH_MAX_TRIPLES`. Before issue #4670 nothing
/// in the payload said so, so a client rendering 5,000 triples while the badge
/// read "9,311 nodes" had no way to know it was looking at a partial graph —
/// and because `list_active` orders by `valid_from` DESC, the dropped triples
/// were silently the oldest ones. `returned_triple_count` / `active_triple_count`
/// / `truncated` make the gap explicit and machine-checkable.
/// What: the triple window plus both the true totals and what was actually
/// returned.
/// Test: `kg_graph_signals_truncation`, `kg_graph_returns_active_triples`.
/// One node in a progressive-exploration response.
///
/// Why (issue #4670): the client must be able to say "48 edges, 3 shown" —
/// that requires the node's degree in the WHOLE graph, not in the fragment it
/// was handed.
/// What: entity name plus its graph-wide in/out/total degree.
/// Test: `kg_graph_seed_ranks_by_degree`.
/// Payload for `GET /api/v1/palaces/{id}/kg/graph/seed`.
///
/// Why (issue #4670): first paint should show the graph's skeleton (the
/// highest-degree nodes and the edges among them), never the whole hairball.
/// Carrying the palace-wide totals alongside the returned slice is what lets
/// the header read "75 of 9,311 nodes shown" instead of implying completeness.
/// What: the seed nodes, the induced edges as `Triple`s (same wire shape as
/// `/kg/graph`'s `triples`, so the client merges without a second parser), the
/// palace-wide totals, and the `limit` actually applied after clamping.
/// Test: `kg_graph_seed_ranks_by_degree`, `kg_graph_seed_clamps_limit`.
/// Payload for `GET /api/v1/palaces/{id}/kg/graph/neighbors`.
///
/// Why (issue #4670): click-to-expand merges this into the rendered set, so it
/// needs the same node/triple shape as the seed. `community_count` is
/// deliberately absent — Louvain is the expensive part of the full-graph call
/// and the client already has the palace's community count from the seed load.
/// What: the reached nodes (origin first, so the client can anchor new nodes
/// on it), the traversed edges, and the echoed traversal parameters.
/// Test: `kg_neighbors_returns_incoming_edges`, `kg_neighbors_clamps_max_hops`.
/// Status payload returned by `GET /api/v1/status`.
///
/// Issue #4637 changed the meaning of the three `total_*` fields: they are now
/// summed over the palaces resident in the registry's open-handle cache, not
/// over every palace on disk, because summing over disk meant force-opening
/// ~5,730 cold palaces per request. `palace_count` still reports the true
/// on-disk total; `cached_palace_count` reports how many of those the totals
/// actually cover.
/// Service-level error type that maps cleanly onto HTTP status codes.
///
/// Why: handlers want to render 400/404/409/500 from a single point; the
/// service methods produce a typed error so the binding layer can pick the
/// right status without parsing strings.
/// What: four variants matching the legacy `ApiError` constructors plus a
/// dedicated `Conflict` for state-clash errors (issue #180: deleting a
/// non-empty palace without `force`).
/// Test: indirectly via the HTTP tests for the corresponding endpoints.
/// Result alias used across the service layer.
pub type ServiceResult<T> = Result;