SessionContext
sectr.SessionContext is the single argument of a handler you write
yourself (adapters also pass it to your
context or deps factory). One SessionContext = one invocation: the
rehydrated transcript plus the polymorphic reason that started it.
The reason — input and decision
Section titled “The reason — input and decision”The reason is polymorphic; these sugars raise TypeError on the wrong kind
so misuse fails loudly:
ctx.input— the user’s message text (str). Only on turns whose reason isuser_message.ctx.decision— the approval decision,{"approved": bool, "approver"?: str}. Only on turns whose reason isapproval_decision.
ctx.messages — the conversation fold
Section titled “ctx.messages — the conversation fold”The canonical transcript view: OpenAI-style message dicts covering the full multi-turn conversation. Framework adapters project this into their native state; hand-written loops feed it straight to a provider.
- The current user message is already in
ctx.messages: the transcript includes this turn’s start. Don’t append it again. - Assistant messages carry their text in
contentand any reasoning inreasoning_content. Keys starting with_sectr_are for framework adapters: drop them before sending messages to a provider yourself. - Tool calls are OpenAI
tool_callson the assistant message that made them, and tool results are{role: "tool"}messages. - On an approval resume, the conversation ends with the unanswered tool call, as the model left it: append its result.
- After a compaction, the conversation starts from the snapshot (see compaction).
How the fold handles the edge cases
Section titled “How the fold handles the edge cases”- User turns come from
INVOCATION_STARTEDenvelopes with auser_messagereason, and assistant messages are rebuilt fromMESSAGE_START/DELTA/END. AMESSAGE_END’sframework_statelands under_sectr_framework_state. - A tool call with no message of its own (a tool-only model response) gets a new, empty assistant message, so it never lands on an earlier turn’s reply. Parallel calls share one message.
- An interrupted tool call — one with no result that the conversation
moved past (the turn crashed or the tool raised, then the user wrote
again) — gets a synthesized tool message, “This tool call did not
complete; no result was recorded.”, so the model never sees a call
without an answer. Derived from the journal, never persisted. Such a call
can no longer be approved:
ctx.pending_tool_callstops reporting it. - Compaction snapshots (
TRANSCRIPT_COMPACTED) positionally reset the fold. - Crash stitching: a message interrupted by a session error is continued with a synthesized “please continue” user message, derived from the journal — never persisted.
Approval resume helpers
Section titled “Approval resume helpers”ctx.pending_tool_call— the unanswered tool call on an approval-resume turn:{name, arguments, tool_call_id, ...}(orNone).ctx.execute(name, call_id=..., **arguments)— run a registered tool. On a gated tool during auser_messageturn it raisesApprovalRequired(the platform journals the approval request and suspends the turn). On an approval-resume turn the decision is already in hand, so executing the pending tool proceeds.ctx.request_approval(name, arguments, reason=...)— explicit sugar that always raisesApprovalRequired: for gating an action no single tool expresses. The optionalreasonis shown with the pending approval.
Transcript shape
Section titled “Transcript shape”ctx.transcript is the raw list of sectr.events.Envelopes (journal rows)
— ctx.messages is a view over it. ctx.session_id / ctx.invocation_id
identify the session and this invocation.