Documentation
¶
Overview ¶
Package lifecycleenv resolves the Sidecar-managed shell context a lifecycle report is written against.
It is the boundary where untrusted provider input meets Sidecar's own view of the world, and the rule at that boundary is one-directional: every field that identifies *where* a report applies — host, tmux server, pane, provider process — is derived here from the environment and from live tmux, and none of it can be supplied or overridden by the caller. Provider input chooses only what to say (a lane, an outcome, a reason code), never who to say it about. That is what stops a hook reporting for another pane, another server, or another user's run.
Failing open ¶
A hook runs in the agent's critical path. The plan's rule is absolute: a reporting failure is diagnostic and must never change the provider's own behavior. So Resolve returns a Context with Managed false — and no error — for every ordinary "not applicable" case: not inside a Sidecar shell, no tmux, no pane, a shell created before this contract existed. The caller exits 0 and says nothing. An error is reserved for a context that claims to be managed but does not check out, which is a real problem worth surfacing.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrContextMismatch = errors.New("lifecycleenv: claimed context does not match the live one")
ErrContextMismatch means the environment claimed a managed shell but the live pane, server, or host did not agree. It maps to agentlifecycle.ErrInvalidContext.
Functions ¶
func CheckReportedKind ¶
CheckReportedKind is the pure decision behind VerifyReportedKind, separated so the rule can be tested without a live process table.
The asymmetry is the whole design: only a positive identification refuses. An occupant that cannot be named is not treated as a mismatch, because the alternative refuses far more than the bug. Process identity is unavailable on platforms with no adapter, and a provider that spawns its hook into a fresh process group leaves the pane's foreground group holding nothing recognizable — in both cases every session binding would silently stop working, which is a worse outcome than the wrong-kind bind this guards, and it would fail in the direction hook surfaces are required not to fail.
So: named and different is a refusal, named and equal passes, unnamed passes. A caller wanting the stricter rule has to ask for evidence it can actually get, and today it cannot.
func LiveProviderGeneration ¶
LiveProviderGeneration reports the generation of the process that occupies the pane right now.
This is deliberately not derived the way Context.ProcessGeneration is. That one walks *up* from the reporting hook to find the provider that spawned it, which answers "who is talking". This one asks the process table which process is currently the pane's provider, which answers "who is running".
The two questions have the same answer while a provider is alive and reporting normally, and different answers in exactly the case the generation fence exists for: a hook whose provider has already exited or been replaced still knows its own ancestry, so a fence built only from the report's own view would let that late report win. Comparing against a value re-derived from live process state at write time is what makes the fence decisive — the same lesson the lifecycle store learned when a stored identity was compared with a copy of itself and every mismatch check became unreachable.
When the pane has no provider running, the pane's own root process is returned. That is a real generation and it will not match any provider's, so a late report loses rather than being accepted for want of something to compare against.
func OccupantKind ¶
OccupantKind names the provider positively identified in the pane's foreground process group, or "" when no provider can be named.
The generation fence answers "is the process that produced this report still the pane's provider". It cannot answer "which provider is it", because a generation is a pid and a start time — identifying, but anonymous. This answers the second question, from process ownership evidence (argv[0] of the foreground process group) rather than from anything the reporter said.
"shell" is folded into "" deliberately. An interactive shell in the foreground means no provider is running, which is an absence of evidence, not evidence that the claim is wrong.
func ReportingProviderGeneration ¶
ReportingProviderGeneration is the generation of the provider this process is actually running under, with no fallback.
Context.ProcessGeneration deliberately falls back to the pane's root process when it cannot walk from the hook to the pane, because for lifecycle lanes a weaker-but-truthful answer beats no answer. For a session binding that fallback is a hole, and a specific one: when a provider exits, a hook it left behind is reparented away from the pane, so its walk breaks and falls back to the pane root — while LiveProviderGeneration, finding no children, returns the pane root too. Two fallbacks agreeing is not evidence, and the late report the fence exists to reject would be accepted.
So this refuses instead. A reporter that cannot prove which provider it belongs to does not get to claim the pane's conversation.
func VerifyReportedKind ¶
VerifyReportedKind refuses a report whose claimed provider is not the provider occupying the pane.
It exists because a hook entry is a file, not a channel. grok reads ~/.claude/settings.json by design — its own shipped docs carry a "Claude Code Compatibility" section — so Sidecar's installed Claude entry fires inside grok sessions and arrives carrying `--kind claude`, describing the file it was installed in rather than the process that ran it.
Types ¶
type Context ¶
type Context struct {
// Managed reports whether this process is inside a Sidecar-managed shell
// with a complete environment contract. When false, every other field is
// zero and a hook must do nothing at all.
Managed bool
// Session is the tmux session name that owns the pane.
Session string
// Namespace is the tmux socket path identifying this host-local namespace.
Namespace string
// Bin is the absolute path of the Sidecar binary that created the shell.
Bin string
// Host, ServerIncarnation, PaneID, and ProcessGeneration are the verified
// identity components.
Host string
ServerIncarnation string
PaneID string
ProcessGeneration string
// PanePID is the pane's root process. It is exposed so a caller that needs
// to ask what occupies the pane *now* — as opposed to what spawned this
// hook — can do so without re-inspecting the pane.
PanePID int
// contains filtered or unexported fields
}
Context is a verified managed-shell context.
Identity is partially filled: Host, ServerIncarnation, PaneID, and ProcessGeneration are resolved here. Provider, RunID, and SessionFingerprint depend on what the caller knows about the provider and are filled by Context.IdentityFor.
func Resolve ¶
Resolve reads and verifies the managed-shell context.
stateDir is where the host salt lives. An empty stateDir still resolves everything except the salt, which degrades fingerprints to unsalted digests rather than failing — a weaker privacy property, but a working one, and the alternative is a hook that cannot report because a directory was missing.
func (Context) Fingerprint ¶
Fingerprint returns the host-salted digest of a provider session identifier.
The digest is all the lifecycle store ever retains. The exact reference, where session restore needs it, travels separately to the agentsession-owned shell binding — so a leaked lifecycle log cannot be used to address anyone's provider sessions.
func (Context) IdentityFor ¶
func (c Context) IdentityFor(provider, sessionID string) agentlifecycle.Identity
IdentityFor completes the identity for one provider and optional provider session identifier.
The run id is derived from the process generation and the session fingerprint together, so a run rotates when either changes: a provider restart in the same pane is a new run even if it resumes the same session, and a session switch inside one process is a new run even though the process did not change. Both are discontinuities that must not let earlier reports keep authority.