README Source History Refs Reviews Compare Notes Search Review guide CI runs refactor(core): Session::append derives the parent from the selected leaf; stored parent links enter only through frames
68397e9cb53620577f804ea5bb91ce74b049d2e3
Oliver Jan Krylow <oliver@bugabinga.net > · 26 Sep 2026
unsigned
Browse files Compare with HEAD
Changed files (8) Diff layout Auto Unified Split
.system/plans/SMH-PLAN-82POBOKB-session-arena/index.md Modified +1 / −1 text lines
48 48 Session API:Session API:50 - - `Session::append(&mut self, parent, content: &EntryContent) -> &Entry` encodes once into the arena.50 + - `Session::append(&mut self, content: &EntryContent) -> Result< &Entry> ` encodes once into the arena after the selected leaf; stored parent links outside that chain only enter through frames .51 51 - `Session::content(&self, entry: &Entry) -> Result<EntryContent>` decodes on demand.- `Session::content(&self, entry: &Entry) -> Result<EntryContent>` decodes on demand.52 52 - `Session::bytes(&self, entry: &Entry) -> &[u8]` for writers and hashing.- `Session::bytes(&self, entry: &Entry) -> &[u8]` for writers and hashing.53 53 - `Session::fork_at` copies the retained records and one arena slice.- `Session::fork_at` copies the retained records and one arena slice.smith-core/src/frame.rs Modified +1 / −1 text lines
600 600 let mut session = Session::new(); let mut session = Session::new();601 - let record = * session.append (None , & content).unwrap ();601 + let record = * session.append (& content).unwrap ();602 602 let from_arena = let from_arena = 603 603 encode_entry (& record, session.bytes (& record), DEFAULT_MAX_FRAME_BYTES ).unwrap (); encode_entry (& record, session.bytes (& record), DEFAULT_MAX_FRAME_BYTES ).unwrap ();604 604 let owned = Frame::Known { let owned = Frame::Known {smith-core/src/lib.rs Modified +1 / −1 text lines
45 45 fn session_creates_and_appends () { fn session_creates_and_appends () {46 46 let mut session = Session::new(); let mut session = Session::new();47 47 let msg = Message::with_text(Role::User, "hello" ); let msg = Message::with_text(Role::User, "hello" );48 - let entry = * session.append (None , & EntryContent::Message(msg)).unwrap ();48 + let entry = * session.append (& EntryContent::Message(msg)).unwrap ();49 49 assert_eq! (session.active_branch ().entries (), vec! [entry]); assert_eq! (session.active_branch ().entries (), vec! [entry]);50 50 assert_eq! (session.selected_id (), Some (entry.id)); assert_eq! (session.selected_id (), Some (entry.id));smith-core/src/session.rs Modified +40 / −37 text lines
482 - pub fn append (&mut self, parent : Option < EntryId > , content: & EntryContent) -> Result <& Entry> {483 - self.append_with (parent , content, |_, _| Ok (()))482 + pub fn append (&mut self, content: & EntryContent) -> Result <& Entry> {483 + self.append_with (content, |_, _| Ok (()))493 493 pub fn append_with ( pub fn append_with (494 494 &mut self, &mut self,495 - parent: Option <EntryId>,
496 495 content: & EntryContent, content: & EntryContent,497 496 persist: impl FnOnce(&Entry, &[u8]) -> Result <()>, persist: impl FnOnce(&Entry, &[u8]) -> Result <()>,498 497 ) -> Result <& Entry> { ) -> Result <& Entry> {
498 + let parent = self.selected_id ();499 499 let offset = self.payloads.len (); let offset = self.payloads.len ();500 500 let staged = content let staged = content501 501 .encode_into (&mut self.payloads) .encode_into (&mut self.payloads)532 532 kind: COMPACTION_KIND .to_string (), kind: COMPACTION_KIND .to_string (),535 - self.append (self . selected_id ( ) , & content).copied ()535 + self.append (& content).copied ()794 794 frames_of (session, & session.active_branch ().entries) frames_of (session, & session.active_branch ().entries)
799 + fn stored (entries: & [EntryFrame]) -> (SessionHeader, Vec <Frame>) {800 + let header = SessionHeader {801 + session_id: SessionId::new(),802 + branch_id: BranchId::new(),804 + let mut frames = vec! [Frame::Header { header }];805 + frames.extend (entries.iter ().cloned ().map (|entry| Frame::Known { entry }));797 809 fn ids (branch: & Branch) -> Vec <EntryId> { fn ids (branch: & Branch) -> Vec <EntryId> {798 810 branch.entries.iter ().map (|entry| entry.id).collect () branch.entries.iter ().map (|entry| entry.id).collect ()802 814 fn sibling_entries_restore_as_separate_branches () { fn sibling_entries_restore_as_separate_branches () {804 - let mut scratch = Session::new();805 - let root = * scratch . append (None , & message ("root" )).unwrap ();806 - let first = * scratch . append (Some (root.id), & message ("first" )).unwrap ();807 - let second = * scratch . append (Some (root.id), & message ("second" )).unwrap ();808 - let stored = frames (& scratch);816 + let root = EntryFrame :: new (None , & message ("root" )).unwrap ();817 + let first = EntryFrame :: new (Some (root.id), & message ("first" )).unwrap ();818 + let second = EntryFrame :: new (Some (root.id), & message ("second" )).unwrap ();819 + let (header, stored) = stored (& [root.clone (), first.clone (), second.clone ()]);809 820 let session = Session::from_frames(& stored).unwrap (); let session = Session::from_frames(& stored).unwrap ();810 821 assert_eq! (session.branches.len (), 2 ); assert_eq! (session.branches.len (), 2 );813 824 assert_eq! (session.selected_id (), Some (second.id)); assert_eq! (session.selected_id (), Some (second.id));814 - assert_eq! (session.active_branch ().id, scratch . header ( ) .branch_id);825 + assert_eq! (session.active_branch ().id, header.branch_id);815 826 assert_eq! (ids (session.active_branch ()), vec! [root.id, second.id]); assert_eq! (ids (session.active_branch ()), vec! [root.id, second.id]);816 827 let sibling = & session.branches[1 - session.active_branch_idx]; let sibling = & session.branches[1 - session.active_branch_idx];817 828 assert_eq! (ids (sibling), vec! [root.id, first.id]); assert_eq! (ids (sibling), vec! [root.id, first.id]);819 830 assert_eq! ( assert_eq! (820 831 session.content (& sibling.entries[0 ]).unwrap (), session.content (& sibling.entries[0 ]).unwrap (),821 - scratch.content (& root).unwrap ()832 + EntryContent::decode(& root.content).unwrap ()824 835 let again = Session::from_frames(& stored).unwrap (); let again = Session::from_frames(& stored).unwrap ();833 844 output: "boom" .to_string (), output: "boom" .to_string (),834 845 call_id: ToolCallId::new(), call_id: ToolCallId::new(),836 - let entry = * session.append (None , & content).unwrap ();847 + let entry = * session.append (& content).unwrap ();837 848 assert_eq! (entry.kind, content.kind ()); assert_eq! (entry.kind, content.kind ());838 849 assert_eq! (session.content (& entry).unwrap (), content); assert_eq! (session.content (& entry).unwrap (), content);839 850 assert_eq! (session.bytes (& entry).len (), entry.content.len as usize ); assert_eq! (session.bytes (& entry).len (), entry.content.len as usize );856 867 fn failed_persist_leaves_the_session_unchanged () { fn failed_persist_leaves_the_session_unchanged () {857 868 let mut session = Session::new(); let mut session = Session::new();858 - session.append (None , & message ("kept" )).unwrap ();869 + session.append (& message ("kept" )).unwrap ();859 870 let before = session.clone (); let before = session.clone ();860 871 let err = session let err = session861 872 .append_with ( .append_with (862 - session.selected_id (),
863 873 & EntryContent::Meta { & EntryContent::Meta {864 874 kind: COMPACTION_KIND .to_string (), kind: COMPACTION_KIND .to_string (),865 875 detail: serde_json::json! ({}), detail: serde_json::json! ({}),878 888 fn fork_preserves_ids_order_leaf_and_compaction_boundary () { fn fork_preserves_ids_order_leaf_and_compaction_boundary () {879 889 let mut session = Session::new(); let mut session = Session::new();880 - let root = * session.append (None , & message ("root" )).unwrap ();890 + let root = * session.append (& message ("root" )).unwrap ();881 891 let boundary = session let boundary = session882 892 .append_compaction (serde_json::json! ({"summarized" : 1 })) .append_compaction (serde_json::json! ({"summarized" : 1 }))883 893 .unwrap (); .unwrap ();884 - let after = * session885 - .append (Some (boundary.id), & message ("after" ))894 + let after = * session.append (& message ("after" )).unwrap ();888 896 let forked = session.fork_at (boundary.id).unwrap (); let forked = session.fork_at (boundary.id).unwrap ();889 897 assert_ne! (forked.id, session.id); assert_ne! (forked.id, session.id);930 938 second.id = first.id; second.id = first.id;931 939 second.timestamp_ms = first.timestamp_ms; second.timestamp_ms = first.timestamp_ms;932 940 let mut session = Session::new(); let mut session = Session::new();933 - let a = * session.append (None , & EntryContent::Message(first)).unwrap ();935 - .append (None , & EntryContent::Message(second))941 + let a = * session.append (& EntryContent::Message(first)).unwrap ();942 + let b = * session.append (& EntryContent::Message(second)).unwrap ();937 943 assert_eq! (session.bytes (& a), session.bytes (& b)); assert_eq! (session.bytes (& a), session.bytes (& b));948 954 fn fork_rejects_broken_ancestry_and_forks_sibling_branches () { fn fork_rejects_broken_ancestry_and_forks_sibling_branches () {950 - let mut scratch = Session::new();951 - let orphan = * scratch952 - . append (Some (EntryId::new()), & message ("orphan" ))954 - let child = * scratch . append (Some (orphan.id), & message ("child" )).unwrap ();955 - let broken = Session::from_frames(& frames (& scratch)).unwrap ();956 + let orphan = EntryFrame :: new (Some (EntryId::new()), & message ("orphan" )). unwrap ( ) ; 957 + let child = EntryFrame :: new (Some (orphan.id), & message ("child" )).unwrap ();958 + let broken = Session::from_frames(& stored (& [orphan, child.clone ()]).1 ).unwrap ();956 959 assert_eq! (broken.selected_id (), Some (child.id)); assert_eq! (broken.selected_id (), Some (child.id));957 960 let err = broken.fork_at (child.id).unwrap_err (); let err = broken.fork_at (child.id).unwrap_err ();958 961 assert_eq! (err.code (), "ENTRY_NOT_FOUND" ); assert_eq! (err.code (), "ENTRY_NOT_FOUND" );961 - let mut scratch = Session::new();962 - let root = * scratch . append (None , & message ("root" )).unwrap ();963 - let first = * scratch . append (Some (root.id), & message ("first" )).unwrap ();964 - scratch . append (Some (root.id), & message ("second" )).unwrap ();965 - let siblings = Session::from_frames(& frames (& scratch)).unwrap ();964 + let root = EntryFrame :: new (None , & message ("root" )).unwrap ();965 + let first = EntryFrame :: new (Some (root.id), & message ("first" )).unwrap ();966 + let second = EntryFrame :: new (Some (root.id), & message ("second" )).unwrap ();968 + Session::from_frames(& stored (& [root.clone (), first.clone (), second]).1 ).unwrap ();966 969 let forked = siblings.fork_at (first.id).unwrap (); let forked = siblings.fork_at (first.id).unwrap ();967 970 assert_eq! (ids (forked.active_branch ()), vec! [root.id, first.id]); assert_eq! (ids (forked.active_branch ()), vec! [root.id, first.id]);968 971 assert_eq! (text_of (& forked, & forked.active_branch ().entries[1 ]), "1" ); assert_eq! (text_of (& forked, & forked.active_branch ().entries[1 ]), "1" );972 975 fn loading_copies_content_bytes_without_decoding_them () { fn loading_copies_content_bytes_without_decoding_them () {973 976 let mut scratch = Session::new(); let mut scratch = Session::new();974 - let root = * scratch.append (None , & message ("root" )).unwrap ();977 + let root = * scratch.append (& message ("root" )).unwrap ();975 978 let mut stored = frames (& scratch); let mut stored = frames (& scratch);976 979 if let Frame::Known { entry } = &mut stored[1 ] { if let Frame::Known { entry } = &mut stored[1 ] {977 980 entry.content = vec! [0xFF , 0x00 ]; entry.content = vec! [0xFF , 0x00 ];990 993 fn content_of_a_record_whose_kind_lies_is_a_kind_mismatch () { fn content_of_a_record_whose_kind_lies_is_a_kind_mismatch () {991 994 let mut scratch = Session::new(); let mut scratch = Session::new();992 - scratch.append (None , & message ("root" )).unwrap ();995 + scratch.append (& message ("root" )).unwrap ();993 996 let mut stored = frames (& scratch); let mut stored = frames (& scratch);994 997 let lie = EntryKind::ToolCall { let lie = EntryKind::ToolCall {995 998 call_id: ToolCallId::new(), call_id: ToolCallId::new(),1011 1014 fn compaction_boundary_survives_a_frame_round_trip () { fn compaction_boundary_survives_a_frame_round_trip () {1012 1015 let mut session = Session::new(); let mut session = Session::new();1013 - session.append (None , & message ("root" )).unwrap ();1016 + session.append (& message ("root" )).unwrap ();1014 1017 let boundary = session.append_compaction (serde_json::json! ({})).unwrap (); let boundary = session.append_compaction (serde_json::json! ({})).unwrap ();1015 1018 let restored = Session::from_frames(& frames (& session)).unwrap (); let restored = Session::from_frames(& frames (& session)).unwrap ();1016 1019 assert_eq! (restored.compaction_boundary, Some (boundary.id)); assert_eq! (restored.compaction_boundary, Some (boundary.id));smith-core/src/store.rs Modified +2 / −5 text lines
731 731 let (source_session, boundary_id, leaf_id) = { let (source_session, boundary_id, leaf_id) = {732 732 let mut opened = open_session (& source_path, cfg.clone ()).unwrap (); let mut opened = open_session (& source_path, cfg.clone ()).unwrap ();733 - let root = * opened.session.append (None , & message ("root" )).unwrap ();733 + let root = * opened.session.append (& message ("root" )).unwrap ();736 736 .append_entry (& root, opened.session.bytes (& root)) .append_entry (& root, opened.session.bytes (& root))744 744 .append_entry (& boundary, opened.session.bytes (& boundary)) .append_entry (& boundary, opened.session.bytes (& boundary))745 745 .unwrap (); .unwrap ();748 - .append (Some (boundary.id), & message ("after" ))746 + let tip = * opened.session.append (& message ("after" )).unwrap ();752 749 .append_entry (& tip, opened.session.bytes (& tip)) .append_entry (& tip, opened.session.bytes (& tip))smith-core/src/tools.rs Modified +1 / −2 text lines
396 396 pub (crate ) fn record (&mut self, content: & EntryContent) -> Result <Entry> { pub (crate ) fn record (&mut self, content: & EntryContent) -> Result <Entry> {397 - let parent = self.session.selected_id ();
398 397 let writer = self.writer.as_mut (); let writer = self.writer.as_mut ();399 398 self.session self.session400 - .append_with (parent , content, |entry, bytes| {399 + .append_with (content, |entry, bytes| {401 400 writer.map_or (Ok (()), |writer| writer.append_entry (entry, bytes)) writer.map_or (Ok (()), |writer| writer.append_entry (entry, bytes))403 402 .copied () .copied ()smith-core/src/trace.rs Modified +4 / −4 text lines
130 130 fn trace_reports_tool_activity_and_compaction_in_order () { fn trace_reports_tool_activity_and_compaction_in_order () {131 131 let mut session = Session::new(); let mut session = Session::new();135 - & EntryContent::Message(Message::with_text(Role :: User , " hello " ) ) , 133 + . append ( & EntryContent::Message(Message::with_text(137 137 .unwrap (); .unwrap ();138 138 let mut tools = ToolSession::new(std::env::temp_dir()); let mut tools = ToolSession::new(std::env::temp_dir());139 139 tools.session = session; tools.session = session;smith-core/tests/alloc_bounds.rs Modified +1 / −1 text lines
26 26 let mut session = Session::new(); let mut session = Session::new();27 27 for turn in 0 .. len { for turn in 0 .. len {28 28 let content = EntryContent::Message(Message::with_text(Role::User, format! ("turn {turn}" ))); let content = EntryContent::Message(Message::with_text(Role::User, format! ("turn {turn}" )));29 - session.append (session . selected_id ( ) , & content).unwrap ();29 + session.append (& content).unwrap ();