Documentation
¶
Overview ¶
Package sessionref resolves which session a pager invocation belongs to.
The CLI, the MCP server, and the hooks all resolve through this one type, so a message can never be attributed by one rule on one path and a different rule on another. An earlier design described only the MCP path, which would have left `pager send` — the primary sending interface — refused by pager's own policy.
The tiers, and why detection is trusted at all, are in docs/session-binding.md.
Index ¶
Constants ¶
const ( Unknown = "unknown" Claude = "claude" Codex = "codex" )
Host tool labels.
Variables ¶
This section is empty.
Functions ¶
func Alive ¶
Alive reports whether inst still names the process it was recorded for, and whether this platform can answer the question at all.
The start token is what makes this worth doing here rather than in a caller: `kill -0 <pid>` says only that some process holds the number, so a recycled pid reads as the original still running. Comparing the token is the check Instance's doc comment says the field exists for.
known is the difference between "no" and "cannot tell", and the two must not collapse. A caller acts on them differently — mail addressed to an inbox whose process is definitely gone is stranded, while mail whose process cannot be checked is merely unverified, and hiding the second on the strength of the first drops notifications nobody has abandoned. An instance that was never detected (Valid is false) is unknowable for the same reason: there is no identity to ask about.
func AliveAt ¶
AliveAt is Alive for a caller holding the two columns rather than an Instance.
The store keeps host_pid and host_start as separate values, so every caller reading a session out of the database has the pair and not the struct. This exists so they do not each rebuild it: deliver.HostProbe is written in terms of the pair precisely so that deliver never has to name a sessionref type.
func CodexThreadStates ¶
CodexThreadStates observes loaded threads on the existing managed daemon. It never starts a server or loads a thread. Missing entries are unknown; an error can accompany useful partial observations.
Types ¶
type Instance ¶
Instance identifies a host process.
Start is an opaque, platform-relative process-start token (darwin: start time in milliseconds; linux: starttime in clock ticks; windows: creation time in nanoseconds). It is only ever compared for equality between processes on the same machine, so its unit does not matter — only its stability does. Its job is to make a recycled pid a miss rather than a misattribution.
Zero means the token could not be read, which windows can answer for a process this user may not open. It is never a real token, so it must never be stored as one: two different processes that both refuse to be opened would compare equal on it, and the recycled pid this field exists to catch would read as the original still running.
func Detect ¶
Detect resolves the host tool label and the host process in a single walk up the ancestry from this process.
The hook, the CLI the agent runs through its Bash tool, and the MCP server are all descendants of the same host, so all three converge on the same Instance — which is what lets one of them record a session and the others look it up. See docs/session-binding.md.
PAGER_CLIENT pins the label when it is set; otherwise either known client is accepted. PAGER_ is not a CONTEXT_ prefix, so it survives Claude Code's environment scrub.
Setting it to something unrecognised means no host, not "any host". An operator who names a client and gets a different one silently attributed is worse served than one whose send is refused, and it gives a script a way to say it is deliberately outside any session.
One source serves the whole walk: a platform that can only answer about a parent by reading the entire process table reads it once, not once per level.
func (Instance) Valid ¶
Valid reports whether the instance names a real, non-root process with a usable identity.
A pid alone is not an identity — see Start. An instance carrying a pid but no token names a process nobody can distinguish from the next one to hold that number, so callers treat it as no host at all rather than as a weaker one.
type LookupFunc ¶
LookupFunc returns the session recorded against a host key, or "" when no live session holds it. In production this is backed by store.SessionByHost.
type Ref ¶
type Ref struct {
SessionID string
Source Source
// Client and Host carry the detected host process, set whenever detection
// succeeded — including when an earlier tier supplied the session id. A
// hook needs both: it learns its session from its own stdin and uses these
// to record that session against the host every other path looks up.
Client string
Host Instance
}
Ref is the outcome of a resolution.
func (Ref) Attributed ¶
Attributed reports whether a session was resolved. An unattributed send is refused unless the caller asserts --human.
type Resolver ¶
type Resolver struct {
// contains filtered or unexported fields
}
Resolver resolves session references. Use New; the zero value is not usable.
func New ¶
func New(lookup LookupFunc) *Resolver
New returns a Resolver wired to the real environment and host detection.
func (*Resolver) Resolve ¶
Resolve applies the five tiers in order, where flag is the --session value and is empty when it was not given.
Failing to resolve is not an error: it yields SourceUnattributed and lets the caller decide. Only a lookup that genuinely failed returns an error, so a broken database is never mistaken for "no session".
Detection runs first even when a flag or the environment will answer, because the hook path needs the host key regardless. It costs one walk up the process ancestry.