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
//! Palace recall/remember/note over the daemon socket, with no MCP (#8352).
//!
//! Why: `tm memory` exposed only `import` and `import-auto-memory`, so a PM
//! whose `mcp__trusty-memory__*` connection is dead had no way to reach the
//! palace at all. The parity target is trusty-search, which is reachable from
//! its own CLI without an MCP client in the middle. Owner requirement, verbatim:
//! "memory should \[be\] an in process mpm command as well as mcp".
//!
//! What: [`run_verb`] resolves the palace and the socket, then issues ONE
//! direct-method JSON-RPC call — `memory_recall`, `memory_remember` or
//! `memory_note`, the three names trusty-memory's `TOOL_METHODS` allowlist
//! already forwards to the same handlers the MCP tools reach — through
//! `trusty_common::memory_rpc::call_memory_tool_at`. The arguments this module
//! builds are the MCP tools' own schema keys; nothing here invents a parameter.
//!
//! **Every write goes over the socket.** This module opens no palace store of
//! its own, so it cannot become the second in-process writer of a palace the
//! daemon already serves — the redb exclusive-lock hazard #1078 fixed and
//! `trusty_common::memory_core::registry` documents. It therefore adds no
//! `memory-core` edge: the default `trusty-mpm` build pays no HNSW/ONNX cost
//! for these verbs, and there is deliberately NO no-daemon read path (a
//! read-only snapshot fallback would need exactly that edge, behind a
//! non-default feature, to answer a question a running daemon already answers).
//!
//! Test: `memory_verbs_tests.rs`, plus `tests/memory_verbs_socket.rs` for the
//! end-to-end binary path.
use std::path::PathBuf;
use serde::Serialize;
use serde_json::{Map, Value, json};
use trusty_common::memory_rpc::{call_memory_tool_at, resolve_memory_socket};
use trusty_common::palace_resolve::resolve_palace;
/// The trusty-memory method `tm memory recall` calls.
pub const RECALL_METHOD: &str = "memory_recall";
/// The trusty-memory method `tm memory remember` calls.
pub const REMEMBER_METHOD: &str = "memory_remember";
/// The trusty-memory method `tm memory note` calls.
pub const NOTE_METHOD: &str = "memory_note";
/// Why a verb could not run, or could not be answered.
///
/// Why a typed error (#8352): the binary layer turns each of these into a
/// distinct non-zero exit message, and the socket-down arm must NAME the socket
/// rather than report a generic transport failure — that path is the whole
/// point of the verbs, so an operator has to be able to see which socket was
/// dialled. No arm degrades to an empty success.
/// What: [`Self::Palace`] when no palace could be resolved for a write,
/// [`Self::Socket`] when the socket path itself could not be derived, and
/// [`Self::Call`] when the daemon did not answer — or answered with an error.
/// Test: `a_dead_socket_is_an_error_naming_it`,
/// `a_write_without_a_resolvable_palace_is_refused`.
// #8372: non_exhaustive from its first release, so a new failure is not an API break.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum MemoryVerbError {
/// A write verb, and no palace could be resolved for the working directory.
#[error("no palace resolved for {cwd}: {detail} — pass --palace <slug>")]
Palace {
/// The directory resolution started from.
cwd: String,
/// What resolution reported.
detail: String,
},
/// The trusty-memory socket path could not be derived.
#[error("could not resolve the trusty-memory socket: {detail}")]
Socket {
/// What resolution reported.
detail: String,
},
/// Nothing answered the socket, or the daemon answered with an error.
#[error("trusty-memory did not answer `{method}` at {socket}: {detail}")]
Call {
/// The JSON-RPC method that was called.
method: &'static str,
/// The socket that was dialled — the operator-actionable fact.
socket: String,
/// What the transport or the daemon reported.
detail: String,
},
}
/// One `tm memory` verb and the arguments it carries.
///
/// Why: the three verbs differ only in method name and argument keys, so
/// modelling them as data keeps [`run_verb`] a single path and keeps the
/// argument keys — which are trusty-memory's own MCP schema keys — in one
/// place to audit against `crates/trusty-memory/src/tools/definitions.rs`.
/// What: [`Self::Recall`] mirrors `memory_recall`, [`Self::Remember`]
/// `memory_remember`, [`Self::Note`] `memory_note`.
/// Test: `arguments_carry_only_the_schema_keys_supplied`.
#[derive(Debug, Clone)]
pub enum MemoryVerb {
/// Progressive L0+L1+L2 recall.
Recall {
/// The search text.
query: String,
/// `top_k`; omitted lets the daemon's own default (10) stand.
top_k: Option<u64>,
/// Restrict the semantic layer to one room.
room: Option<String>,
/// Restrict the L2 search to one wing's rooms.
wing: Option<String>,
/// Relevance floor applied to query-scored hits.
min_score: Option<f64>,
},
/// Store a memory, subject to the daemon's content gates.
Remember {
/// The memory text.
text: String,
/// Room to file it in.
room: Option<String>,
/// Tags to store alongside it.
tags: Vec<String>,
},
/// Store a short curated fact (`DrawerType::UserFact`, importance 1.0).
Note {
/// The fact.
content: String,
/// Room to file it in.
room: Option<String>,
/// Tags to store alongside it.
tags: Vec<String>,
},
}
impl MemoryVerb {
/// The JSON-RPC method this verb calls.
pub fn method(&self) -> &'static str {
match self {
Self::Recall { .. } => RECALL_METHOD,
Self::Remember { .. } => REMEMBER_METHOD,
Self::Note { .. } => NOTE_METHOD,
}
}
/// Does this verb write to the palace?
///
/// Why it matters here: a write needs a palace NAMED, while a read with no
/// palace is answered by the daemon with a palace index (#6318) that tells
/// the caller which palace to name next.
pub fn is_write(&self) -> bool {
!matches!(self, Self::Recall { .. })
}
/// The tool arguments, without `palace`.
///
/// What: only the keys the caller actually supplied are present, so an
/// omitted flag leaves the daemon's own default in force rather than
/// pinning it from here.
/// Test: `arguments_carry_only_the_schema_keys_supplied`.
fn arguments(&self) -> Map<String, Value> {
let mut args = Map::new();
match self {
Self::Recall {
query,
top_k,
room,
wing,
min_score,
} => {
args.insert("query".into(), json!(query));
insert_opt(&mut args, "top_k", top_k.map(Value::from));
insert_opt(&mut args, "room", room.clone().map(Value::from));
insert_opt(&mut args, "wing", wing.clone().map(Value::from));
insert_opt(&mut args, "min_score", min_score.map(Value::from));
}
Self::Remember { text, room, tags } => {
args.insert("text".into(), json!(text));
insert_opt(&mut args, "room", room.clone().map(Value::from));
insert_tags(&mut args, tags);
}
Self::Note {
content,
room,
tags,
} => {
args.insert("content".into(), json!(content));
insert_opt(&mut args, "room", room.clone().map(Value::from));
insert_tags(&mut args, tags);
}
}
args
}
}
/// Insert `value` under `key` only when it is present.
fn insert_opt(args: &mut Map<String, Value>, key: &str, value: Option<Value>) {
if let Some(value) = value {
args.insert(key.to_string(), value);
}
}
/// Insert a non-empty `tags` array.
fn insert_tags(args: &mut Map<String, Value>, tags: &[String]) {
if !tags.is_empty() {
args.insert("tags".to_string(), json!(tags));
}
}
/// Where a verb runs: which palace, which socket, from which directory.
#[derive(Debug, Clone, Default)]
pub struct MemoryVerbOptions {
/// `--palace`; outranks every derived level, including the env override.
pub palace: Option<String>,
/// `--memory-socket`; outranks `TRUSTY_MEMORY_SOCKET` and the derived path.
pub socket: Option<PathBuf>,
/// Directory palace resolution starts from. `None` means the process cwd.
pub cwd: Option<PathBuf>,
}
/// What one verb produced, in the shape `--json` prints.
///
/// Why a fixed envelope: an agent parsing this must not have to tell three
/// response shapes apart, and must be able to see which palace and which socket
/// answered — the two facts that decide whether an empty recall is a wrong
/// palace or an empty one. Every key is always present, `null` where it does
/// not apply, so a parser never branches on key existence.
/// What: `verb`, `palace`, `socket`, `count` (recall hits, else `null`) and
/// `result` — the daemon's own tool body, verbatim.
/// Test: `json_envelope_keys_are_always_present`.
#[derive(Debug, Clone, Serialize)]
pub struct MemoryVerbOutcome {
/// `recall` / `remember` / `note`.
pub verb: String,
/// The palace the call named, when one was resolved.
pub palace: Option<String>,
/// The socket that answered.
pub socket: String,
/// Number of recall hits; `null` for a write.
pub count: Option<usize>,
/// The daemon's own tool result, unmodified.
pub result: Value,
}
/// Resolve the palace this verb addresses.
///
/// Why: the verbs have to land in the SAME palace the MCP tools would, so this
/// goes through `trusty_common::palace_resolve::resolve_palace` — the single
/// entry point whose level 1 is `TRUSTY_MEMORY_PALACE`, the variable a managed
/// session's environment injects (`core::mcp_session_env`). `--palace` sits
/// above all four levels because it is the caller's explicit instruction.
/// What: `Ok(Some(id))` when a palace was named or derived; `Ok(None)` when
/// nothing could be derived and the verb is a READ, which the daemon answers
/// with a palace index rather than an error (#6318); `Err` when nothing could be
/// derived and the verb WRITES, because a write has nowhere to land.
/// Test: `explicit_palace_outranks_the_derived_levels`,
/// `a_write_without_a_resolvable_palace_is_refused`.
pub fn resolve_verb_palace(
verb: &MemoryVerb,
opts: &MemoryVerbOptions,
) -> Result<Option<String>, MemoryVerbError> {
if let Some(explicit) = opts.palace.as_ref().map(|p| p.trim())
&& !explicit.is_empty()
{
return Ok(Some(explicit.to_string()));
}
let cwd = match opts.cwd.clone() {
Some(cwd) => cwd,
None => std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")),
};
match resolve_palace(&cwd) {
Ok(resolution) => Ok(Some(resolution.id)),
Err(e) if verb.is_write() => Err(MemoryVerbError::Palace {
cwd: cwd.display().to_string(),
detail: e.to_string(),
}),
Err(e) => {
tracing::debug!("no palace resolved for {}: {e}", cwd.display());
Ok(None)
}
}
}
/// Run one verb against the running trusty-memory daemon.
///
/// Why: this is the whole no-MCP path — resolve, call, hand back the daemon's
/// own body. It performs no local storage access of any kind, so a palace the
/// daemon holds open is never opened a second time (#1078).
///
/// # Errors
///
/// [`MemoryVerbError::Palace`] when a write has no palace,
/// [`MemoryVerbError::Socket`] when the socket path cannot be derived, and
/// [`MemoryVerbError::Call`] — naming the socket — when nothing answers it or
/// the daemon refuses. The wait is bounded by
/// `trusty_common::memory_rpc::DEFAULT_TIMEOUT`, so a dead daemon fails fast
/// rather than hanging.
///
/// Test: `recall_sends_the_resolved_palace`, `a_write_sends_a_write_method_over_the_socket`,
/// `a_dead_socket_is_an_error_naming_it`.
pub async fn run_verb(
verb: &MemoryVerb,
opts: &MemoryVerbOptions,
) -> Result<MemoryVerbOutcome, MemoryVerbError> {
let palace = resolve_verb_palace(verb, opts)?;
let socket = match opts.socket.clone() {
Some(socket) => socket,
None => resolve_memory_socket().map_err(|e| MemoryVerbError::Socket {
detail: format!("{e:#}"),
})?,
};
let mut args = verb.arguments();
if let Some(palace) = palace.clone() {
args.insert("palace".to_string(), Value::String(palace));
}
let method = verb.method();
let result = call_memory_tool_at(&socket, method, Value::Object(args))
.await
.map_err(|e| MemoryVerbError::Call {
method,
socket: socket.display().to_string(),
detail: format!("{e:#}"),
})?;
let count = if verb.is_write() {
None
} else {
Some(
result
.get("results")
.and_then(Value::as_array)
.map(Vec::len)
.unwrap_or(0),
)
};
Ok(MemoryVerbOutcome {
verb: verb_label(verb).to_string(),
palace,
socket: socket.display().to_string(),
count,
result,
})
}
/// The CLI name of a verb, as `--json` reports it.
fn verb_label(verb: &MemoryVerb) -> &'static str {
match verb {
MemoryVerb::Recall { .. } => "recall",
MemoryVerb::Remember { .. } => "remember",
MemoryVerb::Note { .. } => "note",
}
}
#[cfg(test)]
#[path = "memory_verbs_tests.rs"]
mod tests;