Files
Cognition-OS/kernel/supervisor-go/main.go

103 lines
2.7 KiB
Go

package main
import (
"bufio"
"crypto/sha256"
"encoding/hex"
"flag"
"fmt"
"os"
"strings"
"time"
"cognitiveos/supervisor/ledger"
)
func sha256Hex(b []byte) string {
h := sha256.Sum256(b)
return hex.EncodeToString(h[:])
}
func decideRoute(req string) (tier string, agent string, reason string) {
l := strings.ToLower(req)
if strings.Contains(l, "@fast") {
return "fast", "lab-code-fast", "explicit @fast"
}
if strings.Contains(l, "@heavy") {
return "heavy", "lab-code-heavy", "explicit @heavy"
}
for _, kw := range []string{"one-liner", "quick", "regex", "jq", "bash"} {
if strings.Contains(l, kw) {
return "fast", "lab-code-fast", "keyword match: " + kw
}
}
for _, kw := range []string{"multi-file", "architecture", "restructure", "rewrite", "subsystem", "src/"} {
if strings.Contains(l, kw) {
return "heavy", "lab-code-heavy", "scope implies: " + kw
}
}
return "default", "lab-code", "default routing"
}
func childWorkerStub(agent string, req string) string {
// Deterministic placeholder for demo.
return fmt.Sprintf("[stub:%s] Received request (%d chars).", agent, len(req))
}
func main() {
ledgerPath := flag.String("ledger", "ledger.jsonl", "path to append-only ledger file")
flag.Parse()
routerStream := "router-session-1"
l := ledger.New(*ledgerPath, routerStream)
// initialize last-hash from existing file if present
_ = l.LoadLastHash()
fmt.Println("CognitiveOS Supervisor (demo). Type a request, then press Enter. Ctrl-D to exit.")
sc := bufio.NewScanner(os.Stdin)
for sc.Scan() {
req := sc.Text()
if strings.TrimSpace(req) == "" {
continue
}
tier, agent, reason := decideRoute(req)
_, _ = l.Append("route_decision", map[string]any{
"tier": tier,
"target_agent": agent,
"reason": reason,
})
childSession := "child-" + fmt.Sprintf("%d", time.Now().UnixNano())
payloadHash := sha256Hex([]byte(req))
_, _ = l.Append("session_spawn", map[string]any{
"child_session": childSession,
"child_agent": agent,
"payload_hash": payloadHash,
})
childOut := childWorkerStub(agent, req)
outHash := sha256Hex([]byte(childOut))
_, _ = l.Append("child_output_received", map[string]any{
"child_session": childSession,
"output_hash": outHash,
"output_len": len(childOut),
})
preamble := fmt.Sprintf("Delegating to %s because %s.", agent, reason)
final := preamble + "\n" + childOut
_, _ = l.Append("final_output_emitted", map[string]any{
"proof_ref": "proof-" + fmt.Sprintf("%d", time.Now().UnixNano()),
"output_hash": sha256Hex([]byte(final)),
})
fmt.Println(final)
fmt.Println()
}
if err := sc.Err(); err != nil {
fmt.Fprintln(os.Stderr, "read error:", err)
os.Exit(1)
}
}