home / skills / lookatitude / beluga-ai / implement_feature

implement_feature skill

/.agent/skills/implement_feature

This skill guides end-to-end feature implementation in Beluga AI, enforcing ISP patterns, configuration, metrics, and robust error handling.

npx playbooks add skill lookatitude/beluga-ai --skill implement_feature

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
5.3 KB
---
name: Implement Feature
description: End-to-end feature implementation following Beluga AI patterns
personas:
  - backend-developer
---

# Implement Feature

This skill guides you through implementing a new feature in Beluga AI, ensuring compliance with all framework patterns and standards.

## Prerequisites

- Understand the feature requirements
- Identify which package(s) will be affected
- Review similar implementations in the codebase

## Steps

### 1. Plan the Implementation

Before writing code:

1. Identify affected packages
2. Define interfaces needed (following ISP)
3. Plan the configuration structure
4. List dependencies (following DIP)
5. Design error handling approach

### 2. Create/Update Package Structure

If creating a new package:

```bash
mkdir -p pkg/<package_name>/{iface,internal,providers}
```

Required files:
- `pkg/<package_name>/<package_name>.go` - Main API
- `pkg/<package_name>/config.go` - Configuration
- `pkg/<package_name>/metrics.go` - OTEL metrics
- `pkg/<package_name>/errors.go` - Error definitions
- `pkg/<package_name>/test_utils.go` - Test helpers
- `pkg/<package_name>/README.md` - Documentation

### 3. Define Interfaces

In `iface/interfaces.go`:

```go
package iface

import "context"

// Use -er suffix for single-method interfaces
type Processor interface {
    Process(ctx context.Context, input Input) (Output, error)
}

// Use descriptive nouns for multi-method interfaces
type DataStore interface {
    Get(ctx context.Context, key string) ([]byte, error)
    Set(ctx context.Context, key string, value []byte) error
    Delete(ctx context.Context, key string) error
}
```

### 4. Implement Configuration

In `config.go`:

```go
package mypackage

import "github.com/go-playground/validator/v10"

type Config struct {
    Timeout    time.Duration `mapstructure:"timeout" yaml:"timeout" validate:"required,min=1s"`
    MaxRetries int           `mapstructure:"max_retries" yaml:"max_retries" validate:"gte=0,lte=10"`
}

func (c *Config) Validate() error {
    return validator.New().Struct(c)
}

// Functional options
type Option func(*Component)

func WithTimeout(t time.Duration) Option {
    return func(c *Component) { c.timeout = t }
}
```

### 5. Implement OTEL Metrics

In `metrics.go`:

```go
package mypackage

import (
    "go.opentelemetry.io/otel/metric"
    "go.opentelemetry.io/otel/trace"
)

type Metrics struct {
    operationsTotal   metric.Int64Counter
    operationDuration metric.Float64Histogram
    errorsTotal       metric.Int64Counter
    tracer            trace.Tracer
}

func NewMetrics(meter metric.Meter, tracer trace.Tracer) (*Metrics, error) {
    ops, err := meter.Int64Counter("mypackage_operations_total")
    if err != nil {
        return nil, err
    }
    dur, err := meter.Float64Histogram("mypackage_operation_duration_seconds")
    if err != nil {
        return nil, err
    }
    errs, err := meter.Int64Counter("mypackage_errors_total")
    if err != nil {
        return nil, err
    }
    return &Metrics{
        operationsTotal:   ops,
        operationDuration: dur,
        errorsTotal:       errs,
        tracer:            tracer,
    }, nil
}
```

### 6. Implement Error Handling

In `errors.go`:

```go
package mypackage

type ErrorCode string

const (
    ErrCodeInvalidInput ErrorCode = "INVALID_INPUT"
    ErrCodeTimeout      ErrorCode = "TIMEOUT"
    ErrCodeRateLimit    ErrorCode = "RATE_LIMIT"
)

type Error struct {
    Op   string    // Operation that failed
    Err  error     // Underlying error
    Code ErrorCode // Classification
}

func (e *Error) Error() string {
    return fmt.Sprintf("%s: %s: %v", e.Op, e.Code, e.Err)
}

func (e *Error) Unwrap() error {
    return e.Err
}
```

### 7. Implement Core Logic

In `<package_name>.go`:

```go
package mypackage

func NewComponent(opts ...Option) *Component {
    c := &Component{
        // defaults
    }
    for _, opt := range opts {
        opt(c)
    }
    return c
}

func (c *Component) Process(ctx context.Context, input Input) (Output, error) {
    ctx, span := c.metrics.tracer.Start(ctx, "component.process")
    defer span.End()

    start := time.Now()
    defer func() {
        c.metrics.operationDuration.Record(ctx, time.Since(start).Seconds())
    }()

    c.metrics.operationsTotal.Add(ctx, 1)

    // Implementation...

    if err != nil {
        c.metrics.errorsTotal.Add(ctx, 1)
        span.RecordError(err)
        return Output{}, &Error{Op: "Process", Err: err, Code: ErrCodeInvalidInput}
    }

    return output, nil
}
```

### 8. Write Tests

Create comprehensive tests following test standards:

- Table-driven tests
- Concurrency tests
- Error scenario tests
- OTEL validation tests

### 9. Run Quality Checks

```bash
make fmt
make lint
make test-unit
make security
```

### 10. Update Documentation

- Update package README.md
- Add examples if appropriate
- Update main documentation if needed

## Validation Checklist

- [ ] Interfaces follow ISP (small, focused)
- [ ] Configuration has validation tags
- [ ] OTEL metrics implemented
- [ ] Error handling uses Op/Err/Code pattern
- [ ] All public methods have tracing
- [ ] Tests cover happy path, errors, edge cases
- [ ] `make lint` passes
- [ ] `make test-unit` passes

## Output

A complete feature implementation with:
- Clean interfaces
- Configuration with validation
- OTEL observability
- Comprehensive tests
- Documentation

Overview

This skill guides developers through end-to-end feature implementation in Go following Beluga AI patterns and conventions. It enforces package layout, interface design, configuration validation, OTEL observability, structured error handling, and testing standards. Use it to produce consistent, maintainable features that integrate cleanly with the codebase.

How this skill works

The skill inspects the planned change, recommends package structure and interfaces, and provides templates for config, metrics, and errors. It outlines core implementation steps including tracer-instrumented public methods, OTEL metric creation, and functional options for configuration. It also prescribes test patterns and a checklist for quality gates and documentation updates.

When to use it

  • Adding a new feature that will become a reusable package
  • Refactoring or introducing new public interfaces
  • Implementing observability and metrics for core operations
  • Enforcing consistent error classification and propagation
  • Preparing code for unit and concurrency testing before merging

Best practices

  • Design small, focused interfaces following ISP and prefer -er suffix for single-method interfaces
  • Use functional options and validate configuration with struct tags and a validator
  • Instrument all public methods with tracing and record OTEL metrics for success/error and duration
  • Classify errors with Op/Err/Code pattern and implement Unwrap for compatibility
  • Write table-driven, concurrency, and error-path tests; include OTEL validation in tests
  • Run formatting, linting, unit tests, and security checks before submitting PR

Example use cases

  • Create a new pkg/<name> with iface, internal, providers, and standard files for API, config, metrics, and errors
  • Implement a Processor interface and a concrete Component with functional options and timeouts
  • Add OTEL counters, histograms, and a tracer to an existing component for latency and error monitoring
  • Define error codes and wrap underlying errors with operation context for clearer logs and retries
  • Add comprehensive unit tests including concurrency scenarios and OTEL instrumentation assertions

FAQ

How do I choose between creating a new package or extending an existing one?

Create a new package when the feature is reusable, has distinct responsibilities, or will be consumed independently. Extend existing packages only for closely related, small enhancements.

What minimum OTEL metrics should I add?

At minimum add an operations counter, an operation duration histogram, and an errors counter, plus a tracer for spans on public methods.