74 lines
1.9 KiB
Go
74 lines
1.9 KiB
Go
package fs
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/user"
|
|
|
|
"ollie/elevate"
|
|
olog "ollie/log"
|
|
"ollie/skills"
|
|
coresession "ollie/session"
|
|
)
|
|
|
|
// Config carries all runtime dependencies for the filesystem tree.
|
|
// It is the single input to NewRoot.
|
|
type Config struct {
|
|
Ctx context.Context
|
|
Log *olog.Logger
|
|
Sink *olog.Sink
|
|
AgentsDir string
|
|
SessionsDir string
|
|
MkdirAll func(string, os.FileMode) error
|
|
NewCore func(sessionID, agentName, cwd string) (*coresession.Session, error)
|
|
Yolo bool
|
|
SkillsRegistry *skills.Registry
|
|
ModelCache *ModelCache
|
|
ElevateBroker *elevate.Broker
|
|
Shutdown func()
|
|
Invalidate func()
|
|
}
|
|
|
|
// NewRoot creates the complete 9P filesystem tree from the spec.
|
|
// It validates, resolves ownership inheritance, wires handlers,
|
|
// populates root state, and returns the ready-to-serve *Tree.
|
|
func NewRoot(cfg Config) *Tree {
|
|
// Resolve root ownership from the current system user.
|
|
u, err := user.Current()
|
|
treeSpecUID = "ollie"
|
|
treeSpecGID = "agent"
|
|
if err == nil && u != nil {
|
|
treeSpecUID = u.Username
|
|
}
|
|
treeSpec.UID = treeSpecUID // root node: owner is system user
|
|
treeSpec.GID = treeSpecGID // root node: group is "agent"
|
|
|
|
// Build the root context with all available deps.
|
|
ctx := HandlerCtx{
|
|
Root: nil, // set after tree is built
|
|
Log: cfg.Log,
|
|
Skills: cfg.SkillsRegistry,
|
|
Models: cfg.ModelCache,
|
|
Elevate: cfg.ElevateBroker,
|
|
Shutdown: cfg.Shutdown,
|
|
Invalidate: cfg.Invalidate,
|
|
}
|
|
|
|
// Build the tree from the spec.
|
|
// Validation happens inside BuildTree.
|
|
tree := BuildTree(treeSpec, ctx)
|
|
ctx.Root = tree // now handlers can access root tree via ctx.Root
|
|
|
|
// Initialize root state.
|
|
rs := &rootState{
|
|
cfg: cfg,
|
|
sessions: make(map[string]*Session),
|
|
}
|
|
tree.Data = rs
|
|
globalRootState = rs
|
|
|
|
// Restore persisted sessions from disk.
|
|
restoreAllSessions(rs)
|
|
|
|
return tree
|
|
} |