codexinstance

package
v0.2.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 15, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package codexinstance publishes and discovers live, attachable Codex instances.

A registry is scoped to a private runtime directory. Descriptor filenames are derived from both the canonical Intercom broker socket and the validated peer name, so independent brokers and peers can safely share one registry. Publish is a cross-process claim operation: a different live owner is never overwritten, while an owner whose PID no longer exists is replaced. A repeated publish with the same instance nonce is an idempotent owner update.

Remove is similarly ownership-aware. It removes a descriptor only while its nonce still matches, under the same lock used by Publish. Cleanup from an old instance therefore cannot unlink a concurrently published replacement.

Index

Constants

View Source
const SchemaVersion = 2

SchemaVersion is the only descriptor schema this package currently accepts.

Variables

View Source
var (
	// ErrAlreadyLive identifies a Publish failure caused by a different
	// descriptor owner whose recorded PID still exists.
	ErrAlreadyLive = errors.New("codex instance is already live")

	// ErrStale identifies a descriptor that is structurally valid but whose
	// recorded owner PID no longer exists.
	ErrStale = errors.New("codex instance descriptor is stale")
)

Functions

func CanonicalBrokerSocket

func CanonicalBrokerSocket(path string) (string, error)

CanonicalBrokerSocket resolves path against the current working directory and returns the clean absolute socket identity used in descriptor keys.

func CanonicalCWD

func CanonicalCWD(path string) (string, error)

CanonicalCWD resolves path against the current working directory and returns its clean absolute spelling. It intentionally does not resolve symlinks: the Codex thread identity uses the lexical absolute cwd supplied at startup.

func CanonicalUnixEndpoint

func CanonicalUnixEndpoint(endpoint string) (string, error)

CanonicalUnixEndpoint validates endpoint and returns its normalized unix:///absolute/path spelling. Host, user info, query, and fragment components are forbidden.

func NewNonce

func NewNonce() (string, error)

NewNonce returns a cryptographically random, 128-bit instance nonce in lowercase hexadecimal form.

Types

type AlreadyLiveError

type AlreadyLiveError struct {
	Existing Descriptor
}

AlreadyLiveError reports the descriptor that prevented a new owner from claiming the same broker-and-peer key.

func (*AlreadyLiveError) Error

func (e *AlreadyLiveError) Error() string

func (*AlreadyLiveError) Unwrap

func (e *AlreadyLiveError) Unwrap() error

type Descriptor

type Descriptor struct {
	SchemaVersion          int             `json:"schemaVersion"`
	Peer                   string          `json:"peer"`
	CWD                    string          `json:"cwd"`
	BrokerSocketIdentity   string          `json:"brokerSocketIdentity"`
	DownstreamUnixEndpoint string          `json:"downstreamUnixEndpoint"`
	ThreadID               string          `json:"threadId"`
	PID                    int             `json:"pid"`
	InstanceNonce          string          `json:"instanceNonce"`
	CodexVersion           string          `json:"codexVersion"`
	ExecutionPolicy        ExecutionPolicy `json:"executionPolicy"`
}

Descriptor is the complete discovery record for one live Codex instance. CWD and BrokerSocketIdentity are canonical absolute filesystem paths. DownstreamUnixEndpoint is the canonical unix:///absolute/path form.

func (Descriptor) Validate

func (d Descriptor) Validate() error

Validate checks descriptor compatibility, identity fields, and canonical path/endpoint representation. It does not probe the process or either Unix socket; those are lifecycle concerns handled by the publisher and attacher.

type ExecutionPolicy added in v0.2.2

type ExecutionPolicy string
const (
	ExecutionWorkspaceWrite   ExecutionPolicy = "workspace-write"
	ExecutionDangerFullAccess ExecutionPolicy = "danger-full-access"
)

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry stores live descriptors in one mode-0700 directory. Registry values are safe for concurrent goroutines and cooperating processes.

func New

func New(liveDir string) (*Registry, error)

New creates or opens liveDir and forces its mode to 0700. The final path component must be a real directory, not a symlink. Later operations reject a directory whose type or permissions have changed.

func (*Registry) Dir

func (r *Registry) Dir() string

Dir returns the canonical registry directory.

func (*Registry) Load

func (r *Registry) Load(brokerSocket, peer string) (*Descriptor, error)

Load returns the strictly validated, live descriptor for brokerSocket and peer. It returns nil, nil when no descriptor currently exists and ErrStale when a valid descriptor's PID no longer exists. Loads need no lock: Publish uses rename, so a reader observes either a complete old descriptor or a complete new one.

func (*Registry) Path

func (r *Registry) Path(brokerSocket, peer string) (string, error)

Path returns the deterministic descriptor path for brokerSocket and peer. The full SHA-256 digest of the canonical broker-and-peer key makes cross-broker filename collisions negligible; the validated peer prefix keeps directory listings intelligible.

func (*Registry) Publish

func (r *Registry) Publish(d Descriptor) (string, error)

Publish atomically claims or updates d's broker-and-peer key and returns its descriptor path. It behaves as follows while holding a cross-process lock:

  • no prior descriptor: publish d;
  • the same instance nonce: atomically update the owner's descriptor;
  • a different nonce whose PID no longer exists: replace the stale record;
  • a different nonce whose PID exists: return ErrAlreadyLive unchanged.

A malformed or insecure prior descriptor is never silently discarded. If publication reaches rename but the following directory sync fails, Publish removes the renamed descriptor only while d's nonce still owns it.

func (*Registry) Remove

func (r *Registry) Remove(brokerSocket, peer, nonce string) (bool, error)

Remove deletes the descriptor only if nonce still owns it. The bool reports whether a file was removed. A missing descriptor or a nonce mismatch returns false, nil, making shutdown cleanup idempotent and safe after stale-record replacement. The nonce comparison and unlink occur under the Publish lock.

type StaleError

type StaleError struct {
	Descriptor Descriptor
}

StaleError reports the no-longer-live descriptor found by Load. Callers can use errors.As to include its PID or endpoint in a diagnostic. Publish, rather than Load, owns stale-record replacement.

func (*StaleError) Error

func (e *StaleError) Error() string

func (*StaleError) Unwrap

func (e *StaleError) Unwrap() error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL