Documentation
¶
Overview ¶
Package urn is the Go implementation of the Hadron URN library, paired with @hadron-memory/urn-lib-js. Both run the SAME conformance corpus (fixtures/corpus.json) — the corpus is the contract, so the two cannot drift.
This is increment 1: the pure "scheme + registry + normalize + slug" slice, ported verbatim (v1 parity) from hadron-server/src/lib/urn.ts. parse/format/ compose and the grammar-v2 flat forms land in later increments.
Index ¶
- Constants
- Variables
- func AgentSlugFromUrn(agentURN string) string
- func AssertFullyQualifiedUrn(input, expectedType string) error
- func ComposeAppRunUrnV2(root, app, runID string) (string, error)
- func ComposeDataFragmentV2(parentURN string) (string, error)
- func ComposeEdgeUrn(memURN, loc string) (string, error)
- func ComposeInstalledAgentUrn(appURN, agentURN string) (string, error)
- func ComposeNodeRevUrnV2(root, mem, loc, rev string) (string, error)
- func ComposeNodeUrn(memURN, loc string) (string, error)
- func ComposeSecretUrnV2(root, name string) (string, error)
- func ComposeUrnV2(typ, root string, segments ...string) (string, error)
- func ComposeWorkerUrnV2(root, app, workerSlug string) (string, error)
- func DeriveSlugFromName(name string) (string, error)
- func FormatCanonicalUrn(typ, bareValue string) (string, error)
- func FormatUrn(typ, value string) string
- func HasSchemePrefix(input string) bool
- func IsFlatV2(input string) bool
- func IsParserCanonical(input string) bool
- func IsValidSlug(slug string) bool
- func LegacyMemoryUrnToCanonical(bareLegacyURN string) string
- func NormalizeScheme(input string) string
- func NormalizeUrnForLookup(bareURN string) string
- func ToParserCanonical(input string) (string, error)
- func ValidateAtomShape(input, atom string) error
- func ValidateOrgSlug(slug string) error
- func ValidateUrnType(parsed LegacyParsedURN, expected string) *string
- func ValidateUserHandle(handle string) error
- func ValidateUserSlug(slug string) error
- type LegacyParsedURN
- type NodeURNParts
- type NotQualifiedError
- type ParseError
- type ParseForResult
- type ParsedDisplayURN
- type ParsedNodeRevURNV2
- type ParsedURN
- type ParsedURNV2
- type Reason
- type UrnRow
Constants ¶
const ( CanonicalScheme = "hrn" LegacyScheme = "urn" )
Scheme prefixes (issue #239). hrn: is canonical; legacy urn: is accepted on input forever. Every emission path composes hrn:.
const MaxAtomLen = 64
MaxAtomLen is the maximum length of a single URN atom / slug, in bytes (spec cor:urn:010:01, FR-017). Exported as the single source of truth so lib internals and server-side minters stop re-typing the bare literal 64 (#715).
Variables ¶
var CoreTypes = []string{
"agent", "app", "app-user", "ai-config", "asset", "edge",
"license", "memory", "node", "org", "platform", "reference",
"secret", "session", "subscription", "usage", "user",
}
CoreTypes are the core URN types.
var DisplayURNTypes = []string{"org", "memory", "agent", "app", "node", "user", "apprun", "worker"}
DisplayURNTypes — the visible URN kinds rendered as a typed chip. Narrower than the canonical registry on purpose: a new visible kind must be added here too, or ParseDisplayUrn renders it as hrn:unknown:...
var NodeParts = []string{"condition", "data"}
NodeParts are the node parts.
var NodeRoles = []string{
"chat", "chat-message", "config", "conversation", "event", "goal", "stage",
}
NodeRoles are the node roles.
var NodeTypes = []string{
"abstract", "partial", "parent", "plan", "prompt", "record", "task", "review",
}
NodeTypes are the node types.
var RoleMarkers = []string{"system", "app-mem", "app-user", "group-mem", "priv", "anon"}
RoleMarkers are the memory-role markers (slug validation reserves their prefixes).
TypeMarkers are type-marker words that may appear inside path-segments.
var V2URNTypes = []string{
"org", "user", "mem", "agent", "app", "node", "edge", "asset", "secret",
"apprun", "worker", "noderev", "appkey", "aiconf", "tool", "server", "userapikey",
"agentschedule", "agentwebhook", "license", "subscription", "usage",
"reference", "session", "platform",
}
V2URNTypes — the v2 type registry (SEEDED; finalized in #698). Note the renames vs v1 (memory->mem) and additions (apprun, noderev, appkey, ...); `data` is demoted to a #fragment and is NOT a type.
Functions ¶
func AgentSlugFromUrn ¶
AgentSlugFromUrn extracts an agent's slug path, stripping the leading org segment. Robust to both legacy single-colon and canonical double-colon forms.
func AssertFullyQualifiedUrn ¶
AssertFullyQualifiedUrn rejects inputs that lack the fully-qualified shape for expectedType. Checks SHAPE, not full canonical grammar.
func ComposeAppRunUrnV2 ¶
ComposeAppRunUrnV2 composes hrn:apprun:<root>:<app>:<run-id>.
func ComposeDataFragmentV2 ¶
ComposeDataFragmentV2 appends the #data fragment to a v2 node or apprun URN.
func ComposeEdgeUrn ¶
ComposeEdgeUrn builds a canonical hrn:edge:<mem>::<loc> URN.
func ComposeInstalledAgentUrn ¶
ComposeInstalledAgentUrn composes the R2 canonical install URN for an Agent installed in an App: hrn:agent:<installing-org>::<app-slug>::<author-org>:<agent-slug>. Inputs MUST be 2-segment (org + slug) URNs. Install-by-self collapses via cat 1.
func ComposeNodeRevUrnV2 ¶
ComposeNodeRevUrnV2 composes hrn:noderev:<root>:<mem>:<loc...>:<rev>. `loc` may be a multi-atom node loc (colon-joined); it is split into its atoms so the terminal rev stays end-anchored.
func ComposeNodeUrn ¶
ComposeNodeUrn builds a canonical hrn:node:<mem>::<loc> URN.
func ComposeSecretUrnV2 ¶
ComposeSecretUrnV2 composes hrn:secret:<root>:<name> (org/user root + name).
func ComposeUrnV2 ¶
ComposeUrnV2 composes a canonical grammar-v2 flat URN.
func ComposeWorkerUrnV2 ¶
ComposeWorkerUrnV2 composes hrn:worker:<root>:<app>:<worker-slug> — a Worker, the named casting of an Agent into an App (cor:dmo:050:11). The leaf atom is the Worker's stored `slug` column, DERIVED from its name at cast time and iterated to uniqueness; it is never the display name, which carries no charset constraint and may collide after slugification (cor:agt:020:02).
func DeriveSlugFromName ¶
DeriveSlugFromName derives a valid, lowercase slug atom from a free-form name. Slugification only — it does NOT reject reserved words. Returns ReasonEmptyDerivedSlug when nothing usable survives.
func FormatCanonicalUrn ¶
FormatCanonicalUrn composes and canonicalizes a full URN from an entity type and a bare-stored value (legacy single-colon or canonical :: hierarchy). Always returns the canonical hrn:<type>: form.
func HasSchemePrefix ¶
HasSchemePrefix reports whether input leads with a scheme prefix.
func IsParserCanonical ¶
IsParserCanonical reports whether ParseUrn(input).ParserCanonical == input with no rewrites.
func IsValidSlug ¶
IsValidSlug is the boolean form of ValidateUserSlug: true when slug is a legal NEW entity slug (charset + length + reserved-word + lowercase), false on any violation. Convenience predicate for callers that don't want an error (#715).
func LegacyMemoryUrnToCanonical ¶
LegacyMemoryUrnToCanonical converts a bare legacy single-colon memory URN to canonical :: hierarchy, preserving the marker:id boundary for role-marker memories. Input MUST be the bare legacy form.
func NormalizeScheme ¶
NormalizeScheme rewrites a leading legacy urn: scheme to canonical hrn:. Bare and already-canonical inputs pass through.
func NormalizeUrnForLookup ¶
NormalizeUrnForLookup collapses the canonical :: hierarchy form to the bare single-colon lookup form.
func ToParserCanonical ¶
ToParserCanonical parses and returns the parser-layer canonical form.
func ValidateAtomShape ¶
ValidateAtomShape checks the FR-016 charset + FR-017 length only. Deliberately case-lenient (the read/parse path); the lowercase rule is enforced at create in ValidateUserSlug.
func ValidateOrgSlug ¶
ValidateOrgSlug validates an org slug: it must be bare (no scheme prefix, no colon), reuse the shared slug rules, and — for a NEW org root (#692) — be a dotted domain. Create/rename rule only; the parse/read path stays lenient.
func ValidateUrnType ¶
func ValidateUrnType(parsed LegacyParsedURN, expected string) *string
ValidateUrnType returns nil if the parsed type matches expected (with the unknown-always-ok and loc-where-node-expected exceptions), otherwise a pointer to the mismatch message. A *string (nil = ok) mirrors the JS `string | null`.
func ValidateUserHandle ¶
ValidateUserHandle validates a user handle at create/rename (#692): a ValidateUserSlug slug that must additionally be dot-free, so it stays charset-disjoint from a dotted org root in the shared principal pool. Registration policy, not a parse rule.
func ValidateUserSlug ¶
ValidateUserSlug validates a slug at create/rename: charset + length + reserved-word rejection + the lowercase-canonical rule (#575).
Types ¶
type LegacyParsedURN ¶
LegacyParsedURN is the result of ParseUrnInput. JSON tags match the corpus.
func ParseUrnInput ¶
func ParseUrnInput(input string) LegacyParsedURN
ParseUrnInput strips the hrn:/urn:<type>: or loc: prefix and returns the type and bare value. The v2 `mem` type word is normalized to `memory`. Unprefixed inputs are returned as type "unknown".
type NodeURNParts ¶
NodeURNParts is the result of SplitNodeUrn. JSON tags match the corpus.
func SplitNodeUrn ¶
func SplitNodeUrn(input string) (NodeURNParts, error)
SplitNodeUrn splits a fully-qualified node URN into its memory URN and loc. Self-validating (AssertFullyQualifiedUrn(input, "node")).
type NotQualifiedError ¶
NotQualifiedError is returned when a non-ID-shaped input fails URN qualification. Code() returns the stable cross-language contract handle.
func (*NotQualifiedError) Code ¶
func (e *NotQualifiedError) Code() string
Code is the stable contract handle (mirrors urn-lib-js UrnNotQualifiedError.code).
func (*NotQualifiedError) Error ¶
func (e *NotQualifiedError) Error() string
type ParseError ¶
ParseError is returned by every validation/parse entry point on any acceptance violation.
func (*ParseError) Error ¶
func (e *ParseError) Error() string
type ParseForResult ¶
ParseForResult is the ParseFor envelope. JSON tags match the corpus.
func ParseFor ¶
func ParseFor(row UrnRow) (ParseForResult, error)
ParseFor dispatches per-row: a normalized row is parsed canonically and its stored URN returned unchanged (drift throws; scheme differences tolerated); a legacy row is validated with the legacy parser (unknown shape throws).
type ParsedDisplayURN ¶
type ParsedDisplayURN struct {
Type string `json:"type"`
BareValue string `json:"bareValue"`
FullURN string `json:"fullUrn"`
}
ParsedDisplayURN is the result of ParseDisplayUrn. JSON tags match the corpus.
func ParseDisplayUrn ¶
func ParseDisplayUrn(value, typeHint string) ParsedDisplayURN
ParseDisplayUrn parses a displayable URN for chip rendering. `typeHint` is "" for none. Auto-detects a scheme-prefixed display kind; else applies the hint to a BARE value only; else falls back to "unknown".
type ParsedNodeRevURNV2 ¶
type ParsedNodeRevURNV2 struct {
Scheme string `json:"scheme"`
Type string `json:"type"`
Root string `json:"root"`
Memory string `json:"memory"`
Loc string `json:"loc"`
Rev string `json:"rev"`
}
ParsedNodeRevURNV2 is the END-ANCHORED decomposition of a hrn:noderev URN.
func ParseNodeRevUrnV2 ¶
func ParseNodeRevUrnV2(input string) (ParsedNodeRevURNV2, error)
ParseNodeRevUrnV2 parses and END-ANCHORED-decomposes a hrn:noderev URN (#696). The LAST atom is the revision id; the FIRST post-root atom is the memory slug; everything between is the variable-length, opaque node loc.
type ParsedURN ¶
type ParsedURN struct {
Type string `json:"type"`
PathSegments []string `json:"pathSegments"`
ParserCanonical string `json:"parserCanonical"`
InputForm string `json:"inputForm"`
ParserRewrites []string `json:"parserRewrites"`
NeedsResolverCanonicalization bool `json:"needsResolverCanonicalization"`
}
ParsedURN is the result of ParseUrn. JSON tags match the conformance corpus.
func ParseUrn ¶
ParseUrn parses a URN string, applying parser-layer canonicalization (D11 cats 1, 4). Returns a *ParseError on any acceptance violation. The v1 grammar is tried first; a v1-rejected input that is a valid flat grammar-v2 URN (single-colon, pool-rooted, `mem` type word — #697) is delegated to ParseUrnV2 and mapped into the ParsedURN shape. v1-accepted inputs keep their exact v1 result, so no existing behavior changes.
type ParsedURNV2 ¶
type ParsedURNV2 struct {
Scheme string `json:"scheme"`
Type string `json:"type"`
Root string `json:"root"`
Segments []string `json:"segments"`
// Fragment is present only when the URN carried a #<fragment> suffix.
Fragment string `json:"fragment,omitempty"`
}
ParsedURNV2 is the result of ParseUrnV2. JSON tags match the corpus.
func ParseUrnV2 ¶
func ParseUrnV2(input string) (ParsedURNV2, error)
ParseUrnV2 parses a STRICT grammar-v2 flat URN. Rejects v1 constructs (the :: hierarchy separator and the @/user: root sigil). Enforces the #696 per-entity arity and the #data fragment rules.
type Reason ¶
type Reason string
Reason is a machine-stable failure code. These ARE part of the fixture contract (shared with urn-lib-js); the human message text is not.
const ( ReasonUnknownType Reason = "unknown-type" ReasonMalformedGrammar Reason = "malformed-grammar" ReasonEmptySegment Reason = "empty-segment" ReasonTrailingDoubleColon Reason = "trailing-double-colon" ReasonReservedWordSlug Reason = "reserved-word-slug" ReasonInvalidCharset Reason = "invalid-charset" ReasonSlugNotLowercase Reason = "slug-not-lowercase" ReasonSlugTooLong Reason = "slug-too-long" ReasonLocSegmentRejected Reason = "loc-segment-rejected" ReasonInvalidSegmentShape Reason = "invalid-segment-shape" ReasonEmptyBareValue Reason = "empty-bare-value" ReasonAlreadyPrefixedValue Reason = "already-prefixed-bare-value" ReasonOrgUrnNotBare Reason = "org-urn-not-bare" ReasonOrgRootNotDotted Reason = "org-root-not-dotted" ReasonHandleHasDot Reason = "handle-has-dot" ReasonEmptyDerivedSlug Reason = "empty-derived-slug" )