Documentation
¶
Overview ¶
Package devsession provides persistent development-session ownership primitives. It does not supervise children or infer service readiness from a lease or PID.
Index ¶
- Constants
- Variables
- func AcquirePrivateLock(ctx context.Context, directory, name string) (*flock.Flock, error)
- func CaptureProcess(pid int, owner ProcessOwner) (devcontract.ProcessIdentity, error)
- func EnsurePrivateDirectory(path string) (string, error)
- func GenerateToken() ([]byte, error)
- func ProbeViteHMR(ctx context.Context, address string) (string, error)
- func ReadPrivateJSON(path string, value any) error
- func ReadUnixSocketDirectory(runtimeRoot, workspaceID, generation, source string) (string, error)
- func TerminateOwnedProcess(record devcontract.ProcessIdentity, expected ProcessOwner) error
- func ValidatePrivateDirectory(path string) error
- func ValidatePrivateFile(path string) error
- func ValidateUnixSocketDirectoriesRemoved(runtimeRoot string) error
- func VerifyOwnedProcess(record devcontract.ProcessIdentity, expected ProcessOwner) error
- func WritePrivateFile(path string, content []byte) error
- func WritePrivateJSON(path string, value any) error
- type DiscoveryStore
- func (store *DiscoveryStore) Publish(ctx context.Context, descriptor devcontract.InstanceDescriptor, token []byte) (devcontract.Discovery, error)
- func (store *DiscoveryStore) Read(ctx context.Context, workspaceID, generation string) (devcontract.Discovery, error)
- func (store *DiscoveryStore) Remove(ctx context.Context, workspaceID, generation string) error
- type Lease
- type LeaseRequest
- type ProcessBridgeClient
- type ProcessBridgeConfig
- type ProcessExitError
- type ProcessLogPage
- type ProcessOwner
- type ProcessRequest
- type ProcessResponse
- type ProcessStart
- type ProcessStatus
- type Registry
- func (r *Registry) BindFailed(ctx context.Context, key ServiceKey, generation string, port int) error
- func (r *Registry) ConfirmBound(ctx context.Context, key ServiceKey, generation string, actualPort int, ...) error
- func (r *Registry) List(ctx context.Context, workspaceID string) ([]Lease, error)
- func (r *Registry) Reallocate(ctx context.Context, key ServiceKey, generation string) (Lease, error)
- func (r *Registry) Release(ctx context.Context, key ServiceKey, generation string) error
- func (r *Registry) Reserve(ctx context.Context, request LeaseRequest) (Lease, error)
- type RegistryOptions
- type RemoteProcess
- func (p *RemoteProcess) ID() string
- func (p *RemoteProcess) Identity() devcontract.ProcessIdentity
- func (p *RemoteProcess) Logs(ctx context.Context, cursor int64, limit int) (ProcessLogPage, error)
- func (p *RemoteProcess) Status(ctx context.Context) (ProcessStatus, error)
- func (p *RemoteProcess) Stop(ctx context.Context) error
- func (p *RemoteProcess) Wait(ctx context.Context) error
- type ServiceKey
- type UnixSocketDirectory
Constants ¶
const ( LeaseReserved = "reserved" LeaseBound = "bound" LeaseBindFailed = "bind-failed" LeaseReleased = "released" )
Variables ¶
Functions ¶
func AcquirePrivateLock ¶
AcquirePrivateLock acquires a stable-inode private flock in an already ensured dedicated runtime directory. Name must be one portable leaf filename. Never initialize a lock by atomically replacing its path: that can bypass a held inode. This creates without following a leaf symlink/reparse point, restricts the initial handle, and checks that flock acquired that exact inode.
func CaptureProcess ¶
func CaptureProcess(pid int, owner ProcessOwner) (devcontract.ProcessIdentity, error)
CaptureProcess records a kernel-sourced start identity and executable for a process the caller owns. Call this immediately after starting the child, and persist the result privately. This is an ownership attestation, not adoption or authorization of arbitrary processes discovered by PID.
func EnsurePrivateDirectory ¶
EnsurePrivateDirectory creates/canonicalizes a dedicated private runtime directory and applies the platform's owner-only permissions. Callers choose the development root explicitly; this does not infer workspace identity.
func GenerateToken ¶
GenerateToken returns 256 bits of randomness encoded for a bearer token file. Callers must never serialize its value into discovery, inspect or log output.
func ProbeViteHMR ¶
ProbeViteHMR discovers the websocket URL from the ACTUAL served Vite client, including configured hostname/clientPort/HMR path. It requires a real Vite protocol connected frame, not HTTP text, an open port or a guessed WS URL. Returned URL and all errors omit the ephemeral websocket token/source text.
func ReadPrivateJSON ¶
ReadPrivateJSON decodes one complete private JSON document. Workspace and generation checks remain the consuming record's responsibility. Unknown fields and trailing documents are rejected rather than silently discarded.
func ReadUnixSocketDirectory ¶
ReadUnixSocketDirectory authorizes only the exact selected workspace and live supervisor generation. The caller must derive runtimeRoot from its profile, not from an inherited socket-directory environment variable.
func TerminateOwnedProcess ¶
func TerminateOwnedProcess(record devcontract.ProcessIdentity, expected ProcessOwner) error
TerminateOwnedProcess terminates only the exact recorded process, through a process-stable OS handle. It never signals process groups or port owners. Linux uses pidfds; Windows uses a verified process handle. Darwin and other unsupported platforms fail closed rather than check-then-signal a numeric PID. Already exited processes are successful, but malformed/wrong-owner records always fail. No graceful-shutdown or escalation policy is imposed here.
func ValidatePrivateDirectory ¶
ValidatePrivateDirectory checks an existing directory without adopting it or repairing its permissions. Redirected ancestors and non-directory leaves are refused using the same platform ownership policy as private runtime files.
func ValidatePrivateFile ¶
ValidatePrivateFile rejects symlinks, nonregular files, foreign owners and permissions/ACLs that allow other principals. It does not repair/adopt files. This is a point-in-time pathname check, not a retained file handle: appenders must bind validation to their opened handle (SameFile/no-follow checks) and keep their own serialization. Initialize a new log with WritePrivateFile.
func ValidateUnixSocketDirectoriesRemoved ¶
ValidateUnixSocketDirectoriesRemoved is a read-only removal preflight. Even a malformed or interrupted record must survive: it may be the last provenance for an external namespace whose descendants have not been disposed.
func VerifyOwnedProcess ¶
func VerifyOwnedProcess(record devcontract.ProcessIdentity, expected ProcessOwner) error
VerifyOwnedProcess compares all recorded provenance and the live kernel start identity/executable. Permission errors are not evidence of a stale process. Verification alone is not an atomic authorization for a subsequent PID kill.
func WritePrivateFile ¶
WritePrivateFile atomically publishes exact raw bytes, without JSON encoding or stdout output. Use for supervisor/MCP secret files and initial empty logs; appending remains the owner's retained-handle responsibility. Ensure the dedicated private parent directory first.
func WritePrivateJSON ¶
WritePrivateJSON atomically publishes json.Encoder bytes, including its final newline, with platform-private ownership/permissions. Ensure the dedicated parent directory first. Existing public/foreign/nonregular files are rejected. This writes one file; callers own generation checks and multi-file locking.
Types ¶
type DiscoveryStore ¶
type DiscoveryStore struct {
// contains filtered or unexported fields
}
DiscoveryStore owns one dedicated profile runtime directory. Its shared lock serializes publication and generation-checked cleanup across host processes.
func OpenDiscoveryStore ¶
func OpenDiscoveryStore(runtimeDirectory string) (*DiscoveryStore, error)
func (*DiscoveryStore) Publish ¶
func (store *DiscoveryStore) Publish(ctx context.Context, descriptor devcontract.InstanceDescriptor, token []byte) (devcontract.Discovery, error)
Publish writes the private token before atomically advertising its reference. Refreshes within a generation preserve both process identity and token. A new generation may replace only a provably stale host, never an inaccessible one. A lease, PID or endpoint alone is not evidence of application readiness.
func (*DiscoveryStore) Read ¶
func (store *DiscoveryStore) Read(ctx context.Context, workspaceID, generation string) (devcontract.Discovery, error)
Read requires an explicit workspace and never falls back to production. An empty generation selects its current discovery; a nonempty generation must match exactly. Callers must verify process and endpoint identity before use.
func (*DiscoveryStore) Remove ¶
func (store *DiscoveryStore) Remove(ctx context.Context, workspaceID, generation string) error
Remove is idempotent only for absent discovery or its exact generation. It cannot remove another host's descriptor or an arbitrary token-file reference. Superseded/crash-orphan token files are intentionally not swept by a new host.
type Lease ¶
type Lease struct {
Key ServiceKey `json:"key"`
Port int `json:"port"`
Generation string `json:"generation"`
Owner devcontract.ProcessIdentity `json:"owner"`
Process *devcontract.ProcessIdentity `json:"process,omitempty"`
State string `json:"state"`
Strict bool `json:"strict"`
Attempt int `json:"attempt"`
MaxAttempts int `json:"maxAttempts"`
FailedPorts []int `json:"failedPorts,omitempty"`
}
Lease is a cooperating reservation, NOT an OS socket reservation or readiness claim. Bound means the caller acknowledged this exact port for Process after independently verifying the endpoint; it does not mean RPC/controllers ready.
type LeaseRequest ¶
type LeaseRequest struct {
Key ServiceKey
// Owner is the supervisor's captured identity, not the future listener PID.
Owner devcontract.ProcessIdentity
Strict bool
// A nonzero explicit override is always strict, even outside the managed range.
Override int
}
type ProcessBridgeClient ¶
type ProcessBridgeClient struct {
// contains filtered or unexported fields
}
func NewProcessBridgeClient ¶
func NewProcessBridgeClient(config ProcessBridgeConfig) (*ProcessBridgeClient, error)
func (*ProcessBridgeClient) Close ¶
func (c *ProcessBridgeClient) Close()
func (*ProcessBridgeClient) Start ¶
func (c *ProcessBridgeClient) Start(ctx context.Context, start ProcessStart) (*RemoteProcess, error)
type ProcessBridgeConfig ¶
type ProcessBridgeConfig struct {
Endpoint string
TokenFile string
WorkspaceFile string
WorkspaceID string
Generation string
Host devcontract.ProcessIdentity
}
type ProcessExitError ¶
type ProcessExitError struct{ Code int }
func (*ProcessExitError) Error ¶
func (e *ProcessExitError) Error() string
func (*ProcessExitError) ExitCode ¶
func (e *ProcessExitError) ExitCode() int
type ProcessLogPage ¶
type ProcessOwner ¶
ProcessOwner is provenance recorded by the supervisor when it starts a child. Workspace and generation are not inferred from an executable name or a PID. Source is the resolved source/build identity, not a display name.
func (ProcessOwner) Validate ¶ added in v0.7.3
func (o ProcessOwner) Validate() error
Validate checks provenance before a caller starts or records an owned child.
type ProcessRequest ¶
type ProcessRequest struct {
RequestID string `json:"requestID"`
WorkspaceID string `json:"workspaceID"`
Generation string `json:"generation"`
Host devcontract.ProcessIdentity `json:"host"`
Operation string `json:"operation"`
Start *ProcessStart `json:"start,omitempty"`
ProcessID string `json:"processID,omitempty"`
TimeoutMS int `json:"timeoutMS,omitempty"`
Cursor int64 `json:"cursor,omitempty"`
Limit int `json:"limit,omitempty"`
}
type ProcessResponse ¶
type ProcessResponse struct {
RequestID string `json:"requestID"`
WorkspaceID string `json:"workspaceID"`
Generation string `json:"generation"`
Process *ProcessStatus `json:"process,omitempty"`
Logs *ProcessLogPage `json:"logs,omitempty"`
Error *devcontract.Error `json:"error,omitempty"`
}
type ProcessStart ¶
type ProcessStatus ¶
type ProcessStatus struct {
ProcessID string `json:"processID"`
Identity devcontract.ProcessIdentity `json:"identity"`
Running bool `json:"running"`
ExitCode int `json:"exitCode"`
}
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
func OpenRegistry ¶
func OpenRegistry(root string, options RegistryOptions) (*Registry, error)
OpenRegistry uses one dedicated, private root shared by all cooperating workspaces. Canonical paths share the same OS lock, including across processes.
func (*Registry) BindFailed ¶
func (r *Registry) BindFailed(ctx context.Context, key ServiceKey, generation string, port int) error
BindFailed records an observed strict-child bind failure. It does not retry or terminate anything. The caller must reap its failed child before Reallocate.
func (*Registry) ConfirmBound ¶
func (r *Registry) ConfirmBound(ctx context.Context, key ServiceKey, generation string, actualPort int, process devcontract.ProcessIdentity) error
ConfirmBound rejects a child's hidden port increment and verifies its captured process identity. Callers must independently verify endpoint workspace and generation plus service-specific readiness before advertising a ready service.
func (*Registry) List ¶
List returns persisted assignments, including released/stale generations, for an explicit workspace. It makes no liveness or readiness claim.
func (*Registry) Reallocate ¶
func (r *Registry) Reallocate(ctx context.Context, key ServiceKey, generation string) (Lease, error)
Reallocate is the only automatic bind-race retry. Each returned assignment is recorded, skips this generation's failed ports, and consumes a bounded attempt.
type RegistryOptions ¶
type RemoteProcess ¶
type RemoteProcess struct {
// contains filtered or unexported fields
}
func (*RemoteProcess) ID ¶
func (p *RemoteProcess) ID() string
func (*RemoteProcess) Identity ¶
func (p *RemoteProcess) Identity() devcontract.ProcessIdentity
func (*RemoteProcess) Logs ¶
func (p *RemoteProcess) Logs(ctx context.Context, cursor int64, limit int) (ProcessLogPage, error)
func (*RemoteProcess) Status ¶
func (p *RemoteProcess) Status(ctx context.Context) (ProcessStatus, error)
type ServiceKey ¶
type ServiceKey struct {
WorkspaceID string `json:"workspaceID"`
Role string `json:"role"`
PluginID string `json:"pluginID,omitempty"`
}
ServiceKey never depends on plugin selection order or a workspace slug.
type UnixSocketDirectory ¶
type UnixSocketDirectory struct {
// contains filtered or unexported fields
}
UnixSocketDirectory belongs to one captured supervisor lifetime. Close must only be called after that supervisor has disposed all owned descendant groups. Retain the handle on failure so the same owner can retry cleanup.
func CreateUnixSocketDirectory ¶
func CreateUnixSocketDirectory(runtimeRoot string, owner devcontract.ProcessIdentity) (*UnixSocketDirectory, error)
CreateUnixSocketDirectory publishes an intent before creating its exclusive namespace. A crash cannot leave an external path without a durable reference; incomplete intents are conflicts, never permission to adopt a directory.
func (*UnixSocketDirectory) Close ¶
func (d *UnixSocketDirectory) Close() error