Skip to content

Command self-registration in executor to reduce agentic discovery cost #516

@ako

Description

@ako

Problem

The executor has 241 files with no structural guide for where to add a new command. Adding a command requires: (a) creating a cmd_*.go file, (b) finding and editing the central dispatch switch, and (c) knowing the correct context type. An AI code generator (or a new contributor) must read across multiple files to understand the pattern. This increases the cognitive load and the likelihood of missing a step.

Proposed solution

Replace the central dispatch switch with a registration pattern where each cmd_*.go file self-registers its handler via init(). A new command is then entirely contained in one file — no other file needs to change.

Proposed pattern:

// cmd_entities.go
func init() {
    executor.Register(ast.KindCreateEntity, handleCreateEntity)
    executor.Register(ast.KindAlterEntity, handleAlterEntity)
}

func handleCreateEntity(ctx *executor.ExecContext, stmt ast.Statement) error {
    s := stmt.(*ast.CreateEntityStatement)
    return ctx.Backend.DomainModel().CreateEntity(s.Name, ...)
}
// executor/registry.go
var handlers = map[ast.StatementKind]HandlerFunc{}

func Register(kind ast.StatementKind, fn HandlerFunc) {
    handlers[kind] = fn
}

Implementation steps

  1. Add executor/registry.go with Register() and the handler map
  2. Replace the central dispatch switch with a registry lookup
  3. Migrate existing cmd_*.go files to register via init() — one file at a time, running tests after each
  4. Remove the dispatch switch from executor.go

Expected outcome

Adding a new MDL command requires creating exactly one new file. No other file needs to change. The pattern is self-documenting — any existing cmd_*.go serves as a complete example.

See proposal: docs/11-proposals/PROPOSAL_agentic_architecture_improvements.md (Change 3)

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions