repositories / smith

smith

There are many coding harnesses - but this one is fast

owned by bugabinga

docs(system): session arena plan 82POBOKB and spec text for payloads as bytes

e59fc110a4369fb149601561d71a37349fbafa22

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (2)

+89 / −0 text lines

Diff layout

.system/plans/SMH-PLAN-82POBOKB-session-arena/index.md

Added
new file mode 100644

+83 / −0 text lines

1---
2id: SMH-PLAN-82POBOKB
3type: plan
4title: "Session Arena: Payloads as Bytes"
5spec: SMH-SPEC-SPEC0001
6status: draft
7depends_on: [SMH-PLAN-CORE0002]
8---
9
10# Session Arena: Payloads as Bytes
12## Entry
14- Memory law in `.system/RULES.md` (index arena, bytes at rest, lifetime groups).
15- `smith-alloc` bounds pin the current cost: `fork_at(1000)` = 2002 allocations, mock turn = 220.
16- SMH-RESEARCH-4KD6V6IH names `Entry.content: serde_json::Value` and per-entry `String`/`Vec` as the floor.
18## Problem
20`Entry` owns a heap tree (`Message` → `Vec<ContentBlock>` → `String` | `serde_json::Value`).
21Every traversal that must own its result (fork, load, request build) pays two or more allocations per entry, and every entry at rest is scattered across the heap.
22Frames already encode entries as canonical CBOR; the tree is re-created on load only to be re-encoded on write.
24## Decisions
26- Entry content lives as canonical CBOR bytes, at rest and in memory; it is decoded on demand into the consuming scope (turn, frame) and never held decoded in the session.
27- The session owns one contiguous payload arena (`Vec<u8>`); branch records are fixed-size and point into it by `u32` span.
28 Ancestry, trace, fork, and compaction-boundary logic never decode content.
29- The record carries what traversal needs: `EntryKind` with pairing identifiers (`MessageId` + `Role`, `ToolCallId`, `ok`, compaction flag).
30 Anything else is a decode.
31- Wire shape follows: an entry frame is `{id, parent, timestamp_ms, kind, content: bytes}` where `content` is the canonical CBOR of `EntryContent`.
32 `VERSION_ENTRY` becomes 2; no migration (prototype phase).
33- Canonicalization happens once, at `EntryContent` → bytes; decoded content is never canonicalized again.
34- Text pool (spans into one `String` per session) is a later step; it only pays once request build serializes straight from bytes.
36## Shapes
38```text
39Session { id, branches: Vec<Branch>, payloads: Vec<u8>, active_branch_idx, compaction_boundary }
40Branch { branch_id, entries: Vec<Entry>, selected_idx }
41Entry { id: EntryId, parent: Option<EntryId>, timestamp_ms: i64, kind: EntryKind, content: Span }
42Span { offset: u32, len: u32 }
43EntryKind { Message { id: MessageId, role: Role } | ToolCall { call_id } | ToolResult { call_id, ok } | Meta { compaction: bool } }
44EntryFrame{ id, parent, timestamp_ms, kind, content: Vec<u8> } wire shape, owned, serde
45EntryContent unchanged, decoded view
46```
48Session API:
50- `Session::append(&mut self, parent, content: &EntryContent) -> &Entry` encodes once into the arena.
51- `Session::content(&self, entry: &Entry) -> Result<EntryContent>` decodes on demand.
52- `Session::bytes(&self, entry: &Entry) -> &[u8]` for writers and hashing.
53- `Session::fork_at` copies the retained records and one arena slice.
54- `Session::from_frames` copies each frame's `content` bytes into the arena; no decode.
55- `Entry::message(..)`/`Entry::new(..)` disappear; construction goes through the session or `EntryFrame`.
57## Order
591. Add `EntryKind`, `Span`, `EntryFrame`; frame v2 encodes `EntryFrame`; frame tests on the new shape.
602. Move the arena into `Session`; `append`, `content`, `bytes`, `fork_at`, `from_frames` over spans; session tests.
613. Writer and tools record through `Session::append` and write `EntryFrame` from arena bytes.
624. Agent request build and compaction decode per turn; trace and RPC read `EntryKind`.
635. Tighten bounds: `fork_at(1000)` ≤ 3, mock turn measured and pinned, new bound for `from_frames(1000)`.
646. Deslop and compress passes.
66## Interfaces
68- `smith-core::session`: `Session::{append, content, bytes}`, `EntryKind`, `Span`, `EntryFrame`.
69- `smith-core::frame`: `Frame::Known { entry: EntryFrame }`, `VERSION_ENTRY = 2`.
70- `smith-core::trace`: reads `EntryKind` only.
72## Risks
74- Every content read is a decode; hot readers must decode once per turn, not per access.
75 Request build is the only per-turn reader today.
76- `u32` spans cap the arena at 4 GiB per session; the p99 session is ≈ 10 MiB, so the cap is a hard ceiling recorded, not reached.
77- Frame decode still allocates the `EntryFrame.content` `Vec` per frame before the arena copy; the reader can hand a slice instead in a later step.
78- Sibling branches duplicate their shared prefix records (not bytes: spans point at the same arena); acceptable, unchanged from today.
80## Exit
82- `cargo x check`, `lint`, `arch`, `audit` green; bounds tightened, none loosened.
83- Spec text for the in-memory model and frame v2 lives in SMH-SPEC-SPEC0001 Sessions.

.system/specs/SMH-SPEC-SPEC0001-smith-specification/index.md

Modified

+6 / −0 text lines

185185Resuming, cloning, forking, and branch navigation preserve the selected history and compaction boundary.
186186Externally managed entries can initialize an in-memory session without changing their order or IDs.
188In memory a session is an index arena: each branch is a contiguous vector of fixed-size entry records over one contiguous payload arena per session.
189Entry content is canonical CBOR bytes at rest and in memory, decoded on demand into the consuming scope and never held decoded in the session.
190A record carries the entry identity, parent, timestamp, kind, and the pairing identifiers of its kind, so history traversal, tracing, forking, and compaction-boundary logic never decode content.
191An entry frame carries its content as an opaque canonical CBOR byte string; loading copies bytes, it does not parse them.
192Forking a session and loading its frames allocate a constant number of times beyond the frame reader, independent of entry count.
188194Session dumps use JSONL for inspection and interchange.
189195JSONL is not the authoritative persistence format.