proto: memory, the arguments of recall and memory and eight message kinds
Implemented-By: OpenCode session (model recorded in docs/implementer-log.md)
This commit is contained in:
@@ -0,0 +1 @@
|
||||
{"v":1,"id":1,"final":true,"msg":{"kind":"memory_admin","body":{"command":"undo","arg":"2026-09-24-0007"}}}
|
||||
@@ -0,0 +1 @@
|
||||
{"v":1,"id":1,"final":true,"msg":{"kind":"memory_admin_reply","body":{"text":"undone: 2026-09-24-0007"}}}
|
||||
@@ -0,0 +1 @@
|
||||
{"v":1,"id":1,"final":true,"msg":{"kind":"memory_block","body":{}}}
|
||||
@@ -0,0 +1 @@
|
||||
{"v":1,"id":1,"final":true,"msg":{"kind":"memory_block_reply","body":{"text":"# soul.md\nYou are Boxmaker.\n\n# memory.md (0 of 2,200 characters)\n(empty)\n\n# user.md (0 of 1,375 characters)\n(empty)"}}}
|
||||
@@ -0,0 +1 @@
|
||||
{"v":1,"id":3,"final":true,"msg":{"kind":"memory_call","body":{"request":{"session":"mm-r0000000000000000000000001","call":3,"tool":"memory","arguments":"{\"action\":\"note\",\"text\":\"example.org says March.\"}"},"taint":"private","untrusted":true}}}
|
||||
@@ -0,0 +1 @@
|
||||
{"v":1,"id":1,"final":true,"msg":{"kind":"reindex","body":{}}}
|
||||
@@ -0,0 +1 @@
|
||||
{"v":1,"id":1,"final":true,"msg":{"kind":"reindexed","body":{"logs":42,"records":1208,"notes":7}}}
|
||||
@@ -0,0 +1 @@
|
||||
{"v":1,"id":1,"final":true,"msg":{"kind":"session_updated","body":{"session":"mm-r0000000000000000000000001"}}}
|
||||
@@ -0,0 +1,167 @@
|
||||
//! The arguments of `recall` and `memory`: decoding, unknown keys refused, and the shape checks
|
||||
//! (M5a spec, section 5). Do not edit.
|
||||
|
||||
use proto::memory::{MAX_ENTRY, MAX_NOTE, entry_problem};
|
||||
use proto::{ConsolidateEntry, EntryAction, MemoryArgs, MemoryFile, RecallArgs};
|
||||
|
||||
fn memory(json: &str) -> Result<MemoryArgs, String> {
|
||||
serde_json::from_str::<MemoryArgs>(json).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recall_decodes_with_a_default_k_and_checks_its_bounds() {
|
||||
let args: RecallArgs = serde_json::from_str(r#"{"query":"launch window"}"#).unwrap();
|
||||
assert_eq!(args.k, 5);
|
||||
assert_eq!(args.check(), Ok(()));
|
||||
for (json, ok) in [
|
||||
(r#"{"query":"x","k":10}"#, true),
|
||||
(r#"{"query":"x","k":11}"#, false),
|
||||
(r#"{"query":"x","k":0}"#, false),
|
||||
(r#"{"query":" ","k":1}"#, false),
|
||||
] {
|
||||
let args: RecallArgs = serde_json::from_str(json).unwrap();
|
||||
assert_eq!(args.check().is_ok(), ok, "{json}");
|
||||
}
|
||||
let long = RecallArgs {
|
||||
query: "é".repeat(500),
|
||||
k: 1,
|
||||
};
|
||||
assert_eq!(long.check(), Ok(()), "500 characters, not bytes");
|
||||
let longer = RecallArgs {
|
||||
query: "é".repeat(501),
|
||||
k: 1,
|
||||
};
|
||||
assert!(longer.check().is_err());
|
||||
assert!(serde_json::from_str::<RecallArgs>(r#"{"query":"x","limit":3}"#).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_memory_action_decodes() {
|
||||
assert_eq!(
|
||||
memory(r#"{"action":"add","file":"memory","text":"Owner prefers UTC."}"#),
|
||||
Ok(MemoryArgs::Add {
|
||||
file: MemoryFile::Memory,
|
||||
text: "Owner prefers UTC.".to_string()
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
memory(r#"{"action":"replace","file":"user","old":"a","text":"b"}"#),
|
||||
Ok(MemoryArgs::Replace {
|
||||
file: MemoryFile::User,
|
||||
old: "a".to_string(),
|
||||
text: "b".to_string()
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
memory(r#"{"action":"remove","file":"user","old":"a"}"#),
|
||||
Ok(MemoryArgs::Remove {
|
||||
file: MemoryFile::User,
|
||||
old: "a".to_string()
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
memory(r#"{"action":"note","text":"example.org says March."}"#),
|
||||
Ok(MemoryArgs::Note {
|
||||
text: "example.org says March.".to_string()
|
||||
})
|
||||
);
|
||||
let consolidate = memory(
|
||||
r#"{"action":"consolidate","file":"memory","entries":[{"old":"a","keep":true},{"old":"b","text":"B"},{"old":"c","move":true},{"old":"d","drop":true},{"text":"e"}]}"#,
|
||||
)
|
||||
.unwrap();
|
||||
let MemoryArgs::Consolidate { file, entries } = consolidate else {
|
||||
panic!("not a consolidate")
|
||||
};
|
||||
assert_eq!(file, MemoryFile::Memory);
|
||||
let actions: Vec<EntryAction> = entries.iter().map(|e| e.action().unwrap()).collect();
|
||||
assert_eq!(
|
||||
actions,
|
||||
[
|
||||
EntryAction::Keep("a".to_string()),
|
||||
EntryAction::Rewrite {
|
||||
old: "b".to_string(),
|
||||
text: "B".to_string()
|
||||
},
|
||||
EntryAction::Move("c".to_string()),
|
||||
EntryAction::Drop("d".to_string()),
|
||||
EntryAction::New("e".to_string()),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_actions_files_and_keys_are_refused() {
|
||||
for json in [
|
||||
r#"{"action":"forget","file":"memory"}"#,
|
||||
r#"{"action":"add","file":"soul","text":"x"}"#,
|
||||
r#"{"action":"add","file":"memory","text":"x","why":"y"}"#,
|
||||
r#"{"action":"note","text":"x","file":"memory"}"#,
|
||||
r#"{"action":"consolidate","file":"memory","entries":[{"old":"a","keep":true,"why":1}]}"#,
|
||||
r#"{"file":"memory","text":"x"}"#,
|
||||
] {
|
||||
assert!(memory(json).is_err(), "{json}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_consolidate_line_has_exactly_one_action() {
|
||||
let line = |old: Option<&str>, keep: bool, text: Option<&str>, moved: bool, drop: bool| {
|
||||
ConsolidateEntry {
|
||||
old: old.map(str::to_string),
|
||||
keep,
|
||||
text: text.map(str::to_string),
|
||||
moved,
|
||||
drop,
|
||||
}
|
||||
};
|
||||
for bad in [
|
||||
line(Some("a"), false, None, false, false),
|
||||
line(Some("a"), true, None, true, false),
|
||||
line(Some("a"), true, Some("b"), false, false),
|
||||
line(Some("a"), false, Some("b"), false, true),
|
||||
line(None, false, None, false, false),
|
||||
line(None, true, Some("b"), false, false),
|
||||
] {
|
||||
assert!(bad.action().is_err(), "{bad:?}");
|
||||
}
|
||||
let empty = MemoryArgs::Consolidate {
|
||||
file: MemoryFile::User,
|
||||
entries: vec![],
|
||||
};
|
||||
assert!(empty.check().is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn entries_are_not_empty_not_too_long_and_hold_no_separator_line() {
|
||||
assert_eq!(entry_problem("fine", MAX_ENTRY), None);
|
||||
assert_eq!(entry_problem("two\nlines", MAX_ENTRY), None);
|
||||
assert_eq!(entry_problem("a § in a line is fine", MAX_ENTRY), None);
|
||||
assert!(entry_problem("", MAX_ENTRY).is_some());
|
||||
assert!(entry_problem(" \n\t", MAX_ENTRY).is_some());
|
||||
assert!(entry_problem("before\n§\nafter", MAX_ENTRY).is_some());
|
||||
assert!(entry_problem("before\n § \nafter", MAX_ENTRY).is_some());
|
||||
assert_eq!(entry_problem(&"é".repeat(MAX_ENTRY), MAX_ENTRY), None);
|
||||
assert!(entry_problem(&"é".repeat(MAX_ENTRY + 1), MAX_ENTRY).is_some());
|
||||
let note = MemoryArgs::Note {
|
||||
text: "x".repeat(MAX_NOTE + 1),
|
||||
};
|
||||
assert!(note.check().is_err());
|
||||
let rewrite = MemoryArgs::Replace {
|
||||
file: MemoryFile::Memory,
|
||||
old: "fine".to_string(),
|
||||
text: "a\n§\nb".to_string(),
|
||||
};
|
||||
assert!(rewrite.check().is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_files_names_and_limits() {
|
||||
assert_eq!(
|
||||
(MemoryFile::Memory.name(), MemoryFile::Memory.limit()),
|
||||
("memory.md", 2_200)
|
||||
);
|
||||
assert_eq!(
|
||||
(MemoryFile::User.name(), MemoryFile::User.limit()),
|
||||
("user.md", 1_375)
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
//! The eight memory messages against byte-exact fixtures, each refusing an unknown key (M5a spec,
|
||||
//! section 7). Do not edit these or the fixtures.
|
||||
|
||||
use proto::{
|
||||
CallId, DataClass, Empty, Envelope, MemoryAdmin, MemoryAdminReply, MemoryBlockReply,
|
||||
MemoryCall, MemoryCommand, Message, Reindexed, SessionId, SessionUpdated, ToolRequest,
|
||||
};
|
||||
|
||||
const SESSION: &str = "mm-r0000000000000000000000001";
|
||||
|
||||
fn fixture(name: &str) -> String {
|
||||
let path = format!("{}/tests/fixtures/wire/{name}", env!("CARGO_MANIFEST_DIR"));
|
||||
let text = std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("{path}: {e}"));
|
||||
text.trim_end_matches('\n').to_string()
|
||||
}
|
||||
|
||||
/// The fixture must decode to `msg`, `msg` must encode to exactly the fixture's bytes, and the
|
||||
/// body must refuse an extra key.
|
||||
fn check(name: &str, id: u64, msg: Message) {
|
||||
let want = Envelope {
|
||||
v: 1,
|
||||
id,
|
||||
r#final: true,
|
||||
msg,
|
||||
};
|
||||
let text = fixture(name);
|
||||
let got: Envelope = serde_json::from_str(&text).unwrap_or_else(|e| panic!("{name}: {e}"));
|
||||
assert_eq!(got, want, "{name}: decoded value");
|
||||
assert_eq!(
|
||||
serde_json::to_string(&want).unwrap(),
|
||||
text,
|
||||
"{name}: encoded bytes"
|
||||
);
|
||||
let extra = text.replacen("\"body\":{", "\"body\":{\"extra\":1,", 1);
|
||||
assert!(
|
||||
serde_json::from_str::<Envelope>(&extra).is_err(),
|
||||
"{name}: an unknown key is refused"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_eight_messages() {
|
||||
check("memory_block.json", 1, Message::MemoryBlock(Empty {}));
|
||||
check(
|
||||
"memory_block_reply.json",
|
||||
1,
|
||||
Message::MemoryBlockReply(MemoryBlockReply {
|
||||
text: "# soul.md\nYou are Boxmaker.\n\n# memory.md (0 of 2,200 characters)\n(empty)\n\n# user.md (0 of 1,375 characters)\n(empty)".to_string(),
|
||||
}),
|
||||
);
|
||||
check(
|
||||
"session_updated.json",
|
||||
1,
|
||||
Message::SessionUpdated(SessionUpdated {
|
||||
session: SessionId::new(SESSION).unwrap(),
|
||||
}),
|
||||
);
|
||||
check(
|
||||
"memory_call.json",
|
||||
3,
|
||||
Message::MemoryCall(MemoryCall {
|
||||
request: ToolRequest {
|
||||
session: SessionId::new(SESSION).unwrap(),
|
||||
call: CallId(3),
|
||||
tool: "memory".to_string(),
|
||||
arguments: r#"{"action":"note","text":"example.org says March."}"#.to_string(),
|
||||
},
|
||||
taint: DataClass::Private,
|
||||
untrusted: true,
|
||||
}),
|
||||
);
|
||||
check("reindex.json", 1, Message::Reindex(Empty {}));
|
||||
check(
|
||||
"reindexed.json",
|
||||
1,
|
||||
Message::Reindexed(Reindexed {
|
||||
logs: 42,
|
||||
records: 1208,
|
||||
notes: 7,
|
||||
}),
|
||||
);
|
||||
check(
|
||||
"memory_admin.json",
|
||||
1,
|
||||
Message::MemoryAdmin(MemoryAdmin {
|
||||
command: MemoryCommand::Undo,
|
||||
arg: "2026-09-24-0007".to_string(),
|
||||
}),
|
||||
);
|
||||
check(
|
||||
"memory_admin_reply.json",
|
||||
1,
|
||||
Message::MemoryAdminReply(MemoryAdminReply {
|
||||
text: "undone: 2026-09-24-0007".to_string(),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_command_has_its_name() {
|
||||
for (command, name) in [
|
||||
(MemoryCommand::Log, "log"),
|
||||
(MemoryCommand::Undo, "undo"),
|
||||
(MemoryCommand::Pending, "pending"),
|
||||
(MemoryCommand::Accept, "accept"),
|
||||
(MemoryCommand::Reject, "reject"),
|
||||
] {
|
||||
assert_eq!(
|
||||
serde_json::to_string(&command).unwrap(),
|
||||
format!("\"{name}\"")
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user