mirror of
https://github.com/esengine/DeepSeek-Reasonix.git
synced 2026-11-07 10:45:59 -10:00
internal/ was 141 packages side by side, so nothing but a reader's memory
said which were foundations and which assembled the rest. Each now sits in a
layer directory, and the directory is the declaration:
base L0 foundation with no domain in it
contract L1 types and interfaces the layers above agree on
model, tools, safety, ext, state, platform
L2 subsystems, peers of one another
runtime L3 the turn engine
session L4 the controller
assembly L5 the composition root
frontend L6 cli, serve, acp and the host adapters
Only paths change: package names, code and behaviour do not. Imports,
repolint and coverage baselines, sensitive paths, CI package lists and docs
follow the new paths; release notes keep the paths they were written with,
and sample paths inside tests and fixtures keep theirs.
repolint gains layer-order: a package may import its own layer and those
below it, read from where it lives. The 47 imports that point upward today
are recorded as that rule's opening debt (-allow-widen, a new rule's first
baseline) and can only go down. Most of it is config, which still reaches
into eight subsystems and is the next thing to take apart; the rest is
event, history and a few tools reaching into runtime for types that belong
in contract.
151 lines
5.2 KiB
Go
151 lines
5.2 KiB
Go
package extension
|
|
|
|
import (
|
|
"reasonix/internal/ext/extensioncontract"
|
|
|
|
"fmt"
|
|
"maps"
|
|
"strings"
|
|
)
|
|
|
|
// Slot names a replaceable runtime seam. Unlike additive contributions, a
|
|
// slot has exactly one owner at a time: two packages both replacing the
|
|
// system prompt would produce a hybrid neither intended, so the kernel
|
|
// refuses the combination instead of picking one silently.
|
|
type Slot string
|
|
|
|
const (
|
|
SlotSystemPrompt Slot = "system_prompt"
|
|
SlotContext Slot = "context"
|
|
SlotProviderRequest Slot = "provider_request"
|
|
SlotProviderResponse Slot = "provider_response"
|
|
SlotCompaction Slot = "compaction"
|
|
SlotSessionPolicy Slot = "session_policy"
|
|
SlotPermission Slot = "permission"
|
|
SlotFrontendEvents Slot = "frontend_events"
|
|
)
|
|
|
|
// namedSlots is the closed set of bare slot names ParseSlot accepts. The set
|
|
// is deliberately closed: a slot is a runtime seam the kernel knows how to
|
|
// honor, so inventing one in a manifest must fail loudly, not be ignored.
|
|
var namedSlots = map[Slot]bool{
|
|
SlotSystemPrompt: true,
|
|
SlotContext: true,
|
|
SlotProviderRequest: true,
|
|
SlotProviderResponse: true,
|
|
SlotCompaction: true,
|
|
SlotSessionPolicy: true,
|
|
SlotPermission: true,
|
|
SlotFrontendEvents: true,
|
|
}
|
|
|
|
const (
|
|
// slotToolPrefix namespaces per-tool replacement slots.
|
|
slotToolPrefix = "tool:"
|
|
// slotProviderPrefix namespaces per-provider-ref replacement slots.
|
|
slotProviderPrefix = "provider:"
|
|
)
|
|
|
|
// SlotTool returns the replacement slot for one tool, e.g. "tool:bash".
|
|
func SlotTool(name string) Slot { return Slot(slotToolPrefix + name) }
|
|
|
|
// SlotProviderRef returns the replacement slot for one provider ref, e.g.
|
|
// "provider:openai/gpt-5".
|
|
func SlotProviderRef(ref string) Slot { return Slot(slotProviderPrefix + ref) }
|
|
|
|
// ParseSlot validates a slot string. Bare names must be one of the declared
|
|
// slots; the tool:/provider: forms must carry a well-formed target so a typo
|
|
// cannot create a slot nothing will ever read.
|
|
func ParseSlot(s string) (Slot, error) {
|
|
if namedSlots[Slot(s)] {
|
|
return Slot(s), nil
|
|
}
|
|
if rest, ok := strings.CutPrefix(s, slotToolPrefix); ok {
|
|
if rest == "" || strings.ContainsAny(rest, " \t\n") {
|
|
return "", fmt.Errorf("extension: invalid tool slot %q: empty or whitespace tool name", s)
|
|
}
|
|
return Slot(s), nil
|
|
}
|
|
if rest, ok := strings.CutPrefix(s, slotProviderPrefix); ok {
|
|
if !extensioncontract.ValidProviderTarget(rest) {
|
|
return "", fmt.Errorf("extension: invalid provider slot %q: want provider:<name>/<model> or provider:plugin/<plugin>/<name>/<model>", s)
|
|
}
|
|
return Slot(s), nil
|
|
}
|
|
return "", fmt.Errorf("extension: unknown slot %q", s)
|
|
}
|
|
|
|
// IsProviderRef reports whether ref is a kernel-shaped provider ID
|
|
// ("<name>/<model>", exactly one slash). Legacy provider catalogs can carry
|
|
// refs the kernel rejects at validation time — a bare provider name, or a
|
|
// model that itself contains a slash — so assemblers wrapping those catalogs
|
|
// use this to pre-filter entries instead of failing the whole build.
|
|
func IsProviderRef(ref string) bool {
|
|
_, _, ok := extensioncontract.SplitProviderRef(ref)
|
|
return ok
|
|
}
|
|
|
|
// SlotClaimer is implemented by contribution payloads that replace a runtime
|
|
// seam. The kernel enforces single ownership per slot at resolve time; it
|
|
// does not judge whether a contributor was entitled to claim a slot — that
|
|
// legitimacy check (claims ⊆ what the contributor declared) belongs to the
|
|
// caller at contribution time, because only the caller knows the
|
|
// contributor's manifest.
|
|
type SlotClaimer interface {
|
|
ReplacementSlots() []Slot
|
|
}
|
|
|
|
// SlotConflictError reports a second claimant for an already-owned slot.
|
|
type SlotConflictError struct {
|
|
Slot Slot
|
|
Owners []ContributionSource
|
|
}
|
|
|
|
func (e *SlotConflictError) Error() string {
|
|
labels := make([]string, 0, len(e.Owners))
|
|
for _, s := range e.Owners {
|
|
labels = append(labels, s.label())
|
|
}
|
|
return fmt.Sprintf("extension: replacement slot %q claimed by %v", e.Slot, labels)
|
|
}
|
|
|
|
// ReplaceClaims tracks slot ownership during resolution. It is per-build
|
|
// state: the winning owners are frozen into the snapshot's Replacements map.
|
|
type ReplaceClaims struct {
|
|
owners map[Slot]ContributionSource
|
|
}
|
|
|
|
// NewReplaceClaims returns an empty claim table.
|
|
func NewReplaceClaims() *ReplaceClaims {
|
|
return &ReplaceClaims{owners: map[Slot]ContributionSource{}}
|
|
}
|
|
|
|
// Claim records src as the owner of slot. The second claimant for a slot gets
|
|
// a *SlotConflictError naming both owners — never a silent override. Claiming
|
|
// an invalid slot string is an error for the same reason ParseSlot rejects
|
|
// it.
|
|
func (c *ReplaceClaims) Claim(slot Slot, src ContributionSource) error {
|
|
if _, err := ParseSlot(string(slot)); err != nil {
|
|
return err
|
|
}
|
|
if owner, taken := c.owners[slot]; taken {
|
|
return &SlotConflictError{Slot: slot, Owners: []ContributionSource{owner, src}}
|
|
}
|
|
c.owners[slot] = src
|
|
return nil
|
|
}
|
|
|
|
// Owner returns the current owner of slot.
|
|
func (c *ReplaceClaims) Owner(slot Slot) (ContributionSource, bool) {
|
|
owner, ok := c.owners[slot]
|
|
return owner, ok
|
|
}
|
|
|
|
// Claims returns a copy of the ownership table, so callers cannot mutate
|
|
// build state through the result.
|
|
func (c *ReplaceClaims) Claims() map[Slot]ContributionSource {
|
|
out := make(map[Slot]ContributionSource, len(c.owners))
|
|
maps.Copy(out, c.owners)
|
|
return out
|
|
}
|