Documentation
¶
Overview ¶
Package shellstate owns validation and persistence of Sidecar shell display names. It is deliberately independent of both the TUI and CLI transports.
Index ¶
- Constants
- Variables
- func AddAtPath(path string, def Definition) error
- func CheckWritableVersion(version int) error
- func IsAlready(err error) bool
- func IsNotFound(err error) bool
- func IsUnknownVersion(err error) bool
- func IsValidation(err error) bool
- func NormalizeName(name string) (string, error)
- func RemoveAtPath(path string, id Identity) error
- func RemoveIfUnchangedAtPath(path string, id Identity, observedAt time.Time) error
- func ResetTombstoneRetention()
- func SetTombstoneRetention(window time.Duration)
- func TombstoneRetention() time.Duration
- type Definition
- type Error
- type ErrorKind
- type Identity
- type LookupResult
- type OriginInfo
- type RenameRequest
- type RenameResult
- type Tombstone
Constants ¶
const ( NameEnv = "SIDECAR_SHELL_NAME" SessionEnv = "SIDECAR_SHELL" )
NameEnv carries the current display name into the shell's environment, and SessionEnv the tmux session that owns it. An agent that can read its own shell name can tell a default ("Shell 9") from a real one, which is what makes the rename instruction actionable rather than a rule to remember.
const CurrentVersion = 2
CurrentVersion is the shells.json schema version this binary writes.
Version 2 is version 1 plus the `tombstones` array. The bump exists so the version field stops being decoration: a writer that finds a version it does not understand now refuses the write instead of marshalling its own narrower struct over the file and silently dropping every key it could not parse. That is the same failure mode the whole shell-record durability plan is about — one writer destroying information it does not understand — one level up, in the format itself.
A v1 file is upgraded in place on the first write: v1 is a subset of v2, so there is nothing to migrate beyond the number.
const MaxNameBytes = 50
const NamingInstruction = "This terminal is a Sidecar project shell. Learn its display name from $" + NameEnv +
" as a cue (it may be empty on older shells, or lag after a rename in this process), or run " +
"`sidecar shell name` for the authoritative name from Sidecar's manifest. \"Shell 3\" is the unset default; " +
"a previous task's name is equally stale. Keep the name aligned with your current task: at the start of work " +
"and whenever context changes materially, if the name does not describe what you are doing now, run " +
"`sidecar shell rename \"short context\"`. Update at meaningful boundaries, not every sub-step. Prefer the " +
"outcome (\"shell rename implementation\") over the model or a transient action (\"Codex running tests\"). " +
"These commands act only on the current Sidecar shell; never edit shells.json or rename tmux sessions directly."
NamingInstruction is the canonical guidance for keeping a shell's display name useful. It lives here, next to the rules it describes, so every delivery channel — AGENTS.md, a harness system-prompt append, help text — quotes one source instead of drifting copies.
The trigger is "name does not describe current work," not only the generated "Shell N" default — a leftover previous-task name is equally stale.
Variables ¶
var ErrShellChanged = errors.New("shell entry was replaced since it was observed")
ErrShellChanged reports that the entry on disk is not the one the caller looked at, so the removal was refused.
var ErrUnknownVersion = errors.New("written by a newer Sidecar; upgrade Sidecar to write it")
ErrUnknownVersion reports a manifest written by a newer Sidecar than this one. Callers that want to distinguish "the file is from the future" from an ordinary IO failure test for it with errors.Is.
var ObserveLiveCountWrite = func(path string, before, after int, identityRemoval bool) {}
ObserveLiveCountWrite is the shells.json writer-boundary hook. Every path that replaces the file calls it after computing the next live list and before the rename. before and after are len(shells), not tombstones. identityRemoval is true only for RemoveAtPath, RemoveIfUnchangedAtPath, and the workspace RemoveShell equivalent — the only functions allowed to shrink the live list.
Tests wrap this instead of grepping call sites.
Functions ¶
func AddAtPath ¶ added in v1.0.0
func AddAtPath(path string, def Definition) error
AddAtPath records one shell definition with the same locked, atomic read-before-write semantics used by rename. It is the state-free persistence boundary used by project and cross-project shell hosts.
func CheckWritableVersion ¶ added in v1.9.0
CheckWritableVersion reports whether a manifest carrying this version may be rewritten. Reads are always allowed — a newer file still parses into the fields this build knows, and refusing to read it would break `sidecar shell name` against a manifest a newer binary touched. Only writes are refused, because a write is what loses the fields.
func IsNotFound ¶ added in v1.6.0
func IsUnknownVersion ¶ added in v1.9.0
IsUnknownVersion reports whether err is a refusal to rewrite a newer-than-understood manifest.
func IsValidation ¶
func NormalizeName ¶
func RemoveAtPath ¶ added in v1.0.0
RemoveAtPath forgets one exact shell identity. A missing entry is already the requested state and therefore succeeds. The definition is moved to tombstones rather than dropped, so RestoreAtPath can put it back.
func RemoveIfUnchangedAtPath ¶ added in v1.0.0
RemoveIfUnchangedAtPath forgets one shell only when the entry on disk is still the incarnation the caller observed.
An auto-close decides a shell is dead, and then takes a moment to confirm it. A shell can be created under the same tmux name inside that moment — the global browser's create writes a fresh definition — and deleting the entry then would delete a live shell's identity (td-6a4100). The comparison runs inside the same exclusive lock the creating write takes, so the two orderings are the only two possible and both are correct: create-then-remove is refused, remove-then-create leaves the new entry alone.
A zero observedAt means the caller has no incarnation to check and accepts an unconditional removal.
func ResetTombstoneRetention ¶ added in v1.9.0
func ResetTombstoneRetention()
ResetTombstoneRetention drops the cached window so the next caller re-reads config.json.
func SetTombstoneRetention ¶ added in v1.9.0
SetTombstoneRetention overrides the resolved window for this process. It is for tests and for a caller that has already loaded config and wants to skip the lazy read; it does not write config.json.
func TombstoneRetention ¶ added in v1.9.0
TombstoneRetention is how long a forgotten shell record stays restorable before the next write drops it, from `shells.tombstoneRetention` in config.json. Zero means forgotten records are never expired.
The config file is read once per process and cached: every manifest write asks this question, and one of those writes is on the startup path, where AGENTS.md §Startup Latency counts file opens.
Types ¶
type Definition ¶
type Definition struct {
TmuxName string `json:"tmuxName"`
DisplayName string `json:"displayName"`
// Namespace is the resolved tmux socket path. Empty identifies a legacy
// entry that a headless rename must never claim as a side effect.
Namespace string `json:"namespace,omitempty"`
CreatedAt time.Time `json:"createdAt"`
AgentType string `json:"agentType,omitempty"`
SkipPerms bool `json:"skipPerms,omitempty"`
// WorkDir is the parent worktree path this shell was created in. Empty on
// pre-td-4819be manifests; callers infer or treat empty as current workDir.
WorkDir string `json:"workDir,omitempty"`
}
Definition contains all information needed to recreate a shell session.
func ListAtPath ¶ added in v1.0.0
func ListAtPath(path string) ([]Definition, error)
ListAtPath returns every shell definition recorded in the manifest at path.
It exists so callers that need to decide *which* shells to act on — the worktree delete, which must forget the shells rooted in the directory it is about to remove — can read the manifest through this package instead of unmarshalling shells.json themselves. A missing manifest is an empty project, not an error.
The result is a copy; mutating it does not affect the file. Removal is still one exact Identity at a time, so a caller that lists and then removes must tolerate the manifest changing in between (RemoveAtPath treats an entry that is already gone as success).
func RestoreAtPath ¶ added in v1.6.0
func RestoreAtPath(path string, id Identity) (Definition, error)
RestoreAtPath moves a forgotten definition from tombstones back onto shells. Display name, agent type, skip-perms, and workdir are left intact.
A live record is already in that state (KindAlready). An identity that is in neither list is KindNotFound.
type LookupResult ¶ added in v0.97.0
func LookupCurrent ¶ added in v0.97.0
func LookupCurrent(stateDir string, id Identity) (LookupResult, error)
LookupCurrent returns the display name of the current Sidecar project shell from the registered manifest. It does not create state or depend on process environment cues, so it works for shells created before SIDECAR_SHELL_NAME injection existed.
type OriginInfo ¶ added in v1.0.0
type OriginInfo struct {
TmuxName string `json:"tmuxName"`
Namespace string `json:"namespace"`
ProjectKey string `json:"projectKey"`
WorkDir string `json:"workDir"`
DisplayName string `json:"displayName"`
}
func LookupOrigin ¶ added in v1.0.0
func LookupOrigin(stateDir string, id Identity) (OriginInfo, error)
LookupOrigin returns origin identity and workspace root for the current shell.
type RenameRequest ¶
type RenameResult ¶
type RenameResult struct {
Shell string `json:"shell"`
OldName string `json:"oldName"`
Name string `json:"name"`
Changed bool `json:"changed"`
}
func RenameAtPath ¶
func RenameAtPath(path string, req RenameRequest) (RenameResult, error)
RenameAtPath performs a locked read-before-write mutation of one manifest.
func RenameCurrent ¶
func RenameCurrent(stateDir string, req RenameRequest) (RenameResult, error)
RenameCurrent searches registered project manifests without creating state.
type Tombstone ¶ added in v1.6.0
type Tombstone struct {
Definition
DeletedAt time.Time `json:"deletedAt"`
}
Tombstone is a forgotten shell definition kept so RestoreAtPath can move it back, for as long as TombstoneRetention says.
It arrived with schema version 2. An older binary — one that predates CurrentVersion being read at all — ignores the field on read and drops the key on its next write. That degradation is accepted and unavoidable in that direction; what version 2 buys is the other direction, where this build refuses to rewrite a manifest whose version it does not understand rather than dropping the fields it could not parse (see CheckWritableVersion).
func ExpireTombstones ¶ added in v1.9.0
ExpireTombstones drops forgotten records deleted longer ago than the retention window. A zero or negative window keeps everything, and a tombstone with no deletedAt (written before the field existed, or by hand) is kept rather than expired immediately — the failure mode of guessing is deleting a record nobody asked to delete.
Expiry is applied at every writer boundary rather than by a sweeper: a manifest only grows when something writes it, so sweeping on write is what bounds it, and no background job has to know where the file lives.
Letting a tombstone go also makes a still-running session of that name adoptable again (EnsureShells and mergeShellState both skip forgotten names). That is intended: after the window, Sidecar no longer remembers the forget, and a session that is genuinely running should come back as a row rather than stay invisible forever.
func ListTombstonesAtPath ¶ added in v1.6.0
ListTombstonesAtPath returns forgotten shell definitions still in the manifest. A missing file is an empty list, not an error.
Records past the retention window are filtered out even though the next write is what physically removes them, so a caller never offers a restore that the write behind it would refuse.