scope

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Explicit scope parents supplement layered scopes, matching bindScopeParent, scopeParentOf, and scopeChainOf upstream. Resolution walks the ancestor chain. Bindings form a DAG: reject links that would return to the starting scope so all consumers can safely walk to the root.

Package scope provides layered registry primitives matching core/scope. Tools, skills, commands, and credentials share host defaults with session, user, or project overrides. Resolution uses the nearest matching layer; within a layer higher rank wins, then later registration breaks ties. Anonymous entries never participate in named lookup and appear only in full traversal, in registration order.

Index

Constants

This section is empty.

Variables

View Source
var ErrAlreadyBound = errors.New("scope: key already bound to a parent")

ErrAlreadyBound rejects a second parent binding for a key.

View Source
var ErrCycle = errors.New("scope: parent link would form a cycle")

ErrCycle indicates that a proposed parent link would create a cycle.

Functions

This section is empty.

Types

type Key

type Key string

Key identifies a scope layer, such as host, user:xxx, session:xxx, or project:xxx.

type Layer

type Layer[V any] struct {
	// contains filtered or unexported fields
}

Layer contains entries for one scope key. A slice retains registration order for deterministic later-registration precedence.

func NewLayer

func NewLayer[V any](key Key) *Layer[V]

NewLayer creates an empty layer for a scope key.

func (*Layer[V]) Get

func (l *Layer[V]) Get(name string) (V, bool)

Get performs named lookup within this layer only.

func (*Layer[V]) Has

func (l *Layer[V]) Has(name string) bool

Has reports whether this layer has a named entry.

func (*Layer[V]) Key

func (l *Layer[V]) Key() Key

Key returns the layer identifier.

func (*Layer[V]) Register

func (l *Layer[V]) Register(name string, value V, rank int) uint64

Register adds an entry and returns its sequence number. Empty names are anonymous; higher ranks win same-key conflicts within the layer.

func (*Layer[V]) Unregister

func (l *Layer[V]) Unregister(name string)

Unregister removes all entries with the name, leaving anonymous entries unchanged.

type ParentTree

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

ParentTree is a concurrency-safe scope-parent registry.

func NewParentTree

func NewParentTree() *ParentTree

NewParentTree creates an empty parent registry.

func (*ParentTree) Bind

func (t *ParentTree) Bind(key, parent Key) error

Bind assigns a parent once. Existing bindings return ErrAlreadyBound; a parent chain reaching key returns ErrCycle.

func (*ParentTree) ChainOf

func (t *ParentTree) ChainOf(key Key) []Key

ChainOf returns key and its ancestors nearest-first, ending at the root.

func (*ParentTree) ParentOf

func (t *ParentTree) ParentOf(key Key) (Key, bool)

ParentOf returns a key's parent, or false for a root scope.

func (*ParentTree) Rebind

func (t *ParentTree) Rebind(key, parent Key) error

Rebind replaces or establishes a parent link after cycle detection.

type ResolvedEntry

type ResolvedEntry[V any] struct {
	Name  string // Named key; empty for anonymous entries.
	Value V
	Rank  int
}

ResolvedEntry is one merged result.

type ScopedLayers

type ScopedLayers[V any] struct {
	// contains filtered or unexported fields
}

ScopedLayers orders layers from host at index zero to the nearest scope last. Later layers have higher precedence.

func NewScopedLayers

func NewScopedLayers[V any]() *ScopedLayers[V]

NewScopedLayers creates an empty layered registry.

func (*ScopedLayers[V]) Get

func (sl *ScopedLayers[V]) Get(name string) (V, bool)

Get resolves a name from nearest scope to host, returning the first match or false when absent everywhere.

func (*ScopedLayers[V]) Merge

func (sl *ScopedLayers[V]) Merge() []ResolvedEntry[V]

Merge returns a deterministic view. Named entries resolve once by nearest scope, rank, and registration order. All anonymous entries remain in registration order across layers. Anonymous entries precede lexically sorted named entries.

func (*ScopedLayers[V]) Push

func (sl *ScopedLayers[V]) Push(layer *Layer[V]) *ScopedLayers[V]

Push appends the nearest, highest-priority layer; push the host layer first.

Jump to

Keyboard shortcuts

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