pkg/service/leaf_access_source.go
package service
import (
"context"
"git.cafe/ezterry/ez-agent/pkg/service/agent"
"git.cafe/ezterry/ez-agent/pkg/service/config"
"git.cafe/ezterry/ez-agent/pkg/service/session"
)
// leafAccessConfigResolver supplies the same next-run configuration used for
// admission. Keeping it separate avoids changing construction/router contracts.
type leafAccessConfigResolver interface {
resolveLeafAccessConfig(*session.Leaf) *config.Config
}
type leafAccessConfigContextResolver interface {
resolveLeafAccessConfigContext(context.Context, *session.Leaf) (*config.Config, error)
}
func (s *Server) resolveLeafAccessConfig(leaf *session.Leaf) *config.Config {
return s.resolveConfigFromLeaf(nil, leaf)
}
// leafRuntime is called under the leaf capture gate for both projections.
func (o *leafOperations) leafRuntime(leaf *session.Leaf) agent.LeafRuntimeSnapshot {
return o.leafRuntimeContext(context.Background(), leaf)
}
func (o *leafOperations) leafRuntimeContext(ctx context.Context, leaf *session.Leaf) agent.LeafRuntimeSnapshot {
runtime := agent.LeafRuntimeSnapshot{State: "unloaded"}
ag := o.resolveOpenAgent(leaf.ID)
if ag != nil {
runtime = ag.LeafRuntime()
}
runtime.State = o.backgroundState(leaf.ID, runtime.State)
if runtime.AccessSource != "" {
return runtime
}
runtime.AccessSource = "unknown"
var cfg *config.Config
if resolver, ok := o.router.(leafAccessConfigContextResolver); ok && ctx.Done() != nil {
var err error
cfg, err = resolver.resolveLeafAccessConfigContext(ctx, leaf)
if err != nil {
return runtime
}
} else if resolver, ok := o.router.(leafAccessConfigResolver); ok {
cfg = resolver.resolveLeafAccessConfig(leaf)
}
if cfg == nil {
return runtime
}
info := agent.ResolveModelInfo(cfg, leaf.Model, o.deps.remoteProviders)
if ag != nil {
if observed := ag.IdleAccessSource(cfg, leaf.Model); observed != "" {
info.AccessSource = observed
}
}
info = o.rememberedLeafModelInfo(leaf.ID, info)
if info.AccessSource != "" {
runtime.AccessSource = info.AccessSource
}
return runtime
}
// Empty source is missing evidence, not an explicit unknown. Storage checks
// the selected model transactionally before saving; only matching resolved
// models may reuse remembered values.
func (o *leafOperations) rememberLeafModelInfo(leafID string, info session.LeafModelInfo) session.LeafModelInfo {
if o.storage == nil || info.Model == "" {
return info
}
if err := o.storage.RememberLeafModelInfo(leafID, info); err != nil {
return info
}
return o.rememberedLeafModelInfo(leafID, info)
}
// Snapshot reads must not mutate metadata, including during CLI preflight.
// Only selection and execution save observations; missing fields here can
// still use the last saved values for the same resolved route.
func (o *leafOperations) rememberedLeafModelInfo(leafID string, info session.LeafModelInfo) session.LeafModelInfo {
if o.storage == nil || info.Model == "" {
return info
}
if saved, err := o.storage.GetLeafModelInfo(leafID); err == nil && saved.Model == info.Model {
if info.MaxContext <= 0 {
info.MaxContext = saved.MaxContext
}
if info.AccessSource == "" {
info.AccessSource = saved.AccessSource
}
}
return info
}
// Discovery is also used by read-only CLI preflight. Publish its cache-backed
// source without writing leaf properties. Snapshot capture re-reads the selected
// model under the leaf gate; selection and execution own persistence.
func (s *Server) publishDiscoveredLeafModelInfo(ctx context.Context, leafID string) {
if leafID != "" && s.leaves != nil && ctx.Err() == nil {
s.PublishLeafSnapshot(leafID, "")
}
}