kernel

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2026 License: MIT Imports: 14 Imported by: 0

README ΒΆ

Aisphere Kernel

Aisphere Kernel is a breaking-rewrite microservice foundation for Aisphere projects. It is AI-native: every module ships a single-entry README, a Go-doc-rich doc.go, runnable examples, and an AI coding recipe.

New here? Read docs/README.md for the doc map, then errorx/README.md and logx/README.md for the two foundational modules. Run go run ./examples/errorx-basic to see errorx in action.


What's inside

Module Status One-line Entry doc
errorx/ βœ… stable Unified error semantics, stdlib-only errorx/README.md
logx/ βœ… stable slog-based structured logging logx/README.md
transport/http/ 🚧 in progress HTTP server / client / middleware β€”
transport/grpc/ 🚧 in progress gRPC server / client + errorx adapter β€”
configx/ βœ… stable Multi-source config (file/env/remote) configx/README.md
middleware/ 🚧 in progress recovery / ratelimit / logging / selector β€”
selector/ βœ… stable Load balancing (wrr/p2c/random/ewma) β€”
registry/ βœ… stable Service discovery registry/README.md
encoding/ βœ… stable json / yaml / xml / form / proto encoding/README.md
cmd/kernel/ βœ… stable Project scaffolding CLI β€”

Status legend: βœ… stable / 🚧 in progress / ⬜ planned


Core design

  • errorx is the only runtime/business error semantics package.
  • The legacy root package github.com/aisphereio/kernel/errors has been removed.
  • HTTP/gRPC/middleware/selector/contrib adapters now produce or consume errorx.
  • Proto error-code generation is retained, but it now generates errorx helpers.
  • Business code must not return raw errors.New or fmt.Errorf as API/business errors.
  • Transport layers expose stable error_code values instead of Kratos-style code + reason status objects.

Quick example: errorx

return errorx.NotFound(
    "AIHUB_SKILL_NOT_FOUND",
    "skill not found",
    errorx.WithMetadata("skill_id", skillID),
    errorx.WithPublicMetadata("resource", "skill"),
)

HTTP response shape (produced by httpx middleware, see examples/errorx-http):

{
  "code": "AIHUB_SKILL_NOT_FOUND",
  "message": "skill not found",
  "metadata": { "resource": "skill" }
}

HTTP status stays in the HTTP status code. The JSON code field is the stable business error code.

See errorx/README.md for the full guide and docs/ai/errorx.md for the AI coding recipe.


Quick example: logx

cfg := logx.DefaultConfig("dev")
cfg.ServiceName = "aihub"
logger, _, err := logx.New(cfg)
if err != nil {
    panic(err)
}

logger.Info("service started", logx.String("addr", ":8000"))

See logx/README.md and docs/design/logx.md.


Proto error generation

The proto generation capability is not deleted. It is converted to produce errorx helpers.

Retained compatibility pieces:

  • third_party/errors/errors.proto
  • cmd/protoc-gen-go-errors/
  • --go-errors_out=paths=source_relative:.

The name is kept for proto compatibility with existing Kratos-style annotations, but the generated Go code imports github.com/aisphereio/kernel/errorx and returns *errorx.Error.

Example proto:

import "errors/errors.proto";

enum SkillErrorReason {
  option (errors.default_code) = 500;

  SKILL_NOT_FOUND = 0 [(errors.code) = 404];
  SKILL_FORBIDDEN = 1 [(errors.code) = 403];
}

Generated helpers look like:

func IsSkillNotFound(err error) bool
func NewSkillNotFound(message string, opts ...errorx.Option) *errorx.Error
func ErrorSkillNotFound(format string, args ...interface{}) *errorx.Error

So old proto contracts can remain, while runtime errors are standardized on errorx.


Documentation map

Quick start (everyone)
β”œβ”€β”€ README.md                  ← you are here
β”œβ”€β”€ AGENTS.md                  ← AI work rules
β”œβ”€β”€ errorx/README.md           ← error handling
β”œβ”€β”€ logx/README.md             ← logging
β”œβ”€β”€ configx/README.md          ← configuration
└── docs/README.md             ← full doc index

AI coding guides
β”œβ”€β”€ docs/ai/errorx.md          ← complete errorx recipe (10 scenarios + forbidden patterns)
└── docs/ai/configx.md         ← complete configx recipe (Source + Scan + Watch)

Deep specs (architects / PR review)
β”œβ”€β”€ docs/design/errorx.md      ← 1250-line design spec
β”œβ”€β”€ docs/contracts/errorx.md   ← unbreakable contract
β”œβ”€β”€ docs/design/configx.md     ← configx design spec
β”œβ”€β”€ docs/contracts/configx.md  ← configx contract
└── docs/design/logx.md        ← logx design

Acceptance & ops (CI/CD)
β”œβ”€β”€ docs/process/errorx-acceptance-checklist.md
β”œβ”€β”€ docs/process/errorx-test-report.md
└── docs/process/module-acceptance.md

Runnable examples
β”œβ”€β”€ examples/errorx-basic/     ← minimal: Wrap + Inspect
└── examples/errorx-http/      ← full HTTP handler with 7 error scenarios

See docs/README.md for the complete index with recommended reading paths.


What was removed

The following old runtime package is intentionally gone:

  • errors/
  • imports of github.com/aisphereio/kernel/errors

The proto generator and proto extension directory are retained because they are part of the contract-generation toolchain, not the old runtime error package.


Verify

Use Go 1.25+ locally. Linux/macOS can use Make directly:

make tools
make test-errorx
make verify-errorx
make test
make vet

Windows is a first-class path. You can either keep using Make:

make tools
make test-cmd
make verify-errorx

or run the cmd wrappers directly, which also avoids PowerShell script-signing issues:

scripts\tools.cmd
scripts\test-cmd.cmd
scripts\verify-errorx.cmd

Contributing

See CONTRIBUTING.md and AGENTS.md.

For new modules, follow the documentation pattern established by errorx/:

  1. Single entry README in the package directory (e.g. httpx/README.md)
  2. Rich doc.go so go doc <pkg> gives useful output
  3. example_test.go with ExampleXxx for every public constructor
  4. One AI guide at docs/ai/<module>.md (recipes + forbidden patterns in one file)
  5. Design spec at docs/design/<module>.md for deep reference
  6. Contract at docs/contracts/<module>.md for unbreakable behaviors
  7. Runnable example at examples/<module>-*/
  8. Index entry in docs/README.md and root README.md

See docs/process/module-acceptance.md for the full checklist.

Documentation ΒΆ

Overview ΒΆ

Package kernel is the root package for Aisphere Kernel.

Runtime capabilities are implemented in focused subpackages such as core, errorx, logx, contextx, and httpx.

Index ΒΆ

Constants ΒΆ

View Source
const Release = "v0.0.0-baseline"

Release is the current kernel version.

Variables ΒΆ

This section is empty.

Functions ΒΆ

func NewContext ΒΆ

func NewContext(ctx context.Context, s AppInfo) context.Context

NewContext returns a new Context that carries value.

Types ΒΆ

type App ΒΆ

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

App is an application components lifecycle manager.

func New ΒΆ

func New(opts ...Option) *App

New create an application lifecycle manager.

func (*App) Endpoint ΒΆ

func (a *App) Endpoint() []string

Endpoint returns endpoints.

func (*App) ID ΒΆ

func (a *App) ID() string

ID returns app instance id.

func (*App) Metadata ΒΆ

func (a *App) Metadata() map[string]string

Metadata returns service metadata.

func (*App) Name ΒΆ

func (a *App) Name() string

Name returns service name.

func (*App) Run ΒΆ

func (a *App) Run() error

Run executes all OnStart hooks registered with the application's Lifecycle.

func (*App) Stop ΒΆ

func (a *App) Stop() (err error)

Stop gracefully stops the application.

func (*App) Version ΒΆ

func (a *App) Version() string

Version returns app version.

type AppInfo ΒΆ

type AppInfo interface {
	ID() string
	Name() string
	Version() string
	Metadata() map[string]string
	Endpoint() []string
}

AppInfo is application context value.

func FromContext ΒΆ

func FromContext(ctx context.Context) (s AppInfo, ok bool)

FromContext returns the Transport value stored in ctx, if any.

type Option ΒΆ

type Option func(o *options)

Option is an application option.

func AfterStart ΒΆ

func AfterStart(fn func(context.Context) error) Option

AfterStart run funcs after app starts

func AfterStop ΒΆ

func AfterStop(fn func(context.Context) error) Option

AfterStop run funcs after app stops

func BeforeStart ΒΆ

func BeforeStart(fn func(context.Context) error) Option

BeforeStart run funcs before app starts

func BeforeStop ΒΆ

func BeforeStop(fn func(context.Context) error) Option

BeforeStop run funcs before app stops

func Context ΒΆ

func Context(ctx context.Context) Option

Context with service context.

func Endpoint ΒΆ

func Endpoint(endpoints ...*url.URL) Option

Endpoint with service endpoint.

func ID ΒΆ

func ID(id string) Option

ID with service id.

func Logger ΒΆ

func Logger(logger *slog.Logger) Option

Logger with service logger.

func Metadata ΒΆ

func Metadata(md map[string]string) Option

Metadata with service metadata.

func Name ΒΆ

func Name(name string) Option

Name with service name.

func Registrar ΒΆ

func Registrar(r registry.Registrar) Option

Registrar with service registry.

func RegistrarTimeout ΒΆ

func RegistrarTimeout(t time.Duration) Option

RegistrarTimeout with registrar timeout.

func Server ΒΆ

func Server(srv ...transport.Server) Option

Server with transport servers.

func Signal ΒΆ

func Signal(sigs ...os.Signal) Option

Signal with exit signals.

func StopTimeout ΒΆ

func StopTimeout(t time.Duration) Option

StopTimeout with app stop timeout.

func Version ΒΆ

func Version(version string) Option

Version with service version.

Directories ΒΆ

Path Synopsis
Package accessx provides a small authn + authz + audit facade for handlers.
Package accessx provides a small authn + authz + audit facade for handlers.
Package auditx defines durable audit recording contracts.
Package auditx defines durable audit recording contracts.
Package authn cached.go β€” cache decorator for Authenticator + TokenService.
Package authn cached.go β€” cache decorator for Authenticator + TokenService.
callback
Package callback contains reusable HTTP helpers for browser based authn callback flows.
Package callback contains reusable HTTP helpers for browser based authn callback flows.
casdoor
Package casdoor adapts the official Casdoor Go SDK to Kernel authn contracts.
Package casdoor adapts the official Casdoor Go SDK to Kernel authn contracts.
Package authz defines Kernel's authorization boundary.
Package authz defines Kernel's authorization boundary.
spicedb
Package spicedb contains SpiceDB adapter contracts and configuration for authz.
Package spicedb contains SpiceDB adapter contracts and configuration for authz.
Package cachex provides the unified cache API for Aisphere Kernel.
Package cachex provides the unified cache API for Aisphere Kernel.
redis
Package redis registers the "redis" driver for cachex.
Package redis registers the "redis" driver for cachex.
cmd
kernel module
Package configx provides the unified configuration API for Aisphere Kernel.
Package configx provides the unified configuration API for Aisphere Kernel.
env
Package contextx provides business-facing request context utilities for Aisphere Kernel.
Package contextx provides business-facing request context utilities for Aisphere Kernel.
dbx
Package dbx provides the unified, batteries-included database API for Aisphere Kernel.
Package dbx provides the unified, batteries-included database API for Aisphere Kernel.
mysql
Package mysql registers the "mysql" driver for dbx.
Package mysql registers the "mysql" driver for dbx.
postgres
Package postgres registers the "postgres" driver for dbx.
Package postgres registers the "postgres" driver for dbx.
proto
Package proto defines the protobuf codec.
Package proto defines the protobuf codec.
xml
Package errorx defines Aisphere Kernel's shared error semantics.
Package errorx defines Aisphere Kernel's shared error semantics.
examples
authn-casdoor command
Command authn-casdoor exercises Kernel authn against a real local Casdoor.
Command authn-casdoor exercises Kernel authn against a real local Casdoor.
authz-spicedb command
Command authz-spicedb exercises Kernel authz against a real local SpiceDB.
Command authz-spicedb exercises Kernel authz against a real local SpiceDB.
cachex-basic command
Package main demonstrates basic cachex usage with Redis.
Package main demonstrates basic cachex usage with Redis.
configx-basic command
configx-env command
Package main demonstrates configx layered file + env override.
Package main demonstrates configx layered file + env override.
configx-watch command
Package main demonstrates configx Watch hot reload.
Package main demonstrates configx Watch hot reload.
contextx-basic command
Package main demonstrates contextx basic usage.
Package main demonstrates contextx basic usage.
dbx-basic command
Package main demonstrates the basic dbx flow with Postgres, mirroring aisphere-hub's skill.go patterns.
Package main demonstrates the basic dbx flow with Postgres, mirroring aisphere-hub's skill.go patterns.
dbx-scenarios command
Package main is a comprehensive scenario validator for dbx.
Package main is a comprehensive scenario validator for dbx.
errorx-basic command
errorx-http command
Package main demonstrates a complete HTTP handler that returns errorx errors.
Package main demonstrates a complete HTTP handler that returns errorx errors.
logx-basic command
Package main demonstrates logx basic usage.
Package main demonstrates logx basic usage.
logx-http command
Package main demonstrates logx in a complete HTTP server with access log middleware, panic recovery, request-scoped fields, and dynamic level control.
Package main demonstrates logx in a complete HTTP server with access log middleware, panic recovery, request-scoped fields, and dynamic level control.
metricsx-basic command
Package main demonstrates metricsx basic usage.
Package main demonstrates metricsx basic usage.
objectstorex-basic command
Package main demonstrates basic objectstorex usage with Minio.
Package main demonstrates basic objectstorex usage with Minio.
internal
group
Package group provides a sample lazy load container.
Package group provides a sample lazy load container.
Package logx is the Aisphere Kernel logging package.
Package logx is the Aisphere Kernel logging package.
Package metricsx provides business-facing metrics for Aisphere Kernel.
Package metricsx provides business-facing metrics for Aisphere Kernel.
Package objectstorex provides the unified object storage API for Aisphere Kernel.
Package objectstorex provides the unified object storage API for Aisphere Kernel.
minio
Package minio registers the "minio" driver for objectstorex.
Package minio registers the "minio" driver for objectstorex.
Package securityx groups external configuration for authn, authz and auditx.
Package securityx groups external configuration for authn, authz and auditx.
p2c
wrr

Jump to

Keyboard shortcuts

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