Documentation
¶
Overview ¶
Package sessionname gives every session abcd's hooks run in a persona name, unique across the machine (itd-2610111707085098, spc-2610152057143798).
A session that starts unnamed takes the next name of its repository's persona sequence that no running named session holds: Alice, Bob, Carol and on through the list, then Alice Two, Bob Two and so on. The names handed out are kept in one machine-scoped register, the sessions folder of abcd's home, beside an entry per running session naming the host process that ran its session-start hook. An entry whose process is gone, or whose process id now belongs to a process started at another time, has ended; when every entry has ended the handed-out set is emptied and naming starts again at Alice.
The package decides and records; it never prints. Every outcome carries its diagnostics as notes for the front door to put on stderr, and naming never fails a hook: anything that goes wrong leaves the session unnamed with one note saying why (C11). The person's own name always wins: a session that arrives with a title is never given one (C4).
Everything the register reads beyond its arguments, the home folder, the process table, the process the host walk starts from and the lock's wait, is a field of Machine, so a test drives a whole machine of sessions from a fixture table and a temporary home.
Index ¶
Constants ¶
const PersonasRel = ".abcd/development/personas.json"
PersonasRel is the repository's persona list, repo-relative: the registry press-release composers attribute quotes from, and the source of the names sessions take.
Variables ¶
This section is empty.
Functions ¶
func Acts ¶
Acts reports whether Start reads the repository's persona list for req, which is the one thing it needs req.Root for, so a front door resolves the root (a git call) only then. It answers no for every payload naming never acts on (acts), and for a startup with a title, which only records that the title is the person's (keep) and needs no repository (iss-2610152324354563).
func IsShell ¶ added in v0.17.0
IsShell reports whether a process of this name is one of the shells the host walk passes over (shells), a login shell's leading dash dropped. abcd, which the walk also passes over, is not a shell.
func NameOf ¶ added in v0.16.2
NameOf is the persona name the register holds for sessionID, "" when it holds none.
func SameHost ¶ added in v0.16.2
SameHost reports whether two readings name one process, each read a second or so apart from the other: sameHost, for a reader outside this package.
func SessionAbove ¶ added in v0.16.2
SessionAbove is the session whose host process m.From descends from, and its name; "" and "" when no ancestor is a session's host. It walks every ancestor rather than stopping where FindHost does: a launching process can sit under a make or a terminal multiplexer, which is no host of a session and does not hide one above it.
Types ¶
type Ancestry ¶ added in v0.16.2
type Ancestry struct {
// Hosts is every process the walk passed, nearest first.
Hosts []Host
// Finished says the walk reached the machine's first process.
Finished bool
// Missing is the process the table did not hold where the walk stopped, 0 when it
// did not stop at one. Neither Finished nor Missing says its parent links do not end.
Missing int
}
Ancestry is a walk upward through the table and how it ended.
type Host ¶
type Host struct {
// PID is the host process's id.
PID int
// StartUnix is when it started, in Unix seconds: the table's read time
// minus the process's age. With the pid it tells the process apart from a
// later one the kernel gave the same id.
StartUnix int64
// Slack is how far StartUnix may be early: the length of the table read
// it came from, capped at maxReadSlack. The read's moment is taken before
// the read and each age somewhere inside it, so a start time is early by
// up to the read's length and never late. Zero is a fast read, and an
// entry written before the length was recorded.
Slack time.Duration
}
Host is the process a session's hooks run under: the host program itself, not the shell a hook command starts in. A register entry is keyed on it, so its liveness is the session's (decision 4).
func FindHost ¶ added in v0.16.0
FindHost walks up from the process from (the hook process's parent) to the first process that is not a shell and not abcd, and returns it with its start time. A walk that leaves the table, loops, or reaches the machine's first process (pid 1, launchd or init) names no host: keyed on pid 1, an entry would never end.
type Machine ¶
type Machine struct {
// Home is the person's home directory, absolute; abcd's home is under it.
// Empty names no home, and nothing is named.
Home string
// From is the process the host walk starts from: the hook process's
// parent.
From int
// ReadTable reads the process table once.
ReadTable func() (Table, error)
// ReadTableContext is ReadTable bound to ctx, which the prompt hook reads
// through so that its read ends at maxReadTook (Prompt). Nil reads
// through ReadTable, unbounded but for the reader's own limit.
ReadTableContext func(ctx context.Context) (Table, error)
// LockTimeout bounds the wait for the register's lock.
LockTimeout time.Duration
}
Machine is everything the register reads beyond its arguments.
type Outcome ¶
type Outcome struct {
Name string
Notes []string
// Notice is the one line the prompt that sets a name in place of the
// title the session carried says to the session and the person: which
// name it now has, and why. Empty on every other outcome.
Notice string
}
Outcome is what a hook does with naming: the name (from Start, the name reserved; from Prompt, the title to set on this prompt; "" for none) and the notes for stderr, raw (the front door masks them as it prints).
func Prompt ¶ added in v0.17.0
Prompt is the prompt hook's naming: the title to set on this prompt (Name, "" for none) and, when that title replaces the one the session carries, the notice that says so. req.Title is the session's current title from the prompt's payload, and req.Root the repository whose persona list names a session that has to take a new name here.
The name session start reserved is owed once, at the first prompt (C1, C3, decision 9), and only to a session that has no title yet: a title equal to the name is already set, and any other title is the person's own, which always wins (C4). Either way the entry is marked sent. The one title replaced is a name abcd gave that a running session now holds, carried by a resumed session abcd named or by a fork (decision 10), and the prompt that replaces it carries the notice. The host discards a hook's whole output when the hook overruns, so a rename stays owed until a prompt shows the new name: a prompt that still carries the replaced title is sent the new name and the notice once more, and after that the title is the person's to keep, so a person who takes the old title back is not fought. A name set is recorded in Given, so a later resume or clear knows the title is abcd's; the person's own name is recorded as "", so a later resume never reads it as inherited.
Every prompt also re-asserts the session's hold on its name, so a hold that session start could not make (its lock or its process-table read not done in time) is made at a later prompt instead of never, as a client re-creates its ephemeral state once its session is lost. Three cases call for it, and each is settled under the register's lock (reclaim, claim):
- the session has no entry while it carries the name abcd gave it (Given): the entry is made again for the process now running the session;
- the session has no entry and no record of its own, and a live entry accounts for its title (accountsFor): on this same host process that entry is the session before a clear whose hold was not made, and its record and its name follow the clear; on any other host the title is the person's own, and it is recorded as theirs;
- the session carries the name of its entry, and an earlier live entry holds the same name: two sessions hold one name.
In either case, when another running session holds the name, this session is the later claim and takes the next free name, which this prompt sets in place of the title, with the notice. The register's live entries are kept in the order their names were claimed (reserve, claim and rekey append), and both sessions read that one order under the lock, so exactly one of them is the later claim: the other keeps its name, is told nothing, and is never displaced while its host runs (prune, by process id and start time).
The register is first read without the lock: the file is only ever replaced by a rename, so an unlocked read sees one whole version of it, and on every prompt but the rare one it is all that is needed. A session whose entry is there, its own and first of its name, with nothing owed, costs that one read: no lock and no process table. A title owed takes the lock; only a missing or shared hold reads the process table, bounded at maxReadTook, and then takes the lock. A machine with no register touches nothing, nor does one whose sessions folder is a symlink: session start refuses that with its note, and the prompt hook does not repeat it on every prompt.
Nothing here fails the hook. A register that cannot be read or written, a lock not free within LockTimeout, or a process table not read, or read more slowly than maxReadTook, changes nothing: the title is kept, no name is allocated, no notice is given, and one note says why. The next prompt tries again.
func Start ¶
Start is the session-start hook's naming.
Start reserves; it never sets a title. The host ignores a prompt-hook title equal to the one a session already has, so a title set here would never reach the session list (decision 9). The returned Name is the name reserved, which the prompt hook sets at the session's first prompt (Prompt).
A sub-agent is never named, nor a payload with no session id. A new session (startup, or a fork) with no title reserves the next free name. A startup with a title is the person's own name: it is not named, and the register records that abcd gave it nothing (keep). A resumed or cleared session whose title is the name abcd gave it (Given) keeps it, and the register holds the name for the process now running it, so no other session is given it (C5). A fork, which the host starts as a new session id carrying its parent's title with source fork (seen live), inherits the name abcd gave the parent, and is held the same way. Either way, when a running session on another host holds that name, the session reserves the next free name instead, which its first prompt sets in place of the title (decision 10). Any other title is the person's, persona name or not, and is neither held nor replaced (C4). A compaction of a titled session changes nothing: the host keeps the title through it.
A resume, clear or compaction with no title is a session whose first prompt has not set its name yet (rejoin): its entry is re-keyed to the process now running it, or, when it has none, it reserves the next free name as a new session does.
type Request ¶
type Request struct {
// Root is the repository root the session works in, whose persona list
// names it; empty takes the bundled list.
Root string
// SessionID is the host's id for the session. A session without one is
// never named: the prompt hook finds its entry by it.
SessionID string
// Source is why the session started: startup, resume, clear, compact or
// fork.
Source string
// Title is the session's current custom title: a name the person gave it,
// or one a hook set earlier. Empty when it has none.
Title string
// AgentID is set on a sub-agent's payload, which is never named (C12).
AgentID string
}
Request is what a session-start hook's payload says about the session.
type Table ¶
type Table struct {
Procs []machineload.Proc
ReadAt time.Time
// Took is how long the read took: how far, at most, a start time computed
// from the table is early.
Took time.Duration
}
Table is one reading of the process table and the moment the read began, which is what a process's age is measured from.
func (Table) Alive ¶
Alive reports whether h is running: its process id is in the table, and the process holding it started when h says, within startTolerance widened by each reading's slack (C9).