Defines the YAML frontmatter schema (Frontmatter) embedded between ---\n delimiters at
the top of agent-doc documents.
parse(content) splits a document string into (Frontmatter, body_str). Documents that do
not start with ---\n return a default Frontmatter and the full content as body.
Unterminated frontmatter (no closing ---) returns Err.
write(fm, body) serialises Frontmatter to YAML and prepends it to body, producing a
complete document string.
set_session_id / set_resume_id are convenience wrappers: parse → mutate one field →
write. They create frontmatter when none exists.
ensure_session is idempotent: if session is already set the document is returned
unchanged; otherwise a UUID v4 is generated and written.
set_format_and_write atomically sets agent_doc_format + agent_doc_write and clears
the deprecated agent_doc_mode field.
merge_fields(content, yaml_fields) applies additive key/value patches to the frontmatter;
unknown keys are logged to stderr and discarded; the document body is preserved unchanged.
set_tmux_session is retained for backward compatibility; tmux_session in frontmatter is
deprecated — runtime session is now determined by --window or current_tmux_session().
AgentDocFormat (append | template) controls document structure; serialised as inline /
template in YAML; inline and append are accepted as aliases on deserialise.
AgentDocWrite (merge | crdt) controls the write/merge strategy.
ResolvedMode is the canonical (format, write) pair after deprecation migration.
Frontmatter::resolve_mode() applies a three-level priority: explicit agent_doc_format /
agent_doc_write fields > deprecated agent_doc_mode string > defaults
(format=template, write=crdt).
Deprecated agent_doc_mode values: "append" → format=Append; "template" or "stream"
→ format=Template; all keep write=Crdt.
parse never panics; it returns Err only for an unterminated frontmatter block or a YAML
deserialisation failure. All missing optional fields default to None.
write(parse(doc).0, parse(doc).1) round-trips the document: re-parsing the result yields
semantically equivalent Frontmatter and an identical body string.
ensure_session is idempotent: calling it twice on the same document returns the same
session ID and does not modify the document on the second call.
merge_fields is additive: it never removes existing fields unless an explicit null
value is supplied for that field in yaml_fields.
set_format_and_write is the only function that actively clears a field (mode = None);
all other helpers are strictly additive.
resolve_mode is pure and total: it always returns a valid ResolvedMode regardless of
which combination of deprecated and current fields are populated.
Serialised YAML always uses the canonical field names (agent_doc_session,
agent_doc_mode, agent_doc_format, agent_doc_write); legacy aliases are read-only.
Ensure the document has a session ID. If no frontmatter exists, creates one
with a new UUID v4. If frontmatter exists but session is None/null, generates
a UUID and sets it. If session already exists, returns as-is.
Returns (updated_content, session_id).
Parse YAML frontmatter from a document. Returns (frontmatter, body).
If no frontmatter block is present, returns defaults and the full content as body.