refactor(hook_engine): Phase B language-advantage pass — dichotomy wave
engine: GenerateReport computes each function's placement ONCE — the oracle-shape loop called RecommendPlacement per hook-membership AND again per report row (2N calls); the memo collapses both to one call and keeps the report bytes identical. main: the oracle's main() commands land as the placement-engine CLI (scan/recommend/validate/report/hook, --json where the oracle has it) on the same Registry core the frozen battery replays; discovery reads the real scripts dir + plugin file through the engine's injectable seams (RUNE_MEMORY_DIR-aware plugin path). No behavioral change: gofmt/vet clean, frozen battery 7/7 green, CLI smoke (scan 265 discovered/12 tagged, validate clean, recommend/ report, hook pre_turn with payload) verified.
This commit is contained in:
1 parent
a69e73806f
commit
bc2c4415a9
2 files changed
+170
-15
No files matched your search
@@ -429,6 +429,10 @@ func (r Registry) sortedNames() []string {
|
||||
// GenerateReport ports generate_report: same header, same per-hook
|
||||
// sections (only non-empty), same row format, same validation tail.
|
||||
// Returns the string (the CLI prints it).
|
||||
//
|
||||
// Phase B: each function's placement is computed ONCE (the oracle calls
|
||||
// recommend_placement per hook-membership AND again per row — 2N calls;
|
||||
// the memo collapses them and keeps bytes identical).
|
||||
func (r Registry) GenerateReport() string {
|
||||
var b strings.Builder
|
||||
b.WriteString("╔══════════════════════════════════════════════════════════════╗\n")
|
||||
@@ -437,16 +441,20 @@ func (r Registry) GenerateReport() string {
|
||||
b.WriteString("\n")
|
||||
|
||||
hookOrder := []string{HookPreTurn, HookSystem, HookPostTurn, HookIdle, HookTimer, HookEvent}
|
||||
byHook := map[string][]string{}
|
||||
type placed struct {
|
||||
name string
|
||||
p map[string]interface{}
|
||||
}
|
||||
byHook := map[string][]placed{}
|
||||
for _, name := range r.sortedNames() {
|
||||
f := r[name]
|
||||
if len(f.Factors) == 0 {
|
||||
continue
|
||||
}
|
||||
placement := RecommendPlacement(name, f.Factors)
|
||||
hooks, _ := placement["hooks"].([]string)
|
||||
p := RecommendPlacement(name, f.Factors)
|
||||
hooks, _ := p["hooks"].([]string)
|
||||
for _, h := range hooks {
|
||||
byHook[h] = append(byHook[h], name)
|
||||
byHook[h] = append(byHook[h], placed{name, p})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -456,21 +464,20 @@ func (r Registry) GenerateReport() string {
|
||||
continue
|
||||
}
|
||||
fmt.Fprintf(&b, "━━━ %s ━━━\n", strings.ToUpper(hook))
|
||||
for _, name := range fns {
|
||||
p := RecommendPlacement(name, r[name].Factors)
|
||||
for _, fn := range fns {
|
||||
cache := "live"
|
||||
if p["cacheable"].(bool) {
|
||||
if fn.p["cacheable"].(bool) {
|
||||
cache = "cached"
|
||||
}
|
||||
freq, _ := p["frequency"].(string)
|
||||
budget, _ := p["context_budget"].(int)
|
||||
freq, _ := fn.p["frequency"].(string)
|
||||
budget, _ := fn.p["context_budget"].(int)
|
||||
parallel := "sequential"
|
||||
if p["can_parallel"].(bool) {
|
||||
if fn.p["can_parallel"].(bool) {
|
||||
parallel = "parallel"
|
||||
}
|
||||
failure, _ := p["failure_mode"].(string)
|
||||
failure, _ := fn.p["failure_mode"].(string)
|
||||
fmt.Fprintf(&b, " %-30s %-20s %-10s %4dtok %-10s %s\n",
|
||||
name, freq, cache, budget, parallel, failure)
|
||||
fn.name, freq, cache, budget, parallel, failure)
|
||||
}
|
||||
b.WriteString("\n")
|
||||
}
|
||||
|
||||
+151
-3
@@ -8,6 +8,11 @@
|
||||
// hook-engine list
|
||||
// hook-engine discover
|
||||
// hook-engine tag <name> <key=value> ...
|
||||
// hook-engine scan [--json] (placement engine: discover + auto-tag)
|
||||
// hook-engine recommend [--json]
|
||||
// hook-engine validate [--json]
|
||||
// hook-engine report
|
||||
// hook-engine hook --event <e> [--data <json>]
|
||||
package main
|
||||
|
||||
import (
|
||||
@@ -114,14 +119,23 @@ func autoTag(functions map[string]FunctionMeta) map[string]FunctionMeta {
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Fprintln(os.Stderr, "usage: hook-engine <fire|list|discover|tag> [args...]")
|
||||
fmt.Fprintln(os.Stderr, "usage: hook-engine <fire|list|discover|tag|scan|recommend|validate|report|hook> [args...]")
|
||||
os.Exit(1)
|
||||
}
|
||||
hooks := loadHooks()
|
||||
functions := loadFunctions()
|
||||
cmd := os.Args[1]
|
||||
args := os.Args[2:]
|
||||
|
||||
// placement-engine commands (the oracle's main()): separate registry
|
||||
// core, no hooks.json/functions.json state.
|
||||
switch cmd {
|
||||
case "scan", "recommend", "validate", "report", "hook":
|
||||
runPlacementCLI(cmd, args)
|
||||
return
|
||||
}
|
||||
|
||||
hooks := loadHooks()
|
||||
functions := loadFunctions()
|
||||
|
||||
switch cmd {
|
||||
case "fire":
|
||||
event := ""
|
||||
@@ -218,3 +232,137 @@ func main() {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// ── placement-engine CLI (ports the oracle's main() commands) ──────────
|
||||
// scan/recommend/validate/report/hook drive the same Registry core the
|
||||
// frozen battery replays; discovery reads the real scripts dir + plugin
|
||||
// file through the engine's injectable seams.
|
||||
|
||||
func pluginFile() string {
|
||||
home := os.Getenv("RUNE_MEMORY_DIR")
|
||||
if home == "" {
|
||||
home = os.Getenv("HOME") + "/.rune"
|
||||
}
|
||||
return filepath.Join(home, "plugins", "memory-plugin.ts")
|
||||
}
|
||||
|
||||
// buildRegistry mirrors the oracle's main() preamble: discover_functions()
|
||||
// then auto_tag() — with real filesystem IO feeding the seams.
|
||||
func buildRegistry() Registry {
|
||||
r := NewRegistry()
|
||||
entries, err := os.ReadDir("src/rune")
|
||||
if err == nil {
|
||||
var names []string
|
||||
for _, e := range entries {
|
||||
names = append(names, e.Name())
|
||||
}
|
||||
var plugin *string
|
||||
if pb, err := os.ReadFile(pluginFile()); err == nil {
|
||||
s := string(pb)
|
||||
plugin = &s
|
||||
}
|
||||
r.DiscoverFunctions(names, plugin, "src/rune", pluginFile())
|
||||
}
|
||||
r.AutoTag()
|
||||
return r
|
||||
}
|
||||
|
||||
func runPlacementCLI(cmd string, args []string) {
|
||||
jsonFlag := false
|
||||
var event, data string
|
||||
for i := 0; i < len(args); i++ {
|
||||
switch args[i] {
|
||||
case "--json":
|
||||
jsonFlag = true
|
||||
case "--event":
|
||||
if i+1 < len(args) {
|
||||
event = args[i+1]
|
||||
i++
|
||||
}
|
||||
case "--data":
|
||||
if i+1 < len(args) {
|
||||
data = args[i+1]
|
||||
i++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
switch cmd {
|
||||
case "hook":
|
||||
if event == "" {
|
||||
event = "unknown"
|
||||
}
|
||||
var payload map[string]interface{}
|
||||
if data != "" {
|
||||
if err := json.Unmarshal([]byte(data), &payload); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "bad --data JSON: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
out, _ := json.Marshal(HandleHook(event, payload))
|
||||
fmt.Println(string(out))
|
||||
|
||||
case "scan":
|
||||
r := buildRegistry()
|
||||
tagged := 0
|
||||
for _, f := range r {
|
||||
if len(f.Factors) > 0 {
|
||||
tagged++
|
||||
}
|
||||
}
|
||||
fmt.Printf("Discovered %d functions, %d tagged\n", len(r), tagged)
|
||||
if jsonFlag {
|
||||
out := map[string]Factors{}
|
||||
for n, f := range r {
|
||||
if len(f.Factors) > 0 {
|
||||
out[n] = f.Factors
|
||||
}
|
||||
}
|
||||
b, _ := json.MarshalIndent(out, "", " ")
|
||||
fmt.Println(string(b))
|
||||
}
|
||||
|
||||
case "recommend":
|
||||
r := buildRegistry()
|
||||
for _, name := range r.sortedNames() {
|
||||
f := r[name]
|
||||
if len(f.Factors) == 0 {
|
||||
continue
|
||||
}
|
||||
p := RecommendPlacement(name, f.Factors)
|
||||
if jsonFlag {
|
||||
b, _ := json.MarshalIndent(map[string]interface{}{
|
||||
"name": name, "placement": p}, "", " ")
|
||||
fmt.Println(string(b))
|
||||
} else {
|
||||
hooks, _ := p["hooks"].([]string)
|
||||
fmt.Printf("%s: [%s] %s\n", name,
|
||||
strings.Join(hooks, ", "), p["frequency"])
|
||||
}
|
||||
}
|
||||
|
||||
case "validate":
|
||||
r := buildRegistry()
|
||||
issues := r.ValidatePlacements()
|
||||
if jsonFlag {
|
||||
clean := len(issues) == 0
|
||||
b, _ := json.Marshal(map[string]interface{}{
|
||||
"issues": issues, "clean": clean})
|
||||
fmt.Println(string(b))
|
||||
} else {
|
||||
for _, issue := range issues {
|
||||
fmt.Printf(" ⚠ %s\n", issue)
|
||||
}
|
||||
if len(issues) == 0 {
|
||||
fmt.Println(" ✓ No issues detected")
|
||||
}
|
||||
}
|
||||
|
||||
case "report":
|
||||
fmt.Print(buildRegistry().GenerateReport())
|
||||
|
||||
default:
|
||||
fmt.Fprintf(os.Stderr, "unknown command: %s\n", cmd)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user